/*
 * jQuery jRotator
 * version 1.0 beta
 * 
 * Developed by Paul Amelin
 * 16 October 2009
 * 
 * Usage: $('#my_id').jRotator(options, callback)
 * Options
 * Dependencies:  jQuery 1.3.2 or higher (http://jquery.com/)
 * 
 * This plugin is dual-licensed under the GNU General Public License and the MIT License
 */
(function($) {
 
   $.fn.jRotator = function(o) {

	   o = $.extend({
	       speed: 500,
	       delay: 10000,
	       easing: null,
	       btnGo: null,
	       btnPause: null
	   }, o || {});
	   
	   this.each(function() {

	        var running = false, div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size();

	        ul.prepend(tLi.slice(tl-1).clone()).append(tLi.slice(0,1).clone());

	        var li = $("li", ul), itemLength = li.size(), curr = 1;

	        var liSize = li[0].offsetWidth;   // Full li size(incl margin)-Used for animation
	        liSize = 338;
	        var liHeight = 128;
/*	        jQuery.each(li, function() {
	        	if($(this).height() > liHeight){
	        		liHeight = $(this).height();
	        	}
	        });*/


	        li.css({height: liHeight});
	        ul.css("width", liSize * itemLength + "px").css("left", -(curr*liSize));
	        div.css("width", liSize + "px");                     // Width of the DIV. length of visible images

	        var rotatorInterval = setInterval(function(){go(curr+1);}, o.delay);

	        if(o.btnGo)
	                $(o.btnGo).click(function() {
	                	clearInterval(rotatorInterval);
	                	go(curr+1);
	                	rotatorInterval = setInterval(function(){go(curr+1);}, o.delay);	                	
	                    return false;
	                });


	        if(o.btnPause)
	            $(o.btnPause).click(function() {
	                clearInterval(rotatorInterval);
	                return false;
	            });
	        
	        function go(to) {
	            if(!running) {
                    if(to>=itemLength) {
                        ul.css("left", -liSize + "px" );
                        curr = 2;
                    } else curr = to;
                    
	                running = true;

	                ul.animate(
	                	{left: -(curr*liSize)},
	                	o.speed,
	                	o.easing,
	                	function() {running = false;}
	                );
	            }
	            return false;
	        };
	    });
 
	   return this;
 
   };
	function css(el, prop) {
	    return parseInt($.css(el[0], prop)) || 0;
	};
   
})(jQuery);