/* BUG SOLVE
 *
 * This code solves many problems related to when IE6 users have page loads set to always 
 ************************************************************************************************/
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


$(document).ready( function() {
	var popupbox=$("div#product-selector-holder div.product-popup").get()[0];
	var productsstage=$("div#product-selector-holder div.products-stage").get()[0];
	var productsfade=$("div#product-selector-holder div.products-fade").get()[0];
	var popupboxwidth=525;
	var popupboxheight=276;
	var productstageleftoffset=200;
	var currentlyfading=false;
	
	
	$("div#product-selector-holder div.product-block").click( function() {
		if (currentlyfading) {
			return false;	
		}
		// Only active (or selected) products can be clicked
		if (this.className.indexOf("selected") == -1) {
			return false;	
		}
		currentlyfading=true;
		$(popupbox).find("h5").text($(this).find("span.range").text());
		$(popupbox).find("h4").text($(this).find("span.title").text()).css({color: "#"+$(this).find("span.headline-colour").text()});
		$(popupbox).find("h6").text($(this).find("span.tag-line").text());
		$(popupbox).find("p").text($(this).find("span.description").text());
		$(popupbox).find("img.main-image").attr("src", $(this).find("span.image-src").text());
		$(popupbox).find("div.hold-graphic").css({background: "url(/assets/ui/product-selector/"+$(this).find("span.hold-graphic").text()+")"});
		// $(popupbox).find("img").attr("src", $(this).find("img").attr("src"));

		var stagewidth=$(productsstage).width()-2;
		var stageheight=$(productsstage).height();
		
		$(productsfade).css({width: stagewidth, height: stageheight, left: productstageleftoffset, opacity: 0, display: "block"})
		var popupleft=parseInt((stagewidth-popupboxwidth)/2+productstageleftoffset);
		var popuptop=parseInt((stageheight-popupboxheight)/2);
		$(popupbox).css({top:popuptop, left:popupleft, opacity: 0, display:"block"});

		$(productsfade).animate({opacity: 0.75}, 500, function() {
			$(this).css({opacity: 0.75});
			$(popupbox).animate({opacity: 1}, 500, function() {
				$(this).css({opacity: 1});
				currentlyfading=false;	
			});
		});
		return false;
	});
	
	$("div#product-selector-holder div.product-popup a.close-button").click( function() {
		if (currentlyfading) {
			return false;	
		}
		currentlyfading=true;
		
		$(popupbox).animate({opacity: 0}, 500, function() {
			$(productsfade).animate({opacity: 0}, 500, function() {
				$(popupbox).find("img").attr("src","/assets/ui/empty.image.gif");
				$(popupbox).css({opacity: 0, display:"none"});
				$(this).css({opacity: 0, display:"none"});
				currentlyfading=false;
			});
		});
		return false;
	});

	$("div#product-selector-holder ul.range input").click( function() {
		var thisrange=$(this).parent().find("label").text();
		var thisischecked=$(this).attr("checked");
		if (thisischecked===true) {
			$("div#product-selector-holder div.product-block").each(function() {
				if ($(this).find("span.range").text().indexOf(thisrange) !== -1) {
					thisproductblock=this;
					thisproducttype=$(this).find("span.product-type").text();
					$("div#product-selector-holder ul.product-type input").each( function() {
						var typeischecked=$(this).attr("checked");
						var thistype=$(this).parent().find("label").text();
						if (typeischecked===true && thisproducttype.indexOf(thistype)!=-1) {
							$(thisproductblock).addClass("product-block-selected");
						}
					});
				}
			});
		}
		else {
			$("div#product-selector-holder div.product-block").each(function() {
				if ($(this).find("span.range").text().indexOf(thisrange) !== -1) {
					$(this).removeClass("product-block-selected");
				}
			});
		}
	});


	$("div#product-selector-holder ul.product-type input").click( function() {
		var thistype=$(this).parent().find("label").text();
		var thisischecked=$(this).attr("checked");
		if (thisischecked===true) {
			// Add products
			// Now iterate through each product
			$("div#product-selector-holder div.product-block").each(function() {
				// And check that it is of the selected product type
				if ($(this).find("span.product-type").text().indexOf(thistype) !== -1) {
					thisproductblock=this;
					thisproductrange=$(this).find("span.range").text();
					$("div#product-selector-holder ul.range input").each( function() {
						var rangeischecked=$(this).attr("checked");
						var thisrange=$(this).parent().find("label").text();

						if (rangeischecked===true && thisproductrange.indexOf(thisrange)!=-1) {
							$(thisproductblock).addClass("product-block-selected");
						}
					});
				}
			});
		}
		else {
			$("div#product-selector-holder div.product-block").each(function() {
				if ($(this).find("span.product-type").text().indexOf(thistype) !== -1) {
					$(this).removeClass("product-block-selected");
				}
			});
		}
	});

	$("div#product-selector-holder a.close-window").click( function() {
		window.close();
		return false;
	});
	
});