/*
 * Ticker Plugin
 * Author:Jared Harbour
 * Version: 0.1
 * Description: This plugin doesn't write out any HTML or get any data from podium, it is strictly behavioral, with one exception.  
 * 				The pager option creates a pager for the items that can be positioned anywhere on the page via css. 
*/
(function($){
	
    $.ticker = function(el, options){
        var base = this;
        
        base.$el = $(el);
        base.el = el; 
        
        base.$el.data("ticker", base);
        
        base.init = function(){
            base.options = $.extend({},$.ticker.defaultOptions, options);
            
            //adding a refrence to all the li tags
			base.$items = base.$el.children();
			
			//check to see if we have more items to show
			if(base.$items.length > base.options.itemstoshow){
            	base.itemCount = base.$items.length-1;
            	base.currentItem = 0;
            	base.nextItem = base.currentItem + base.options.itemstoshow;
            	base.timeout = "";
            	base.ac = new Array(base.options.itemstoshow+1);
            	
            	for(var i = 0; i < base.ac.length; i++){
            		base.ac[i] = i;
            	}
            	
				//some basic CSS so the transitions look ok
				base.$el.css("position","relative");
				base.$items.css({"position":"absolute","left":"0px"});
				
				var count = 0,top = 0;
				base.$items.each(function(){
					var elem = $(this);
					if(count < base.options.itemstoshow){
						elem.css("top",parseInt(top) + "px");
					}else{
						elem.css("top",base.$el.height() + "px");
					}
					top+=elem.height();
					count++;
				});
            	
            	base.tickerActions();
            }
        }
        
		base.tickerActions = function(){	
			//start animations
			base.timeout = setTimeout(function(){base.advance();},base.options.delay);
			
			base.$items.hover(function(){
				clearTimeout(base.timeout);
			},function(){
				base.timeout = setTimeout(function(){base.advance();},base.options.delay);
			});
		}
		
		base.advance = function(){
			base.nextItem++;
			if(base.nextItem > base.itemCount){
				base.nextItem = 0;
			}
			base.animate();
			base.timeout = setTimeout(function(){base.advance();},base.options.delay);
		}
		
		base.animate = function(){
			var temp;		
			$(base.$items[base.ac[base.ac.length -1 ]]).css("top",base.$el.height() + "px");
			for(var i = 0; i < base.ac.length; i++){
				temp = $(base.$items[base.ac[i]])	
			  	temp.animate({"top":parseInt(temp.css("top").split("p")[0]) - temp.height()},base.options.speed,"",base.animateCallback(i));
            }
		}
		
		base.animateCallback = function(i){
			if(base.$items[base.ac[i]] == base.$items[base.ac[base.ac.length -1]] ){
				base.ac.shift();
            	base.ac.push(base.nextItem);
            }
		}
        
        base.init();
    }

    $.ticker.defaultOptions = {
        delay:5000,
        speed:500,
        itemstoshow:1
    }

    $.fn.ticker = function(options){
        return this.each(function(){
            (new $.ticker(this, options));
        });
    }
    
})(jQuery);