/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*/
//					AdRotatorPlus.js
//
//	Script developed by:	Muhammad Shehabeddeen
//	Description:			Has the main AdRotatorPlus functionality,
//							and should be used with the control
//
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*/

// The type definition of the Ad entity
function Ad()
{
	this.ImageURL = '';
	this.URL = '';
	this.Time = 0;
	this.Image = new Image();
	this.Type = '';
	this.JS = '';
}

// Called when the images finish loading to start the ad rotation
function setImageLoaded(UID, totalFreq, controlID, linkTarget, checkedHeight, checkedWidth, hStretch, vStretch)
{
	eval("arrAd_" + UID + "[randomIndex_" + UID + "].Image.onerror = arrAd_" + UID + "[randomIndex_" + UID + "].Image.onload = null;	" +
	"if(arrAd_" + UID + ".length > 1)" +
		"window.setTimeout(\"changeAd('" + UID + "', '" + totalFreq + "', '" + controlID + "', '" + linkTarget + "', '" + checkedHeight + "', '" + checkedWidth + "', " + hStretch + ", " + vStretch + ")\", arrAd_" + UID + "[randomIndex_" + UID + "].Time * 1000);");
}


// This renders the main HTML based on the type of the Ad
function setupHTML(UID , url, controlID, linkTarget, checkedHeight, checkedWidth )
{
	var code = "switch(arrAd_" + UID + "[randomIndex_" + UID + "].Type)" +

	"{" +
		"case 'image':" +
		"{" +
			"document.getElementById('" + controlID + "').rows[0].cells[0].innerHTML = \"<a id='AdLink_"  + UID + "' href='#' target='" + linkTarget + "'><img alt='ad image' style='" +
						"height: " + checkedHeight + ";width: " + checkedWidth + ";' border='0' id='AdImage_" + UID + "' src='" + url + "' /><\/a>\";"+
			"break;" +
		"}" +
		"case 'swf':"+
		"{" +
			"/*if(window.ActiveXObject)//IE" +
			"{*/" +
				"document.getElementById('" + controlID + "').rows[0].cells[0].innerHTML =" +
							"\"<a id='AdLink_"  + UID + "' href='#' target='" + linkTarget + "'><object style='height: " +
						checkedHeight + "; width: " + checkedWidth + ";' border='0' id='AdImage_" + UID + "' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://www.macromedia.com/go/getflashplayer'>"+
						"<param name='allowScriptAccess' value='sameDomain' />"+
						"<param name='wmode' value='transparent' />"+
						"<param name='movie' value='" + url + "' />"+
						"<param name='quality' value='high' />"+
						"<param name='bgcolor' value='#ffffff' />"+
						
						
						"<embed src='" + url + "' type='application/x-shockwave-flash'"+
						" pluginspage='http://www.macromedia.com/go/getflashplayer' quality='high'"+
						" wmode='transparent' style='height: " + checkedHeight + "; width: " + checkedWidth + ";'></embed>"+
						"<\/object><\/a>\";" +
			"/*}" +
			"else" +
			"{" +
				"document.getElementById('" + controlID + "').rows[0].cells[0].innerHTML =" +
							"\"<a id='AdLink_"  + UID + "' href='#' target='" + linkTarget + "'><object type='application/x-shockwave-flash' style='height: " +
						checkedHeight + ";width: " + checkedWidth+ ";' border='0' id='AdImage_" + UID + "' data='" + url + "'>"+
						"<param name='allowScriptAccess' value='sameDomain' />"+
						"<param name='quality' value='high' />"+
						"<param name='bgcolor' value='#ffffff' />"+
						"<param name='wmode' value='transparent' />"+
						"<param name='swliveconnect' value='true'>"+
						"<\/object><\/a>\";" +
			"}*/" +
			"break;" +
		"}" +
		"case 'iframe':" +
		"{" +
			"document.getElementById('" + controlID + "').rows[0].cells[0].innerHTML =	\"<iframe src='" + url + "' style='height: " +
						checkedHeight + ";width: " + checkedWidth + ";' border='0' id='AdImage_" + UID + "' marginWidth='0' marginHeight='0' frameBorder='no' scrolling='no' hspace='0' vspace='0'><\/iframe>\";" +
			"break;" +
		"}" +
		"case 'popup':" +
		"{" +
			"window.open(url, '_blank', 'resizable=no, scrollbars=no, status=no, toolbar=no, menubar=no, location=no, width=" + checkedWidth+ ", height=" + checkedHeight+ "');" +
			"break;" +
		"}" +
		"case 'js':" +
		"{" +
			"document.write(arrAd_" + UID + "[randomIndex_" + UID + "].JS);" +
			"break;" +
		"}" +
				
	"}" +

	"if(document.getElementById('AdLink_" + UID + "') != null)" +
		"document.getElementById('AdLink_" + UID + "').href = arrAd_" + UID + "[randomIndex_" + UID + "].URL;";
eval(code);
}


//This rotates the Ads based on multiple parameters, and depending on the calculated frequency or probability of appearance and time
function changeAd(UID, totalFreq, controlID, linkTarget, checkedHeight, checkedWidth, hStretch, vStretch)
{
var code =	"randomIndex_" + UID + " = priorityList_" + UID + "[Math.floor( Math.random()* " + totalFreq + ")];" +	
	
	
	"if(arrAd_" + UID + "[randomIndex_" + UID + "].Type != 'js' && (arrAd_" + UID + "[randomIndex_" + UID + "].Image.src == null || arrAd_" + UID + "[randomIndex_" + UID + "].Image.src == '' || !arrAd_" + UID + "[randomIndex_" + UID + "].Image.complete))" +
	"{" +
		"arrAd_" + UID + "[randomIndex_" + UID + "].Image.onerror = arrAd_" + UID + "[randomIndex_" + UID + "].Image.onload = new function() {setImageLoaded('" + UID + "', '" + totalFreq + "', '" + controlID + "', '" + linkTarget + "', '" + checkedHeight + "', '" + checkedWidth + "', " + hStretch + ", " + vStretch + ");};" +
		
		"arrAd_" + UID + "[randomIndex_" + UID + "].Image.src = arrAd_" + UID + "[randomIndex_" + UID + "].ImageURL;" +
		
		"setupHTML('" + UID + "', arrAd_" + UID + "[randomIndex_" + UID + "].ImageURL, '" + controlID + "', '" + linkTarget + "', '" + checkedHeight + "', '" + checkedWidth + "');" +

  "if(" + hStretch + " && document.getElementById('AdImage_" + UID + "'))document.getElementById('AdImage_" + UID + "').style.width = arrAd_" + UID + "[randomIndex_" + UID + "].Image.width;" +
  "if(" + vStretch + " && document.getElementById('AdImage_" + UID + "'))document.getElementById('AdImage_" + UID + "').style.height = arrAd_" + UID + "[randomIndex_" + UID + "].Image.height;" +

	"}" +
	"else" +
	"{" +
		
"setupHTML('" + UID + "', arrAd_" + UID + "[randomIndex_" + UID + "].ImageURL, '" + controlID + "', '" + linkTarget + "', '" + checkedHeight + "', '" + checkedWidth + "');" +

		  "if(" + hStretch + " && document.getElementById('AdImage_" + UID + "'))document.getElementById('AdImage_" + UID + "').style.width = arrAd_" + UID + "[randomIndex_" + UID + "].Image.width;" +
		  "if(" + vStretch + " && document.getElementById('AdImage_" + UID + "'))document.getElementById('AdImage_" + UID + "').style.height = arrAd_" + UID + "[randomIndex_" + UID + "].Image.height;" +
		"window.setTimeout(\"changeAd('" + UID + "', '" + totalFreq + "', '" + controlID + "', '" + linkTarget + "', '" + checkedHeight + "', '" + checkedWidth + "', " + hStretch + ", " + vStretch + ")\", arrAd_" + UID + "[randomIndex_" + UID + "].Time * 1000);" +				
		
	"}";	
eval(code);
}
