var page = 1, hpCarousel;

jQuery.ajax({
    type: "GET",
    url: "/content/xml_flash_promo/home_carousel.xml",
    dataType: "xml",
    success: parseCarouselXML
 });
 
function parseCarouselXML(xml) {
	var $xml = jQuery(xml);
	
	//var hdr = '<h3><a href="' + $xml.find('headline link').text() + '">' + $xml.find('headline title').text() + '</a></h3><div id="widgetCarouselIndicators"><span id="wci1" class="on"></span><span id="wci2"></span><span id="wci3"></span></div>';
	var hdr = '<h3>' + $xml.find('headline title').text() + '</h3><div id="widgetCarouselIndicators"><span id="wci1" class="on"></span><span id="wci2"></span><span id="wci3"></span></div>';
	
	
	var content = '<table border="0" cellpadding="0" cellspacing="0"><tr>';
	var j = 1;
	
	$xml.find('item').each(function(index){
		var $this = jQuery(this);
		if ( (index % 3 + 1) == 3 )
			content += '<td class="last">';
		else
			if ( (index % 3 ) == 0 ) content += '<td id="page' + j++ + '">';
			else content += '<td>';
		content += '<div class="twPromo clearfix">';
		content += '<a href="' + $this.find('link').text() + '"><img src="' + $this.find('tout').text() + '" alt="' + $this.find('header').text() + ' ' + $this.find('title').text() + '" border="0" /></a>';
		content += '<div class="txt"><p class="hdr"><strong>' + $this.find('header').text() + '</strong></p><p><a href="' + $this.find('link').text() + '"><strong>' + $this.find('title').text() + '</strong></a></p></div>';
		content += '</div></td>';	
	});
	
	content += '</tr></table>';
	//content += '<a href="#" id="widgetCarouselPrev" class="btnArrow"></a><a href="#" id="widgetCarouselNext" class="btnArrow"></a>';
	
	jQuery('.mainPromoLeft').append('<div id="widgetCarousel"><div id="widgetCarouselTop"></div><div id="widgetCarouselContent"></div></div>');
	
	jQuery('#widgetCarouselTop').html(hdr);
	jQuery('#widgetCarouselContent').html(content);
	jQuery('#widgetCarouselContent table').css('left', '-1210px').animate({
		left : 0
	}, 2000, function(){
		checkCarouselPage();
		hpCarousel = setInterval(function(){
			rotateCarouselPage();	
		}, 7000);
	});
	
	jQuery('#widgetCarouselIndicators span').each(function(index){		
		jQuery(this).click(function(){
			clearInterval(hpCarousel);
			var l = -(jQuery('#page' + (index+1)).position().left);
			jQuery('#widgetCarouselContent table').animate({left: l}, 1000, function(){
				page = index + 1;
				checkCarouselPage();
			});
			//jQuery('#widgetCarouselContent table').css('left', l);
			//page = index + 1;
			//checkCarouselPage();
		});						
	});
	
	//jQuery('#widgetCarouselContent').css('height', jQuery('#widgetCarouselContent table').height());
	
}

function rotateCarouselPage() {
	if ( page == 3 ) {
		page = 0;
		clearInterval(hpCarousel);
	}
	jQuery('#widgetCarouselContent table').animate({
		left : -605
	}, 1000, function(){
		checkCarouselPage();		
		jQuery(this).find('td:lt(3)').appendTo(jQuery(this).find('tr'));
		jQuery(this).css('left','0px');		
	});
	page += 1;	
}

function checkCarouselPage() {
	jQuery('#widgetCarouselIndicators span').removeClass('on');
	jQuery('#wci'+page).addClass('on');	
}


