/**
* Global variables
*/
var popup, popup2, popup3;

/**
* @name hideCurrentMediaSelector
* @description hide the media selector pop-up
*/
function hideCurrentMediaSelector() {
    if (popup != null) {
        popup.style.visibility = "hidden";
        popup.style.display = "none";
        //popup.style.zindex = "0";
    }

    var globalnavlbar = $("div.globalnav_lbar");
    var colRight = $("div.columnRight");
    if (null != globalnavlbar && null != colRight) {
        globalnavlbar.removeClass("globalNavBarColOnBottom");
        colRight.removeClass("rightColOnTop");
    }
}

/**
* @name utf8_encode
* @description encodes text strings in UTF-8 (used for url encoding)
* @param txt a string that will be encoded in UTF-8
* @return returns the UTF-8 encoded string given by parameter 'txt'
*/
function utf8_encode(txt) {
    txt = txt.replace(/\r\n/g, "\n");
    var utftext = "";

    for (var n = 0; n < txt.length; n++) {
        var c = txt.charCodeAt(n);
        if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if ((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }
    }

    return utftext;
}

/**
* @name initSearchBar
* @description register the search event on the global search (textbox and button)
* @example initSearchBar('http://localhost/uk/Search', 'ctl00_PageHeader1_tbSearchQuery', 'ArvatoServices.Web.Localization.Controls.Hyperlink');
* @param searchUrl the url the search page
* @param textboxId the element id of the textbox containing the search string
* @param searchButtonId the element id of the button triggering the search
* @return true if the event listeners were successfully installed; otherwise false
*/
function initSearchBar(searchUrl, textboxId, searchButtonId) {
    var tbQuery = $("#" + textboxId), btnSearch = $("#" + searchButtonId);
    if (tbQuery == null || btnSearch == null) { return false; }

    /* search function */
    var search = function() {
        var query = $(tbQuery).val();
        if (query.length > 0) {
            var url = searchUrl + '?q=' + escape(utf8_encode(query));
            try {
                /* try this for normal browsers */
                parent.window.location = url;
            } catch (browserErr) {
                try {
                    /* try this for the ie6 */
                    parent.location.replace(url);
                } catch (IE6err) { }
            }
        }
    };

    /* on button click */
    $(btnSearch).click(function(e) { search(); });

    /* on return */
    $(tbQuery).keydown(function(e) {
        var keycode = e.keyCode;
        if (keycode == 13) {
            /* trigger the search */
            search();

            /* prevent default behaviour */
            return false;
        }

        return true;
    });

    return true;
}

/**
* @name cookieCheck
* @description validates if the current browser accepts cookies or not; if the browser does not support cookies - redirect to an error page
* @example cookieCheck();
* @param noCookieCallback a function which will be executed if the current browser does not support cookies
* @return returns true if cookies are supported; otherwise false
*/
function cookieCheck(noCookieCallback) {
    var cookieName = 'test4Cookie' + (new Date().getTime());
    var cookieEnabled = (navigator.cookieEnabled) ? true : false

    /* if not IE4+ nor NS6+ */
    if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled) {
        document.cookie = cookieName + '=cookieValue';
        cookieEnabled = (document.cookie.indexOf(cookieName) != -1) ? true : false;
    } else if (!cookieEnabled) {
        if (document.cookie) {
            if (document.cookie == '') {
                /* try to set a cookie */
                document.cookie = cookieName + '=cookieValue';

                if (document.cookie.indexOf(cookieName) != -1) {
                    /* if it succeeds, set variable */
                    cookieEnabled = true;
                }
            } else {
                /* there was already a cookie */
                cookieEnabled = true;
            }
        }
    }

    if (!cookieEnabled) {
        if (noCookieCallback == null) {
            /* default behaviour: redirect to error page */
            window.location.href = 'Error/NoCookies.aspx';
        } else {
            /* execute callback function */
            noCookieCallback();
        }

        return false;
    }

    return true;
}

$(document).ready(function(e) {
    /* cookie check */
    var _funcCookieCallback = function() { /* redirect to error page */window.location.href = 'Error/NoCookies.aspx'; };
    cookieCheck(_funcCookieCallback);

    /* initialize the search bar */
    try {
        initSearchBar(SearchBar.searchUrl, SearchBar.textboxId, SearchBar.searchButtonId);
    } catch(e) { }
});