$(function()
{
	

	


	
	// language select
	$('#languageSelectLink').bind('mouseover click', function()
	{
		$('#languageSelect').css({'visibility':'visible'});
	});
	$('#languageSelectLink a, #languageSelect a').mouseover(function()
	{
		$('#languageSelect').css({'visibility':'visible'});
	});
	// out
	$('#languageSelectWrap').mouseout(function()
	{
		$('#languageSelect').css({'visibility':'hidden'});
	});
	
	
	
	
	
	
	// add pointy hands to buttons
	$('input[type=button], input[type=submit]').css({'cursor':'pointer'});


	// index featured product slider
	var fl = $('#indexFeatureSlider li').length;
	if ( fl > 3 )
	{
		$('#indexFeatureSlider').jcarousel(
		{
			auto: 5,
			animation: 'slow', 
			wrap: 'circular',
			scroll: 1, 
			initCallback: p8moor_initCallback, 
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
	}
	else
	{
		$('#indexFeatureSliderWrap').hide();
	}


	
	// togglers on forum
	$('.toggler').toggle(
		function()
		{
			$('#topic_' + $(this).attr('id').replace(/toggler_/, '')).show();
			$('span.label', this).text(TextTranslation_hide);
		}, 
		function()
		{
			$('#topic_' + $(this).attr('id').replace(/toggler_/, '')).hide();
			$('span.label', this).text(TextTranslation_view);
		}
	);

	$('.topicWrap table.forum tr.row_post a.togglerReplies').toggle(
		function()
		{
			id = $(this).attr('id');
			$('tr.row_reply.' + id).show();
			$('span.label', this).text(TextTranslation_hide);
		}, 
		function()
		{
			$('tr.row_reply', $(this).closest('tbody')).hide();
			$('tr.row_reply.' + id).hide();
			$('span.label', this).text(TextTranslation_view);
		}
	);


	
	// external links
	$('a.external').click(function()
	{
		$(this).attr('target', '_blank');
	});
	
	// h1 logo link
	$('#logoWrap')
	.css({'cursor':'pointer'})
	.click(function()
	{
		window.location.href = '/';
	});
	
	

	// append a asterisk to the required fields labels
	$('label.required').append('<img src="/img/forms/required.gif" alt="Required" style="margin:0 0 5px 5px;" />');

	// 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;" />)</p></div>';
	*/
	
	var errorAlert = '<div id="errorAlert" class="message error"><p style="margin:0;">' + TextTranslation_errorAlertText + '</p></div>';
	
	$('form').submit(function()
	{
		
		$('.error').remove();
		
		var ret = true;
		var f = $(this).parent();
		var errorText = '<p><strong>' + TextTranslation_errorAlertHeading + '</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.split('#')[0] += '#';
		}
		
		return ret;
		
	});

	

	
	

	
	
	
	
	
	
	// alt table row colours
	$('table.admintable tr:odd td').css('background-color', '#f6f6f6');
	$('table.admintable tr:even td').css('background-color', '#f0f0f0');
	
	
	// orders page date range
	$('#date_from').datepicker(
	{
		altField: '#date_from_formatted', 
		altFormat: 'd/mm/yy', 
		showOn: 'button',
		buttonImage: '/img/icons/calendar.png',
		buttonImageOnly: true
	});
	$('#date_to').datepicker(
	{
		altField: '#date_to_formatted', 
		altFormat: 'd/mm/yy', 
		showOn: 'button',
		buttonImage: '/img/icons/calendar.png',
		buttonImageOnly: true
	});
	
	
	// promo code date range
	$('#expiry_date_from').datepicker(
	{
		altField: '#expiry_date_from_formatted', 
		altFormat: 'DD, d MM, yy', 
		showOn: 'button',
		buttonImage: '/img/icons/calendar.png',
		buttonImageOnly: true
	});
	
	$('#expiry_date_to').datepicker(
	{
		altField: '#expiry_date_to_formatted', 
		altFormat: 'DD, d MM, yy', 
		showOn: 'button',
		buttonImage: '/img/icons/calendar.png',
		buttonImageOnly: true
	});
	
	
	
	
	
	


	
	
	
	
	
	
	
	
	
	
	
	


	
	// payment choice
	$('.paymentChoiceBox').click(function()
	{
		$(this).css('border', '2px solid #f00 !important;');
		$('input[type=radio]', '.paymentChoiceBox').attr('checked', false);
		$('img', '.paymentChoiceBox').each(function()
		{
			var c = $(this).attr('src');
			var cn = c.replace(/_c/, '_g');
			$(this).attr('src', cn);
		});
		
		$('input[type=radio]', $(this)).attr('checked', 'checked');
		var t = $('img', $(this)).attr('src');
		var tn = t.replace(/_g/, '_c');
		$('img', $(this)).attr('src', tn);

		$('#checkoutFinalSubmit').attr('value', 'Pay via ' + $('img', $(this)).attr('alt'));
	});
	
	
	
	
	// corner the th's inside table.nice
	$('tr th', 'table.nice').each(function()
	{
		var t = $(this).html();
		$(this).css({'padding':'0'}).html('<div class="cornerMe" style="padding:4px;">' + t + '</div>');
	});
	
	
	
	// corners...
	corners();
	cornersInvert();
	cornersGrey();
	
	
	
	
	
	
	
	// product page - product thumbs / main image swap
	$('#productThumbs img').each(function()
	{
		$(this)
		.css('cursor', 'pointer')
		.click(function()
		{
			n = $(this).attr('src').replace(/_sqr/, '_l');
			l = $(this).attr('src').replace(/_sqr/, '_xl');
			t = $(this).attr('title');
			$('#productMainImage').attr('src', n);
			$('#productMainImageLink').attr('href', l).attr('title', t).lightBox();
			$('#productMainImageDescription em').text(t);
		});
	});
	
	$('#productMainImageLink').lightBox();
	$('#content a.lb').lightBox();
	
	
	$('#productThumbs img:first').trigger('click');

	
	
	// append css3 stylesheet to the head...
	$('head').append('<link href="/css/css3.css" rel="stylesheet" type="text/css" media="screen" />');
	
	
	
	

	
	
});

