/* List Ticker by Alex Fish 
// www.alexefish.com
//
// options:
//
// effect: fade/slide
// speed: milliseconds

// navigation controls added by Jakeline Santos
// jakeline.santos@gmail.com
*/
var intTicker;
var pauseTicker = false;

var navControls = function(effect,objUL,direction){
	list = objUL.children();
	toHide = list.eq(0);
	toShow = list.eq(1);
	
	if(effect == 'slide'){
		toHide.slideUp();
		toShow.slideDown(function(){
			if(direction == "next") toHide.remove().appendTo(objUL);
			else if(direction == "previous") toHide.remove().prependTo(objUL);
		});
	} else if(effect == 'fade'){
		toHide.fadeOut(function(){
			objUL.css('height',toShow.height());
			toShow.fadeIn();
			if(direction == "next") toHide.remove().appendTo(objUL);
			else if(direction == "previous") toHide.remove().prependTo(objUL);
		});
	}
};

(function($){
  $.fn.list_ticker = function(options){
    
    var defaults = {
      speed:4000,
	  effect:'slide',
	  run_once:false
    };
    
    var options = $.extend(defaults, options);
    
    return this.each(function(){
      
      var obj = $(this);
      var list = obj.children();
      var count = list.length - 1;

//      list.not(':first').hide();
      
      intTicker = setInterval(function(){
        if(pauseTicker) return false;		
		
		navControls(options.effect,obj,"next");
		
		count--;
		
		if((count == 0 && options.run_once)){
			clearInterval(intTicker);
		}
      }, options.speed)
	  
	  obj.parent().children(".ticker-controls .next").click(function(){
		pauseTicker = false;
		navControls(options.effect,obj,"next");
		pauseTicker = true;
	  });
	  obj.parent().children(".ticker-controls .previous").click(function(){
		pauseTicker = false;
		navControls(options.effect,obj,"previous");
		pauseTicker = true;
	  });
	  obj.parent().mouseover(function(){
		pauseTicker = true;
	  });
	  obj.parent().mouseout(function(){
		pauseTicker = false;
	  });
    });
  };
})(jQuery);

