$(document).ready(function () {
    $("#ribbonWrapper").draggable({ containment: 'parent', handle: '.ms-cui-topBar2' });

    //Set cookie and language URL
    var COOKIE_NAME = 'BF_Language';
    var options = { path: '/', expires: 10 };

    //Set cookie by number of days
    $('.LanguageBar a').click(function () {
        //Set new language cookie
        var selectedLang = $(this).attr('lang');
        $.cookie(COOKIE_NAME, selectedLang, options);
        //Redirect to the same page in the selected language
        var urlPath = window.location.pathname;
        var host = "http://" + window.location.hostname + "/";
        var lang = urlPath.substring(1, 3);
        if (lang.toUpperCase() === "EN" || lang.toUpperCase() === "NL" || lang.toUpperCase() === "FR") {
            //Check if current url is English or French
            if (lang.toUpperCase() === "EN" || lang.toUpperCase() === "FR") {
                if (selectedLang === "NL") {
                    //alert(selectedLang + urlPath.substring(3).toLowerCase().replace('pages', 'paginas'));
                    window.location = host + selectedLang + urlPath.substring(3).toLowerCase().replace('pages', 'paginas');
                }
                else {
                    //alert(selectedLang + urlPath.substring(3));
                    window.location = host + selectedLang + urlPath.substring(3);
                }
            }
            else {
                if (selectedLang != "NL") {
                    //alert(selectedLang + urlPath.substring(3).toLowerCase().replace('paginas', 'pages'));
                    window.location = host + selectedLang + urlPath.substring(3).toLowerCase().replace('paginas', 'pages');
                }
                else {
                    //alert(selectedLang + urlPath.substring(3));
                    window.location = host + selectedLang + urlPath.substring(3);
                }
            }
        }
        else {
            window.location = host;
        }
        return false;
    });

    //Redirect to the correct Search Center
    $('input[id$="InputKeywords"]').attr('id', 'searchBox').attr('onkeypress', '');
    $('.ms-sbgo a').attr('href', '');
    //Create a hidden input form, this will not return an error on IE
    $('#hidden').append('<input id="ctl00_PlaceHolderSearchArea_ctl01_S3031AEBB_InputKeywords"').hide();
    $('#searchBox').keypress(function (event) {
        //Check if the user pressed it's enter key
        if (event.keyCode == '13') {
            if ($('#searchBox').val() != "" && $('#searchBox').val() != null) {
                SearchRedirect();
            }
        }
    });
    $('.ms-sbgo a').click(function (event) {
        if ($('#searchBox').val() != "" && $('#searchBox').val() != null) {
            SearchRedirect();
        }
        return false;
    });

    function SearchRedirect() {
        var urlPath = window.location.pathname;
        var host = "http://" + window.location.hostname + "/";
        var lang = urlPath.substring(1, 3);
        //Redirect to the correct search page
        window.location = host + lang + "/search/results.aspx?k=" + $('#searchBox').val();
    }
});
