var haschanged = false;
var lastPageNum=1;
var compareVisible=false;
var lastviewedVisible=false;
setTimeout(getResults, 1000);

function customize_slider(){
	$(".ui-slider-label:odd").css("display","none");
	
	$("#handle_pick_priceFrom > span.ui-slider-tooltip").css("display","none");
	$("#handle_pick_priceTo > span.ui-slider-tooltip > span.ttContent").html("from "+$("#handle_pick_priceFrom > span.ui-slider-tooltip > span.ttContent").html()+" to "+$("#handle_pick_priceTo > span.ui-slider-tooltip > span.ttContent").html());
};

function slider_information(){
	$("#handle_pick_priceTo").change(function(){
		$("#handle_pick_priceTo > span.ui-slider-tooltip > span.ttContent").html("from "+$("#handle_pick_priceFrom > span.ui-slider-tooltip > span.ttContent").html()+" to "+$("#handle_pick_priceTo > span.ui-slider-tooltip > span.ttContent").html());
	});
};
	
function stylefilter(){
	$('input').customInput();	
	$('fieldset.papersize label').html("");
	$('select#pick_priceFrom, select#pick_priceTo')
		.selectToUISlider({
			labels:12,
			sliderOptions: {
							animate: true,								
							change: function(event, ui) {
								haschanged=true;
						        /* if sliders on same value, change one so that they aren't on top of each other */
								var curTo=$("select#pick_priceTo").attr("selectedIndex");
						        var curFrom=$("select#pick_priceFrom").attr("selectedIndex")+1;
						        if (parseInt(curFrom) >= parseInt(curTo) ) {							        
						        	var newVal=curTo-1;
						        	$("select#pick_priceFrom").find('option').eq(newVal).attr('selected', 'selected');
						        	$("select#pick_priceFrom").change();
						        }
							}
			}
		})
		.queue(customize_slider());
    $("div.sliderbounding label").attr("style","display:none;");
    $("div.sliderbounding select").attr("style","display:none;");	
};

function staticInit() {

	if ($("#prodSelectTable").length > 0){
	    $("#prodSelectTable").tablesorter(); 
	    $("#prodSelectTable").tablesorterPager({container: $("#pager")});
	    if ($("#product_search_text").length > 0){
		    $("#prodSelectTable").tablesorterFilter({filterContainer: $("#product_search_text"),
		        filterColumns: [0,1,2],
		        filterCaseSensitive: false});        
	    }
	    lastPageNum=1;	    
	}
}

function getResults() {			
	if (haschanged == true) {
		$("#flashintro").css("display", "none");
		$("#popframe_flashintro").css("display", "none");
		$("#prodSelect_results").hide("slow");
		$("#prodSelect_results_message").show();
        var pricefrom = $("select#pick_priceFrom").val()
        var priceto = $("select#pick_priceTo").val()
        var productType = $("#productType").val()

        if (productType == "jp" || productType == "psc" || productType == "pscf") {        	
        	var pick_papersize_a7 = document.getElementById("pick_papersize-a7").checked;
        	var pick_papersize_a6 = document.getElementById("pick_papersize-a6").checked;
        	var pick_papersize_a5 = document.getElementById("pick_papersize-a5").checked;
        	var pick_papersize_a4 = document.getElementById("pick_papersize-a4").checked;
        	var pick_papersize_a3 = document.getElementById("pick_papersize-a3").checked;
        	var pick_colour_colour = document.getElementById("pick_colour_colour").checked;
        	var pick_colour_bw = document.getElementById("pick_colour_bw").checked;
        }
        
        
        if (productType == "jp") {
        	var pick_feature_duplex = document.getElementById("pick_feature_duplex").checked;
        	var pick_feature_lowertray = document.getElementById("pick_feature_lowertray").checked;
        	var pick_feature_postscript = document.getElementById("pick_feature_postscript").checked;        	
        	var pick_feature_usb = document.getElementById("pick_feature_usb").checked;
        	var pick_connection_wireless = document.getElementById("pick_connection_wireless").checked;
        	var pick_connection_usb = document.getElementById("pick_connection_usb").checked;
        	var pick_connection_parallel = document.getElementById("pick_connection_parallel").checked;
        	var pick_connection_wired_network = document.getElementById("pick_connection_wired_network").checked;
        	
        	$.get(selectorpath + '/productselector.cfc?method=search',{priceFrom:pricefrom,
        																	priceTo:priceto,
        																	productType:productType,
        																	pick_feature_duplex:pick_feature_duplex,
        																	pick_feature_lowertray:pick_feature_lowertray,
        																	pick_feature_postscript:pick_feature_postscript,
        																	pick_feature_highfax:pick_feature_highfax,
        																	pick_feature_usb:pick_feature_usb,
        																	pick_connection_wireless:pick_connection_wireless,
        																	pick_connection_usb:pick_connection_usb,
        																	pick_connection_parallel:pick_connection_parallel,
        																	pick_connection_wired_network:pick_connection_wired_network,
        																	pick_papersize_a7:pick_papersize_a7,
        																	pick_papersize_a6:pick_papersize_a6,
        																	pick_papersize_a5:pick_papersize_a5,
        																	pick_papersize_a4:pick_papersize_a4,
        																	pick_papersize_a3:pick_papersize_a3,
        																	pick_colour_colour:pick_colour_colour,
        																	pick_colour_bw:pick_colour_bw},
                    function(result,status) {
                         showTheResults(result,status);				
                    })    
                    
        } else if (productType == "jf") {
         	var pick_feature_handset = document.getElementById("pick_feature_handset").checked;
        	var pick_feature_answerphone = document.getElementById("pick_feature_answerphone").checked;
        	var pick_feature_modem = document.getElementById("pick_feature_modem").checked;
        	
        	$.get(selectorpath + '/productselector.cfc?method=search',{priceFrom:pricefrom,
        																	priceTo:priceto,
        																	productType:productType,
        																	pick_feature_handset:pick_feature_handset,
        																	pick_feature_answerphone:pick_feature_answerphone,
        																	pick_feature_modem:pick_feature_modem},
                    function(result,status) {
                         showTheResults(result,status);				
                    })        	
        	
        } else if (productType == "jl") {	
        	var pick_feature_querty = document.getElementById("pick_feature_querty").checked;
        	var pick_feature_barcoding = document.getElementById("pick_feature_barcoding").checked;
        	var pick_feature_durablelabels = document.getElementById("pick_feature_durablelabels").checked;
        	var pick_feature_labelcutter = document.getElementById("pick_feature_labelcutter").checked;
        	var pick_feature_rfid = document.getElementById("pick_feature_rfid").checked;
        	var pick_feature_rechargable = document.getElementById("pick_feature_rechargable").checked;
        	var pick_connection_network = document.getElementById("pick_connection_network").checked;
        	var pick_connection_usb = document.getElementById("pick_connection_usb").checked;
        	var pick_connection_serial = document.getElementById("pick_connection_serial").checked;
        	
        	$.get(selectorpath + '/productselector.cfc?method=search',{priceFrom:pricefrom,
        																	priceTo:priceto,
        																	productType:productType,
        																	pick_feature_querty:pick_feature_querty,
        																	pick_feature_barcoding:pick_feature_barcoding,
        																	pick_feature_durablelabels:pick_feature_durablelabels,
        																	pick_feature_labelcutter:pick_feature_labelcutter,
        																	pick_feature_rfid:pick_feature_rfid,
        																	pick_feature_rechargable:pick_feature_rechargable,
        																	pick_connection_network:pick_connection_network,
        																	pick_connection_usb:pick_connection_usb,
        																	pick_connection_serial:pick_connection_serial},
                    function(result,status) {
                         showTheResults(result,status);				
                    }) 
                    
        } else if (productType == "psc") {
        	var pick_feature_bluetooth = document.getElementById("pick_feature_bluetooth").checked;
        	var pick_feature_duplex = document.getElementById("pick_feature_duplex").checked;
        	var pick_feature_lowertray = document.getElementById("pick_feature_lowertray").checked;
        	var pick_feature_postscript = document.getElementById("pick_feature_postscript").checked;
        	var pick_feature_highfax = document.getElementById("pick_feature_highfax").checked;
        	var pick_feature_usb = document.getElementById("pick_feature_usb").checked;
        	var pick_connection_wireless = document.getElementById("pick_connection_wireless").checked;
        	var pick_connection_usb = document.getElementById("pick_connection_usb").checked;
        	var pick_connection_parallel = document.getElementById("pick_connection_parallel").checked;
        	var pick_connection_wired_network = document.getElementById("pick_connection_wired_network").checked;
        	
        	$.get(selectorpath + '/productselector.cfc?method=search',{priceFrom:pricefrom,
        																	priceTo:priceto,
        																	productType:productType,
        																	pick_feature_bluetooth:pick_feature_bluetooth,
        																	pick_feature_duplex:pick_feature_duplex,
        																	pick_feature_lowertray:pick_feature_lowertray,
        																	pick_feature_postscript:pick_feature_postscript,
        																	pick_feature_highfax:pick_feature_highfax,
        																	pick_feature_usb:pick_feature_usb,
        																	pick_connection_wireless:pick_connection_wireless,
        																	pick_connection_usb:pick_connection_usb,
        																	pick_connection_parallel:pick_connection_parallel,
        																	pick_connection_wired_network:pick_connection_wired_network,
        																	pick_papersize_a7:pick_papersize_a7,
        																	pick_papersize_a6:pick_papersize_a6,
        																	pick_papersize_a5:pick_papersize_a5,
        																	pick_papersize_a4:pick_papersize_a4,
        																	pick_papersize_a3:pick_papersize_a3,
        																	pick_colour_colour:pick_colour_colour,
        																	pick_colour_bw:pick_colour_bw},
                    function(result,status) {
                         showTheResults(result,status);				
                    })         	
                    
        } else if (productType == "pscf") {
        	var pick_feature_bluetooth = document.getElementById("pick_feature_bluetooth").checked;
        	var pick_feature_duplex = document.getElementById("pick_feature_duplex").checked;
        	var pick_feature_lowertray = document.getElementById("pick_feature_lowertray").checked;
        	var pick_feature_postscript = document.getElementById("pick_feature_postscript").checked;
        	var pick_feature_highfax = document.getElementById("pick_feature_highfax").checked;
        	var pick_feature_usb = document.getElementById("pick_feature_usb").checked;
        	var pick_connection_wireless = document.getElementById("pick_connection_wireless").checked;
        	var pick_connection_usb = document.getElementById("pick_connection_usb").checked;
        	var pick_connection_parallel = document.getElementById("pick_connection_parallel").checked;
        	var pick_connection_wired_network = document.getElementById("pick_connection_wired_network").checked;
        	
        	$.get(selectorpath + '/productselector.cfc?method=search',{priceFrom:pricefrom,
        																	priceTo:priceto,
        																	productType:productType,
        																	pick_feature_bluetooth:pick_feature_bluetooth,
        																	pick_feature_duplex:pick_feature_duplex,
        																	pick_feature_lowertray:pick_feature_lowertray,
        																	pick_feature_postscript:pick_feature_postscript,
        																	pick_feature_highfax:pick_feature_highfax,
        																	pick_feature_usb:pick_feature_usb,
        																	pick_connection_wireless:pick_connection_wireless,
        																	pick_connection_usb:pick_connection_usb,
        																	pick_connection_parallel:pick_connection_parallel,
        																	pick_connection_wired_network:pick_connection_wired_network,
        																	pick_papersize_a7:pick_papersize_a7,
        																	pick_papersize_a6:pick_papersize_a6,
        																	pick_papersize_a5:pick_papersize_a5,
        																	pick_papersize_a4:pick_papersize_a4,
        																	pick_papersize_a3:pick_papersize_a3,
        																	pick_colour_colour:pick_colour_colour,
        																	pick_colour_bw:pick_colour_bw},
                    function(result,status) {
                         showTheResults(result,status);				
                    })       	
        };
		haschanged = false;
	}			
	setTimeout(getResults, 2000);	 	
};

function showlastviewedlist() {
	$.get(rootpath + "/cms/cmsCustom/brotheruk/elements/productdisplayuk/v1.4.0/productdisplayuk.cfc?method=lastViewedPanel&acttime="+(new Date()).getTime(),{},
		function(result,status) {
			$("#panellastviewed").html(result);
		}
	);	
}

function alertResult() {
	showlastviewedlist();
	showcomparelist();
}

function callDoubleClick(picked_connection,picked_color,picked_feature,picked_budget,picked_papersize) {
		var axel = Math.random() + "";
		var a = axel * 10000000000000;
		var dcFrame = "";
		
		$("#productSelector_dcFrame").remove();
		dcFrame = $(document.createElement("iframe"));
		dcFrame.attr("id", "productSelector_dcFrame");
		dcFrame.css("display", "none");
		dcFrame.attr("src", "http://fls.doubleclick.net/activityi;src=2653420;type=count135;cat=09bro257;u13=" + picked_connection + ";u10=" + picked_color + ";u11=" + picked_feature + ";u8=" + picked_budget + ";u9=" + picked_papersize + ";ord=1;num=" + a);
		$("body").prepend(dcFrame);
}

function showTheResults (result,status) {
	$(document).ready(function(){
	
	$("#prodSelect_results_message").hide();
    $("#prodSelect_results").show("slow").html(result);    
    button_rendering_limited("#resultsdiv");
    cufonStyling();
	$("a.lightbox-enabled").fancybox({
		'titlePosition'	: 'inside',
		'overlayOpacity' : '0.8'
	});    
    
    $("#prodSelectTable").tablesorter(); 
    $("#prodSelectTable").tablesorterPager({container: $("#pager")});
    lastPageNum=1;	
    
	});
};

function cufonStyling() {
	Cufon.replace('.product_item .header .title');
	Cufon.replace('.product_item .header .serial');
	Cufon.replace('div.product_item p.title');
	Cufon.replace('div.product_item div.description p.title');
};

function sortIt(what) {
	var myArray = what.split(",");
	var sorting = [[myArray[0],myArray[1]]]; 	
	$("#prodSelectTable").trigger("sorton",[sorting]); 
	return false; 
};


function ShowPage(newpage) {
	$("#pagenumber").attr("value",newpage - 1).change();	
	$("#pagenum" + newpage).attr("class","active");
	$("#pagenum" + lastPageNum).attr("class","");
	lastPageNum=newpage;	
}

function showAll(totalrows) {	
	$(".pagesize").attr("value",totalrows).change();
	$(".first").click();
	lastPageNum=1;	
}

function getDescription(productID, CatID) {	
	if ($("#additional_information_" + productID).is(":hidden")) {
		$.get(selectorpath + '/productselector.cfc?method=showDescription',{productID:productID,catLevel:CatID},
	            function(result,status) {
			    	$("#additional_information_" + productID).empty();
			    	$("#additional_information_" + productID).append(result).slideDown("slow");
			        $("#show_hide_addinfo_" + productID).html("Close description");
	            })	
      } else {
        $("#additional_information_" + productID).slideUp("slow");
        $("#show_hide_addinfo_" + productID).html("View description");
    };	
};

// On product display pages
function zebra_rows(){
	$("div.productselector_specs dl table tr:even").addClass("even");
	$("div.productselector_specs dl table tr:odd").addClass("odd");
}

function resettheform(formid) {
	$(':input','#prodSelect')
	.not(':button, :submit, :reset, :hidden')
	.val('')
	.removeAttr('checked')
	.removeAttr('selected');	
	$('label','#prodSelect').removeAttr('class');
	$('input','#prodSelect').not(':button, :submit, :reset, :hidden').removeAttr('class');	
	haschanged = true;
}

function specstabs_width(){
	li_width = 0;
	li_count = 0;
	ul_width = $("ul.specs_tabs").width()-5;
	$("ul.specs_tabs > li").each(function(y){
		li_width += $(this).children("a").width();
		li_count++;
	});
	
	padding = Math.round(((ul_width - li_width)/li_count)/2);
	rest = ul_width - (li_width + (padding*li_count)*2);
	
	$("ul.specs_tabs > li").children("a")
		.css("padding-left",padding)
		.css("padding-right",padding)
	$("ul.specs_tabs > li:last a").css("padding-right",(padding + rest));
}

function lastviewedpanel_toggle() {	
	if (lastviewedVisible==false) {
		$("#panellastviewed_full").show();
		$("#panellastviewed_summary").attr("style","visibility :hidden");
    	$("#panellastviewed_full").animate( { height: "110px" }, 500 );   	
    	$("#panellastviewed_bottom").show();
    	lastviewedVisible=true;
    } else {        	
    	$("#panellastviewed_full").animate( { height: "0px" }, 500, function() {
    		$("#panellastviewed_summary").attr("style","visibility :visible");
        	$("#panellastviewed_bottom").hide();
        	$("#panellastviewed_full").hide();
    	} );
    	lastviewedVisible=false;
    };
}	
	

function ShowAsBig(bigImage, bigALT) {
	$("#product_image_big").attr("src",bigImage);
	$("#product_image_big_a").attr("href",bigImage);
	$("#product_image_big_a").attr("title",bigALT);	
}	
