var current_cat = '';
var institution_value;
var $current_section = null;

(function($){

  var ecoembes = window.ecoembes = function() {
    return new ecoembes.fn.init();
  };

  ecoembes.fn = ecoembes.prototype = {
    init: function() {
			return ecoembes.fn;
			
    },

    initHeader: function(selector) {
	
			jQuery("<img>").attr("src", "http://www.amarilloverdeyazul.com/wp-content/themes/ecoembes/images/botones_cabecera_overlay_default.jpg");	//preload hover de colores
	
			$(selector + " area").hover(
				function () {
					if ($(selector).parent().hasClass("current")) {
						$(selector).parent().removeClass("current");
						current_cat = $(selector).parent().attr("class");
					}
					$(selector).parent().removeClass();
					$(selector).parent().addClass($(this).attr("id"));
				}, 
				function () {
					$(selector).parent().removeClass();
					if (current_cat != '') {
						$(selector).parent().addClass(current_cat);
					}
				}
			);
			$(selector + " area").click(
				function () {
					$(selector).parent().removeClass();
					$(selector).parent().addClass($(this).attr("id"));
					$(selector + " area").unbind();
				}
			);
    },

		initLightBox: function(selector) {
			jQuery(selector).each(function(){
				jQuery(".image_for_" + jQuery(this).attr("id").split("_")[1]).lightBox({
					imageLoading:'http://www.amarilloverdeyazul.com/wp-content/themes/ecoembes/images/lightbox-ico-loading.gif',
					imageBtnPrev:'http://www.amarilloverdeyazul.com/wp-content/themes/ecoembes/images/lightbox-btn-prev.gif',
					imageBtnNext:'http://www.amarilloverdeyazul.com/wp-content/themes/ecoembes/images/lightbox-btn-next.gif',
					imageBtnClose:'http://www.amarilloverdeyazul.com/wp-content/themes/ecoembes/images/lightbox-btn-close.gif',
					imageBlank:'http://www.amarilloverdeyazul.com/wp-content/themes/ecoembes/images/lightbox-blank.gif',
					txtImage: 'Imagen',
					txtOf: 'de'					
				});
			});
		},
		
		initCategoryDescriptions: function(selector) {
			$(selector).click(function () {
	      $(selector).closest("div").next().slideToggle("fast");
				return false;
	    });
		},
		
    initPngFix: function() {
			if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) <= '6') {
			  DD_belatedPNG.fix('#ava01');
			  DD_belatedPNG.fix('#registro');
			  DD_belatedPNG.fix('#rss');
			  DD_belatedPNG.fix('#title_verde');
			  DD_belatedPNG.fix('#title_amarillo');
			  DD_belatedPNG.fix('#title_azul');
			  DD_belatedPNG.fix('#title_registro');
			  DD_belatedPNG.fix('#title_noticias');
			  DD_belatedPNG.fix('#title_calendario');
			  DD_belatedPNG.fix('#title_links');
			  DD_belatedPNG.fix('#footer');
			  DD_belatedPNG.fix('.post-bottom');
			  DD_belatedPNG.fix('.clip');
			  DD_belatedPNG.fix('.post-container');
			  DD_belatedPNG.fix('.post h2');
			  DD_belatedPNG.fix('.icn-box-mail');
			  DD_belatedPNG.fix('.icn-box-imprimir');
			  DD_belatedPNG.fix('.icn-digg');
			  DD_belatedPNG.fix('.icn-nose');
			  DD_belatedPNG.fix('.icn-facebook');
			  DD_belatedPNG.fix('.icn-tuenti');
			  DD_belatedPNG.fix('.icn-twitter');
			  DD_belatedPNG.fix('.post-navigator');				
			  DD_belatedPNG.fix('.blq-top');
			  DD_belatedPNG.fix('.blq-content');
			  DD_belatedPNG.fix('.blq-bottom');
			  DD_belatedPNG.fix('.blq-etiquetas');
			  DD_belatedPNG.fix('#inicio');
			  DD_belatedPNG.fix('#noticias');
			  DD_belatedPNG.fix('#calendario');
			  DD_belatedPNG.fix('#producto');
			  DD_belatedPNG.fix('#enlaces');
			  DD_belatedPNG.fix('#wrapper');
			  DD_belatedPNG.fix('#registrarse');
			  DD_belatedPNG.fix('.ev-verde');
			  DD_belatedPNG.fix('.ev-rojo');
			  DD_belatedPNG.fix('#ccommons');
			  DD_belatedPNG.fix('.post-video');
			}
    },

		initForm: function(selector) {
			
			if ($("#f_centro").length > 0) {
				institution_value = $("#f_centro").val();
				$("#f_centro").blur(function(){
					if ($(this).val() == '') {
						$(this).val(institution_value);
					}
				});
				$("#f_centro").focus(function(){
					if ($(this).val() == institution_value) {
						$(this).val('');
					}
				});
			}
			
			if (jQuery(selector + " #f_image").length > 0) {
				jQuery(selector + " #f_image").click(
					function () {
						if (jQuery(selector + " input[name='profesor']").is(":checked") && jQuery("#f_privacidad").is(":checked") && jQuery(selector + " #f_email").val() != '' && jQuery(selector + " #f_provincia").val() != '') {
							if (!validateEmail(jQuery(selector + " #f_email").val())) {
								alert("Dirección de e-mail inválida");
							} else{
								return true;
							}
						} else {
							alert("Los campos con asterisco son obligatorios y debes aceptar la política de privacidad");
						}
						return false;
					}
				);
			}
		},

		initHeaderSectionsHovers: function(selector) {
			$(selector + " li a").hover(
				function () {
					if ($(this).parent().hasClass("enabled")) {
						$current_section = $(this).parent();
					}
					$(this).parent().addClass("enabled");
				}, 
				function () {
					$(this).parent().removeClass();
					if ($current_section != null) {
						$($current_section).addClass("enabled");
					}
				}
			);
			$(selector + " li a").click(
				function () {
					$(selector + " li").removeClass();
					$(this).parent().addClass("enabled");
					$(selector + " li a").unbind();
				}
			);		
		},
		
		fixHeight: function() {
			
			if (jQuery("#wrapper").height() < jQuery(window).height()) {
				var new_height = (jQuery(window).height() - jQuery("#wrapper").height()) + "px";
				jQuery("#sidebar").css("margin-bottom",new_height);
				jQuery("#content").css("margin-bottom",new_height);
			}
			
		}
		
  };

})(jQuery);

		
function validateEmail(value){      
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(value); 
}

jQuery(function(){
	ecoembes().initLightBox(".post-img");
	ecoembes().initHeaderSectionsHovers("#header");
	ecoembes().initHeader("#header_map");
	ecoembes().initCategoryDescriptions(".cat_description h2 a");
	ecoembes().initPngFix();
	ecoembes().initForm("#form_newsletter");
	ecoembes().fixHeight();
});
