/** =coverage */

	function calcCoverage(form) {
		if(isNaN(parseFloat(document.getElementById('h_cm').value))) { document.getElementById('h_cm').value = 0; }
		if(isNaN(parseFloat(document.getElementById('h_m').value))) { document.getElementById('h_m').value = 0; }
		if(isNaN(parseFloat(document.getElementById('w_cm').value))) { document.getElementById('w_cm').value = 0; }
		if(isNaN(parseFloat(document.getElementById('w_m').value))) { document.getElementById('w_m').value = 0; }
		
		var h = parseFloat(document.getElementById('h_m').value)+(parseFloat(document.getElementById('h_cm').value)/100);
		var w = parseFloat(document.getElementById('w_m').value)+(parseFloat(document.getElementById('w_cm').value)/100);
		var coverage = (parseFloat(h)*parseFloat(w))/parseInt(document.getElementById('coverage').value);
		if(!document.getElementById('coverage-result')) {
			var p = document.createElement('p');
			p.id="coverage-result";
		} else {
			var p = document.getElementById('coverage-result');
		}
		if(!isNaN(coverage)) {
			p.innerHTML="For one coat you will require approximately "+coverage.toFixed(1)+"L";
			if(!document.getElementById('coverage-result')) { form.parentNode.appendChild(p); }
		}	
		return false;
	}
	
/** =shop */

	function shop() {
		$$("ul#shop-paint-samples li.category ul li").each(function(el){ el.observe("mouseover", function(e) { this.addClassName("active");}); });
		$$("ul#shop-paint-samples li.category ul li").each(function(el){ el.observe("mouseout", function(e) { this.removeClassName("active");});});
		
		/* Lightview buy option */
		$$("div.shop .colour-block a.buynow").each(function(el){
			el.observe("click",function(e){
				var args = getArgs(this.readAttribute("href"));	
				$$("div#colour-example p.colour-name").invoke("setStyle",{"background": "#"+args.colour});
				$$("p#paint-preview").invoke("setStyle",{"backgroundColor": "#"+args.colour});
				$$("input#colour-selection").invoke("setValue",args.colour);
				$$(".dyn-select").each(function(el){el.update("Change your colour<span></span>");});
				$$(".dyn-colour").each(function(el){el.update(args.name);});
				$$(".dyn-colour").invoke("addClassName","active");
				Lightview.hide();
				$("select-options").setStyle({"opacity":1});
				
				$$("#select-options input").each(function(el){el.disabled=false;});
				$$("#select-options select").each(function(el){el.disabled=false;});
				
				Event.stop(e);
			});
		});
	
		/* Paint product page colour selection */
		$$("div#colour-example ul#favourites li a").each(function(el){
			el.observe("click",function(e){
				var args = getArgs(this.readAttribute("href"));	
				$$("div#colour-example p.colour-name").invoke("setStyle",{"background": "#"+args.colour});
				$$("p#paint-preview").invoke("setStyle",{"backgroundColor": "#"+args.colour});
				$$("input#colour-selection").invoke("setValue",args.colour);
				$$(".dyn-select").each(function(el){el.update("Change your colour<span></span>");});
				$$(".dyn-colour").each(function(el){el.update(args.name);});
				$$(".dyn-colour").invoke("addClassName","active");
				Lightview.hide();
				$("select-options").setStyle({"opacity":1});
				$$("#select-options input").invoke("writeAttribute","disabled","");
				$$("#select-options select").invoke("writeAttribute","disabled","");
				
				$$("#select-options input").each(function(el){el.disabled=false;});
				$$("#select-options select").each(function(el){el.disabled=false;});
				Event.stop(e);
			});
		});
		
		/* Some non-standard hover-states */
		$$("ul#shop-home li").each(function(el){ el.observe("mouseover",function(){this.addClassName("active");});});
		$$("ul#shop-home li").each(function(el){ el.observe("mouseout",function(){this.removeClassName("active");});});
		$$("ul#shop-home li").each(function(el){ el.observe("click",function(){ window.location=$(this).select("a")[0].readAttribute("href"); });});
		
		$$("ul#shop-swatches li").each(function(el){ el.observe("mouseover",function(){this.addClassName("active");});});
		$$("ul#shop-swatches li").each(function(el){ el.observe("mouseout",function(){this.removeClassName("active");});});
		$$("ul#shop-swatches li").each(function(el){ el.observe("click",function(){ window.location=$(this).select("a")[0].readAttribute("href"); });});
		
		$$("ul#shop-paint-samples li.category ul li").each(function(el){ el.observe("mouseover",function(){this.addClassName("active");});});
		$$("ul#shop-paint-samples li.category ul li").each(function(el){ el.observe("mouseout",function(){this.removeClassName("active");});});
		$$("ul#shop-paint-samples li.category ul li").each(function(el){ el.observe("click",function(){ window.location=$(this).select("a")[0].readAttribute("href"); });});	
		
		$$("ul.colour-gateway li").each(function(el){ el.observe("mouseover",function(){this.addClassName("active");});});
		$$("ul.colour-gateway li").each(function(el){ el.observe("mouseout",function(){this.removeClassName("active");});});
		$$("ul.colour-gateway li").each(function(el){ el.observe("click",function(){ window.location=$(this).select("a")[0].readAttribute("href"); });});
		
	}

/** ==misc */

	function getQVar(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
		return pair[1]; } }
	} 

	function getArgs(fullURL) {
		var args = new Object(); 
		var query = fullURL.split("?");
		var pairs = query[1].split("&"); 
		for(var i = 0; i < pairs.length; i++) { 
		var pos = pairs[i].indexOf('='); 
		if (pos == -1) continue; 
			var argname = pairs[i].substring(0,pos); 
			var value = pairs[i].substring(pos+1); 
			args[argname] = unescape(value); 
		} 
		return args; 
	}
	
	function paintColours(){						
		if($("paint-related")) {	
			
			/* Price list interaction */
			$('price-list').hide();
			
			$('price-list-cta').observe("click", function(e) {
				element = Event.element(e);
				
				if(element.hasClassName('active')) {
					element.removeClassName('active');
					$('price-list').hide();
				} else {	
					element.addClassName('active');
					$('price-list').show();									
				}					
				
				Event.stop(e);	
			});
			
			/* ajax the paint colours up*/
			function listFavs() {
				var timestamp = Number(new Date());
				new Ajax.Request("/release/includes/php/fetch-favourites.php?output=html&nocache="+timestamp, {
					method: 'get',
					onSuccess: function(transport) {
						$("favourites").innerHTML=transport.responseText;
						$$("ul#favourites span.options a").each(function(el){
							el.observe("click",function(e){
								var args = getArgs($(this).readAttribute("href"));
								var timestamp = Number(new Date());
								new Ajax.Request("/release/includes/php/remove-favourites.php?cookieValue="+args.cookieValue+"&nocache="+timestamp, {
									method: 'get',
									onSuccess: function(transport) { listFavs(); }
								});
								Event.stop(e);
							}.bind(el));	
						});
					}
				});
			}
		
			Ajax.Responders.register({
				onCreate: function() { $$("div#paint-favourites h2").invoke("setStyle",{"backgroundImage":"url(/release/images/icons/ajax-loader.gif)"}); },
	  			onComplete: function() { $$("div#paint-favourites h2").invoke("setStyle",{"backgroundImage":"none"}); }
	  		});
	  		
			/* Just add to favourites */
			$$(".colour-block a.fav").each(function(el){
				el.observe("click",function(e){
					var args = getArgs($(this).readAttribute("href"));
					var timestamp = Number(new Date());
					new Ajax.Request("/release/includes/php/store-favourites.php?cookieValue="+args.cookieValue+"&nocache="+timestamp, {
						method: 'get',
						onSuccess: function(transport) { listFavs(); }
					});
					new Effect.ScrollTo("container");
					Event.stop(e);
				}.bind(el));	
			});
			
			/* Add to favourites then continue to shop */
			$$(".colour-block ul.options a.buy").each(function(el){
				el.observe("click",function(e){
					var args = getArgs($(this).readAttribute("href"));
					var timestamp = Number(new Date());
					new Ajax.Request("/release/includes/php/store-favourites.php?cookieValue="+args.cookieValue+"&nocache="+timestamp, {
						method: 'get',
						onSuccess: function(transport) { window.location="/paint-shop/paint-and-paint-samples/"; }
					});
					Event.stop(e);
				}.bind(el));	
			});
			
			/* Remove from favs */
			$$("ul#favourites span.options a").each(function(el){
				el.observe("click",function(e){
					var args = getArgs($(this).readAttribute("href"));
					var timestamp = Number(new Date());
					new Ajax.Request("/release/includes/php/remove-favourites.php?cookieValue="+args.cookieValue+"&nocache="+timestamp, {
						method: 'get',
						onSuccess: function(transport) { listFavs(); }
					});
					Event.stop(e);
				}.bind(el));	
			});
		}
		
	}

document.observe("dom:loaded", function() {

	shop();
	paintColours();
	
	/** =flash-alts */
	if(document.getElementById('interactive-room')) swfobject.embedSWF("/release/includes/swf/Room_Interactive.swf", "interactive-room", "920", "610", "8.0.0"); 

	/* news */
	if(document.getElementById('news-navigation')) {
		// Grab the name of the first anchor in each news item and assign an ID to the item
		var newsitems = document.getElementById('news-items').getElementsByTagName('div');
		for(i=0;i<newsitems.length;i++) {
			if(newsitems[i].className="item") {
				var anchors = newsitems[i].getElementsByTagName('a');
				for(x=0;x<anchors.length;x++) {
					if(anchors[x].name) {
						newsitems[i].id=anchors[x].name;
					}	
				}
			}
			
		}
		var newsjumplist = document.getElementById('news-navigation').getElementsByTagName("ul");
		news = newsjumplist[0].getElementsByTagName('a');
		for(i=0;i<news.length;i++) {
			news[i].onclick=function(){ newsJump(this); return false; };
		}
		 newsJump(news[0]);
	}
	
	/* coverage-calculator */
	if(document.getElementById('coverage-calculator')) { document.getElementById('calculator').onsubmit=function(){calcCoverage(this); return false;}; }

	/* Default template gallery */
	if($$("#gallery img.thumb").length>0) {
		$$("#gallery img.thumb").invoke('setStyle',{'cursor':'pointer'});
		$$("#gallery img.thumb")[0].setStyle({"opacity":0.5});
		$$("#gallery img.full")[0].setStyle({"zIndex":20});
		
		$$("#gallery img.thumb").each(function(el){
			el.observe("click",function(e){
				$$("#gallery img.thumb").invoke("setStyle",{"opacity":1});
				this.setStyle({"opacity":0.5});
				$$("#gallery img.full").invoke("setStyle",{"zIndex":10});
				$$("#gallery img.full[alt='"+this.readAttribute("alt")+"']").invoke("setStyle",{"zIndex":20});
			}.bind(el));
		});
	}
	
	/* Some form responses */
	var formerror = false;
	formerror = getQVar("formerror");
	if(formerror=="subscribe") {
		var error = document.createElement("p");
		error.className="error";
		error.innerHTML="Please enter your name and a valid email address.";
		$$('.subscribe').invoke("insert",{before:error});
	}
	
	if(formerror=="contact") {
		var error = document.createElement("p");
		error.className="error";
		error.innerHTML="Please enter your name, email address and enquiry.";
		$$('.form-contact').invoke("insert",{before:error});
	}
	
	if(formerror=="competition") {
		var error = document.createElement("p");
		error.className="error";
		error.innerHTML="Please enter your name, email address and postcode.";
		$$('.form-competition').invoke("insert",{before:error});
	}
	
});