$(document).ready(function() {
	$('.spotlight img.flash').each(function(idx) {
		this.id = 'spot_' + idx;
	});
	var promoShownNo = 1;
	var promoNo = $('.spotlight').length;
	// transform img to swf object
	var swfs = [], swfLinks = [], swfVars = [], swfNode, vars, keyValue, varObj;
	var slideDurs = [], orgHtml, thePromo, theDur;
	for (var i = 0; i < promoNo; i++) {
		thePromo = $('.spotlight:eq(' + i + ')');
		// construct slide display duaration
		theDur = thePromo.attr('rel');
		if (theDur == undefined)
			slideDurs[i] = 7000;
		else {
			theDur = parseFloat(theDur);
			if (theDur != NaN)
				slideDurs[i] = theDur * 1000;
			else
				slideDurs[i] = 7000;
		}
		// re-constructing flashvars object, if any
		swfNode = thePromo.find('img.flash');
		if (swfNode.length == 0)
			continue;
		orgHtml = swfNode.parent().html();
		swfs[i] = swfNode.attr('src');
		swfLinks[i] = swfNode.parent('a').attr('href');
		varObj = { clickTag: swfLinks[i], moreLink: swfLinks[i] };
		vars = swfNode.attr('rel');
		if (vars != undefined) {
			vars = vars.split('&');
			for (var j = 0; j < vars.length; j++) {
				keyValue = vars[j].split('=');
				eval('varObj.' + keyValue[0] + '="' + decodeURI(keyValue[1]) + '"');
			}
		}
		swfVars[i] = varObj;
		swfNode.parent('a').remove();
		thePromo.html(orgHtml);
	}
	if (swfs[0] != undefined) {
		swfobject.embedSWF(swfs[0], 'spot_0', '990', '467', '9.0.0', {}, swfVars[0],
						   { quality: 'high', wmode: 'opaque' });
	}
	
	// make the width of banner div fit to all spotlight banners
	$('#spots').css('width', (1000 * promoNo) + 'px');
	var promoIndex = 0, promoPgDot;
	var i, timeoutId;
	if (promoNo > promoShownNo) {
		// promo scroller
		var promoScroller = function() {
			// remove current active dot
			promoPgDot.removeAttr('class', '');
			promoIndex++;
			if (promoIndex >= promoNo)
				promoIndex = 0;
			if (swfs[promoIndex] != undefined) {
				swfobject.embedSWF(swfs[promoIndex], 'spot_' + promoIndex,
								   '990', '467', '9.0.0', {}, swfVars[promoIndex], 
								   { quality: 'high', wmode: 'opaque' });
			}
			$('#spotOuter').scrollTo('.spotlight:eq(' + promoIndex + ')', 300, {axis: 'x'});
			var theNum = promoIndex;
			promoPgDot = $('.spotNav li:eq(' + theNum + ') a');
			promoPgDot.attr('class', 'pg-active');
			timeoutId = setTimeout(promoScroller, slideDurs[promoIndex]);
		};
		
		// setup promo nav
		var dots = promoNo;
		var navHtml = '';
		for (i = 0; i < dots; i++) {
			if (i == 0)
				navHtml += '<li><a class="pg-active" href="#">' + i + '</a></li>';
			else
				navHtml += '<li><a href="#">' + i + '</a></li>';
		}
		$('.spotNav').html(navHtml);
		promoPgDot = $('.spotNav li:eq(0) a');
		timeoutId = setTimeout(promoScroller, slideDurs[promoIndex]);
		$('.spotNav li a').click(function(e) {
			e.preventDefault();
			// remove current active dot
			promoPgDot.attr('class', '');
			var theNum = parseInt($(this).text());
			promoIndex = theNum;
			if (swfs[promoIndex] != undefined) {
				swfobject.embedSWF(swfs[promoIndex], 'spot_' + promoIndex,
								   '990', '467', '9.0.0', {}, swfVars[promoIndex],
								   { quality: 'high', wmode: 'opaque' });
			}
			$('#spotOuter').scrollTo('.spotlight:eq(' + promoIndex + ')', 300, {axis: 'x'});
			promoPgDot = $(this);
			promoPgDot.attr('class', 'pg-active');
			clearTimeout(timeoutId);
			// 20 secs auto-rotate after no user action.
			timeoutId  = setTimeout(promoScroller, 20000); 
		});
		
		
	}

});
