/*global jQuery:false, document:false, jQuery:false, setTimeout:false, clearTimeOut:false */
/*
 * jQuery ctRotator Plugin
 * Examples and documentation at:http://thecodecentral.com/2008/11/12/ctrotator-a-flexible-itemimage-rotator-script-for-jquery
 * Under MIT license http://www.opensource.org/licenses/mit-license.php
 *
 * @author: Cuong Tham
 * @version: 1.0
 * @requires jQuery v1.2.6 or later
 *
 * @headsTip: Customizable rotating script for displaying large quantity of items in an interesting way
 */
 
function insertItem(item, container, options){
	var rendered = options.itemRenderer(item);
	var newLi = jQuery(rendered);
	if(newLi.size() === 0){
		newLi = jQuery('<li></li>').append(rendered);
	}
	jQuery(container).prepend(newLi);
	
	if(options.useTooltip){
		newLi.tooltip(jQuery.extend({
		fade:50,
		delay:800,
		opacity: 1,
		bodyHandler: function(){
			return item.tip;
		}
		}, options.tooltipOptions));
	}
	
	return newLi;
}

function updateRotation(rotator, container, options){
	var item = rotator.gotoNext();
	var newLi = insertItem(item, container, options);
	
	if(options.fadeEffect && jQuery.support.opacity){
		newLi.hide();
		var inSpeed = options.fadeInSpeed;
		var outSpeed = options.fadeOutSpeed;
		jQuery(container).children(':last').fadeOut(outSpeed, function(){
			jQuery(this).remove();
			newLi.fadeIn(inSpeed, function(){
			setTimeout(function(){updateRotation(rotator, container, options);}, options.speed);
			});
		});
	}else{
		jQuery(container).children(':last').remove();
		setTimeout(function(){updateRotation(rotator, container, options);}, options.speed);
	}

}




function ctRotatorList(list, shuffle){
  this.cursor = 0;
  this.list = list;
  this.shuffle = shuffle === null ? false : shuffle;
  if(this.shuffle){
    this.list = this.shuffleArray(this.list);
  }
}

ctRotatorList.prototype = { 
  gotoNext:function(){
	  if(this.cursor >= this.getCount() - 1){ 
        this.cursor = 0;
	  }else{
	    this.cursor += 1;
	  }
      return this.list[this.cursor];
  },
  getCount:function(){
    return this.list.length;
  },
  shuffleArray:function(o){ //credit http://snippets.dzone.com/posts/show/849
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i, 10), x = o[--i], o[i] = o[j], o[j] = x) {}
      return o;
  }
};

 function doRotating(dataSource, container, options){
  var rotator = new ctRotatorList(dataSource, options.shuffle);
  var showCount = options.showCount;
  for(var i = 0; i < showCount; i += 1){
	if(typeof(__ie6)!=="undefined" && __ie6) {
		var it = rotator.gotoNext();
		var r = options.itemRenderer(it);
		var li = jQuery(r);
		if(li.size()===0) {
			li = jQuery('<li></li>').append(r);
		}
		li.find('p:first-child').css({display:'block',margin:'0px'});
		jQuery(container).prepend(li);
	} else {
    	insertItem(rotator.gotoNext(), container, options);
	}
  }
  if(typeof(__ie6)==="undefined" || __ie6 === false) {
	  if(options.speed>0) {
		updateRotation(rotator, container, options);
	  }
  }
}

jQuery(function(jQuery) {
	jQuery.fn.ctRotator = function(dataSource, options){
	  options = jQuery.extend({
		 showCount: 5,
		 speed: 6000, //in milliseconds
		 fadeInSpeed: 750,
		 fadeOutSpeed: 250,
		 fadeEffect: true,
		 useTooltip: false,
		 shuffle: false,
		 itemRenderer: function(item){ 
		   if(item.url === null){
			 return '<li class="ctrotator-item">' + item.title + '</li>'; 
		   }else{
			 return '<li class="ctrotator-item"><a href="' + item.url+ '">' + item.title + '</a></li>'; 
		   } 
		 },
		 tooltipOptions: {}
	  }, options);
	  
	  if(options.showCount < 1){
		throw('options.showCount must be greater than 0');
	  }
	  
	  return this.each(function(){
		jQuery(this).empty();
		doRotating(dataSource, this, options);
	  });
	};
});