/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*/
//					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.AdName = '';
}

function Ad(adName)
{
	this.ImageURL = '';
	this.URL = '';
	this.Time = 0;
	this.Image = new Image();
	this.Type = '';
	this.JS = '';
	this.LinkID = -1;
	AdName = adName;
}

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 )
{
	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 + "' onclick=\\\"javascript:urchinTracker('" + AdName + "');\\\"><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 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='movie' value='" + url + "' />"+
						"<param name='Src' value='" + url + "'>" + 
						"<param name='quality' value='high' />"+
						"<param name='bgcolor' value='#ffffff' />"+
						"<param name='Play' VALUE='-1'>" +
						"<param name='Loop' value='0'>" + 
						"<param name='Quality' value='High'>" + 
						"<param name='Scale' value='ShowAll'>" + 
						"<param name='_cx' value='5477'>" +
						"<param name='_cy' value='2117'>" +
						"<param name='FlashVars' VALUE=''>" + 
						"<param name='SAlign' value=''>" +
						"<param name='Menu' value='-1'>" +
						"<param name='Base' value=''>" +
						"<param name='DeviceFont' value='0'>" +
						"<param name='EmbedMovie' value='0'>" +
						"<param name='SWRemote' value=''>" +
						"<param name='MovieData' value=''>" +
						"<param name='SeamlessTabbing' value='1'>" +
						"<param name='Profile' value='0'>" +
						"<param name='ProfileAddress' value=''>" +
						"<param name='ProfilePort' value='0'>" +
						"<param name='AllowNetworking' value='all'>" +
						"<param name='AllowFullScreen' value='false'>" +				
						"<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" +
			"{ alert('test');" +
				"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='Src' value='" + url + "'>" + 
						"<param name='quality' value='high' />"+
						"<param name='Play' VALUE='-1'>" +
						"<param name='Loop' value='0'>" + 
						"<param name='Quality' value='High'>" + 
						"<param name='Scale' value='ShowAll'>" +
						"<param name='_cx' value='5477'>" +
						"<param name='_cy' value='2117'>" +
						"<param name='FlashVars' VALUE=''>" + 
						"<param name='SAlign' value=''>" +
						"<param name='Menu' value='-1'>" +
						"<param name='Base' value=''>" +
						"<param name='DeviceFont' value='0'>" +
						"<param name='EmbedMovie' value='0'>" +
						"<param name='SWRemote' value=''>" +
						"<param name='MovieData' value=''>" +
						"<param name='SeamlessTabbing' value='1'>" +
						"<param name='Profile' value='0'>" +
						"<param name='ProfileAddress' value=''>" +
						"<param name='ProfilePort' value='0'>" +
						"<param name='AllowNetworking' value='all'>" +
						"<param name='AllowFullScreen' value='false'>" +					
						"<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.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 + '?from=' + window.location.href.split('?')[0] + '&linkid=' + arrAd_" + UID + "[randomIndex_" + UID + "].LinkID + '&to=' + 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 + "].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 + "');" +

		"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" +
		"{" +
			"/*alert( timerStatus_" + UID +");*/"+
			"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 + "');" +

			"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);

}


function getDocHeight(doc) {
  var docHt = 0, sh, oh;
  if (doc.height) docHt = doc.height;
  else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);
  }
  return docHt;
}
 
function setIframeHeight(iframeName) {
  var iframeWin = window.frames[iframeName];
  var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
  if ( iframeEl && iframeWin ) {
    iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous  
    var docHt = getDocHeight(iframeWin.document);
    // need to add to height to be sure it will all show
    if (docHt) iframeEl.style.height = docHt + 30 + "px";
  }
}
 
function loadIframe(iframeName, url) {
  if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;   
    return false;
  }
  else return true;
}


