/*
 *
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 *
 */
 
 jQuery.timer = function (interval, callback)
 {
 /**
  *
  * timer() provides a cleaner way to handle intervals  
  *
  *	@usage
  * $.timer(interval, callback);
  *
  *
  * @example
  * $.timer(1000, function (timer) {
  * 	alert("hello");
  * 	timer.stop();
  * });
  * @desc Show an alert box after 1 second and stop
  * 
  * @example
  * var second = false;
  *	$.timer(1000, function (timer) {
  *		if (!second) {
  *			alert('First time!');
  *			second = true;
  *			timer.reset(3000);
  *		}
  *		else {
  *			alert('Second time');
  *			timer.stop();
  *		}
  *	});
  * @desc Show an alert box after 1 second and show another after 3 seconds
  *
  * 
  */

	var interval = interval || 100;

	if (!callback)
		return false;
	
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		
		this.internalCallback = function () {
			callback(self);
		};
		
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);
			
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
 };

 function rotate_banner (id, interval)
 {
 	var did = 0;
	var size = 0;
	var first = "";
	var barr = new Array();
	
	$("#"+id+" div").each(function(i) 
	{
		if(i==0)
			first = this.id;
		else if(i!=0)
		{
			barr[i-1] = this.id;
			size++;
		}
	});

        $.timer(interval*1000, function(timer)
        {
		$("#"+first).html($("#"+barr[did]).html());
		did=(did+1)%size;
	});
 }

 var fifo_idx = 1;
 function fadeInOut_banner(ids, interval)
 {
        len = ids.length;
        if(len>1)
        {
                for(i=1;i<len;i++)
                        $("#"+ids[i]).hide();
                                                                                                                  
                $.timer(interval*1000, function(timer)
                {
                        $("#"+ids[fifo_idx==0?len-1:fifo_idx-1]).fadeOut(1000);
                        $("#"+ids[fifo_idx]).delay(1000).fadeIn(2000);
                        fifo_idx = (fifo_idx+1)%len;
                });
        }
}

/*
 function rotate_banner (id, p1, p2, p3, interval)
 {
        $("#"+id).load("/aa?"+p1+","+p2+","+p3+",a");
        $.timer(interval*1000, function(timer)
        {
                $("#"+id).load("/aa?"+p1+","+p2+","+p3+",a");
        });
 }

 var fifo_idx = 0;
 function fadeInOut_banner(id1, id2, interval)
 {
	$("#"+id2).hide();
        $.timer(interval*1000, function(timer)
        {
		if(fifo_idx%2==0)
		{
			$("#"+id1).fadeOut(1000);
			$("#"+id2).delay(1000).fadeIn(2000);
		}
		else
		{
			$("#"+id2).fadeOut(1000);
			$("#"+id1).delay(1000).fadeIn(2000);
		}
		fifo_idx++;
	});
}
*/
