var laguage_select_dropdown_mouse_in = false;

function laguage_select_dropdown_click(lang_button) { 
    update_language_select_dropdown_links();

    document.getElementById('langs').style.display = 'block';
}

function laguage_select_dropdown_mouseover() {
   laguage_select_dropdown_mouse_in = true;
}

function laguage_select_dropdown_mouseout() {
    laguage_select_dropdown_mouse_in = false;
    setTimeout ("laguage_select_dropdown_hide_popup()", 250);
}

function laguage_select_dropdown_hide_popup() {
    if(!laguage_select_dropdown_mouse_in) {
       var popupDiv = document.getElementById("language_select_dropdown_popup");
       document.getElementById('langs').style.display = 'none';
    }
}

function update_language_select_dropdown_links() {
    for(var i in langs) {
        var link_id = "language_select_dropdown_flag_" + langs[i];
        var link_obj = document.getElementById(link_id);
        if(link_obj != null) {
            var new_link = update_one_language_select_dropdown_link(langs[i]);
            link_obj.href = new_link;
        }
    }
}

function update_one_language_select_dropdown_link(lang) {
     //check if the selected language is supported by readoz, if not show the "coming soon" message
    var isSupported = false;
    for(var i in supportedLangues) {
        if(lang == supportedLangues[i]) {
            isSupported = true;
        }
    }

    var new_link = null;
    if(lang_selection_in_coming_soon_page && isSupported) {
        /*if(lang_selection_location_param != null) {
           new_link = lang_selection_location_param;
        }
        else {    */
            new_link = lang_selection_link_to_root + "?lang=" + lang;
       /* }*/
        return new_link;
    }
    //if it's not supported
    else if(!lang_selection_in_coming_soon_page && !isSupported) {
        new_link = lang_selection_link_to_root + "main/lang_coming_soon?location=" + encodeURIComponent(window.location.href) + "&lang=" + lang;
        return new_link;
    }



    //search part is the part after ?, but it doesn't include the part after the #
    var locationSearch = window.location.search;

    //if the language was already set in the URL
    if(locationSearch.indexOf("lang=") != -1) {
        //get the part before "lang="
        var langIndex = locationSearch.indexOf("lang=");
        var beginLocation = locationSearch.substr(0, langIndex);

        //get the part after "lang="
        var nextIndex = locationSearch.indexOf("&", langIndex + 1);
        var endLocation = "";
        if(nextIndex != -1) {
            endLocation = locationSearch.substr(nextIndex);
        }

        //join everything but with the new language
        locationSearch = beginLocation + "lang=" + lang + endLocation;
    }
    //if the language wasn't set in the URL
    else {
        //set the language, but first check if there are more paramters to choose is to use ? or &
        var index = locationSearch.indexOf("?");
        locationSearch += index == -1 ? "?" : "&";
        locationSearch += "lang=" + lang;
    }

    new_link =  window.location.protocol + "//" + window.location.host;

    if(window.location.port != 80 && window.location.host.indexOf(":") == -1) {
        new_link += ":" + window.location.port;
    }

    if(window.location.pathname != "") {
        new_link += window.location.pathname;
    }
    new_link += locationSearch;

    if(window.location.hash != "") {
       new_link += window.location.hash;
    }

    return new_link;

}
