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

	if ($$('.stockwarning').length > 0)
	{
		var aStockMessage = $$('.stockwarning').dispose();
		aStockMessage.inject($('deliverydetails'), 'after');
	}
	
	if($('extraimages')) $('extraimages').getElement('li').setStyle('border', '1px solid #ff9a32');
	
	$$('.extraproductimagelink').addEvent('click', function(e){
		e.stop();
		
		$('extraimages').getElements('li').setStyle('border', '1px solid #B5B18C');
		this.getParent('li').setStyle('border', '1px solid #ff9a32');
		
		aNewImageFile = this.get('href');
		aCurrentImageDisplaying = $('productlink').get('href').replace('images/productimages/large/', '');
		
		$('productlink').set('href', 'images/productimages/large/' + aNewImageFile);
		$$('#mainproductimage .productimage')[0].set('src', 'images/productimages/medium/' + aNewImageFile);
		
		//this.set('href', aCurrentImageDisplaying);
		//this.getElement('img').set('src', 'images/productimages/small/' + aCurrentImageDisplaying);
	});

	if ($('ProductsPerPage'))
	{
		$('ProductsPerPage').addEvent('change', function(){
			aProdsPerPage = this.get('value');
			
			aCurrentUrl = document.URL;
			aCurrentUrl = aCurrentUrl.replace(/&PerPage=[0-9][0-9]/,'');
			aCurrentUrl = aCurrentUrl.replace(/\?PerPage=[0-9][0-9]/,'');
			aCurrentUrl = aCurrentUrl.replace(/&PerPage=All/,'');
			aCurrentUrl = aCurrentUrl.replace(/\?PerPage=All/,'');
			
			aCurrentUrl = aCurrentUrl.replace(/&AgeFilter=[0-9][0-9]/,'');
			aCurrentUrl = aCurrentUrl.replace(/\?AgeFilter=[0-9][0-9]/,'');
			
			if (aCurrentUrl.indexOf('?') == -1) aNewLocation = aCurrentUrl + '?PerPage=' + aProdsPerPage;
			else aNewLocation = aCurrentUrl + '&PerPage=' + aProdsPerPage;
			
			window.location = aNewLocation;
		});
	}
	
	if ($('productlink'))
	{
		initImageZoom({overlay: true});
    }

	if ($$('#nostockin').length > 0)
	{
		ShowStockreminderForm();
		if ($('addbasketlink')) $('addbasketlink').setStyle('display', 'none');
	}

	if($('Size'))
	{
		$('Size').addEvent('change', function(){
			aSizeValue = this.getSelected().get('text').toString();
			aSizeID = this.getSelected().get('value');
			
			aOOSFound = aSizeValue.indexOf('out of stock');
			
			if(aOOSFound != -1) ShowStockreminderForm(aSizeID);
			else HideStockreminderForm();
		});
	}

	if ($('SubmitStockAlert'))
	{
		$('SubmitStockAlert').addEvent('click', function(e){
			if ($('AlertEmailAddress').get('value') == '')
			{
				e.stop();
				alert('You must supply an email address to receive stock alerts');
			}
		});
	}
	
	if ($('addbasketlink'))
	{
		if ($('perswarning')) $('perswarning').destroy();
		
		$('addbasketlink').addEvent('click', function (e) {
			
			if ($('perswarning')) $('perswarning').destroy();
			
			if ($('Personalise'))
			{
				if ($('Personalise').get('defaultValue') == $('Personalise').get('value'))
				{
					e.stop();
					
					var aPersonBlankMessageBox = new Element('p#perswarning');
					
					aPersonBlankMessageBox.set('html', 'Please add personalisation details before adding the product to your basket');
					aPersonBlankMessageBox.inject($('content'), 'top');
				}
			}
		});
	}
	
	if ($('updatebasketbutton'))
	{
		if (Browser.Platform.win && Browser.firefox)
		{
			addNewStyle2('input#updatebasketbutton {padding-bottom:7px !important;} #content #basketform input#checkoutproceedbutton {padding-bottom:5px !important;}');
		}
	}
	
	if (Browser.Platform.mac && Browser.firefox)
	{
		addNewStyle('#rightcolumn {top:146px;}');
	}
});

function ShowStockreminderForm(aSizeID)
{
	$('StockAlertForm').setStyle('display', 'block');
	$$('#StockAlertForm #SizeID')[0].set('value', aSizeID);
}

function HideStockreminderForm()
{
	$('StockAlertForm').setStyle('display', 'none');
	$$('#StockAlertForm #SizeID')[0].set('value', '');
}

function addNewStyle2(newStyle) {
    var styleElement = document.getElementById('styles_js');
    if (!styleElement) {
        styleElement = document.createElement('style');
        styleElement.type = 'text/css';
        styleElement.id = 'styles_js';
        document.getElementsByTagName('head')[0].appendChild(styleElement);
    }
    styleElement.appendChild(document.createTextNode(newStyle));
}
