/***** Options *****/
var fadetime = 500; //The length of the image fade
var pagetime = 8000 //Length of time each image is displayed for in milliseconds
var xml_path = '/index.php/rotator_feed';
/***** End Options *****/


//Set some variables to hold the current item, the last item, the XML feed and the interval for the timer
var item, old_item, xml_feed, interval, item_num = 1, num_items;

$(document).ready(function()
{
	//Set an interval to go to the next page every X milliseconds
	interval = setInterval(autoPage, pagetime);
	
	//Retrieve the XML feed with the information for the rotator
	$.get(xml_path, function(xml)
	{
		//Get the first item from the XML
		item = $(xml).find('item:first');
		
		//Get the number of items and display it in the Next button
		num_items = $(xml).find('item').size();
		$('#rotator_next .count').text(item_num+'/'+num_items);
		Cufon.replace('#rotator_next .count');
		
		//Put the xml into a global variable so we can use it elsewhere
		xml_feed = xml;
		
		//Display the item
		displayItem();
		
	}, 'xml');
	
	//Switch to the next item
	$('#rotator_next').click(function()
	{
		clearInterval(interval);
		old_item = item;
		
		if(item.next('item').length)
		{
			item = item.next('item');
			item_num++;
		}
		else
		{
			item = $(xml_feed).find('item:first');
			item_num = 1;
		}
		
		displayItem();
	});

	//Switch to the previous item
	$('#rotator_prev').click(function()
	{
		clearInterval(interval);
		old_item = item;
		
		if(item.prev('item').length)
		{
			item = item.prev('item');
		}
		else
		{
			item = $(xml_feed).find('item:last');	
		}
		
		displayItem();
	});

	function displayItem ()
	{	
		title = item.find('title').text()
		image = item.find('image').text()
		url = item.find('link').text()
		
		//If there is an old image load it above the next image then fade to the next image
		if(old_item)
		{
			old_image = old_item.find('image').text()
			
			$('#rotator_image1').attr('src', old_image);
			$('#rotator_image1').show();
			$('#rotator_image2').attr('src', image);
			$('#rotator_image1').fadeOut('slow');
		}
		else
		{
			$('#rotator_image2').attr('src', image);
		}
		
		setTimeout(function()
		{
			$('#rotator_title').text(title);
			$('#rotator_view').attr('href', url);
			
			$('#rotator_next .count').text(item_num+'/'+num_items);
			Cufon.replace('#rotator_next .count');
			
		}, fadetime/2)	

		//Preload the next and previous image;
		if(item.prev('item').length)
		{$.preloadImages(item.prev('item').find('image').text());}
		
		if(item.next('item').length)
		{$.preloadImages(item.next('item').find('image').text());}
		
	}
	
	//Automatically go to the next page
	function autoPage () {
		old_item = item;
		
		if(item.next('item').length)
		{
			item = item.next('item');
			item_num++;
		}
		else
		{
			item = $(xml_feed).find('item:first');
			item_num = 1;
		}
		
		displayItem();
	}
});

//jQuery image preloader plugin
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}