
	$(document).ready( function() {
		
		$("#distributionMap").lightBox({
			imageLoading: '/images/lightbox/loading.gif',
			imageBtnClose: '/images/lightbox/close.gif',
			imageBtnPrev: '/images/lightbox/prev.gif',
			imageBtnNext: '/images/lightbox/next.gif'		
		});

		$('.toggle_other').click( function() {

			var $this = $(this);

			var $other = $( "#" + $this.attr('id') + '_value' );

			if ( $other.attr('disabled') ) {
				$other.removeAttr('disabled')
			}
			else {
				$other.attr('disabled', true )
			}

			return true;
		});


		// -- Determina que menu debe estar activo.		

		var identifiers = {

			"homepage"						: "home",
			"distributors" 					: "distributors",
			"innovation"					: "innovation",
			"about"							: "about"

		};
		
		var 
			url = window.location.href,
			slug = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.html')),
			hash = window.location.hash,
			
			section = ""; //empty initialization to check for template name later
		
			
			
		// -- Menu principal
		if( slug.indexOf("http://") > -1 ) {
			section = "homepage";
		}
		
		if( identifiers[slug] != null ) {
			section = identifiers[slug];
		}
		else {
		
			// -- Se buscan algunas keywords para identificar la seccion.
			if ( slug.indexOf('innovatio') > -1 ) {
				section = "innovation";
			}
		}

		if (m_section != null && typeof(m_section) != "undefined" && section == "") {
			// Miramos el nombre del template
			switch (m_section) {
			case "aboutustemplate.php":
			case "partnertemplate.php":
				section = "about";
				break;
			case "singleproducttemplate.php":
			case "multiproductsthreetemplate.php":
			case "productstemplate.php":
				section = "products";
				break;
			}
		}
		if (section == "") {
//			section = "products";	//don't force anythng
		}
		
		switch ( section ) {
		
			case "homepage":				
				$("#menu-home").addClass("selected");				
				break;
				
			case "products":
				$("#menu-products").addClass("selected");
				break;
				
			case "about":
				$("#menu-about").addClass("selected");
				break;
				
			case "innovation":
				$("#menu-innovation").addClass("selected");
				break;
			
			case "distributors":
				$("#menu-distributors").addClass("selected");
		};		

		// Agregamos el class al header para ir cambiando las imagenes
		$("#header-products-container").addClass(slug);
		$("#header-right-bottom").addClass(slug);
		
		// -- Links en los LI
		$("li[class^='nav-level-'] > a").click(function(){			
			handleLeftMenuLinkClick($(this));				
			return false;
		});
			
		$("li > a[href*='#']").click(function(){			
			handleInternalLeftMenuLinkClick($(this));				
			return true;
		});

		
		//auto hide popups
		$(".distAddress").click( function () {
			$(this).hide();
		});
		$(".inlinePopup").click( function () {
			$(this).hide();
		});
		
		
		$("a.internal").click(function() {
			
			var
				$this = $(this),
				slug = $this.attr("name"),
				url = window.location.href,
				currentSlug = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.html'));
			
			if( slug != null && slug != "" ) {
				
				var newUrl = "";
				
				if ( currentSlug.indexOf("http") < 0 ) {
					newUrl = url.replace(currentSlug, slug);
				}
				else {
					newUrl = '/' + currentLanguage + '/' + slug + '.html';
				}
				
				location.href = newUrl;
			}		
			
			$this.blur();
			return false;
		});
		
		$("#about-categories-container a, #innovation-categories-container a, a.cs-level-2").click(function(){
			
			var 
				$this = $(this),
				related = $(this).attr("rel"),
				$parent = $this.parents("#innovations-categories-container");
			
			$this.toggleClass("selected");
			
			// -- Se hizo click sobre un link de los casos de estudio. 
			if($this.hasClass("cs-level-2")) {
				
				$this.toggleClass("cs-level-2-selected");
				
				$(".case-study-content", $parent).hide();
				
			}			
			
			if($this.hasClass("selected")) {
				$("#" + related + "-container").show();
			}
			else {
				$("#" + related + "-container").hide();
			}
			
			if( related == "case-studies" ) {
				$this.toggleClass("opened");
			}
			
			return false;
		});
		
		$("a").click(function(){
			$(this).blur();
		});
		
		
		setTimeout(checkOpenMenu, 500);
		
	});	
	
	function checkOpenMenu() {
		var
			url = window.location.href,
			slug = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.html')),
			hash = window.location.hash,
			leftActiveSelector = hash == null ? 'a[id="trigger-' + slug + '"]:eq(0)' : 'a[id="trigger-' + slug + hash + '"]:eq(0)'; 
			
		leftActive = $(leftActiveSelector);
		
		// -- Menu lateral.
		if ( leftActive.length > 0 ) {			
			activateLeftMenuLink(leftActive);			
		}

		
		// -- we search for the upper product
		if ($(".nav-level-1-opened").length > 0) {
			var 
				upperPage = $(".nav-level-1-opened").attr("href");
				upperPageClass = "";
				
			upperPageClass = upperPage.substring(upperPage.lastIndexOf('/') + 1, upperPage.lastIndexOf('.html'));
			upperPageClass = "img-header-" + upperPageClass;

			$("#header-products-container").addClass(upperPageClass);
		}

	}
	function handleInternalLeftMenuLinkClick( $link ) {
		
		var 
			internalLink = $link.attr("href"),
			hashV = internalLink.substring(0, internalLink.lastIndexOf("#")),
			level = $link.parents("ul").length;
		
		//get all the links that have the same url
		var aLinks = $("#left-sidebar li > a[href*='" + hashV + "']");
		
		aLinks.each(function(index){
			var jLink = $(aLinks[index]);
			deactivateLeftMenuLink( jLink, level, false);
			jLink.removeClass("nav-level-" + level + "-selected");	
		});
		
		activateLeftMenuLink($link);
	}
	
	function handleLeftMenuLinkClick ( $link ) {		
		if ( $link.attr('rel') != "" ) {
			location.href = '/' + currentLanguage + '/' + $link.attr('rel') + '.html';
			return false;
		}
		
		if ( $link.hasClass("selected")) {
			deactivateLeftMenuLink($link);
		}
		else {
			activateLeftMenuLink($link);
		}
	}
	
	function activateLeftMenuLink ( $link, level ) {
	
		var 
			$this = $link,			
			level = $this.parents("ul").length,
			baseClass = 'nav-level-' + level,
			newClass = baseClass + "-selected";
		
		$this.addClass("selected");
		
		if($this.siblings('ul').length > 0) {
			newClass = baseClass + "-opened";
		}
		
		$this
			.addClass(newClass)
			.siblings('ul:eq(0)').slideDown().end();
		
		// --
		$this.parents("ul").show();
		$this.parents("li.nav-level-1").addClass("opened");
		$this.parents("ul").each(function(index){
			var 
				$this = $(this),
				level = $this.parents("ul").length;			
			
			$this.siblings("a").addClass("selected").addClass("nav-level-" + level + "-opened");			
		});		
	}
	
	function deactivateLeftMenuLink( $link, level, closeIt ) {
		var 
			$this = $link,
			$parent = $link.parents("li:eq(0)");
		
		if (level == null)
			level = $this.parents("ul").length;	
		
		if (closeIt == null)
			$this.siblings("ul").slideUp().end();
		
		$this.removeClass("selected");
		$this.removeClass("nav-level-" + level + "-opened");
		$parent.removeClass("opened");		
	}