var bResize=1;
$(window).load(function(){

	$('#homepageSpecialsToggle').click(function(){
		if ( $('#homepageSpecialsBottom').hasClass('open') ) { closeSpecials();	}
		else {
			openSpecials(); 
			// start homepage special rotation
			rotateSpecials = true;
			intervalHomeSpecials = setInterval('rotateHomeSpecials();', 3000);
		}
	});
	
	$('#homepageSpecialsNav a').click(function() {
		if ( ! $('#homepageSpecialsBottom').hasClass('open') ) { openSpecials();}
		
		var iClickedIndex = $(this).index();
		var current = ($('#homepageSpecialsBottom ul li.On'));
		var next = $('#homepageSpecialsBottom ul li:eq(' + iClickedIndex + ')')

		showSpecial(current,next);
		
		// stop rotation
		clearInterval(intervalHomeSpecials);
	});		
	
	// click anywhere on special to stop rotation
	$('#homepageSpecialsBottom').click( function() {
		clearInterval(intervalHomeSpecials);
	});
	
	$('#homepageSpecialsNav .total').html( $('#homepageSpecialsBottom ul li').size() );
	
	// start homepage special rotation
	rotateHomeSpecials();
	rotateSpecials = true;	
	intervalHomeSpecials = setInterval('rotateHomeSpecials();', 5000);
	
	
	// add listeners to stop rotation when not in focus and restart if needed
	if (window.addEventListener) {
		window.addEventListener('focus', function() {
			if (rotateSpecials) intervalHomeSpecials = setInterval('rotateHomeSpecials();', 5000);
		});
		window.addEventListener('blur', function() {
			clearInterval(intervalHomeSpecials);
		});		
	} else {
		window.attachEvent("focus", function() {
			if (rotateSpecials) intervalHomeSpecials = setInterval('rotateHomeSpecials();', 5000);
		});
		window.attachEvent('blur', function() {
			clearInterval(intervalHomeSpecials);
		});			
	} 		


	
});

var rotateSpecials = true;
var intervalHomeSpecials = null;
var specialsHeight = 280;


function rotateHomeSpecials() {
	var current = ($('#homepageSpecialsBottom ul li.On'));
	var next = current.next();
	if (!next.length) next = $('#homepageSpecialsBottom ul li:first');
	showSpecial(current,next);
}	

function closeSpecials() {
	// stop rotation
	clearInterval(intervalHomeSpecials);
	rotateSpecials = false;

	$('#homepageSpecialsBottom li').css('display','none');
	$('#homepageSpecialsBottom').removeClass('open');
	$('#homepageSpecialsBottom').animate({
		height: 20
	});
	$('#homepageSpecialsToggle').css('background-position','center bottom');
	
}
function openSpecials() {
	$('#homepageSpecialsBottom').addClass('open');
	$('#homepageSpecialsBottom').animate({
		height: specialsHeight
	});
	$('#homepageSpecialsToggle').css('background-position','center top');
	var current = ($('#homepageSpecialsBottom ul li.On'));
	showSpecial(null, current);
}

function showSpecial(current, next){
	if (bResize==1) {
		specialsHeight = $(next).height() + 40;
	}
	bResize = 1;
	$('#homepageSpecialsBottom').animate({
		height: specialsHeight
		},
		'fast',
		function() {
			$(next).fadeIn('fast');
		}
	);

	$(next).addClass('On');
	$(current).hide();
	$(current).removeClass('On')

	$('#homepageSpecialsNav .showing').html( $('#homepageSpecialsBottom ul li').index($(next))+1 );	

	var iIndexCurrent = $('#homepageSpecialsBottom ul li').index($(current));
	$('#homepageSpecialsNav a:eq(' + iIndexCurrent + ')').removeClass('active');
	var iIndexNext = $('#homepageSpecialsBottom ul li').index($(next));
	$('#homepageSpecialsNav a:eq(' + iIndexNext + ')').addClass('active');
	
}

var ytParams = {
		allowScriptAccess: "always",
		rel : "0",
		autoplay: "1",
		fs: "1",
		wmode: "transparent",
		allowFullScreen: "true"
};

