$(document).ready(function() {
	
	$.textRotator(
	{
		itemsContainerID: 'textrotator',
		linksContainerID: 'textrotator-links',
		showFor:5000,
		fadeSpeed:500
	}
	);
	
});

(function( $ ){
	
	$textRotator = $.textRotator = $tr = $.tr = function(settings){
		
		if(settings.itemsContainerID != undefined){
			$tr.defaults.itemsContainerID = settings.itemsContainerID;
			$tr.defaults.itemPreID = $tr.defaults.itemsContainerID + '_';
			
		}
		
		if(settings.linksContainerID != undefined){
			$tr.defaults.linksContainerID = settings.linksContainerID;
			$tr.defaults.linkPreID = $tr.defaults.linksContainerID + '_';
		}
		
		if(settings.showFor != undefined){
			$tr.defaults.showFor = settings.showFor;
		}
		
		if(settings.fadeSpeed != undefined){
			$tr.defaults.fadeSpeed = settings.fadeSpeed;
		}
		
		$tr.initialize();
		
	}
	
	//static variables
	$tr.defaults = {
		
		itemsContainerID: 'textrotator',
		linksContainerID: 'textrotator-links',
		itemPreID: 'tritem_',
		linkPreID: 'trlink_',
		fadeSpeed:500,
		showFor:3000,
		startItem:0

	}
	
	//dynamic variables
	$tr.vars = {
		
		current: 0,
		count: 0,
		itemArray: [],
		itemHash: [],
		linkArray: [],
		linkHash: [],
		runCount: 0,
		fading:false
		
		
				}
	
	/**
		begins the actual animation and is recursively run to handle
		the fade in and out effects
		
		@param first = set to true if calling for the first time so that
		it doesn't fade out as soon as it loads
	**/
	$tr.start = function(first){
		
		
		if(!first){
		
		
			$tr.vars.runCount = $tr.vars.runCount -1;

			if($tr.vars.runCount <= 0){
				$tr.vars.runCount += 1;
				$tr.goto($tr.getNextPosition());
			}
		}
		setTimeout('$.tr.start(false)',$tr.defaults.showFor + $tr.defaults.fadeSpeed*2);
		
	}
	
	$tr.getNextPosition = function(){
		var next = $tr.vars.current + 1;

		return next % count;
		
	}
				
				
	$tr.initialize = function(){
		
		$tr.vars.count = 0;

		$('#' + $tr.defaults.itemsContainerID + ' .item').each(function(){
			//give every item a unique id for easy reference
			var newID = $tr.defaults.itemPreID + $tr.vars.count;
			$(this).prop('id',newID);
			
			$tr.vars.itemArray[$tr.vars.count] = newID;
			$tr.vars.itemHash[newID] = $tr.vars.count;
			//hide all items except the start item
			if($tr.defaults.startItem != $tr.vars.count){

				$(this).hide();
				
			}
			
			$tr.vars.count++;
		});
		
		$tr.setupLinks();
		$tr.start(true);
	}
	
	/**
		Populates the links html based on the number of items and which is selected
	**/
	$tr.setupLinks = function(){
		
		var o = '';
		
		o +=  '<ul>';
		
		for(var i = 0; i < $tr.vars.count; i++){
			
			//make sure the correct start item is selected
			if($tr.defaults.startItem == i){
				var theclass = ' class="selected" ';
			}else{
				var theclass = '';
			}
			
			var id = $tr.defaults.linkPreID + i;
			
			$tr.vars.linkArray[i] = id;
			$tr.vars.linkHash[id] = i;
			
			o +=  '<li '+ theclass + ' id="' + id + '"><a href="#" rel="' + i + '"><span>' + (i + 1) + '</span></a></li>';
		}
		
		o +=  '</ul>';
		
		//put the link html onto the page
		$('#' + $tr.defaults.linksContainerID).html(o);
		
		//set the actions for these new html links
		$('#' + $tr.defaults.linksContainerID + ' ul:first li a').each(function(){
			$(this).click(function(e){
				
				e.preventDefault();
				$tr.vars.runCount = $tr.vars.runCount +1 ;
				var goto = $(this).prop('rel');
				$tr.goto(goto,true);
			});
		});
		
		
	}
	
	/**
		Hide a text item
		@param position = the position of the item to hide (from 0)
	**/
	$tr.hideItem = function(position, callBack){
		var id = $tr.vars.itemArray[position];
		//$('#' + id).fadeOut($tr.defaults.fadeSpeed,callBack);
		
		$tr.vars.fading = true;
		
		$('#'+$tr.defaults.itemsContainerID+' .item').each(function(){
			
			var thisid = $(this).prop('id');
			
			if(thisid == id){
				$(this).fadeOut($tr.defaults.fadeSpeed,callBack);
			}else{
				$(this).fadeOut($tr.defaults.fadeSpeed);
			}
		});
	}
	
	/**
		Show a text item
		@param position = the position of the item to show (from 0)
	**/
	$tr.showItem = function(position){
		var id = $tr.vars.itemArray[position];
		$('#' + id).fadeIn($tr.defaults.fadeSpeed, function(){
				$tr.vars.fading = false;
		});
		
	}
	
	/**
		Change which item is currently displayed by index
		@param position = the index of the item to show
		@param clickChange = was this initialised by user request?
	**/
	$tr.goto = function(position, clickChange){
		
		
		 
		if(clickChange == undefined){
			clickChange = false;
		}
		
		if($tr.vars.fading && clickChange){
			$tr.vars.runCount+=1;
			setTimeout('$.tr.goto('+position+')',500);
		}else if(!$tr.vars.fading){
			$tr.changeLink(position);
			// hide the current item and show the new item
			$tr.hideItem($tr.vars.current,function(){  
				$tr.showItem(position) 
			});
			
			$tr.vars.current = position;
		}
		
	}
	
	/**
		Change which link is currently selected
	**/
	$tr.changeLink = function(position){
		var current = '#' + $tr.vars.linkArray[$tr.vars.current];
		var next = '#' + $tr.vars.linkArray[position];
		
		$(current).removeClass('selected');
		$(next).addClass('selected');
	}
	
	
	
	
})( jQuery );

