var $nav = false;
var timeout = false;
var newsletter_timeout = false;
$(document).ready(function()
{
	if ($.simplyScroll) $('#scroller').simplyScroll({autoMode:'loop',frameRate:44,speed:1});
	$('.tinyscrollbar').tinyscrollbar({sizethumb:17});
	$('.hide').hide();
	$('form').each(function(){$(this).validate();});
	$('.new-window').live('click',function(){return !window.open($(this).attr('href'));});

	$('.trigger').hover(function()
	{
		clearTimeout(timeout);
		$t = $(this);
		var the_id = '#sub'+$t.attr('id');
		if ($nav !== false && '#'+$nav.attr('id') != the_id) $nav.fadeOut();
		$nav = $(the_id);
		var t = $t.offset().top+$t.outerHeight();
		var l = $t.offset().left - ($nav.outerWidth() / 2) + ($t.outerWidth() / 2);
		$nav.css({'top':t,'left':l}).fadeIn();
	},function(){timeout = setTimeout('$nav.fadeOut();',500);});
	$('.subnav').hover(function(){clearTimeout(timeout);},function(){timeout = setTimeout('$nav.fadeOut();',500);});

	$('#nav-newsletter').hover(function()
	{
		$('#subnav-newsletter').css({'top':'-1000px','left':'-1000px'}).show();
		clearTimeout(newsletter_timeout);
		var t = $(this).offset().top + $(this).outerHeight() + 14;
		var l = $(this).offset().left + ($(this).outerWidth() / 2) - ($('#subnav-newsletter').outerWidth() / 2);
		$('#subnav-newsletter').hide().css({'top':t,'left':l}).fadeIn();
	},function()
	{
		if (!$('#subnav-newsletter').hasClass('focused'))
			newsletter_timeout = setTimeout("$('#subnav-newsletter').fadeOut();",500);
	});
	$('#subnav-newsletter').hover(function()
	{
		clearTimeout(newsletter_timeout);
	},function()
	{
		if (!$('#subnav-newsletter').hasClass('focused'))
			newsletter_timeout = setTimeout("$('#subnav-newsletter').fadeOut();",500);
	});

	$('#frm_newsletter_name, #frm_newsletter_email').focus(function()
	{
		$('#subnav-newsletter').addClass('focused');
	});
	$('#frm_newsletter_name, #frm_newsletter_email').blur(function()
	{
		$('#subnav-newsletter').removeClass('focused');
	});

	$('#product-range .img').mouseover(function(){$(this).addClass('hover');});
	$('#product-range .img').mouseout(function(){$(this).removeClass('hover');});
	$('#product-range .img').click(function()
	{
		$('#product-range .hi').removeClass('hi');
		$(this).addClass('hi');
		var the_id = '#'+$(this).attr('id').replace('mini-','');
		$('#range .show').fadeOut('fast',function(){
			$(this).removeClass('show').addClass('hide');
			$(the_id).fadeIn('fast').removeClass('hide').addClass('show');
		});
		return false;
	});

	$('#subnav-range a').click(function()
	{
		if (window.location.pathname.indexOf('/range') != 0)
			return true;
		$('#product-range .hi').removeClass('hi');
		var the_id = $(this).attr('rel').replace('subnav-','');
		$('#mini-'+the_id).addClass('hi');
		$('#range .show').fadeOut('fast',function(){
			$(this).removeClass('show').addClass('hide');
			$('#'+the_id).fadeIn('fast').removeClass('hide').addClass('show');
		});
		if ($nav != false) $nav.fadeOut();
		return false;
	});

	$('.view-ingredients a').click(function()
	{
		$('.view-ingredients').hide();
		$('.hide-ingredients').show();
		$('.ingredients').slideDown('fast');
		return false;
	});

	$('.hide-ingredients a').click(function()
	{
		$('.hide-ingredients').hide();
		$('.view-ingredients').show();
		$('.ingredients').slideUp('fast');
		return false;
	});

	$('.btn .basket').click(function()
	{
		var $quantity = $('input[name = "qty"]',$(this).parent());
		if ($quantity.length == 1)
			$(this).attr('href',$(this).attr('href')+'/'+$quantity.val());
	});

	$('#checkout a').click(function()
	{
		var samples = '';
		var i = 0;
		$('#sample-range .hi').each(function()
		{
			if (i < no_of_samples)
			{
				samples = samples+$(this).attr('id').replace('free-sample-','')+',';
			}
		});
		if (samples != '')
			$(this).attr('href',$(this).attr('href')+'/'+samples);
	});

	$('#sample-range .img').mouseover(function(){
		if ($('#sample-range .hi').length < no_of_samples)
			$(this).addClass('hover');
	});
	$('#sample-range .img').mouseout(function(){$(this).removeClass('hover');});
	$('#sample-range .remove').click(function(){
		$(this).parent().removeClass('hi');
		return false;
	});
	$('#sample-range .img').click(function()
	{
		if ($('#sample-range .hi').length < no_of_samples)
		{
			$(this).addClass('hi');
			var the_id = '#'+$(this).attr('id').replace('mini-','');
			$('#samples .show').fadeOut('fast',function()
			{
				$(this).removeClass('show').addClass('hide');
				$(the_id).fadeIn('fast').removeClass('hide').addClass('show');
			});
		}
		return false;
	});

	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');
	});

	$('#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());
		}
		$('#yourdetailsform').submit();
	});

	$('#submit-code a').click(function()
	{
		$('#discountcodeform').submit();
	});

	$('#submit-newsletter a').click(function()
	{
		$('#newslettersignupform').submit();
	});

	$('#ingredients area').click(function()
	{
		$('#default,div.country').hide();
		$($(this).attr('href')).show().addClass('selected-country');
		return false;
	});
	$('#ingredients area').hover(function(){
		if ($('.selected-country').length == 0){
			$('#default,div.country').hide();
			$($(this).attr('href')).show();
		}
	},function(){
		if ($('.selected-country').length == 0){
			$($(this).attr('href')).hide();
			$('#default').show();
		}
	});

	$('#marine .img img').hover(function()
	{
		return show_marine_content($(this),$('#marine .img img.hi'));
	},function()
	{
		//if ($(this).hasClass('hi'))
			//return;
		//$(this).attr('src',$(this).attr('src').replace('_on','_off'));
	});
	$('#marine .img img').click(function()
	{
		return show_marine_content($(this),$('#marine .img img.hi'));
	});
});

function show_marine_content($this, $hi)
{
	if ($this.hasClass('hi'))
		return false;

	if ($hi.length > 0)
	{
		$hi.removeClass('hi').attr('src',$hi.attr('src').replace('_on','_off'));
		var $id = $hi.parent().attr('href').replace('box-','');
		$($id).hide();
	}

	$this.addClass('hi').attr('src',$this.attr('src').replace('_off','_on'));
	var $id = $this.parent().attr('href').replace('box-','');
	$($id).fadeIn();
	return false;
}
