﻿function update(str) {
    $("#content").css("margin-left", "210px");
    $("#leftColumn").show();

    $("#leftColumn ul ul li").hide();
    $(".filterBox").hide();

    $("#leftColumn h2,#leftColumn h3,#leftColumn h4").removeClass("blockActive")
    var blocks = unescape( str).split('!')[0].split(',');
    var h2 = $("#leftColumn h2:contains('" + blocks[0] + "')");
    h2.addClass("blockActive");
    h2.next().children("li").show();
    if (blocks.length > 1) {
        var h3 = h2.next().find("h3:contains('" + blocks[1] + "')");
        h3.addClass("blockActive");
        h3.next().children("li").show();
        if (blocks.length > 2) {
            var h4 = h3.next().find("h4:contains('" + blocks[2] + "')");
            h4.addClass("blockActive");
            h4.next().show();
            h4.next().find("li").show();
        }
    }

}
Array.prototype.contains = function(element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    return false;
}
Array.prototype.containsAll = function(element) {

    for (var i = 0; i < element.length; i++) {
        if (!this.contains(element)) {
            return false;
        }
    }
    return true;
}
String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}
$(document).ready(function() {
    $(".navigation ul a[href^='#']").click(function() {
        var str2 = $(this).attr("href").toString().split('#');
        if (str2[0].length < 2) {
            if (str2[1] != null) {
                if (str2[1].length > 1) {
                    update(str2[1]);
                }
            }
        }
    });
    $("#leftColumn a").click(function() {
        var str2 = $(this).attr("href").toString().split('#');
        if (str2[0].length < 2) {
            if (str2[1] != null) {
                if (str2[1].length > 1) {
                    update(str2[1]);
                }
            }
        }
    });
    var str = window.location.toString().split('#');
    if (str[1] != null) {
        if (str[1].length > 1) {
            update(str[1]);
        }
    }
    function filterChange() {
        $(".listingItems > div").show();
        var str = window.location.toString().split('#')[1].split('!');
        var fs = new Array();
        if (str[1] != null)
            if (str[1].length > 0)
            fs = str[1].split(',');
        var filteredS = new Array();

        for (var i = 0; i < fs.length; i++) {
            if (fs[i].startsWith("Brands:")) {
                for (var j = 0; j < brands.length; j++) {
                    if (brands[j] == fs[i].split(':')[1]) {
                        $(".listingItems > div").eq(j).hide();
                    }
                }
            }

            if (fs[i].startsWith("Sex:")) {
                filteredS.push(fs[i]);
            }
        }
        if (filteredS.length > 0) {
            for (var j = 0; j < subgroups.length; j++) {
                var SGS = subgroups[j].split(',')
                var sxSGS = new Array();
                for (var k = 0; k < SGS.length;k++ ) {
                    if (SGS[k].startsWith("Sex:")) {
                        sxSGS.push(SGS[k]);
                    }
                }
                if (filteredS.containsAll(sxSGS)) {
                    $(".listingItems > div").eq(j).hide();
                }
            }
        }

        var I = 1;
        $(".listingItems > div:visible").each(function() {
            $(this).removeClass("listingBox1");
            $(this).removeClass("listingBox2");
            $(this).removeClass("listingBox3");
            $(this).addClass("listingBox" + I.toString());
            I++;
            if (I == 4)
                I = 1;

        });
    }
    if ($(".listingItems").length > 0) {
        filterChange();
    }
    if (window.location.toString().split('#').length > 1) {
        var str = window.location.toString().split('#')[1].split('!');
        var fs = new Array();
        if (str[1] != null)
            if (str[1].length > 0)
            fs = str[1].split(',');

        $(".filterBox a").each(function() {

            for (var i = 0; i < fs.length; i++) {
                if ($(this).text() == fs[i].split(':')[1]) {
                    $(this).addClass("off");
                }
            }
        });
    }

    function addfilter(t, f) {

        var str = window.location.toString().split('#')[1].split('!');
        var fs = new Array();
        if (str[1] != null)
            if (str[1].length > 0)
            fs = str[1].split(',');
        fs.push(t + ":" + f);
        if (fs == null || fs.length == 0) {
            window.location = "#" + str[0];
        } else {
            window.location = "#" + str[0] + "!" + fs.join(",");
        }
        filterChange();
    }
    function removefilter(t, f) {

        var str = window.location.toString().split('#')[1].split('!');
        var fs = new Array();
        if (str[1] != null)
            if (str[1].length > 0)
            fs = str[1].split(',');
        fs.splice(fs.indexOf(t + ":" + f), 1);
        if (fs == null || fs.length == 0) {
            window.location = "#" + str[0];
        } else {
            window.location = "#" + str[0] + "!" + fs.join(",");
        }
        filterChange();
    }
    $(".filterBox a").click(function() {
        if ($(this).hasClass("off")) {

            $(this).removeClass("off");
            removefilter($(this).parent().parent().prev().text(), $(this).text());
        } else {
            $(this).addClass("off");
            addfilter($(this).parent().parent().prev().text(), $(this).text());
        }
        return false;
    });

});
