/*!
 * (c) 2008-09 Jason Crosse www.Antanova.com
 * Client: Arc-on
 */

/* for hover bg colour see also main_ie6.css */
$(document).ready( function(){
	antanova.zoom();							
});

var antanova = 
{
	/* adds hover to category pages in IE6 */
	ie6: function()
	{
		$('.catpage li').hover(
			function(e)
			{
				this.style.backgroundColor = '#A1C7DF';
			},
			function(e)
			{
				this.style.backgroundColor = 'transparent';
			}
		);
	},
	
	zoom: function()
	{
		// Bind an event listener so that when the image changes, it fades back up on a load event.
		$('#bigimg').load( function(){$(this).animate({opacity:1}, 750); })
		
		// Remove the href attributes and put them in rel to stop clicking from overriding the javascript
		$('.images ul a')
			.attr('rel', function(){ return this.href; })
			.attr('href','javascript:void(0);')
			.click(
				function(e)
				{
					var self = this;
					// this line prevents it from fasding out and reloading if it's the currently zoomed image. 
					// That's because Safari doesn't trigger a load even in that case, meaning the image fades out and 
					// stays blank.
					var iurl = $('#bigimg').attr('src').split('/');
					if( iurl.pop() == self.rel.split('/').pop() ) return;
					
					// fade out the big image, then when that's complete, change the src
					$('#bigimg')
						.animate({opacity:0}, 750, function(){
							$(this).attr('src', self.rel);
						})
				}
			)
	},
	
	// tables function only used on size-guide page
	tables: function()
	{
		// make every other row background #f8f8f8
		$('tr:nth-child(odd)')
			.css("background-color", "#f8f8f8");
	},
	
	email: function()
	{
		// replace every id of 'e' with an email link to ian
		$('#e').html( '<a href="mailto:' + String.fromCharCode(0x69, 0x61, 0x6e, 0x40, 0x61, 0x72, 0x63, 0x2d, 0x6f, 0x6e, 0x2e, 0x63, 0x6f, 0x2e, 0x75, 0x6b) + '">' + String.fromCharCode(0x69, 0x61, 0x6e, 0x40, 0x61, 0x72, 0x63, 0x2d, 0x6f, 0x6e, 0x2e, 0x63, 0x6f, 0x2e, 0x75, 0x6b) + '</a>');
	},
	
	// function to show images on the news pages.
	newspics: function()
	{
		$('#newsimages')
			.prepend('<p class="enlarged"><img id="enlarged" src="#" alt="Enlarged image"></p>')
		$('#newsimages li a')
			.attr('rel', function(){ return this.href; })
			.attr('href','javascript:void(0);')
			// gets the rel attribute from the a element, then adds it to the href of the enarged image
			// then, at the onload event makes the large image visible.
			.click(function(){
				var h = this.rel;
				if( this.rel.split('/').pop() == $('#enlarged').attr('src').split('/').pop() )
				{
					if($('.enlarged').is(':hidden'))
					{
						$('.enlarged').slideDown();
					}
					else
					{
						$('.enlarged').slideUp();
					}
				}
				else
				{
					$('.enlarged').slideUp('normal',function(){
						$('#enlarged')
							.attr('src',h)
							.load(function(){
								$('.enlarged').slideDown()
							})
					});
					
				}
			})
	},
	
	gallery: function()
	{
		
		// bind load event listener for image change
		$('.rgalleryi img').load(function(){ $(this).animate({opacity:1},750) });
		
		
		$('#newsimages li a')
			.attr('rel', function(){ return this.href; })
			.attr('href','javascript:void(0);')
			// gets the rel attribute from the a element, then adds it to the href of the enarged image
			// then, at the onload event makes the large image visible.
			.click(function(){
				var h = this.rel;
				if( this.rel.split('/').pop() == $('.rgalleryi img').attr('src').split('/').pop() )
				{
					return;
				}
				else
				{
					$('.rgalleryi img').animate({opacity:0},750,function(){
						$('.rgalleryi img')	.attr('src',h)
					})
					
				}
			})
	},
	
	
	/**
	* This works on the racing page and adds click hadling to the large thumbnail images.
	*/
	racingThumbs: function()
	{
		$('.anthumb').hover(
			function()
			{
				// over
				window.status = $(this).find('.ancaption a').attr('href');
			},
			function()
			{
				// out
				window.status = '';
			}
		).click(function()
		{
			// click
			window.location = $(this).find('.ancaption a').attr('href');
		})
	},
	
	/**
	* All products page mouseover functions
	* Temp - delete this?
	*/
	allProducts: function()
	{
		// make the mouseover show with colour changed border and background change
		$('#allprods .apcat').hover(
			function(){
				$(this).addClass('hover');
			},
			function(){
				$(this).removeClass('hover');
			})
			.click(function(){
				window.location = $(this).find('.aplink a').attr('href');
			})
		
		// click moves to the category page if the image has been clicked.
		$('#allprods .apimg').click(function(){
				
		})
		
	},
	
	// use to validate checkout form
	validate: function(form)
	{
		var message = '';
		if(form.title.value.length==0)
		{
			message += 'Please enter your title in the box provided\n\n';
		}
		if(form.firstname.value.length==0)
		{
			message += 'Please enter your first name in the box provided\n\n';
		}
		if(form.surname.value.length==0)
		{
			message += 'Please enter your surname in the box provided\n\n';
		}
		if(form.eaddr.value.length==0)
		{
			message += 'Please enter your email address in the box provided\n\n';
		}
		else if( !(/^.+\@.+\..{2,5}/.test(form.eaddr.value)) )
		{
			message += 'Please check the email address you have entered is correct\n\n';
		}
		if(form.contactno.value.length==0)
		{
			message += 'Please enter your phone number in the box provided\n\n';
		}
		if(form.address1.value.length==0)
		{
			message += 'Please enter the first line of your address in the box provided\n\n';
		}
		if(form.town.value.length==0)
		{
			message += 'Please enter your town in the box provided\n\n';
		}
		if(form.postcode.value.length==0)
		{
			message += 'Please enter your postcode in the box provided\n\n';
		}
		else if( !(/^[A-PR-UWYZ][A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]? [0-9][ABD-HJLNP-UW-Z]{2} *$/i.test(form.postcode.value)) )
		{
			message += 'Please check the postcode you have entered is correct\n\n';
		}
		
		if(message!='')
		{
			alert(message);
			return false;
		}
		else
		{
			return true;
		}
	},
	
	// validation for the contact form
	cvalidate: function(form)
	{
		var message = '';
		if(form.cname.value.length==0)
		{
			message += 'Please enter your name in the box provided\n\n';
		}
		
		if(form.cemail.value.length==0)
		{
			message += 'Please enter your email address in the box provided\n\n';
		}
		else if( !(/^.+\@.+\..{2,5}/.test(form.cemail.value)) )
		{
			message += 'Please check your email address is correct\n\n';
		}
		
		if(form.cmessage.value.length==0)
		{
			message += 'Please enter your message in the box provided\n\n';
		}
		
		if(message!='')
		{
			alert(message);
			return false;
		}
		else
		{
			return true;
		}
	}
}	
