﻿$(document).ready(function () {
    // LIST MODE Start
    var is_state1_button;
    var state1_class = 'state1';
    var button_switch_to_image = '.filter-box .switch .state1';
    var button_switch_to_list = '.filter-box .switch .state2';
    var state = ['.productstate.ps1', '.productstate.ps2'];


    var cookie_product_list_state_name = 'guson_product_list_state_name';
    var cookie_product_list_state_value = cookieMonster.get(cookie_product_list_state_name);

    if (cookie_product_list_state_value) {
        cookie_product_list_state_value = parseInt(cookie_product_list_state_value);
        $(state[0]).hide();
        $(state[1]).hide();
        $(state[cookie_product_list_state_value]).show();
    }
    else {
        //STANDARD DEPENDING ON USER STATE
        if (is_user_logged_in) {
            $(state[0]).hide();
            $(state[1]).show();
        } else {
            $(state[0]).show();
            $(state[1]).hide();
        }
    }


    $(button_switch_to_image + ',' + button_switch_to_list).click(function () {
        is_state1_button = $(this).attr('class').match(state1_class);

        if (is_state1_button) {
            $(state[0]).show();
            $(state[1]).hide();
            cookie_product_list_state_value = cookieMonster.set(cookie_product_list_state_name, '0', 30);
        }
        else {
            $(state[1]).show();
            $(state[0]).hide();
            cookie_product_list_state_value = cookieMonster.set(cookie_product_list_state_name, '1', 30);
        }

        return false;
    });
    // LIST MODE End


    //SORT STATE Start
    var sort_links = $('.top-nav a, .filter-holder a');
    var sort_querystring;
    sort_links.each(function () {
        sort_querystring = this.href.substring(this.href.indexOf('?') + 1);
        if (location.href.match(sort_querystring)) {
            sort_querystring = sort_querystring.substring(sort_querystring.indexOf('&SortOrder=') + '&SortOrder='.length);
            $(this).addClass(sort_querystring);
        }
    });
    //SORT STATE End
});
