
$(document).ready(function () {
    var isIE8 = false;

    InitSearchWords = function () {
        if (jQuery.browser.msie && jQuery.browser.version == 8) {
            isIE8 = true;
        }

        $(".searchwords .list li p").click(function () {
            if ($(this).hasClass("out")) {
                $(this).animate(
                {
                    opacity: 1
                }, 300).removeClass("out");

                $(".searchwords .selected .list ." + $(this).text()).remove();
            }
            else {
                $(".searchwords .selected").fadeIn(300);

                $(this).animate(
                {
                    opacity: 0.3
                }, 300).addClass("out");

                $(".searchwords .selected .list ul").append("<li onclick=\"RemoveSelected(this, '" + $(this).attr('id') + "');\" class=\"" + $(this).text() + "\"><p>" + $(this).text() + "</p></li>");
            }
        });
    };

    ToggleCategory = function (elm) {
        if (isIE8) {
            $("#" + elm + " .list").toggleClass("open");
        }
        else {
            $("#" + elm + " .list").slideToggle();
        }

        $("#" + elm).toggleClass("closed");
    };

    RemoveSelected = function (elm, id) {
        $(elm).remove();
        $("#" + id).css("opacity", "1").removeClass("out");
    };

    UTF8 = {
        encode: function (s) {
            for (var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l;
            s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]
        );
            return s.join("");
        },
        decode: function (s) {
            for (var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
            ((a = s[i][c](0)) & 0x80) &&
            (s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
            o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
        );
            return s.join("");
        }
    };

    OpenSearch = function (keyword) {
        var strSearch = "";
        var arrList = $(".searchwords .selected .list li p");

        for (var i = 0; i < arrList.length; i++) {
            strSearch += arrList[i].innerHTML + " ";
        }

        if (arrList.length > 0) {
            try {
                strSearch = strSearch.toLowerCase();
//                var newword = "";
//                var tmpchr = "";
//                var escchr = "";

//                newword = UTF8.encode(strSearch);

//                for (var i = 0; i < strSearch.length; i++) {
//                    tmpchr = strSearch.substring(i, i + 1);
//                    escchr = escape(tmpchr).toLowerCase();

//                    if (escchr != "%e6" && escchr != "%f8" && escchr != "%e5")
//                        newword += tmpchr;
//                    else if (escchr == "%e6")
//                        newword += "ae";
//                    else if (escchr == "%f8")
//                        newword += "oe";
//                    else if (escchr == "%e5")
//                        newword += "aa";
//                }

//                strSearch = newword;

                window.open("http://www.magasinetmad.dk//SearchResults/mad.aspx?q=" + strSearch + keyword);
                $(".searchwords .error").hide();
            }
            catch (e) {
                //alert(e);
            }
        }
        else {
            $(".searchwords .error").fadeIn();
        }
    };
});
