$(document).ready(function()
{	
	Shadowbox.init();
	
	if	($('#discount').length)
	{
		$('#discount').click(function()
		{
			$('#redeem-a-voucher').slideToggle();
			$(this).html($(this).html() == 'enter a discount code' ? 'I have no discount code' : 'enter a discount code');
			return false;
		});
	}
	
	if	($('header .just-added-to-cart').length)
	{
		$('header .just-added-to-cart').fadeOut('slow').fadeIn('slow').fadeOut('slow').fadeIn('slow').fadeOut('slow').fadeIn();
	}
	
	if	($('#advanced-search').length)
	{
		$('#advanced-search').click(function()
		 {
			 $('#adv-search-box').toggle();
			 return false;
		 })
	}
	
	if	($('#survey-link').length)
	{
		var display_survey = setInterval("$('#survey-link').click().hide()", 1000);
	}

	if	($('nav .product-search').length)
	{
		$('nav .product-search a').hover(function()
		{
			$(this).css('z-index', 50);
			$('nav .product-search form').css({'z-index': 100, 'position': 'absolute', 'top': '1px', 'right': '10px'}).show();
			return false;
		});
	}
	
	if	($('.product-search #search').length)
	{
		display_product_search = setInterval("$('.product-search #search').fadeOut()", 1000);
		$('.product-search #search').hover(function()
		{
			clearInterval(display_product_search);
			display_product_search = null;
		}, function()
		{
			if	(!display_product_search)
			{
				display_product_search = setInterval("$('.product-search #search').fadeOut()", 1000);
			}
		});
	}

	$('#survey-link').click(function()
	{
		Shadowbox.open({
        	content:    '/show_survey.php',
	        player:     'iframe',
	        title:      'GameTree Online survey',
	        width:      800,          
	        height:     480
	    });
		clearInterval(display_survey);
	});

	//	Run our display_products() function every 5 seconds
	var display_products_interval = setInterval("$('#mac-games-next').click()", 5000);
	$('#mac-games-thumbnails li:first-child a').addClass('active');
	var promo			=	$('#mac-games-thumbnails .active span').html();
	$('#mac-games span').addClass(promo).fadeIn();
	
	$('#mac-games-thumbnails li a').hover(function()
	{
		$('#mac-games span').hide().removeClass();

		var new_alt		=	$(this).find('strong').html();
		var new_src		=	$(this).attr('rel');
		var new_link	=	$(this).attr('href');
		var price		=	$(this).find('em').html();

		$('#mac-games-thumbnails li a').removeClass('active');
		$(this).addClass('active');
		var promo		=	$('#mac-games-thumbnails .active span').html();
		$('#mac-games span').addClass(promo).fadeIn('fast');
		
		$('#mac-games li a').attr({href: new_link, title: new_alt});
		$('#mac-games li a em').html(new_alt+' - buy now from GameTree Online');
		$('#mac-games img').attr({src: new_src, alt: new_alt, title: new_alt});
		$('#mac-games-description strong').html(new_alt);
		$('#mac-games-description em').html(price);
		
		//	Animate the Teaser
		$('#mac-games-description').stop(true, true).fadeIn(500);
	}, function()
	{
		$('#mac-games-description').fadeOut(750);
		$(this).addClass('active');
	})
	

	//	maybe move this into the .hover() state to get rid of the flickering
	$('#mac-games a').hover(function()
	{
		$('#mac-games-description strong, #mac-games-description em').hide();
		$('#mac-games-description strong').show().html($('#mac-games-thumbnails .active').find('strong').html());
		$('#mac-games-description em').show().html($('#mac-games-thumbnails .active').find('em').html());
		$('#mac-games-description').stop(true, true).fadeIn();
	}, function()
	{
		$('#mac-games-description').fadeOut();
	});
	
    // pause rotator when the user is selecting a game
	$('#mac-games-rotator').mouseover(function() {
		clearInterval(display_products_interval);
		display_products_interval = null;
	}).mouseout(function () {
		if (!display_products_interval)
		{
			//Only create a new timer if one doesn't already exist
			display_products_interval = setInterval("$('#mac-games-next').click()", 5000);
		}
	});
	
	$('#mac-games-next').click(function()
	{
		$('#mac-games span').hide().removeClass();
		var count_products	=	$('#mac-games-thumbnails li a.active').parent().next().length;
		var next_link		=	count_products > 0 ? $('#mac-games-thumbnails li a.active').parent().next().find('a').attr('rel') : $('#mac-games-thumbnails li:first-child').find('a').attr('rel');
		var	next			=	count_products > 0 ? $('#mac-games-thumbnails li a.active').parent().next() : $('#mac-games-thumbnails li:first-child');
		
		$('#mac-games-thumbnails li a').removeClass('active');
		next.find('a').addClass('active').click();
		var promo			=	$('#mac-games-thumbnails .active span').html();

		$('#mac-games span').addClass(promo).fadeIn('fast');
		
		var new_alt			=	$('#mac-games-thumbnails .active').find('strong').html();
		var new_link		=	$('#mac-games-thumbnails .active').attr('href');
		var price			=	$('#mac-games-thumbnails .active').find('em').html();
		$('#mac-games a').attr({href: new_link, title: new_alt});
		$('#mac-games img').attr({src: next_link, alt: new_alt, title: new_alt});
		$('#mac-games em').html(new_alt+' - buy now from GameTree Online');
		$('#mac-games-description strong').html(new_alt);
		$('#mac-games-description em').html(price);
		return false;		
	});
	
	$('#mac-games-previous').click(function()
	{
		$('#mac-games span').hide().removeClass();
		var count_products	=	$('#mac-games-thumbnails li a.active').parent().prev().length;
		var previous_link	=	count_products > 0 ? $('#mac-games-thumbnails li a.active').parent().prev().find('a').attr('rel') : $('#mac-games-thumbnails li:last-child').find('a').attr('rel');
		var	previous		=	count_products > 0 ? $('#mac-games-thumbnails li a.active').parent().prev() : $('#mac-games-thumbnails li:last-child');

		$('#mac-games-thumbnails li a').removeClass('active');
		previous.find('a').addClass('active').click();
		var promo			=	$('#mac-games-thumbnails .active span').html();

		$('#mac-games span').addClass(promo).fadeIn('fast');

		var new_alt			=	$('#mac-games-thumbnails li a.active').find('strong').html();
		var new_link		=	$('#mac-games-thumbnails li a.active').attr('href');
		var price			=	$('#mac-games-thumbnails li a.active').find('em').html();
		
		$('#mac-games a').attr({href: new_link, title: new_alt});
		$('#mac-games img').attr({src: previous_link, alt: new_alt, title: new_alt});
		$('#mac-games em').html(new_alt+' - buy now from GameTree Online');
		$('#mac-games-description strong').html(new_alt);
		$('#mac-games-description em').html(price);

		return false;
	});
	
	//	tabber
	var tabs = $('.tabber-content');
	tabs.hide().filter(':first').fadeIn();
	
	$('#tabs a').click(function()
	{
		tabs.hide();
		tabs.filter(this.rel).fadeIn(1000);

		$('#tabs a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
	
	$('#flash').hide().fadeIn('slow');
	
	setTimeout(function()
	{
  		$("#flash:not(.keep)").fadeOut("slow", function()
		{
 			 $("#flash:not(.keep)").remove();
      	});
	}, 7000);
	
	if	($('#tweets').length)
	{
		$("#tweets").tweet();
	}
	
	$('#can-you-run-it-link').click(function()
	{
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname)
		{
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length)
			{
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});
	
	if (navigator.platform == 'iPad') return;
	$('img').not('.thumbnail').lazyload();
});
