$(document).ready(function()
{
	$('form').each(function(){$(this).validate();});
	$('.new-window').click(function(){return !window.open($(this).attr('href'));});
	$('.js-hide').hide();
	$('#frm_login_email,#frm_login_password').focus(function(){$(this).parent().find('label').hide();}).trigger('focus');
	$('#frm_login_email,#frm_login_password').blur(function(){if($(this).val() == '')$(this).parent().find('label').show();}).trigger('blur');
	$('.add-to-cart a.buy-now').click(function()
	{
		var q = parseInt($('#frm_qty',$(this).parent()).val());
		var c = $('#frm_colour',$(this).parent()).val();
		$(this).attr('href',$(this).attr('href')+'/'+q+'/'+c);
	});
	$('#basket .qty input').blur(function(){
		var product_id = $(this).attr('id').replace('frm_qty_','');
		var c = '';
		if ($('h3 em',$(this).parent().parent()).length > 0)
			c = $('h3 em',$(this).parent().parent()).text();
		window.location = '/basket/add/'+product_id+'/'+$(this).val()+'/'+c;
	});
	$('.btn .basket').click(function(){
		var $quantity = $('input[name = "qty"]',$(this).parent());
		if ($quantity.length == 1)
			$(this).attr('href',$(this).attr('href')+'/'+$quantity.val());
	});
	$('#submit-code a').click(function(){$('#discountcodeform').submit();});
/*
	if ($('#frm_same_address_1').attr('checked') == true)
		$('#delivery-info').hide();
	$('#frm_same_address_1,#frm_same_address_0').change(function(){
		if ($('#frm_same_address_1').attr('checked') == true)
			$('#delivery-info').slideUp('fast');
		else
			$('#delivery-info').slideDown('fast');
	});
*/
	$('#delivery-info #frm_country_2').change(function()
	{
		var cnt = $('#no_of_products').val();
		var delivery = false;
		if ($(this).val() != 'New Zealand'){
			delivery = $('#delivery_price_int').val();
			//delivery = $('#delivery_price_int').val() * cnt;
		}
		if (delivery === false){
			if (cnt == 1)
				delivery = $('#delivery_price_1').val()
			else if (cnt > 1 && cnt < 5)
				delivery = $('#delivery_price_2_4').val()
			else
				delivery = $('#delivery_price_5_more').val()
		}
		delivery = delivery+'';
		if (delivery.indexOf('.') == -1){
			delivery = delivery+'.00';
		}
		$('#delivery-price span').html(delivery);
	});
	$('#checkout-continue a').click(function(){
/*
		if ($('#frm_same_address_1').attr('checked') == true){
			$('#billing-info input').each(function(){
				$('#'+$(this).attr('id')+'_2').val($(this).val());
			});
			$('#frm_country_2').val($('#frm_country').val());
		}
*/
		$('#billingdetailsform').submit();
	});
	$('#checkout-complete a').click(function(){$('#paymentdetailsform').submit();});
	theRotator();
	$('a[rel="lightbox"]').lightBox();
});

function theRotator()
{
	if ($('ul#slideshow').length == 0)
		return false;
	$('ul#slideshow li').css({opacity: 0.0});
	$('ul#slideshow li:first').css({opacity: 1.0});
	show_caption($('img',$('ul#slideshow li:first')).attr('alt'));
	setTimeout('rotate()',5000);
}

function rotate()
{
	$('#caption').html('');
	var current = ((typeof $('ul#slideshow li.show').attr('class') != 'undefined')?  $('ul#slideshow li.show') : $('ul#slideshow li:first'));
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('ul#slideshow li:first') :current.next()) : $('ul#slideshow li:first'));
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0},2500);
	current.animate({opacity: 0.0},2500).removeClass('show');
	setTimeout('show_caption("'+$('img',next).attr('alt')+'")',500);
	setTimeout('rotate()',6000);
};

function show_caption(caption)
{
	$('#caption').html(caption);
}

jQuery.extend(jQuery.browser,{SafariMobile:navigator.userAgent.toLowerCase().match(/iP(hone|ad)/i)});

