/// <reference path=~/js/jquery-1.3.2.min.js />
/// <reference path=~/js/jquery.address-1.2.2.min.js />
function Add2FavoritesBar(title, url) {
    if (window.sidebar) // Firefox 
    {
        window.sidebar.addPanel(title, url, '');
    } else if (window.opera) { //Opera 
        var a = document.createElement("A");
        a.rel = "sidebar";
        a.target = "_search";
        a.title = title;
        a.href = url;
        a.click();
    } else if (document.all) //IE 
    { window.external.addToFavoritesBar(url, title); }
}


/* Modified to support Opera */
function bookmarksite(title, url) {
    if (window.sidebar) // firefox
    { window.sidebar.addPanel(title, url, ""); }
    else if (window.opera && window.print) { // opera
        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    }
    else if (document.all)// ie
    { window.external.AddFavorite(url, title); }
}

///GVO 20100903
///to support back button
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
///GVO 20100903
///to support back button
///leave expiredays out of the call to expire at the end of the session
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
}
///GVO 20100903
///to support back button
function checkCookie() {
    var cats = getCookie('cats');
    if (cats != null && cats != '') {
        return true;  //alert(cats);
    }
    return false;  //alert('no cats');
}
///GVO 20100903
///to support back button
$().ready(function () {
    function getPageCat() {
        catId = 0;
        if (location.href.match(/C\d+\.aspx/)) {
            catId = location.href.match(/C(\d+)\.aspx/)[1];
        } else if (location.href.match(/&c=\d+&/)) {
            catId = location.href.match(/&c=(\d+)&/)[1];
        } else if (location.href.match(/Category\.aspx\?CategoryId=\d+/)) {
            catId = location.href.match(/Category\.aspx\?CategoryId=(\d+)/)[1];
        }
        return 1 * catId;
    }
    if ($(document).find('.searchCriteria').size() > 0) {
        $.address.change(function (e) {
            try {
                if (e.parameters['catid'] != undefined) {
                    var catId = e.parameters['catid'];
                    var runJs = getCookie('cat' + catId);
                    if (runJs) {
                        eval(runJs);
                    }
                }
            } catch (e) { alert(e); }
            return false;
        });
    }
    $('.searchCriteria').live('click', function (e) {
        e.stopPropagation();
        var elId = $(this).attr('id');
        var catId = elId.match(/^.+_(\d+)$/)[1];
        var runJs = $(this).attr('href').replace('javascript:', '');
        if (runJs) {
            setCookie('cat' + catId, runJs, 30);
            $.address.parameter('catid', catId);
            //eval(runJs);
        }
        return false;
    });
});

