/*
-------------------------------------------------------------------
	HtmlRotator Object
-------------------------------------------------------------------
*/
function HtmlRotator(w,h,delaySeconds,useOnLoadEvent){

	this.Width = w;
	this.Height = h;
	this.Container = null;
	this.ObjectName = null;
	this.AutoRotate = true;
	this.UseOnLoadEvent = useOnLoadEvent;
	this.Delay = delaySeconds*1000;
	this.CurrentSlide = -1;
	this.NextSlide = 0;
	this.Content = new Array();
	this.TimerID = null;
	
	this.footerCreated = false;
	
	//bind to event
	var me = this;
	
	if( this.UseOnLoadEvent ){
		window.onload=function(){me.Start()};
	}
	
}

// Add
//-----------------------------------------------------------------
HtmlRotator.prototype.Add = function(divID,bgImage){
	this.Content[this.Content.length] = new HtmlRotatorItem(divID,bgImage);
}

// ShowSlide
//-----------------------------------------------------------------
HtmlRotator.prototype.ShowSlide = function(){

	this.Reset();	

	hrItem = this.Content[this.NextSlide];
	hrItem.Show();
	
	this.CurrentSlide = this.NextSlide;
	this.NextSlide = this.NextSlide + 1;
	
	//set up next image
	if (this.NextSlide >= this.Content.length) this.NextSlide = 0;
	
	//start rotating
	if ( this.AutoRotate) this.Play();

	this.MakeFooter();
}

// Reset
//-----------------------------------------------------------------
HtmlRotator.prototype.Reset = function(){
	for(i=0;i<this.Content.length;i++){
	
		hrItem = this.Content[i];
		
		div = document.getElementById(hrItem.ID);
		with (div.style){
			//width = this.Width;
			//height = this.Height;
			//backgroundImage = "url(" + hrItem.Image + ")";
			//backgroundRepeat = "no-repeat";
			display = "none";
		}
		
		
		//set background
		if (hrItem.BackgroundCreated){
			divbg = document.getElementById(hrItem.BackgroundID);
		
		}else{

			divbg = document.createElement("DIV");
			divcon = document.getElementById(this.Container);
			divbg.id = hrItem.BackgroundID;
			divbg.className = "HtmlRotatorContentBackground";

			//append to main container
			divcon.appendChild(divbg);

			hrItem.BackgroundCreated = true;
		}
		
		with (divbg.style){
				backgroundImage = "url(" + hrItem.Image + ")";
				backgroundRepeat = "no-repeat";
				display = "none";
		}

		
		
		
		
	}
}

// Start
//-----------------------------------------------------------------
HtmlRotator.prototype.Start = function(){
	this.SetEvents();
	this.ShowSlide();
}

// SetEvents
//-----------------------------------------------------------------
HtmlRotator.prototype.SetEvents = function(){

	var me = this;

	for(i=0;i<this.Content.length;i++){
		//hrItem = this.Content[i];
		div = document.getElementById(this.Content[i].ID);
		div.onmouseover = function(){me.Pause()};
		div.onmouseout = function(){me.Play()};
	}

	//set mouse events on contanier
	/*
	div = document.getElementById(this.Container);
	div.onmouseover = function(){me.Pause()};
	div.onmouseout = function(){me.Play()};
	*/
}

// Pause
//-----------------------------------------------------------------
HtmlRotator.prototype.Pause = function(){
	window.status = "PAUSE";
	window.clearTimeout(this.TimerID);
}

// Play
//-----------------------------------------------------------------
HtmlRotator.prototype.Play = function(){
	window.status = "";
	var me = this;
	this.TimerID = window.setTimeout(function(){me.ShowSlide()},this.Delay);
}


// MakeFooter
//-----------------------------------------------------------------
HtmlRotator.prototype.MakeFooter = function(){

	var oDiv;

	if (this.footerCreated){
		oDiv = document.getElementById(this.Container + "_footer");
	}else{

		//create footer background
		oDiv = document.createElement("DIV");
		div = document.getElementById(this.Container);
		oDiv.id = this.Container + "_footerbg";
		with( oDiv.style){
			position = "absolute";
			width = this.Width + "px";
			height = "20px";
			marginTop = (this.Height-20) + "px";
		}
		oDiv.className = "HtmlRotatorFooterBackground";

		//append to main container
		div.appendChild(oDiv);

		//create footer
		oDiv = document.createElement("DIV");
		div = document.getElementById(this.Container);
		oDiv.id = this.Container + "_footer";
		with( oDiv.style){
			position = "absolute";
			width = this.Width + "px";
			height = "20px";
			marginTop = (this.Height-20) + "px";
		}
		oDiv.className = "HtmlRotatorFooter";

		//append to main container
		div.appendChild(oDiv);
		
		this.footerCreated = true;
	}
	
	oDiv.innerHTML = this.BuildFooterHTML();
}

// BuildFooterHTML
//-----------------------------------------------------------------
HtmlRotator.prototype.BuildFooterHTML = function(){
	var html = "";
	var cmd	= "";
	var css = "";
	var prefix = this.ObjectName + ".";
	var baseurl = document.URL;
	
	if (baseurl.indexOf('#') != -1) {
	    baseurl = baseurl.substr(0, baseurl.indexOf('#'));
	}
	
	//Prev
	html += '<a href="' + baseurl + '#prev" onclick="' + prefix + 'Prev()"><</a>';

	for(i=0;i<this.Content.length;i++){
		cmd = prefix + "Go(" + i + ")";
		if(i==this.CurrentSlide)css = ' class="selected"';
		else css = '';
		html += '<a href="' + baseurl + '#' + (i+1) +'" onclick="' + cmd + '"' + css + '>' + (i+1) + '</a>';
	}

	//Next
	html += '<a href="' + baseurl + '#next" onclick="' + prefix + 'Next()">></a>';
	return html;
}

/*
-------------------------------------------------------------------
	HtmlRotatorItem Object
-------------------------------------------------------------------
*/
function HtmlRotatorItem(divID,bgImage){
	this.ID = divID;
	this.Image = bgImage;
	this.BackgroundID = this.ID + "_bg";
	this.BackgroundCreated = false;
	
	this.Show = function(){
		document.getElementById(this.ID).style.display = "block";
		document.getElementById(this.BackgroundID).style.display = "block";
	}
	this.Hide = function(){
		document.getElementById(this.ID).style.display = "none";
		document.getElementById(this.BackgroundID).style.display = "none";
	}
}

/*
-------------------------------------------------------------------
	HtmlRotator Navigation functions
-------------------------------------------------------------------
*/
HtmlRotator.prototype.Go = function(n){
	window.clearTimeout(this.TimerID);
	this.NextSlide = n;
	this.ShowSlide();
}
HtmlRotator.prototype.Next = function(){
	window.clearTimeout(this.TimerID);
	this.NextSlide = this.CurrentSlide+1;
	if (this.NextSlide >= this.Content.length) this.NextSlide = 0;
	this.ShowSlide();
}
HtmlRotator.prototype.Prev = function(){
	window.clearTimeout(this.TimerID);
	this.NextSlide = this.CurrentSlide-1;
	if (this.NextSlide < 0) this.NextSlide = this.Content.length-1;
	this.ShowSlide();
}
