
function Rotator(container)
{
	var me = this;
	
	me.imageIndex = null;	
	me.timer = null;
	
	me.container = $(container);			
	me.imageCount = me.container.children().length;//find("IMG").length;	
	
	//me.container.find("IMG").hide();
	me.container.children().hide();
		
	
	var img = me.container.find(":first"); //().first(); //find("IMG:first");		
		
	//var height = img.height();		
	//var width = img.width();
	//me.container.height(height);
	var height = me.container.height();
	var width = me.container.width();
		
	var outer = $("<div style='position:relative;'></div>");
	me.container.wrap(outer);
	
	var duration = me.container.attr("duration")
	me.duration = duration ? parseInt(duration) : 10000;	
			
	if (me.container.attr("showPaging"))
	{
		var prevImg = $("<img style='position:absolute; z-index: 9; cursor:pointer;' src='/images/v3/stage_back.png' />");
		var nextImg = $("<img style='position:absolute; z-index: 9; cursor:pointer;' src='/images/v3/stage_forward.png' />");		
	
		me.container.after(prevImg);
		me.container.after(nextImg);
		
		nextImg.mousedown(function() { me.NextImage() });
		prevImg.mousedown(function() { me.PrevImage() });	
		
		prevImg.css({top: height / 2 - 25, left: - 35});
		nextImg.css({top: height / 2 - 25, left: width - 35});
	}
	
		
	if (me.container.attr("showBullets"))
	{
		me.selectorContainer = $("<div style='position:absolute; z-index:99'></div>");
		me.container.after(me.selectorContainer);
	
		for(var i = 0; i < me.imageCount; i++)
		{			
			var selectorImg = $("<img index='" + i + "' style='cursor:pointer; position:relative; margin-left:3px; z-index:999' src='/images/v3/stage_dot" + (i == 0 ?  "1" :"0") + ".png' />");
			me.selectorContainer.append(selectorImg);
			selectorImg.mousedown(function() { me.GotoImage($(this).attr("index")); });	
		}	

		me.selectorContainer.css({top: height - 30, left: 10});	
	}
			
					
	this.GotoImage = function(index)
	{				
		if (me.timer) window.clearTimeout(me.timer);
		this.ShowImage(index);
		me.timer = window.setTimeout(me.NextImage, me.duration);	
	};

	this.ShowImage = function(index)
	{
		var oldIndex = me.imageIndex;
		me.imageIndex = index;

		if (me.selectorContainer)
		{
			me.selectorContainer.find("IMG").attr("src", "/images/v3/stage_dot0.png");
			me.selectorContainer.find("IMG[index=" + index + "]").attr("src", "/images/v3/stage_dot1.png");
		}

		if (oldIndex != me.imageIndex)
		{	
			var images = me.container.children(); //find("IMG");
									
			var $newImage = $(images[me.imageCount - me.imageIndex - 1]);			
			$newImage.hide().css("z-index", 8);
			
			if (oldIndex != null)
			{
				var $oldImage = $(images[me.imageCount - oldIndex - 1]);
				$oldImage.css("z-index", 7);
				$newImage.fadeIn("slow", function() { $oldImage.css("z-index", 1) });
			}
			else
			{				
				$newImage.show();
			}		
		}
	};

	this.NextImage = function()	
	{		
		me.GotoImage((me.imageIndex + 1) % me.imageCount);		
	}

	this.PrevImage = function()
	{		
		var index = me.imageIndex - 1;
		if (index < 0) index = me.imageCount - 1;
		me.GotoImage(index);		
	}
	
	me.GotoImage(0);	
}

