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() {
    var linksToDisable = new Array();
    linksToDisable[0]=461; //Home link
    linksToDisable[1]=464; //The Experience link
    linksToDisable[2]=492; //Luxair Products link
    linksToDisable[3]=473; //In The Air link
    linksToDisable[4]=491; //Luxembourg Airport link
    linksToDisable[5]=472; //The Fleet link
    linksToDisable[6]=465; //Practical Info link
    linksToDisable[7]=494; //Prepare Your Trip link
    linksToDisable[8]=495; //After Traveling link
    linksToDisable[9]=467; //Destinations link
    linksToDisable[10]=470; //Frequent Flyer link
    linksToDisable[11]=468; //EServices link
    linksToDisable[12]=469; //Promos link    


    var len=linksToDisable.length;
    for(var i=0; i<len; i++) {
        var value = linksToDisable[i];
        $("#map"+value).children("a").attr("href",'javascript:void(0);');
        $("#map"+value).children("a").attr('style','cursor: default');
    }

    var linksToRemove = new Array();
    linksToRemove[0]=572; //MyLuxair Validation Success link
    linksToRemove[1]=573; //MyLuxair Validation Failure link
    linksToRemove[2]=498; //Webcheckin Third level link
    linksToRemove[3]=548; //Eretail link
    linksToRemove[4]=662; //Search Result link
    linksToRemove[5]=1007; //Promo VINOPRESS link
    linksToRemove[6]=971; //Promo Crazy Day
    linksToRemove[7]=1205; //Test Primo new form
    linksToRemove[8]=1207; //50000 form	
linksToRemove[9]=1220; //Facebook Tuesdays 	
	linksToRemove[10]=1078; //New destination for the summer : Bilbao !	
	linksToRemove[11]=1010; //Bella Italia  !

    var len2=linksToRemove.length;
    for(var j=0; j<len2; j++) {
        var value2 = linksToRemove[j];
        $("#map"+value2).remove();
    }
}
