var cycleTimer;

$().ready(function() {
	
	$('a[rel=external]').click(function() {
		window.open($(this).attr('href'));
		return false;
	});
	
	
	// Clicking the product delivery popup link on the product delivery screen
	$('#product_delivery_popup_launch').click(function() {
		$("#product_delivery_popup").dialog('open');
		return false;
	});
	// // Hero content cycling
	// if ($('#hero-content li').length > 1)
	// {
	// 	$('#hero-content li:gt(0)').fadeTo(0,0).hide();
	// 	cycleTimer = setTimeout(function() {
	// 		cycleHeroContent();
	// 	}, 5000);
	// }
	if ($('#product_delivery_popup').length > 0) {
			$('#product_delivery_popup').dialog({
				autoOpen: false,
				width: 450,
				height: 395,
				draggable: true,
				modal: true,
				close: function(event, ui) {
					$("#qtyselect").show()
				},
				open: function(event, ui) {
					$("#qtyselect").hide(); //for IE6 when delivery_popup is in use
				}
			});
		};
    
  $('#hero-content').mouseover( function(event) {
    $('#hero-content').cycle('pause');
  });
  
  $('#hero-content').mouseout( function(event) {
    $('#hero-content').cycle('resume');
  });
});

function cycleHeroContent()
{
	var visible = $('#hero-content li:visible');
	var visibleIndex = $('#hero-content li').index($(visible));
	$(visible).fadeTo(1500, 0, function() {
		$(this).hide();
		if (visibleIndex == ($('#hero-content li').length - 1))
		{
			var next = 0;
		}
		else
		{
			var next = visibleIndex + 1;
		}
		$('#hero-content li:eq('+next+')').show().fadeTo(1500, 1, function() {
			cycleTimer = setTimeout(function() {
				cycleHeroContent();
			}, 7000);
		});
	})
}
