// JavaScript Document

$(function(){
	
	
	
	setRequiredFields();

	// go through each form assigning a handler to save buttons
	// making sure that all labels 
	var errorAlert = '<div id="errorAlert" class="message error"><p style="margin:0;">\
						<strong>Error</strong><br/>\
						Please fill in all required fields (marked with a <img src="/img/forms/required.gif" style="background:#fff;padding:3px;" />) before submitting the form.\
						</p></div>';

	$('form').submit(function()
	{
		
		$('.error').remove();
		
		var ret = true;
		var f = $(this).parent();
		var errorText = '<p><strong>Please fix the following errors:</strong><br/>';

		$('label[class="required"]', $(this)).each(function()
		{
			var lf = $(this).attr('for');
			var e = $('*[name="' + lf + '"]', f);
			var v = e.val();
			if ( v == '' )
			{
				e.addClass('errorInput').effect("pulsate", { times: 3 }, 150);
				$('#errorAlert').remove();
				$('p.message').after(errorAlert);
				$('#errorAlert').show();
				errorText += '<span style="width:30%;float:left;">' + lf.replace(/_/g, ' ').replace('cust', '').replace('del', 'Delivery') + '</span>';

				ret = false;
			}
			else
			{
				$('#errorAlert').hide();
				e.removeClass('errorInput');
				// ret = true;
			}
		});

		if ( ret === false )
		{
			$('#errorAlert').remove();
		
			errorText + '</p>';
			$(this).before(errorAlert);
			window.location.href += '#';
		}
		
		return ret;
		
	});

	
	
	$('#searchterm')
	.focus(function()
	{
		if ( $(this).val() == 'product search' )
		{
			$(this).val('');
		}
		
	})
	.blur(function()
	{
		if ( $(this).val() == '' )
		{
			$(this).val('product search');
		}
	});
	
	
	
	// alt table row colours
	$('table.admintable tr:odd td').css('background-color', '#f6f6f6');
	$('table.admintable tr:even td').css('background-color', '#f0f0f0');
	
	
	
	
	// add to basket buttons
	$('#productOptionsForm').submit(function()
	{
		var ret = false;
		var colourSelected = false;
		$('.pQty', $(this)).each(function()
		{
			var v = parseInt($(this).val(), 10);
			var colourID = $(this).attr('id').replace(/pQty/, 'pColour');
			if ( v > 0 && $('option:selected', $('#' + colourID)).val() != '' )
			{
				ret = true;
			}
		});
		
		if ( ret == false )
		{
			alert('Please enter a number in the quantity box and select a colour to continue and add to basket.');
		}
		return ret;
	});
	
	
	

	$('.slow_cycle').cycle(
	{
		speed: 1000, 
		timeout: 8500
	}						 
	)
	
	
	$('.medium_cycle').cycle(
	{
		speed: 1000, 
		timeout: 6500
	}						 
	)
		
		
	$('.fast_cycle').cycle(
	{
		speed: 1000, 
		timeout: 3500
	}						 
	)
	
	$('.d0_cycle').cycle(
	{
		speed: 1000, 
		timeout: 4000
	}						 
	)
	
	$('.d1_cycle').cycle(
	{
		delay: 200,
		speed: 1000, 
		timeout: 4000
	}						 
	)
	
	$('.d2_cycle').cycle(
	{
		delay: 400,
		speed: 1000, 
		timeout: 4000
	}						 
	)
	
	$('.navigation > ul > li').mouseover(function(){
		
		$(this).css('position','relative');
		$('a',this).toggleClass('hover');
		
		if($('ul',this).length){
			$('ul',this).show();
		}
	}).mouseout(function(){
		
		if($('ul',this).length){
			$('ul',this).hide();
			$('a',this).toggleClass('hover');
		}
		else {
			$('a',this).toggleClass('hover');
		}
	});
	
	$("a.lightbox").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200
	});

	
});
