/*

	var objSponsor = new SponsorClass("id da div de controle")
	
	objSponsor.Add('endereco url do site', 'endereco relativo ou absoluto da imagem'[,tempo para execucao]);
	objSponsor.Start([autoplay, true|false]);

	exemplo:
		var objSponsor = new SponsorClass("fullBanner");
		objSponsor.Add('http://www.manlec.com.br', 'imagens/banner_grande_manlec50.gif',50);
		objSponsor.Add('http://www.carmenflores.com.br', 'imagens/super_banner_carmen.gif');
		objSponsor.Start();
		
		
	o objeto ainda possui um Play, Stop, Next e Prior para situaçoes manuais.
	
	Exemplo:
		objSponsor.Play();  //Auto play
		objSponsor.Stop(); //Para o auto play
		objSponsor.Next(); //Proximo sponsor
		objSponsor.Prior(); //Sponsor Anterior
		
	Desenvolvido por Geison (geisonms@gmail.com)
*/
var CONST_TOOLTIP = 0;
var CONST_IMG_URL = 1;
var CONST_ADDRESS = 2;
var CONST_TIME    = 3;
var CONST_OBJECT  = 4;

var internalObjSponsor = null;

function InternalSponsorRun()
{
	internalObjSponsor.RotateIt();
}
function SponsorClass(sponsorId)
{	
	this.defaultTime = 3;
	this.defaultTarget = '_parent';
	this.sponsors = new Array();
	this.currentItem = 0;
	this.currentInterval = 0;
	this.isPlay = false;
	this.objTimeOut = null;
	
	this.sponsorObject = document.getElementById(sponsorId);
	
	if (this.sponsorObject == null)
	{
		alert('Invalid Sponsor Object Id "' + sponsorId + '"');
	}
	
}
SponsorClass.prototype.Add = function (imgAddress, imgUrl, time, toolTip)
{
	var item = new Array();
	var idx = this.sponsors.length;
	var target = this.defaultTarget;
	
	if (typeof time  == 'undefined' || time == null)
	{
		time = this.defaultTime;
	}
	if (typeof toolTip == 'undefined')
	{
		toolTip = imgAddress;
	}
	item[CONST_TIME] = time * 1000; //seconds * 1000 = milliseconds
	
	var elementImg = document.createElement('img');
	elementImg.setAttribute('src', imgUrl);
	elementImg.setAttribute('border','0');
	elementImg.setAttribute('title', toolTip);

	if (typeof imgAddress != 'undefined' && imgAddress != 'null' && imgAddress != null && imgAddress != '')
	{
		var elementA = document.createElement('a');
		elementA.setAttribute('href', imgAddress);
		elementA.setAttribute('target', target);
		elementA.setAttribute('id', 'lnk_' + idx);
		elementA.appendChild(elementImg);
		item[CONST_OBJECT] = elementA;
	}
	else 
	{
		item[CONST_OBJECT] = elementImg;
	}
	
	this.sponsors[idx] = item;
	
}
SponsorClass.prototype.Count = function()
{
	return this.sponsors.length;
}
SponsorClass.prototype.Start = function(autoplay)
{
    if (this.sponsors.length > 0)
    {
    
	    this.WriteElement();
    	
	    if (typeof autoplay == 'boolean')
	    {
		    this.isPlay = autoplay;
	    }
    	
	    this.Run();
	}
	
}
SponsorClass.prototype.RotateIt = function ()
{
	if (this.isPlay == true) 
	{
		this.InternalNext();
	}
	window.clearInterval(this.ObjTimeOut);
	this.ObjTimeOut = window.setTimeout("InternalSponsorRun()", this.currentInterval);
}
SponsorClass.prototype.Run = function()
{
	internalObjSponsor = this;
	this.ObjTimeOut = window.setTimeout("InternalSponsorRun()", this.currentInterval);
}
SponsorClass.prototype.Play = function()
{
	this.isPlay = true;
}
SponsorClass.prototype.Stop = function()
{
	this.isPlay = false;
}

SponsorClass.prototype.WriteElement = function ()
{
	var element = this.sponsorObject.firstChild;

	if (element != null)
	{
		this.sponsorObject.removeChild(element);
	}
	this.sponsorObject.appendChild(this.GetElement());
}

SponsorClass.prototype.GetElement = function()
{
	var item = this.sponsors[this.currentItem];
	this.currentInterval = item[CONST_TIME];
	return item[CONST_OBJECT];
}

SponsorClass.prototype.InternalNext = function ()
{
    if (this.sponsors.length > 0)
    {
	    this.currentItem++;
	    if (this.currentItem >= this.Count())
	    {
		    this.currentItem = 0;
	    }
	    this.WriteElement();
	}
}
SponsorClass.prototype.Next = function()
{
	this.isPlay = false;
	this.InternalNext();
}

SponsorClass.prototype.InternalPrior = function () 
{
	if (this.sponsors.length > 0)
    {
	    this.currentItem--;
	    if (this.currentItem < 0)
	    {
		    this.currentItem = this.Count()-1;
	    }
	    this.WriteElement();
	}
}
SponsorClass.prototype.Prior = function()
{
	this.isPlay = false;
	this.InternalPrior();
}