/*
 * JTicker 0.5 Beta
 * By Jason Levine (http://www.jasons-toolbox.com)
 * A ticker plugin for the jquery library.
 */
jQuery.JTickerObjArray = new Array();
jQuery.fn.newsticker = function(passedOptions) {
	
	var options = {
	    TickerID: "",
		delay: 1000,
		transition: "slide",
		speed: "slow",
		contentcounter: -1,
		ArrayIndex: -1,
		currentitem:null,
		items:null,
		timerid: -1,
		initTicker: function(el){
			options.items = jQuery("li", el);
			for ( var i = 0; i < options.items.length; i++ ) {
				
				//jQuery(options.items[i]).css('z-index', String(options.items.length-i)).css('position', 'absolute');
				jQuery(options.items[i]).hide();
			};
			//jQuery(options.items[0]).appendTo(el);
			jQuery(options.items[0]).show();
			// hide all items (except first one)
			//options.items.not(":eq(0)").hide().end();
			// current item
			options.currentitem = 0;
			
			options.EnterSlide();
		},
	    ExitSlide: function() {
			
			if (options.contentcounter > -1) {
				if (jQuery("#lbCenter").get(0)){
		  
		   			if (jQuery("#lbCenter").css('display')!='none'){
						clearTimeout(options.timerid);
						options.timerid = setTimeout("jQuery.JTickerObjArray[" + options.ArrayIndex + "].ExitSlide();", options.delay);
				  		return;
		  			}
	   			}
				clearTimeout(options.timerid);
				switch (options.transition.toLowerCase()) {
					case "slide":
						jQuery("#" + options.TickerID).slideUp(
							options.speed,
							//options.ExitSlideStep4()
							function(){
								options.ExitSlideStep2()
							}
						);
						break;
					default:
						jQuery("#" + options.TickerID).fadeOut(
							options.speed,
							function(){
							options.ExitSlideStep2()
							}
						);
						break;
				}
			} else {
				
				options.ExitSlideStep2(ArrayIndex);
			}
	    },
	    ExitSlideStep2: function() {
			var tempid;
			
			jQuery(options.items[options.currentitem]).hide();
			options.currentitem = ++options.currentitem % (options.items.size());
			
			tempid = setTimeout("jQuery.JTickerObjArray[" + options.ArrayIndex + "].EnterSlide();", 500);
		},
	    EnterSlide: function() {
			
			options.contentcounter++;
			if (options.contentcounter == options.items.length) {
				options.contentcounter = 0;
			}
			var ArrayIndex = options.ArrayIndex;
			jQuery(options.items[options.currentitem]).show();
			//
			//jQuery(options.items[options.currentitem]).appendTo(jQuery("#" + options.TickerID))
			
			switch (options.transition.toLowerCase()) {
				
				case "slide":
					jQuery("#" + options.TickerID).slideDown(
						options.speed,
						options.EnterSlideStep2()
					);
					break;
				default:
					jQuery("#" + options.TickerID).fadeIn(
						options.speed,
						options.EnterSlideStep2()
					);
					break;
			}
	    },
	    EnterSlideStep2: function() {
			
			options.timerid = setTimeout("jQuery.JTickerObjArray[" + options.ArrayIndex + "].ExitSlide();", options.delay);
			
		}
	};
	if (passedOptions) {
		jQuery.extend(options, passedOptions);
	}
	
	
	jQuery.extend(jQuery.fx.prototype,{
		custom1: function(from, to, unit){
		this.startTime = (new Date()).getTime();
		
		if (!from){
			
			from = 1
		}
		
		if (!to){
			to = 1
		}
		
		this.start = from;
		this.end = to;
		
		this.unit = unit || this.unit || "px";
		this.now = this.start;
		this.pos = this.state = 0;
		this.update();

		var self = this;
		function t(gotoEnd){
			return self.step(gotoEnd);
		}

		t.elem = this.elem;

		jQuery.timers.push(t);

		if ( jQuery.timerId == null ) {
			jQuery.timerId = setInterval(function(){
				var timers = jQuery.timers;
				
				for ( var i = 0; i < timers.length; i++ )
					if ( !timers[i]() )
						timers.splice(i--, 1);

				if ( !timers.length ) {
					clearInterval( jQuery.timerId );
					jQuery.timerId = null;
				}
			}, 13);
		}
	}
		 })
	
	
	return this.each(function(){
		options.TickerID = this.id;
		
		jQuery.JTickerObjArray.push(options);
		options.ArrayIndex = jQuery.JTickerObjArray.length - 1;
		jQuery("#" + options.TickerID).hover(function() {
			clearTimeout(options.timerid);
		}, function() {
			options.timerid = setTimeout("jQuery.JTickerObjArray[" + options.ArrayIndex + "].ExitSlide();", options.delay);
		});
		
		options.initTicker(this);
	});
};