/////////////////////////////////////////////////////////
// home.js
// Taco Bell - Home feature
//
// author: Rayman Wong
// contact: rayman.wong@gmail.com
// created on: 7/18/2011
// last modified: 7/25/2011
//
// dependencies: 
// jQuery (1.4.2), jQuery UI (1.8.14)
//
// comments: still work in progress
/////////////////////////////////////////////////////////

var homeCarousel;
var navDots;

$(document).ready( function(){
	var timeOfDay = getTimeOfDay();
	//timeOfDay = 'evening';
	$('body').addClass(timeOfDay);
	showTimeOfDayIcon(timeOfDay);
	
	navDots = $('.jcarousel-control .dots');
	
	// fill appropriate time of day content (to fill carousel slides)
	// must be done before initializing carousel
	var homeFeature = $('#homeFeature');
	// new code grabs data from within page markup
	homeFeature.append($('#home-'+timeOfDay+' ul'));
	
	// set configs for homepage carousel
	var config = {
		scroll: 1,
		visible: 1,
		initCallback: mycarousel_initCallback,
		itemLoadCallback: mycarousel_itemLoadCallback,
		// This tells jCarousel NOT to autobuild prev/next buttons
		buttonNextHTML: null,
		buttonPrevHTML: null,
		auto: 3,
		animation: "slow",
		easing: "easeInOutCubic",
		wrap: "circular",
		itemFallbackDimension: 920
	}
	// initialize carousel
	homeCarousel = setupCarousel($('#homeFeature'), config);
});

// callback function for carousel
function mycarousel_initCallback(carousel) {
	// generate navigation dots based on # of carousel slides
	var cSize = carousel.size();
	for (var i = 0; i < cSize; i++){
		var index = i+1;
		var htmlStr = '<a href="#" class="navBullet">' + index + '</a>';
		
		navDots.append(htmlStr);
	}
	
	// stop auto advance of carousel on hover
	carousel.clip.hover( 
		function(){
			carousel.stopAuto();
		},
		function(){
			carousel.startAuto();
		}
	);
	
	// delegate click action to 'dots' that are created
	$('.jcarousel-control').delegate('a.navBullet', 'click', function(){
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
	});
	
	// next button click event
	$('#mycarousel-next').bind('click', function() {
		carousel.next();
		return false;
	});
	
	// prev button click event
	$('#mycarousel-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
};

// callback function for carousel
function mycarousel_itemLoadCallback(carousel) {
	
	var navDotLinks = navDots.find('a.navBullet');
	var cntItemIndex;
	
	// calculates current index of carousel slide
	if (carousel.first < 0) cntItemIndex = (carousel.first % carousel.size()) + carousel.size() - 1;
	else {
		cntItemIndex = (carousel.first % carousel.size()) - 1;
		cntItemIndex = (cntItemIndex < 0) ? carousel.size()-1 : cntItemIndex;
	}
	
	// sets active class of nav dots based on current index
	navDotLinks.removeClass('active');
	navDotLinks.eq(cntItemIndex).addClass('active');
}

function showTimeOfDayIcon(td){
	var imgPath = SITE_IMG_PATH + '/day-time-icons/' + td + '.png';
	$('#timeOfDay img').attr('src', imgPath);
	
	$('#timeOfDay:hidden').delay(300).fadeIn(1500);
}

