Number.extend({
	numberFormat : function(decimals, dec_point, thousands_sep) {
		decimals = Math.abs(decimals) + 1 ? decimals : 2;
		dec_point = dec_point || '.';
		thousands_sep = thousands_sep || ',';

		var matches = /(-)?(\d+)(\.\d+)?/.exec((isNaN(this) ? 0 : this) + '');
		var remainder = matches[2].length > 3 ? matches[2].length % 3 : 0;
		return (matches[1] ? matches[1] : '') + (remainder ? matches[2].substr(0, remainder) + thousands_sep : '') + matches[2].substr(remainder).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep) + 
				(decimals ? dec_point + (+matches[3] || 0).toFixed(decimals).substr(2) : '');
	}
});

window.addEvent("domready", function(){
	//var links = $$("a");
	new SmoothScroll();
	new Lightbox();
	
	$$(".volatile").each(function(el){
		$(el).addEvent("focus", function(){
			if(this.getValue() == this.getProperty("title")) {
				this.value = "";
			}
		});
	});
	
	$$("a[rel*='external']").each(function(a){ 
		$(a).setProperty("target", "_blank");
	});
	
	new Accordion($$(".planninglist .addnote"), $$(".planninglist .note"), { "alwaysHide":true, "show":-1 });
	
	
	$$(".latestnews ul").each(function(el){ new Ticker(el, {interval: 5000}) });
	
	if($("homeflash")){
		new Ticker($E("#homeflash ul"), {interval: 5000});
	}
	
	var section = 0;
	
	if($("nav")){
		$$("#nav img").each(function(el, index){
			if (el.hasClass("selected")) {
				section = index;
			}
		});
		$$("#nav ul").each(function(el, index){
			if (el.hasClass("subnav")) {
				if ($ES("li a.selected", el).length > 0) {
					section = index;
				}
			}
		});
	}
	
	new Accordion($$("#leftcol #nav img"), $$("#leftcol #nav li div"), { "alwaysHide":true, "show":section });
	
	
	if($("photos")){
		var photoScroller = new Fx.Scroll($("photos"));
		var photos = $ES("img", $("photos"));
		
		$$(".productthumbnails li").each(function(el,index){
			el.addEvent("click", function(e){
				new Event(e).stop();
				photoScroller.toElement(photos[index]);
			});
		});
	}
	
	if($("addtoplanning_frombasket")) {
		$("addtoplanning_frombasket").addEvent("click", function(e){
			new Event(e).stop();
			$("basketform").action = "/planning-lists/addto/";
			$("basketform").submit();
		});
	}
	
	if($("basketform")){
		var updateButton = $E("input[name=UpdateBasket]");
		var submitButton = $E("input[name=GoToCheckout]");
		
		updateButton.setStyle("display", "none");
		
		var qDefaultValues = [];
		var qValues = [];
		$$(".shoppingbasket .textfield").each(function(q, i){
			qDefaultValues[i] = qValues[i] = q.getValue();
			q.addEvent("keyup", function(e){
				qValues[i] = this.getValue();
				var difference = qDefaultValues.every(function(item,index){ return item == qValues[index] });
				if(!difference){
					updateButton.setStyle("display", "block");
					submitButton.setStyle("display", "none");
				} else {
					updateButton.setStyle("display", "none");
					submitButton.setStyle("display", "block");
				}	
			});
		});
	}
	
	if($E("select[name=DeliveryType]") && $E("input[name=SetDelivery]")){
		$E("input[name=SetDelivery]").setStyle("display","none");
		$E("select[name=DeliveryType]").addEvent("change", function(e){
			this.form.submit();
		});
	}
	
	if($E(".productoptions")){
		$$(".productoptions tr").each(function(row){
			var price = row.getElement(".productprice");
			if(price) price = price.getValue();
			var subTotal = row.getElement(".productoption_subtotal");
			var qtyEl = row.getElement(".productquantity");
			var qtyStock = row.getElement(".StockLevel");
			
			if(qtyEl){
				qtyEl.addEvent("change", function(){
					var val = this.getValue().toInt();
					if(isNaN(val) || val < 1) this.value = 1;
					subTotal.setHTML("&pound;" + (this.getValue() * price).numberFormat(2));
				});
			}
		});
	}

});
