/*
	// AdRotator Object Usage:

	var nAdCount = -1;
	var aAdArray = new Array();

	aAdArray[++nAdCount] = new Array();
	aAdArray[nAdCount][0] = "10001";
	aAdArray[nAdCount][1] = "images/mm_pop1.gif";
	aAdArray[nAdCount][2] = "http://www.sony.com";
	aAdArray[nAdCount][3] = "Sony";

	aAdArray[++nAdCount] = new Array();
	aAdArray[nAdCount][0] = "10002";
	aAdArray[nAdCount][1] = "images/mm_cou1.gif";
	aAdArray[nAdCount][2] = "http://www.universalrecords.com";
	aAdArray[nAdCount][3] = "Universal Records";

	aAdArray[++nAdCount] = new Array();
	aAdArray[nAdCount][0] = "10003";
	aAdArray[nAdCount][1] = "images/mm_uac1.gif";
	aAdArray[nAdCount][2] = "http://www.badboyonline.com";
	aAdArray[nAdCount][3] = "Bad Boy Online";

	nAdCount++;
	nRotationTime = 3;
	sRedirectASP = "AdRotatorRedirect.asp";
	sRedirectQS = "Var1=Value1&Var2=Value2";
	bRotateRandom = true;

	var oAdRotator1 = new AdRotator("oAdRotator1", aAdArray, nAdCount, sRedirectASP, sRedirectQS, nRotationTime, bRotateRandom);
	document.write(oAdRotator.GetAdImageHTML());

	var oAdRotator2 = new AdRotator("oAdRotator2", aAdArray, nAdCount, sRedirectASP, sRedirectQS, nRotationTime, bRotateRandom);
	document.write(oAdRotator.GetAdImageHTML());

	oAdRotator1.Start();
	oAdRotator2.Start();
*/
	
function AdRotator(sAdRotatorObjectName, aAdArray, nAdCount, sRedirectASP, sRedirectQS, nRotationTime, bRotateRandom)
{
	//Member Variables
	this.m_sAdRotatorObjectName = sAdRotatorObjectName;
	this.m_aAdArray = aAdArray;
	this.m_nAdCount = nAdCount;
	this.m_sRedirectASP = sRedirectASP;
	this.m_sRedirectQS = sRedirectQS;
	this.m_nRotationTime = nRotationTime; // Seconds
	this.m_bRotateRandom = bRotateRandom;

	//Initialized Member Variables
	this.m_bIsIE = (document.all ? true : false);
	this.m_nTimerID = 0;

	if (this.m_bRotateRandom)
	{
		this.m_nCurrentAdIndex = Math.floor(this.m_nAdCount * Math.random());
	}
	else
	{
		this.m_nCurrentAdIndex = 0;
	}

	//Static Member Variables
	this.m_sAdImageTagName = sAdRotatorObjectName + "__AdRotatorImage";

	//Member Functions
	this.Start = AdRotator_Start;
	this.Stop = AdRotator_Stop;
	this.Rotate = AdRotator_Rotate;
	this.GetAdImageHTML = AdRotator_GetAdImageHTML;
	this.HandleClick = AdRotator_HandleClick;
	this.PreLoadImages = AdRotator_PreLoadImages;

	this.PreLoadImages();
}

function AdRotator_Start()
{
	if (! document.images[this.m_sAdImageTagName])
	{
		alert("Can not start. No image tag.");
		return;
	}

	this.m_nTimerID = setInterval(this.m_sAdRotatorObjectName + ".Rotate()", this.m_nRotationTime * 1000)
}

function AdRotator_Stop()
{
	if (this.m_nTimerID != 0)
	{
		clearInterval(this.m_nTimerID);
		this.m_nTimerID = 0;
	}
}

function AdRotator_Rotate()
{
	if (! document.images[this.m_sAdImageTagName])
	{
		alert("Can not rotate. No image tag.");
		return;
	}

	if (this.m_bRotateRandom)
	{
		nNextAdIndex = Math.floor(this.m_nAdCount * Math.random());
	}
	else
	{
		nNextAdIndex = this.m_nCurrentAdIndex + 1;

		if (nNextAdIndex == this.m_nAdCount)
		{
			nNextAdIndex = 0;
		}
	}

	this.m_nCurrentAdIndex = nNextAdIndex;

	var sCurrentAdImage = this.m_aAdArray[this.m_nCurrentAdIndex][1];
	var sCurrentAdDesc = this.m_aAdArray[this.m_nCurrentAdIndex][3];

	//eval("document." + this.m_sAdImageTagName).src = sCurrentAdImage;
	document.images[this.m_sAdImageTagName].src = sCurrentAdImage;

	if (this.m_bIsIE)
	{
		document.images[this.m_sAdImageTagName].alt = sCurrentAdDesc;
	}
}

function AdRotator_GetAdImageHTML()
{
	var sAdImageHTML = "";
	var sCurrentAdImage = this.m_aAdArray[this.m_nCurrentAdIndex][1];
	var sCurrentAdDesc = this.m_aAdArray[this.m_nCurrentAdIndex][3];

	sAdImageHTML += " <a";
	sAdImageHTML += " href='#'";
	sAdImageHTML += " onClick='javascript:" + this.m_sAdRotatorObjectName + ".HandleClick();return false;'";
	sAdImageHTML += " >";
	sAdImageHTML += " <img border=0";
	sAdImageHTML += " name='" + this.m_sAdImageTagName + "'";
	sAdImageHTML += " src='" + sCurrentAdImage + "'";

	if (this.m_bIsIE)
	{
		sAdImageHTML += " alt='" + sCurrentAdDesc + "'";
		sAdImageHTML += " onMouseOver='window.status=this.alt;return true;'";
		sAdImageHTML += " onFocus='window.status=this.alt;return true;'";
		sAdImageHTML += " onMouseUp='window.status=this.alt'";
		sAdImageHTML += " onMouseMove='window.status=this.alt'";
		sAdImageHTML += " onMouseOut='window.status=\"\";'";
	}

	sAdImageHTML += " >";
	sAdImageHTML += " </a>";

	return sAdImageHTML;
}

function AdRotator_HandleClick()
{
	var nCurrentAdId = this.m_aAdArray[this.m_nCurrentAdIndex][0];
	var sCurrentAdLink = this.m_aAdArray[this.m_nCurrentAdIndex][2];

	sCurrentAdLink = sCurrentAdLink.replace(/^ */, "").replace(/ *$/, "");

	if ((sCurrentAdLink == "") || (sCurrentAdLink == "http://") || (sCurrentAdLink == "https://"))
	{
		return;
	}

	if (sCurrentAdLink.substring(0, 7).toLowerCase() == "mailto:")
	{
		location.href = sCurrentAdLink;

		return;
	}

	if (this.m_sRedirectASP != "")
	{
		sRedirectURL = this.m_sRedirectASP + "?RedirectURL=" + sCurrentAdLink;
		sRedirectURL += "&AdId=" + nCurrentAdId;

		if (this.m_sRedirectQS != "")
		{
			sRedirectURL += "&" + this.m_sRedirectQS;
		}
	}
	else
	{
		sRedirectURL = sCurrentAdLink;

		if (this.m_sRedirectQS != "")
		{
			sRedirectURL += "?" + this.m_sRedirectQS;
		}
	}

	window.open(sRedirectURL, "AdRotatorWindow");
}

function AdRotator_PreLoadImages()
{
	var i;
	var oTempImage;

	for (i = 0 ; i < this.m_nAdCount ; i++)
	{
		oTempImage = new Image();
		oTempImage.src = this.m_aAdArray[i][1];
	}
}
