var currentLanguage = "EN";

function buildMenuBar() {
	/* We set the plane icon on the first menu item */
	$("#item461").attr("class",'itemPlane');

	/* Drop all the empty 2rd level navigation */
	$("#nav li").each(function(){
		/* We drop the "original Promo link" to create our own */
		if ($(this).children("a").attr("id") == "item469") {
			$(this).remove();
		} else {
			/* We first check if the menu is already selected */			
			var isPlane = $(this).children("a").hasClass("itemPlane");	
			var isPromo = $(this).children("a").attr("id") == "itemPromos";
			
			/* We drop the link and the "hand cursor" on all the first level navigation item having sub navigation */
			if ($(this).children(".subNavigation").children("li").length > 0 && !isPlane) {
				$(this).children("a").attr("href",'javascript:void(0);');				
				$(this).children("a").attr('style','cursor: default');
				/* Add the bottom box (just to get round corners) */
				$(this).children(".subNavigation").append('<li id="subMenuBottom">&nbsp;</li>');

			}

			if (($(this).children(".subNavigation").children("li").length == 0 || isPlane) && !isPromo) {	
				/* We didn't realy drop the ul element but put the class to emptyNavigation instead of subNavigation */
				$(this).children(".subNavigation").children("li").remove();
				$(this).children(".subNavigation").addClass("emptyNavigation");
				$(this).children(".subNavigation").removeClass("subNavigation");
			}
		}
	});
	
	/* Remove webcheckin and myluxair 3rd level */
	$('#subItem610').remove();	
	$('#subItem483').remove();
	
	/* Drop all the empty 3rd level navigation */
	$("#nav li ul li").each(function(){
		/* We drop the link and the "hand cursor" on all the first level navigation item having sub navigation */
		if ($(this).children(".subSubNavigation").children("li").length > 0) {			
			/* Remove the link */		
			$(this).children("a").attr("href",'javascript:void(0);');
			/* Add the bottom box (just to get round corners) */
			$(this).children(".subSubNavigation").append('<li id="subSubMenuBottom">&nbsp;</li>');
			$(this).children(".subSubNavigation").children("li:first").prepend('<div class="subMenuIndicator"></div>');

		}
		if ($(this).children(".subSubNavigation").children("li").length == 0) {
			$(this).children(".subSubNavigation").remove();
		}
	});

	$('ul.sf-menu').superfish({
		hoverClass : 'sfHover',
 		pathClass : 'current',
 		pathLevels : 1,
 		delay : 300,
 		animation : {opacity:'show'},
 		speed : 0,
		autoArrows : true,
		dropShadows : false,
		disableHI : false
	});

	$('ul.sf-menu > li > a .sf-sub-indicator').remove();  
	$('.sf-sub-indicator').html("");

	/* For each menu we provide an "hover" behaviour */
	$("#nav li").each(function(){
		$(this).mouseover(function(){

			/* We first check if the menu is already selected */			
			var isPlane = $(this).children("a").hasClass("itemPlane");	
			var isPromo = $(this).children("a").attr("id") == "itemPromos";

			if(!isPlane) {			
				/* If the nenu own a submenu */
				if ($(this).children(".subNavigation").children("li").length > 0 || isPromo ) {
					$(this).attr("class",'itemOver');			
				} else {
					$(this).attr("class",'itemOverFull');
				}		
			}					
			$(this).prev().attr("class",'item');
			$(this).siblings().attr("class",'item');
		});
	});
}

function setPlaneMenuItemOn() {
	$("#item461").children(".menu_left").attr("class",'menu_left_on');	
	$("#item461").children(".menu_middle").attr("class",'menu_middle_on');	
	$("#item461").children(".menu_right").attr("class",'menu_right_on');
}

function setPlaneMenuItemOff() {
	$("#item461").children(".menu_left_on").attr("class",'menu_left');	
	$("#item461").children(".menu_middle_on").attr("class",'menu_middle');	
	$("#item461").children(".menu_right_on").attr("class",'menu_right');
}

function setHeaderBehaviour(currentLang, currentSite) {
	setHeaderLoginText();

	/* Set an hover behaviour in order to display the login / language change box */
	$("#header-top-interaction").hover(function(event){
		$("#header-top-interaction").addClass('active');
		var content = $('.content', this);
		if(!content.is(':animated')) content.show();
	}, function(){
		$(this).removeClass('active');
		var content = $('.content', this);
		if(!content.is(':animated')) content.hide();			
	}); 

	/* Configure the home link on the header logo */
	$("#headerLink").click(function() {
       	window.location = "http://www.luxair.lu/cms/luxair.php?page&p=" + currentLang + ",17652,,,,,";
       });	


	/* Configure the language dropDown */
	$(".dropdown dt a").click(function() {
                $(".dropdown dd ul").toggle();
       });

	$(".dropdown2 dt a").click(function() {
                $(".dropdown2 dd ul").toggle();
       });
			                 
       $(".dropdown dd ul li a").click(function() {
		var text = $(this).html();
              $(".dropdown dt a span").html(text);
              $(".dropdown dd ul").hide();
		window.location = $(this).children("span").text();
       });

	$(".dropdown2 dd ul li a").click(function() {
		var text = $(this).html();
              $(".dropdown2 dt a span").html(text);
              $(".dropdown2 dd ul").hide();
		window.location = $(this).children("span").text();
       });

	$(document).bind('click', function(e) {
		var $clicked = $(e.target);
              if (! $clicked.parents().hasClass("dropdown"))
              $(".dropdown dd ul").hide();
       });

	$(document).bind('click', function(e) {
		var $clicked = $(e.target);
              if (! $clicked.parents().hasClass("dropdown2"))
              $(".dropdown2 dd ul").hide();
       });

       $("#flagSwitcher").click(function() {
       	$(".dropdown img.flag").toggleClass("flagvisibility");
       });

	/* Set the selected language */
	if (currentLang) {
		currentLanguage  = currentLang;
		$("#languageSelector dd ul li").each(function() {
			if ($(this).children("a").children("span").html().toUpperCase().indexOf(currentLang.toUpperCase()) != -1) {
				var text = $(this).children("a").html();
				$("#languageSelector dt a span").html(text);
				$("#languageSelector dd ul").hide();
			}
		});
	}
	/* Set the selected site */
	if (currentSite) {
		$("#countrySelector dd ul li").each(function() {
			if ($(this).children("a").children("span").html().toUpperCase().indexOf(currentSite.toUpperCase()) != -1) {
				var text = $(this).children("a").html();
				$("#countrySelector dt a span").html(text);
				$("#countrySelector dd ul").hide();
			}
		});
	}
}

/* Set the myLuxair labels in the top header */
function setHeaderLoginText() {
	var cookieFirstName = $.cookie("lgl_reg_fn");
	var cookieLastName = $.cookie("lgl_reg_ln");
	var cookieLogin = $.cookie("lgl_reg_u");
	var cookieToken = $.cookie("lgl_reg_k");
	/* If there is one, we can login the customer automaticaly */
	if (cookieFirstName != null) {
		var welcomeMessage = $("#myLuxairLoginWelcome").html();
		var linkHREF = "/cms/luxair.php?page&p=" + currentLanguage + ",17529,483,,1,,";
		$("#myLuxairLogin").html("<a style='font-weight:bold' href='"+linkHREF+"'>" + welcomeMessage + " " + cookieFirstName + " " + cookieLastName+"</a>");	
		$("#header-profilBox").addClass("header-profilBoxLoggedIn");		
		var element=document.getElementById('myLuxairLogin');
		var padding = element.offsetWidth + 10;
		$("#languageBox").css( "padding-right", padding.toFixed(0)+"px");
		$("#loggedBox").hide();
		$("#unLoggedBox").show();	
	} else if (cookieLogin != null){
		var actionForm = "https://www.luxair.lu/profile/ProfileCheckCookieService.action";
		$("#unLoggedBox").append("<form id='cookieForm' name='cookieForm' action='" + actionForm +"' method='post'></form>");
		$("#cookieForm").append("<input type='hidden' id='lang' name='lang' value='" + currentLanguage +"'></input>");
		$("#cookieForm").submit();
	} else {
		$("#myLuxairLogin").html($("#myLuxairLoginConnexion").html());
		$("#header-profilBox").removeClass("header-profilBoxLoggedIn");
		var element=document.getElementById('myLuxairLogin');	
		var padding = element.offsetWidth + 10;	
		$("#languageBox").css( "padding-right", padding.toFixed(0) +"px");		
		$("#loggedBox").show();
		$("#unLoggedBox").hide();
	}

	var authError = $.getUrlVar('error');
	if(authError!="undefined" && authError=="true"){
		$("#smallLoginError").show();
		$(".content").show();
	}

	$('#smallLoginForminput').bind('keypress', function(e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code == 13) {//Enter keycode
			$('#smallLoginForminput').submit();
		}
	});
}

function doMyLuxairRefresh() {
	window.location="https://www.luxair.lu/cms/luxair.php?page&p=" + currentLanguage + ",17529,,,,,";
}

function arrangeSiteMap() {
	/* Drop Medecine Nucleaire link */
	$("#map995").remove();	
	/* Drop Longhaul link */
	$("#map997").remove();
	/* Drop MyLuxair Validation */	
	$("#map572").parent().remove();		
	/* Drop Webcheckin Third level */	
	$("#map498").remove();
	/* Drop Eretail Link */	
	$("#map548").remove();	
	$("#map996").remove();	
	/* Drop Search Result */	
	$("#map662").remove();	
	$("#map996").remove();
}