var ShepherdMarine = {};
/**
 * Stolen shamelessly from Ben Nolan's Behaviour, but using Prototype's $$ to do the selector-magic
 */
ShepherdMarine.Behaviour = {
	/**
	 * Contains the rules that will be applied when go is called
	 * @type {Array}
	 */
	rulesets: [],
	/**
	 * Adds a ruleset to the rules array
	 * @param {Object} rules An object-literal (hash) of css-selector=>function(element) pairs
	 */
	add: function(rules) {
		ShepherdMarine.Behaviour.rulesets.push(rules);
	},
	/**
	 * Applies all currently registered roles against the supplied context
	 * @param {Object} [d] A context for the getElementsBySelector call, defaults to document
	 */
	go: function(d) {
		d = d || document;
		ShepherdMarine.Behaviour.rulesets.each(function(ruleset) {
			$H(ruleset).each(function(pair) {
				Element.getElementsBySelector(d,pair.key).each(function(node) {
					pair.value($(node));
				});
			});
		});
	}	
};

ShepherdMarine.rules = {
	'#nav li.level0 a': function(a) {
		a.observe('click', function(e) {
			$$('#nav ul.level1').invoke('hide');
			if (a.up('li').down('ul')) {
				a.up('li').down('ul').show();
				Event.stop(e);
				return false;
			}
		});
	},
	'#custom_options': function(dl) {
		var fs  = dl.up('fieldset');
		setTimeout(function() {
			fs.insertBefore(dl,fs.down());
		},0.5);
		var select = dl.down('select');
		var value = window.location.toString().split('-').last().sub(/\/$/,'');
		var option = $A(select.options).find(function(opt) {
			return opt.innerHTML.toLowerCase().strip() == value;
		});
		if(option) {
			option.selected = true;
		}	
		select.observe('change', function(e) {
			$('configurable_product_spinner').show();
			var sel = Event.element(e);
			var range = sel.options[sel.selectedIndex].innerHTML.toLowerCase().strip();
			var ranges = ['jotun', 'ral', 'standard', 'bs4800', 'bs381c'];
			if(ranges.indexOf(range)==-1) {
				return;
			}
			var url = window.location.toString();
			var new_url = url.replace(/-[a-z0-9]+\/?$/i,'-' + range);
			window.location = new_url;
		});
	},
/*	'#product-options-wrapper dl:first-child dd:nth-child(2)': function(dd) {
		var select = dd.down('select');
		var other_select = dd.next('dd').down('select');
		select.observe('change', function(e) {
			var value = $F(other_select);
			setTimeout(function() {
				console.log(value);
			},1);
		});
	},
*/
	'#product-options-wrapper dl dd.last select.super-attribute-select': function(select) {
		if(!ShepherdMarine.ConfigOptions) {
			return;
		}
		var colour_block = Builder.node('span',{
			className:'colour_block'
		},'____');
		
		select.parentNode.insertBefore(colour_block, select.nextSibling);
		var brand_name = select.up().previous().down().innerHTML.replace(/([a-z0-9]+).+/i,'$1');
		select.observe('change', function(e) {
			var sel = Event.element(e);
			var add_to_cart = sel.up('form').down('fieldset.add-to-cart-box').down('button.form-button');
			add_to_cart.disabled = true;
			
			//get the text from the select, looks like 'Light Blue(Jotun 1234)' 
			var text = sel.options[sel.selectedIndex].innerHTML;
			var options = {
				method: 'get',
				parameters: {
					value: text,
					brand: brand_name
				},
				onComplete: function(xhr) {
					var code = xhr.responseText;
					colour_block.style.backgroundColor = '#' + code;
				}
			};
			//then lookup the hex-code
			new Ajax.Request('/catalog/product/colourpreview', options);
			
			var size_id = $F(sel.up('dl').down('select'));
			var colour_id = $F(sel);
			//var simple_id = ShepherdMarine.ConfigOptions[size_id][colour_id];
			var colour_attribute_id = sel.id.replace('attribute', '');
			var size_attribute_id = sel.up('dl').down('select').id.replace('attribute', '');
			
			
			//then grab the actual price of the simple product and update the HTML
			var attribute = 'id=' + sel.up('form').product.value + '&attribute[' + colour_attribute_id + ']=' + colour_id + '&attribute[' + size_attribute_id + ']=' + size_id; 
			var options2 = {
				parameters: attribute,
				onComplete: function(xhr) {
					add_to_cart.disabled = false;
					var hash = xhr.responseText.evalJSON();
					var price = hash.price;
					$$('span.price').each(function(el) {
						el.up().update('<span class="price">' + price + '</span>');
					});
					//switch the form-action so we add the Simple Product to our cart
					sel.up('form').action = sel.up('form').action.replace(/\/\d+\/$/,'/' + hash.id + '/');
				}
			};
			new Ajax.Request('/catalog/product/getprice', options2);
			
			
		});
	}
}

ShepherdMarine.Behaviour.add(ShepherdMarine.rules);

Event.observe(window,"load",function(e) {
	ShepherdMarine.Behaviour.go();
	
	var top_height = 0;
	var cols = document.getElementsByClassName('collumn');
	for (i=0; i<cols.length; i++) {
		var height = cols[i].getHeight();
		if (height > top_height) {
			top_height = height;
		}
	}
	// Now set all to this height
	for (i=0; i<cols.length; i++) {
		var height = cols[i].getHeight();
		if (height != top_height) {
			
			var new_height;
			if (cols[i].id == 'left_menu_body') {
				new_height = top_height+6;
			} else {
				if (cols[i].id == 'blue_body') {
					new_height = top_height;
				} else {
					new_height = top_height;
				}
			}
			
			cols[i].style.height = new_height + 'px';
		}
	}
	
	Event.observe($('search_submit_button'),"click",function(e) {
		$('search_mini_form').submit();
	});
	
});