/*** 
** Carousel Image for GOLTV 2.0
** Coded done by Abdullah R.
** 24 June 2008
** ---------------------------------------------------
** The following files are needed for this to operate 
** JS JQuery File
** - Located in /js/jquery.js
** CSS File 
** - Located in /css/carousel.css
** XML File
** - Located in /xml/carousel.xml
***********************/

var carousel = {
	selection 	: 0 ,
	cTimer		: '',
	xmlFile		: '/xml/carousel.xml' ,
	interval	: 10000,
	showImage	: true,
	totalElem	: 2,
	updateNav	: function(cIndex) {
		$(".carouselNavDiv a").removeClass();
		// taking care of modulo 
		if( cIndex == 0 ) cIndex = carousel.totalElem;
		$("#cn"+(cIndex+1)).addClass('clSelected');	
	}
}

function startCarousel() {
	
	$.get(carousel.xmlFile, function(data) {
		carousel.totalElem = $(data).find('entry').length;
	});
	
	$('.navSelect a').click( function() {
		var $thisValue = parseInt($(this).attr("id").slice(2));
		clearTimeout( carousel.cTimer ); 
		carousel.selection = ($thisValue-2)%carousel.totalElem;
		swapContent();
		return false;
	});
	
	swapContent();
}

/** The function for swapping images in a timed interval ***/
function swapContent()	{
			
	var htmlCode;
	var typeTxt;
	var urlTxt;
	var captionTxt;
	var temp_counter = 0;
	
	if( carousel.showImage != false ) {
		$(".carouselContentDiv").fadeTo('fast', 0);	
	} else {
	
	$.get(carousel.xmlFile+"?"+Math.ceil(Math.random()*10000000), function(data) {

		$(data).find('entry').each(function() {
			var $entry = $(this);
			if( temp_counter == (carousel.selection%carousel.totalElem) ) {
				typeTxt = $entry.find('type').text();		
				urlTxt = $entry.find('url').text();			
				captionTxt = $entry.find('caption').text();
				carousel.updateNav(carousel.selection%carousel.totalElem);
				if( typeTxt == "image" ) {
					var linkTxt = $entry.find('link').text();
					var altTxt = $entry.find('alt').text();
					htmlCode = '<a href="'+linkTxt+'" title="'+ altTxt +'">'+'<img src="'+urlTxt+'" alt="'+altTxt+'" border="0" />' + '</a>';
				}
			}
			temp_counter++;
		});
		
		if( typeTxt == "html") {
			$(".carouselContentDiv").empty().fadeTo("normal", 1).load(urlTxt);	
			$(".navSelect .carouselInfo").html(captionTxt);			
		} else {
			$(".carouselContentDiv").empty().fadeTo("normal", 1).html(htmlCode);
			$(".navSelect .carouselInfo").html(captionTxt);
		}
	});	
	
	}
	
	if( carousel.showImage == false ) {
		carousel.showImage = true;
		carousel.cTimer = setTimeout( 'swapContent()', carousel.interval);	
	} else {
		carousel.showImage = false;
		carousel.selection++;
		carousel.cTimer = setTimeout( 'swapContent()', carousel.interval*.05 );
	}
}
