// Kleenheat Gas JavaScript Document

Cufon.replace(".banner_cufon", { fontFamily: 'frutiger_reg' });
Cufon.replace("span.promotitle", { fontFamily: 'frutiger_reg' });
Cufon.replace("span.more", { fontFamily: 'frutiger_reg' });
Cufon.replace(".product_desc h3", { fontFamily: 'frutiger_reg' });
Cufon.replace(".rtitle h3", { fontFamily: 'frutiger_reg' });

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); 
        }

        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};



var searchVals = new Array();
var typeVals = new Array();
var brandVals = new Array();
var refall = 0;

$(document).ready(function() {

    searchVals[0] = 0;
    searchVals[1] = 100;
    
    searchVals[4] = 1;
    searchVals[5] = 6;

    searchVals[6] = 1;
    searchVals[7] = 6;

//    if ($.cookie("roomFrom") != null) {
//        searchVals[0] = $.cookie("roomFrom");
//    }

//    if ($.cookie("roomTo") != null) {
//        searchVals[1] = $.cookie("roomTo");
//    }

//    if ($.cookie("starFrom") != null) {
//        searchVals[4] = $.cookie("starFrom");
//    }

//    if ($.cookie("starTo") != null) {
//        searchVals[5] = $.cookie("starTo");
//    }

//    if ($.cookie("bbqFrom") != null) {
//        searchVals[6] = $.cookie("bbqFrom");
//    }

//    if ($.cookie("bbqTo") != null) {
//        searchVals[7] = $.cookie("bbqTo");
//    }

    $("#productContainer .product").hover(
    function() { $(this).addClass("onhover"); },
    function() { $(this).removeClass("onhover"); }
    );
	
	$("#filter_options").slideDown();
    $("#category_search a.refine").click(function() {
    $("#filter_options").slideDown();
    $(".total_results").addClass("filtering");
    });
    $("#filter_options a.close").click(function() {
    $("#filter_options").slideUp();
    $(".total_results").removeClass("filtering");
    });

    /* Slider Control */
    
    try {
    var rsF = searchVals[0];
    var rsT = searchVals[1];
	
	$("#roomSize").slider({
    range: true,
    min: 0,
    max: 100,
    values: [rsF,rsT],
    slide: function(event, ui) {
            if (ui.values[1] - ui.values[0] < 13) {
                refineSearch(1);
                return false;
            } else {
                refineSearch(1);
                return true;
            }
       },
       change: function(event,ui){
    $("#roomSizeAmt").html(ui.values[0] + ' - ' + ui.values[1] + 'sqm');
    $("#roomStatus").html(ui.values[0] + ' - ' + ui.values[1] + 'sqm');
    searchVals[0] = ui.values[0];
    searchVals[1] = ui.values[1];
			
    $.cookie("roomFrom", ui.values[0]);
    $.cookie("roomTo", ui.values[1]);
            
    refineSearch(1);
    }
    });
    $("#roomSizeAmt").html($("#roomSize").slider("values", 0) + ' - ' + $("#roomSize").slider("values", 1) + 'sqm');
    $("#roomStatus").html($("#roomSize").slider("values", 0) + '- ' + $("#roomSize").slider("values", 1) + 'sqm');
    } catch(ex) {
	
	}
	
    var maxRangeSet = 0;
    var url = "";
    
    if (getCookie("is_east") == "no") {
        url = "/UserControls/Kleenheat/MaxProdPrice.aspx?category=" + ghcategory + "&east=no&pid=" + new Date().getTime(); 
    } else {
        url = "/UserControls/Kleenheat/MaxProdPrice.aspx?category=" + ghcategory + "&east=yes&pid=" + new Date().getTime();
    }    

        
    $.post(url, function(data) {
    searchVals[2] = 1;
    searchVals[3] = data;
    maxRangeSet = data;
	var prF = searchVals[2];
    var prT = searchVals[3];
    $("#priceRange").slider({
    range: true,
    min: 1,
    max: maxRangeSet,
    values: [prF,prT],
    slide: function(event, ui) {

            if (ui.values[1] - ui.values[0] < ((maxRangeSet/100)*12)) {
                refineSearch(1);
                return false;
            } if (ui.values[1] > (maxRangeSet)) {
                return false;
            } else {
                refineSearch(1);
                return true;
            }
       },
       change: function(event,ui){
            $("#priceRangeAmt").html('$' + ui.values[0] + ' - $' + ui.values[1]);
            $("#priceStatus").html('$' + $("#priceRange").slider("values", 0) + ' - $' + $("#priceRange").slider("values", 1));				
            searchVals[2] = ui.values[0];
            searchVals[3] = ui.values[1];
			
            $.cookie("priceFrom", ui.values[0]);
            $.cookie("priceTo", ui.values[1]);
            
            refineSearch(1);

    }
    });

    
    $("#priceRangeAmt").html('$' + $("#priceRange").slider("values", 0) + ' - $' + $("#priceRange").slider("values", 1));
    $("#priceStatus").html('$' + $("#priceRange").slider("values", 0) + ' - $' + $("#priceRange").slider("values", 1));	
	
    });

	var sF = searchVals[4];
    var sT = searchVals[5];
    $("#starRange").slider({
    range: true,
    min: 1,
    max: 6,
    values: [sF,sT],
    slide: function(event, ui) {
    
            if (ui.values[1] - ui.values[0] < 1) {
                refineSearch(1);
                return false;
            } else {
                refineSearch(1);
                return true;
            }
       },
       change: function(event,ui){
    $("#starRangeAmt").html(ui.values[0] + ' - ' + ui.values[1]);
    $("#starStatus").html(ui.values[0] + ' - ' + ui.values[1]);
    searchVals[4] = ui.values[0];
    searchVals[5] = ui.values[1];
			
    $.cookie("starFrom", ui.values[0]);
    $.cookie("starTo", ui.values[1]);
            
    refineSearch(1);
    }
    });

    
    $("#starRangeAmt").html($("#starRange").slider("values", 0) + ' - ' + $("#starRange").slider("values", 1));
    $("#starStatus").html($("#starRange").slider("values", 0) + ' - ' + $("#starRange").slider("values", 1));
	
	
	
	var bF = searchVals[6];
    var bT = searchVals[7];
    $("#bbqAmt").slider({
    range: true,
    min: 1,
    max: 6,
    values: [bF,bT],
    slide: function(event, ui) {
    
            if (ui.values[1] - ui.values[0] < 1) {
                refineSearch(1);
                return false;
            } else {
                refineSearch(1);
                return true;
            }
       },
       change: function(event,ui){
    $("#bbqBurnerAmt").html(ui.values[0] + ' - ' + ui.values[1]);
    $("#bbqStatus").html(ui.values[0] + ' - ' + ui.values[1]);
    searchVals[6] = ui.values[0];
    searchVals[7] = ui.values[1];
			
    $.cookie("bbqFrom", ui.values[0]);
    $.cookie("bbqTo", ui.values[1]);
            
    refineSearch(1);
    }
    });

    $("#bbqBurnerAmt").html($("#bbqAmt").slider("values", 0) + ' - ' + $("#bbqAmt").slider("values", 1));
    $("#bbqStatus").html($("#bbqAmt").slider("values", 0) + ' - ' + $("#bbqAmt").slider("values", 1));

 refineSearch(1);

});



function setType(indexVal,checkItem) {
	if (checkItem.checked == true) {
		typeVals[indexVal] = 1;
	} else if (checkItem.checked == false) {
		typeVals[indexVal] = 0;
	}
}

function setBrand(indexVal,checkItem) {
	if (checkItem.checked == true) {
		brandVals[indexVal] = 1;
	} else if (checkItem.checked == false) {
		brandVals[indexVal] = 0;
	}
}

    function loadProducts() {
        var hr = document.location.href;
        var urlArr = new Array();
        urlArr = hr.split("?");
        var endArr = urlArr[urlArr.length - 1];
        endArr = endArr.replace("#","");
        var splitEnd = endArr.split("=");
        
        if (splitEnd[0] == "category") {
            getProducts(splitEnd[1]);
        } 
    }
