/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*/
//					AdRotatorPlus.js
//
//	Script developed by:	IDS - Research and Development
//	Description:			Has the main AdRotator functionality,
//							and should be used with the control
//
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*/

/*-+-+-+-+-+-+-+-+-+-+-+-+-Gloabals-+-+-+-+-+-+-+-+-+-+-+-*/
//this must be defined for the impression statistics (the clicks management)
//set it to the intermediate page that does the calculations
var IntermediatePage = "IntermediateAdPage.aspx";

//this is an array that will hold all the adrotator timers on the page
var allTimers = new Array();

var focusTarget; //used for event handling
var lastTimeout; //handle for the timeout
/*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*/

//pause all the timers
function pauseAllTimers()
{
	for(var i = 0; i < allTimers.length; i++)
	{	
		allTimers[i].setValue('paused');	
	}
}

//resume all the timers
function resumeAllTimers()
{
	for(var i = 0; i < allTimers.length; i++)
	{
		allTimers[i].setValue('running');
	}
}

//traverses the DOM tree in search for textboxes and assigns the onfocus and onblur events
function setTextboxEvents(element)
{
	for(var i = 0; i < element.childNodes.length; i++)
	{
		var child = element.childNodes[i];
		setTextboxEvents(child);
		if(child.tagName != null && child.tagName.toLowerCase() == 'input' && child.type == 'text')
		{
			child.onfocus = keyUpHandler;
			child.onkeyup = keyUpHandler;
			child.onblur = resumeAllTimers;
			
		}
	}
}

//used to blur the focused textbox after a certain amount of time from last key press
function keyUpHandler(e)
{
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	focusTarget = targ;
	window.clearTimeout(lastTimeout);
	pauseAllTimers();
	lastTimeout = window.setTimeout('focusTarget.blur()', 10000);	
}


// The type definition of the Ad entity
function Ad()
{
	this.ImageURL = '';
	this.URL = '';
	this.Time = 0;
	this.Image = new Image();
	this.Type = '';
	this.JS = '';
	this.LinkID = -1;
	this.TargetUrl = '';
}

function TimerStatus()
{
	this.value = 'running';	
}
TimerStatus.prototype.setValue = function(val){this.value = val;}

// 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, targetUrl )
{	
	var code = "switch(arrAd_" + UID + "[randomIndex_" + UID + "].Type)" +

	"{" +
		"case 'image':" +
		"{" +
			"document.getElementById('" + controlID + "').rows[0].cells[0].innerHTML ="+
			" \"<a onmouseover='timerStatus_" + UID + ".value = \\\"paused\\\"' onmouseout='timerStatus_" + UID + ".value = \\\"running\\\"' 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 onmouseover='timerStatus_" + UID + ".value = \\\"paused\\\"' onmouseout='timerStatus_" + UID + ".value = \\\"running\\\"' id='AdLink_"  + UID + "' href='#' target='" + linkTarget + "'>"+ 
						"<object border='0' id='AdImage_" + UID + "' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' width='" + checkedWidth + "' height='" + checkedHeight + "' codebase='http://www.macromedia.com/go/getflashplayer'>"+
						//"<object 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='movie' value='" + url + "' />"+
						"<param name='quality' value='high' />"+
						
						"<embed src='" + url + "' type='application/x-shockwave-flash'"+
						" pluginspage='http://www.macromedia.com/go/getflashplayer' quality='high'"+
						" style='height: " + checkedHeight + "; width: " + checkedWidth + ";'></embed>"+
						//" wmode='transparent'></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='" + targetUrl + "' 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 'js':" +
		"{" +
			"document.getElementById('" + controlID + "').rows[0].cells[0].innerHTML = arrAd_" + UID + "[randomIndex_" + UID + "].JS;" +
			"break;" +
		"}" +
				
	"}" +

	"if(document.getElementById('AdLink_" + UID + "') != null){" +
		"document.getElementById('AdLink_" + UID + "').href = IntermediatePage + '?id=' + arrAd_" + UID + "[randomIndex_" + UID + "].LinkID;" +
		"document.getElementById('AdLink_" + UID + "').target = '_blank';}"
		;
		
		
	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 + "].Image.src == null || arrAd_" + UID + "[randomIndex_" + UID + "].Image.src == '' || (arrAd_" + UID + "[randomIndex_" + UID + "].Type == 'image' && !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 + "', arrAd_" + UID + "[randomIndex_" + UID + "].TargetUrl);" +

		"if (arrAd_" + UID + "[randomIndex_" + UID + "].Type == 'image'){"+
		"var size = getProportionalSize(arrAd_" + UID + "[randomIndex_" + UID + "].Image.width, arrAd_" + UID + "[randomIndex_" + UID + "].Image.height, checkedWidth, checkedHeight, vStretch, hStretch);" + 
		"document.getElementById('AdImage_" + UID + "').style.width = size[0];" + 
		"document.getElementById('AdImage_" + UID + "').style.height = size[1];" + 
		"}" +
	"}" +
	 	"else" +
		"{" +
			"if(timerStatus_" + UID +  ".value == 'paused'){window.setTimeout(\"changeAd('" + UID + "', '" + totalFreq + "', '" + controlID + "', '" + linkTarget + "', '" + checkedHeight + "', '" + checkedWidth + "', " + hStretch + ", " + vStretch + ")\", arrAd_" + UID + "[randomIndex_" + UID + "].Time * 1000);}" + 
			"else {setupHTML('" + UID + "', arrAd_" + UID + "[randomIndex_" + UID + "].ImageURL, '" + controlID + "', '" + linkTarget + "', '" + checkedHeight + "', '" + checkedWidth + "', arrAd_" + UID + "[randomIndex_" + UID + "].TargetUrl);" +
			
			"if (arrAd_" + UID + "[randomIndex_" + UID + "].Type == 'image'){" + 
			"var size = getProportionalSize(arrAd_" + UID + "[randomIndex_" + UID + "].Image.width, arrAd_" + UID + "[randomIndex_" + UID + "].Image.height, checkedWidth, checkedHeight, vStretch, hStretch);" + 
			"document.getElementById('AdImage_" + UID + "').style.width = size[0];" + 
			"document.getElementById('AdImage_" + UID + "').style.height = size[1];" + 
			"}" +

			"window.setTimeout(\"changeAd('" + UID + "', '" + totalFreq + "', '" + controlID + "', '" + linkTarget + "', '" + checkedHeight + "', '" + checkedWidth + "', " + hStretch + ", " + vStretch + ")\", arrAd_" + UID + "[randomIndex_" + UID + "].Time * 1000);" +
		"}" +				
		
	"}";
		
	eval(code);
}

function getProportionalSize(imageWidth, imageHeight, locationWidth, locationHeight, vStretch, hStretch)
{
	var width = 0;
	var height = 0;
	
	if (hStretch)
	{
		if (imageWidth <= locationWidth)
		{
			width = imageWidth;
			height = imageHeight;	
		}
		else
		{
			width = locationWidth;
			height = imageHeight * locationWidth / imageWidth;
		}
	}
	else if (vStretch)
	{	
		if (imageHeight <= locationHeight)
		{
			height = imageHeight;
			width = imageWidth;
			if (imageWidth >= locationWidth)
			{
				width = locationWidth;
			}
		}
		else
		{
			height = locationHeight;
			width = imageWidth * locationHeight / imageHeight;
			if (width >= locationWidth)
				width = locationWidth;
		}
	}
	
	var size = new Array();
	size[0] = width;
	size[1] = height;
	
	return size;
}

function loadPopupAds(url, width, height)
{
	var code = "window.open(url, '_blank', 'resizable=no, scrollbars=no, status=no, toolbar=no, menubar=no, location=no, width=" + width + ", height=" + height+ "');";
	eval(code);
}