var hoverDelayID;
var hoverHideDelayID;

$(function(){
	//deals hover and pagination utilities
	dealsPerPage = 8; //number of deals to show at once
	initialHoverDelayMilliseconds = 6000; //milliseconds to wait before collapsing deals popout
	hoverDelayMilliseconds = 2000; //milliseconds to wait before collapsing deals popout
	
	dealsAreVisible = false;
	
	if ($.browser.mozilla || $.browser.safari)//|| ($.browser.msie && $.browser.version >= 7))
		$('img.featured-deals-tag').attr('src','/resources/eda49c056d/images/vrfresh/deals-featured-tag-bg24.png');
		
	if ($('#featured-deals-list > li:not(.paginate)').length < dealsPerPage) //dont show pagination if deals are less than dealsPerPage
		$('#featured-deals-list > li.paginate').hide();
	
	$('#featured-deals-list > li:not(.paginate):gt('+parseInt(dealsPerPage-1)+')').hide();
	$('#featured-deals-list > li.paginate > strong > a.next').click(function(){
		paginateDeals('next'); return false;
	});
	
	$('#featured-deals-list > li.paginate > strong > a.prev').click(function(){
		paginateDeals('prev'); return false;
	});

	$('.featured-deals-tag > a').click(function(){
		return false;
	});
	
	$('.featured-deals-tag').hover(
		function(){
			clearTimeout(hoverDelayID);
			//$('#featured-deals-list-bg').show();
			$('.featured-deals-tag').stop({clearQueue:true});
			$('#featured-deals-list-bg').animate({right:'254px', opacity:1 }, 500, function(){
				$('.green-arrow').show();
			});
			dealsAreVisible = true;
		},
		function(){
			clearTimeout(hoverHideDelayID);
			if (dealsAreVisible) {
				$('.featured-deals-tag').animate({opacity:1.0}, hoverDelayMilliseconds, function(){
					$('.green-arrow').hide();
					$('#featured-deals-list-bg').animate({right:'20px', opacity:0}, 500, function(){
						$('.green-arrow').hide();
					});
				});
				dealsAreVisible = false;
			}
		}
	);
	
	$('.featured-deals-tag > strong, .featured-deals-tag > a').hover(
		function(){
			dealsAreVisible = false;
			$('.featured-deals-tag').stop({clearQueue:true});
			//$('#featured-deals-list-bg').stop({clearQueue:true});
		},
		function(){
			dealsAreVisible = false;
			$('.featured-deals-tag').stop({clearQueue:true});
			//$('#featured-deals-list-bg').stop({clearQueue:true});
		}
	);
	
	$('#featured-deals-list-bg').hover(
		function(){
			$('.featured-deals-tag').stop({clearQueue:true});
			dealsAreVisible = true;
		},
		function(){
			$('.featured-deals-tag').animate({opacity:1.0}, hoverDelayMilliseconds, function(){
				$('.green-arrow').hide();
				$('#featured-deals-list-bg').animate({right:'20px', opacity:0}, 500, function(){
					$('.green-arrow').hide();
				});
			});
		}
	);
	
	hoverDelayID = setTimeout(function(){
		$('.featured-deals-tag').trigger('mouseover');
	}, hoverDelayMilliseconds);
	
	hoverHideDelayID = setTimeout(function(){
		$('.featured-deals-tag').trigger('mouseout');
	}, initialHoverDelayMilliseconds);

});

function paginateDeals(direction) {
	$elems = (direction == "next") ? $('#featured-deals-list > li:not(.paginate):visible:last').nextAll('li:not(.paginate)') : $('#featured-deals-list > li:visible:first').prevAll();
	$('#featured-deals-list > li:not(.paginate)').hide();
	count = 0;
	$elems.each(function(){ count++; if (count <= dealsPerPage) $(this).show();});
	if ($('#featured-deals-list > li:not(.paginate):last:visible').length > 0) $('.next').hide();
	else $('.next').show();
	if ($('#featured-deals-list > li:first:visible').length == 0) $('.prev').show();
	else $('.prev').hide();
}


