/*** 
** Carousel Image Updating for WFN.TV 2.0
** Coded done by Abdullah Rubiyath.
** 24 June 2008
** ---------------------------------------------------
** The following files are needed for this to operate 
** JS JQuery File
** - Located in /includes/js/jquery.js
** CSS File 
** - Located in /includes/css/carousel.css
** XML File - Default is located below
** - Located in /includes/js/carousel.xml
***********************/

var simpleCarousel = {
	selection		: 0 ,
	timer	 		: '',
	xml				: '/includes/xml/carousel.xml',
	defaultInterval : 8000,
	interval		: 8000,
	total			: 2,
	pause			: '',
	updateNav		: function(cIndex) {
		$(".carouselNavDiv a").removeClass();
		$("#cn"+(cIndex+1)).addClass('clSelected');			
	} ,
	swapContent		: function() {
		var	 thisEntry = {
			htmlCode 	: '', typeTxt  		: '',	
			urlTxt		: '', tempCounter 	: 0,		
			linkTxt		: '', altTxt		: '',
			interval	: ''
		}
	$.get(simpleCarousel.xml, function(data) {

		$(data).find('entry').each(function() {
			var $entry = $(this);
			if( thisEntry.tempCounter == (simpleCarousel.selection%simpleCarousel.total) ) {
				thisEntry.typeTxt 		= $entry.find('type').text();
				thisEntry.urlTxt 		= $entry.find('url').text();
				thisEntry.interval 		= $entry.find('interval').text();
				simpleCarousel.interval = thisEntry.interval ? parseInt(thisEntry.interval)*1000 : simpleCarousel.defaultInterval;

				simpleCarousel.updateNav(simpleCarousel.selection%simpleCarousel.total);
				if( thisEntry.typeTxt == "image" ) {
					thisEntry.linkTxt 	= $entry.find('link').text();
					thisEntry.altTxt 	= $entry.find('alt').text();
					thisEntry.htmlCode 	= '<a href="'+thisEntry.linkTxt+'" title="'+ thisEntry.altTxt +'">'+'<img src="'+thisEntry.urlTxt+'" alt="'+thisEntry.altTxt+'" border="0" />' + '</a>';
				}
			}
			thisEntry.tempCounter++;
		});
		
		if( thisEntry.typeTxt == "html") {
			$(".carouselContentDiv").fadeOut( 'normal', function() {
				$(this).load(thisEntry.urlTxt, { } , function() { $(this).fadeIn("slow") });
			});		
		} else {
			$(".carouselContentDiv").fadeOut('normal', function() {
				$(this).html( thisEntry.htmlCode ).fadeIn("slow");
			});	
		}

		simpleCarousel.selection++;
		if (simpleCarousel.selection == simpleCarousel.total && simpleCarousel.pause !== '') {
			$(simpleCarousel.pause).show();
			simpleCarousel.selection = 0;
		}
		else {
			simpleCarousel.timer = setTimeout(simpleCarousel.swapContent, simpleCarousel.interval);
		}
	});	
	

	} ,
	restart		: function() {
		if( simpleCarousel.pause != '' ) {
			$(simpleCarousel.pause).hide();
		}
		simpleCarousel.selection = 0;
		simpleCarousel.swapContent();	
	}
}


function startCarousel(xmlFile) {
	
	if( xmlFile ) {
		simpleCarousel.xml = xmlFile;
	} 
		
	$('.navSelect a').click( function() {		
		clearTimeout( simpleCarousel.timer );
		simpleCarousel.selection = $(this).html()-1;
		simpleCarousel.swapContent();
		return false;
	});

	$.get(simpleCarousel.xml, function(data) {	
		simpleCarousel.total = $(data).find('entry').length; 
		simpleCarousel.swapContent();	
	});	
}

