

(function(){



	//vars

	var BRC = {
		prodDetailsPrice:'',
		prodDetailsExVatPrice:'',
		prodDetailsImage:'',
		prodDetailsImageLarge:''
	};


	/*	DOM ready... and so it begins...  */

	window.addEvent('domready', function(){

	
		// ::: javascript for all pages

		//adds all styles and events to product listing slider

		$$('.prod').addEvents({
		    'mouseover': function(){
		    	var el = this.getElements('div');
		    	el.set('tween', {duration:100, transition:Fx.Transitions.Quad.easeIn});
		    	el.tween('bottom', 0);
		    },

		    'mouseout': function(){
		    	var slidePos = $$('body.home').length > 0 ? BRC.prodSlide.home : BRC.prodSlide.list;
		        var div = this.getElements('div').tween('bottom', slidePos);
		    },

		    'click': function(){
		    	var aLink = this.getElement('a');
		    	if(aLink) { window.location = aLink.getAttribute('href'); }
		    }

		}).setStyle('cursor', 'pointer');


		// textbox help text show/hide		

		$$('input.lowlight').addEvents({
		    'focus': function(){
		        initTextboxHelpToggle(this);
		    },

		    'blur': function(){
		        initTextboxHelpToggle(this);
		    }
		});


		// showhide link div toggle

		$$('.showhide').addEvent('click', function(){ showhideEl(this); });


		// ::: page spacific javascript 

		if($$('body.details').length > 0){
			
			//for product images			
			var zoom = new PanZoomer({
		    	imageMain : $('zoomimage'),
		        imageWrapper : $('prodimg-wrap'),
		        zoomLink: $('zoomlink'),
		        thumbWrapper: $('prodimg-thumb'), 
		        enlargeLink: $('enlargelink')
			});

			SqueezeBox.assign($$('a[rel=boxed]'));

			//event for options box

			if($('ddlopt'))	{ 
				BRC.prodDetailsPrice = $('product_price').get('text');
			
				$('ddlopt').addEvent('change', function(e){ changeProductAttributes(this, e); }); 				
				
				$('addtobasket').addEvent('click', function(e){ 
					if($('ddlopt').get('value')==0){
						e.stop();
						alert('Please Choose an option first');
					}
				}); 
			}

		}
		

		if($$('body.basket').length > 0){
			//add events to drop downs

			$$('tbody select').addEvent('change', function(e){ 
				var id=this.get('id').replace('ddlname', '');
				window.location = '/pages/basket.html?id='+id+'&q='+this.get('value');
			});

			//add js back

			$('shopping-links-goback').addEvent('click', function(e){ 
				e.stop();
				history.go(-1);
			});
		}

	});
	
	
	var changeProductAttributes = function(el, e){
		
		//change price on product details page		
		var total = BRC.prodDetailsPrice;
		
		//get id for attribute 
		var id = el.get('value');
		
		if(id==0){
			$('product_price').set('text', BRC.prodDetailsPrice);
			return false;
		}
		
		var data = 'sID=attribute&id='+id;
		
		var doOutput = function(resp){
			if(resp!='false'){
				var obj = JSON.decode(resp);
				$('product_price').set('text', obj.price);
			}
		};
		var req = new Request({url:'/site/services/index.html', onSuccess:doOutput}).send(data);
	};
	
	var startBannerRotation = function(selector,delay){ 
		var i = 0; 
		var layers = $$(selector); 
		delay = (delay == undefined)? 6000:delay;

		//fade funcion as a var
		var runFadeInit = function(){
			layers[i].fade('out'); 
			i = (i == layers.length-1)? 0 : i+1; 
			layers[i].fade('in');
		}
		runFadeInit();//so it runs once before the interval fires
		
		setInterval(runFadeInit,delay);
	};

	var initTextboxHelpToggle = function(el){

		//when a textbox has helpful text in by default - show/hide it

		var title = el.get('title');

		var value = el.get('value');

		

		if(title == value){

			el.set('value', '');

			el.removeClass('lowlight');

		}

		else if(value == ''){

			el.set('value', el.get('title'));

			el.addClass('lowlight');

		}

		

	}; 

	var navMenuShowHide = function(el, nav, event){

		event.stop(); //stop link from firing

		nav.getElements('li.level1 > ul').addClass('hide'); //hide all

		el.getNext().toggleClass('hide');					//show current

	}



	

	var initProductOptions = function(el, e){

		var value = (el.options[el.selectedIndex].title);

		if(el.get('value')==0) value = BRC.prodDetailsPrice;			

		$('product_price').set('text', value);

	}


	var showhideEl = function(el){

		var target = $(el.getProperty('rel'));

		if(target.hasClass('hide'))

			{ target.removeClass('hide');}

		else 

			{ target.addClass('hide');}

	}

})();


Number.implement({

	/*
	Property: numberFormat
		Format a number with grouped thousands.

	Arguments:
		decimals, optional - integer, number of decimal percision; default, 2
		dec_point, optional - string, decimal point notation; default, '.'
		thousands_sep, optional - string, grouped thousands notation; default, ','

	Returns:
		a formatted version of number.

	Example:
		>(36432.556).numberFormat()  // returns 36,432.56
		>(36432.556).numberFormat(2, '.', ',')  // returns 36,432.56
	*/

	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) + ''); // returns matches[1] as sign, matches[2] as numbers and matches[3] as decimals
		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) : '');
	}


});
