﻿$(document).ready(function() {

    // Expand Panel
    $("#open").click(function() {
        $("div#langList").slideDown("slow");
        $("div#langList").focus();
    });

    // Collapse Panel
    $("#close").click(function() {
        $("div#langList").slideUp("slow");
    });

    // Switch buttons from "up" to "down" on click
    $("#toggle a").click(function() {
        $("#toggle a").toggle();
    });

    $("div#langList").blur(function() {
        hideDiv();
    });

    if ($.cookie("shDir") == "rtl") {
        changeToRTL();
    }
    else
        $("#open").html("Language:&nbsp;&nbsp; " + $.cookie("shLang"));

    $("a .tabs_toggle").parent().click(function() {
        var tab = $(this).parent();

        if ($(tab).attr('class') == "category tab_expand") {
            $(tab).removeClass('tab_expand').addClass('tab_closed');
            $('#' + $(this).attr('rel')).toggle('fast');
        }
        else if ($(tab).attr('class') == "category tab_closed") {
            $(tab).removeClass('tab_closed').addClass('tab_expand');
            $('#' + $(this).attr('rel')).toggle('fast');
        }
    });

    $("a .tabs_toggle").next().css('text-indent', '22px');
});

function hideDiv() {
    if (document.getElementById("close").style.display != "none") {
        $("div#langList").slideUp("slow");
        $("#toggle a").toggle();
    }
}

function setLang(direction, obj) {
    $.cookie("shDir", direction, { expires: 365, path: '/' });

    var lText;
    if (document.all)
        lText = obj.innerText;
    else
        lText = obj.textContent;

    $.cookie("shLang", jQuery.trim(lText), { expires: 365, path: '/' });

    hideDiv();
    if ($.cookie("shDir") == "rtl")
        changeToRTL();
    else
        removecssfile();
    /*   alert(obj.innerText);
    alert($.cookie("shDir"));*/
}

function changeToRTL() {
    $("head").append("<link>");
    css = $("head").children(":last");
    css.attr({
        rel: 'stylesheet',
        type: 'text/css',
        href: '/Scripts/rtl.css'
    });
    $("#open").html("Language:&nbsp;&nbsp; " + $.cookie("shLang"));
}

function removecssfile() {
    var allsuspects = document.getElementsByTagName("link")
    for (var i = allsuspects.length; i >= 0; i--) { //search backwards within nodelist for matching elements to remove
        if (allsuspects[i] != null && allsuspects[i].getAttribute("href").indexOf("rtl.css") != -1)
            allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
    }
    $("#open").html("Language:&nbsp;&nbsp; " + $.cookie("shLang"));
}
function searchprod(obj) {
    var value = $('#'+obj).parent().children(':first').val();

    this.location.href = "/search.aspx?result=" + value;
}


