
var pageData = new dataObject();
var xmlRequest;
var currentPage=0;
var currentMenuItem=0;
var query;


function dataObject()
{
}

function swapClass(i,c) { 
	if (document.getElementById) {  
	document.getElementById(i).className =  c;  
	} else {
	document.all[i].className = c;	
	}
}

function getElement( base, i ) {
	if (base.getElementById) {  
		return base.getElementById(i);  
	} else {
		return base.all[i];	
	}
}

function swapSrc(i,c) { 
	if (document.getElementById) {  
	document.getElementById(i).src =  c;  
	} else {
	document.all[i].src = c;	
	}
}


function viewMenuPage( pagenum )
{
	for ( var x=0; x<10; x++ )
	{
		var menuitem = getElement( document, "menuitem" + x );
		if ( menuitem != null )
		{
			menuitem.style.display = "none";
			swapClass( "menu" + x, "normal_txt" );
		}
	}
	var menuitem = getElement( document, "menuitem" + pagenum );
	if ( menuitem != null )
	{
		menuitem.style.display = "inline";
		swapClass( "menu" + pagenum, "red_txt" );
	}
}


function loadAdvertisingPage(xmlFile)
{
	var xmlFile;
	query = parseQueryString();
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		xmlRequest = new XMLHttpRequest();
		try
		{
			xmlRequest.overrideMimeType('text/xml');
		}
		catch (err)
		{
		}
	} else if (window.ActiveXObject) { // IE
		try
		{
			xmlRequest= new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (err)
		{
			try
			{
				xmlRequest= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (err2)
			{
			}
		}

	}

//	if ( query['campaign'] != null && query['campaign'] != "" )
//	{
//		xmlFile = 'assets/' + query['campaign'] + '/content.xml';
//		campaign = query['campaign'];
//	}


	xmlRequest.onreadystatechange = parseAdvertisingData;
	xmlRequest.open( 'GET', xmlFile, true );
	xmlRequest.send(null);
//alert ('loading xml');
}

function parseAdvertisingData()
{
	//Here we parse the xml page data
	//This is also a good opportunity to preload and choose the load order
	//of images before they are plugged into the page.

	//Choosing to preload first image first, then all the thumbs (in order)

	if ( xmlRequest.readyState == 4  && xmlRequest.status == 200 )
	{
		var xmldoc = xmlRequest.responseXML;
		var advertising_node = xmldoc.getElementsByTagName('advertising').item(0);

		pageData.basePath = advertising_node.getAttribute("basepath");
		pageData.menuItems = new Array();

		var menuitems = advertising_node.getElementsByTagName("menuitem");
		for ( var x=0; x<menuitems.length; x++ )
		{
			var item = menuitems[x];
			pageData.menuItems[x] = new dataObject();

			if (item.getElementsByTagName("copy")[0] && item.getElementsByTagName("copy")[0].firstChild)
			{
				pageData.menuItems[x].copy = item.getElementsByTagName("copy")[0].firstChild.data;
			}
			else
			{
				pageData.menuItems[x].copy = "";
			}

			if (item.getElementsByTagName("title")[0] && item.getElementsByTagName("title")[0].firstChild)
			{
				pageData.menuItems[x].title = item.getElementsByTagName("title")[0].firstChild.data;
			}
			else
			{
				pageData.menuItems[x].title = "";
			}

			if (item.getElementsByTagName("slide")[0] )
			{
				pageData.menuItems[x].slidePrefix = item.getElementsByTagName("slide")[0].getAttribute('imgpfx');
				pageData.menuItems[x].slideType = item.getElementsByTagName("slide")[0].getAttribute('type');
				pageData.menuItems[x].slideURL = item.getElementsByTagName("slide")[0].getAttribute('url');

				//preload first image
				//changed to "x == 1" from "x == 0" below to accomidate a jpg being the first item
				
				if ( x == 0 && pageData.menuItems[x].slideType == "jpg")
				{
					preloadImg( "Screens/" + pageData.menuItems[x].imagePrefix + ".jpg" );
				}				

				preloadSlideThumbs( pageData.menuItems[x].slidePrefix );

			}
			else
			{
				pageData.menuItems[x].slidePrefix = "";
				pageData.menuItems[x].slideType = "";
			}



		}

		chooseAdPage(0);
	}
	else
	{
		//alert('readystate: ' + xmlRequest.readyState + 'status: ' + xmlRequest.status);
	}
	

}

function preloadSlideThumbs( slidepfx )
{
	preloadImg( pageData.basePath + "Thumbs/" + slidepfx + ".jpg" );
	preloadImg( pageData.basePath +  "Thumbs/" + slidepfx + "_off.jpg" );
	preloadImg( pageData.basePath + "Thumbs/" + slidepfx + "_roll.jpg" );
}

function preloadHomePageRolls()
{
	preloadImg('images/hp/hp_logo.gif');
	preloadImg('images/hp/hp_header.gif');
	preloadImg('images/hp/hp_nav_personal_off.gif');
	preloadImg('images/hp/hp_nav_sm_commercial_off.gif');
	preloadImg('images/hp/hp_nav_lg_commercial_off.gif');
	preloadImg('images/hp/hp_nav_personal_on.gif');
	preloadImg('images/hp/hp_nav_sm_commercial_on.gif');
	preloadImg('images/hp/hp_nav_lg_commercial_on.gif');
	preloadImg('images/hp/hp_main.jpg');
}

function preloadNavRolls()
{
	preloadImg('images/pers_insurance/travelers_logo.gif');
	preloadImg('images/pers_insurance/find_agent_off.gif');
	preloadImg('images/pers_insurance/find_agent_on.gif');
	preloadImg('images/rightnav/nav_main_off.gif');
	preloadImg('images/rightnav/nav_main_off.gif');
	preloadImg('images/rightnav/nav_personal_off.gif');
	preloadImg('images/rightnav/nav_sm_commercial_off.gif');
	preloadImg('images/rightnav/nav_lg_commercial_off.gif');
	preloadImg('images/btn_back_off.gif');
	preloadImg('images/rightnav/nav_main_on.gif');
	preloadImg('images/rightnav/nav_personal_on.gif');
	preloadImg('images/rightnav/nav_sm_commercial_on.gif');
	preloadImg('images/rightnav/nav_lg_commercial_on.gif');
	preloadImg('images/btn_back_on.gif');	
}

function preloadLCModules()
{
	preloadImg('images/lg_commercial/travelers_ad_off.jpg');	
	preloadImg('images/lg_commercial/travelers_ad_on.jpg');	
}
function preloadSCModules()
{
	preloadImg('images/sm_commercial/challenge_off.jpg');	
	preloadImg('images/sm_commercial/in_action_off.jpg');	
	preloadImg('images/sm_commercial/travelers_ad_off.jpg');	
	preloadImg('images/sm_commercial/challenge_on.jpg');	
	preloadImg('images/sm_commercial/in_action_on.jpg');	
	preloadImg('images/sm_commercial/travelers_ad_on.jpg');	
}
function preloadPersModules()
{
	preloadImg('images/pers_insurance/challenge_off.jpg');	
	preloadImg('images/pers_insurance/in_action_off.jpg');	
	preloadImg('images/pers_insurance/travelers_ad_off.jpg');	
	preloadImg('images/pers_insurance/challenge_on.jpg');	
	preloadImg('images/pers_insurance/in_action_on.jpg');	
	preloadImg('images/pers_insurance/travelers_ad_on.jpg');	
}

//need to store the images globally so they don't go out of scope and not load.
//they appear to load in the order they are referenced on most browsers, so you
//can expect images to appear in the order you preload them.
preloadedImages = new Array();
function preloadImg( imgurl )
{
	var img = new Image();
	img.src = imgurl;
	preloadedImages[preloadedImages.length] = img;
}


function nextAdPage()
{
	chooseAdPage( currentPage + 1 );
}

function previousAdPage()
{
	chooseAdPage( currentPage - 1 );
}

function chooseAdPage( pagenumber )
{	
//alert( "rendering page: " + pagenumber );
	currentMenuItem=0;
	currentPage=pagenumber;
	
	if ( currentPage < 0 )
	{
		currentPage = 0;
	}
	if ( Math.ceil(pageData.menuItems.length / 6) <= currentPage )
	{
		currentPage = Math.ceil(pageData.menuItems.length / 6) - 1;
	}

	
	toggleAdArrows();
	displayAdData();	
}

function toggleAdArrows()
{
	var leftArrow = getElement( document, "arrowLeft");
	var rightArrow = getElement( document, "arrowRight");
	
	if ( currentPage <= 0 )
	{
		leftArrow.style.visibility = "hidden";
	}
	else
	{
		leftArrow.style.visibility = "visible";
	}
	if ( (currentPage*6) + 6 >= pageData.menuItems.length )
	{
		rightArrow.style.visibility = "hidden";
	}
	else
	{
		rightArrow.style.visibility = "visible";
	}
}

function chooseAdMenuItem( itemnumber )
{
	currentMenuItem=itemnumber;
	displayAdData();	
}

function launchSite( theurl )
{
	var newwindow = window.open( theurl, "_blank", "", "true" );
	newwindow.focus();
}

function launchFullScreen( theurl )
{
	URLen = theurl;
	var swfWidth = 500;
	var swfHeight = 400;
	
	// defaults, if we're not able to retrieve actual dimensions
	var winHeight = 800
	var winWidth = 600;

	var windowNamen="mainwindow";
	var browserName=navigator.appName;
	var operatingSystem=navigator.platform;
	var version = parseFloat(navigator.appVersion);
	var winWidth = window.screen.availWidth;
	var winHeight = window.screen.availHeight;

	// Netscape check version 4.0+ on Win
	if (browserName.indexOf("Netscape")!=-1 && version>=4.0 && operatingSystem.indexOf("Mac")!=-1)
	{
		 window.open(URLen,windowNamen,'titlebar=no,top=0,left=0,width=' + winWidth +',height='+winHeight+',screenX=0,screenY=0,top=0,left=0')
	}

	// MSIE Mac check
	else if (browserName.indexOf("Microsoft Internet Explorer")!=-1 && operatingSystem.indexOf("Mac")!=-1)
	{
		window.open(URLen,windowNamen,'titlebar=no,top=0,left=0,width=' + winWidth +',height='+ winHeight +',screenX=0,screenY=0,top=0,left=0')
	}

	// Netscape Mac check
	else if (browserName.indexOf("Netscape")!=-1 && operatingSystem.indexOf("Mac")!=-1)
	{
		winWidth = screen.width;
		winHeight = screen.height;
		window.open(URLen,windowNamen,'width='+ winWidth +',height='+ winHeight +',top=0,left=0');
	}

	// MSIE Windows
	else if (browserName.indexOf("Microsoft Internet Explorer")!=-1 && operatingSystem.indexOf("Win")!=-1)
	{
		//window.open(URLen,windowNamen,'fullscreen=1,top=0,left=0');		 
		var win = window.open(URLen,windowNamen,'titlebar=no,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=0,top=0,left=0,width=' + winWidth +',height='+ winHeight +',screenX=0,screenY=0,top=0,left=0');
		win.resizeTo(screen.width, screen.height);
		//win.moveTo(1, 1);
		//win.moveTo(0, 0);
		//win.resizeTo(screen.width, screen.height);
	}

		// Netscape Windows
	else if (browserName.indexOf("Netscape")!=-1 && operatingSystem.indexOf("Win")!=-1)
	{
		winWidth = screen.width;
		winHeight = screen.height;		 
		window.open(URLen,windowNamen,'width='+ winWidth +',height='+ winHeight +',top=0,left=0');
	}

	else
	{
		window.open(URLen,windowNamen);
	}
	window.winWidth = winWidth;
	window.winHeight = winHeight;
}

function launchSoftFullScreen( theurl,windowid )
{
	URLen = theurl;
	var swfWidth = 500;
	var swfHeight = 400;
	
	// defaults, if we're not able to retrieve actual dimensions
	var winHeight = 800
	var winWidth = 600;

	var windowNamen=windowid;
	var browserName=navigator.appName;
	var operatingSystem=navigator.platform;
	var version = parseFloat(navigator.appVersion);
	var winWidth = window.screen.availWidth;
	var winHeight = window.screen.availHeight;

	// Netscape check version 4.0+ on Win
	if (browserName.indexOf("Netscape")!=-1 && version>=4.0 && operatingSystem.indexOf("Mac")!=-1)
	{
		 window.open(URLen,windowNamen,'titlebar=no,top=0,left=0,width=' + winWidth +',height='+winHeight+',screenX=0,screenY=0,top=0,left=0')
	}

	// MSIE Mac check
	else if (browserName.indexOf("Microsoft Internet Explorer")!=-1 && operatingSystem.indexOf("Mac")!=-1)
	{
		window.open(URLen,windowNamen,'titlebar=no,top=0,left=0,width=' + winWidth +',height='+ winHeight +',screenX=0,screenY=0,top=0,left=0')
	}

	// Netscape Mac check
	else if (browserName.indexOf("Netscape")!=-1 && operatingSystem.indexOf("Mac")!=-1)
	{
		winWidth = screen.width;
		winHeight = screen.height;
		window.open(URLen,windowNamen,'width='+ winWidth +',height='+ winHeight +',top=0,left=0');
	}

	// MSIE Windows
	else if (browserName.indexOf("Microsoft Internet Explorer")!=-1 && operatingSystem.indexOf("Win")!=-1)
	{
		//window.open(URLen,windowNamen,'fullscreen=1,top=0,left=0');		 
		var win = window.open(URLen,windowNamen,'titlebar=no,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,top=0,left=0,width=' + winWidth +',height='+ winHeight +',screenX=0,screenY=0,top=0,left=0');
		win.resizeTo(screen.width, screen.height-25);
		//win.moveTo(1, 1);
		//win.moveTo(0, 0);
		//win.resizeTo(screen.width, screen.height);
	}

		// Netscape Windows
	else if (browserName.indexOf("Netscape")!=-1 && operatingSystem.indexOf("Win")!=-1)
	{
		winWidth = screen.width;
		winHeight = screen.height;		 
		window.open(URLen,windowNamen,'width='+ winWidth +',height='+ winHeight +',top=0,left=0');
	}

	else
	{
		window.open(URLen,windowNamen);
	}
	window.winWidth = winWidth;
	window.winHeight = winHeight;
}


function displayAdData()
{

	var contentCopy = getElement( document, "spotCopyElem");
	contentCopy.innerHTML = pageData.menuItems[(currentPage * 6) + currentMenuItem].copy;
	var titleCopy = getElement( document, "spotTitleElem");
	titleCopy.innerHTML = pageData.menuItems[(currentPage * 6) + currentMenuItem].title;


	displayAdMenuItems();

}

function displayAdMenuItems()
{


	for (var x=(currentPage * 6);  x < ((currentPage * 6) + 6); x++)
	{
		var menunumber = x - (currentPage * 6);
		var menuElem = getElement( document, "menuItem" + menunumber);
//alert( "processing slide: " + x + " menu elem: " + menuElem);

		var itemhtml = "";
		if ( x<pageData.menuItems.length )
		{
			itemhtml += "<a href='javascript:chooseAdMenuItem(" + menunumber + ")' ";
			if (menunumber == currentMenuItem)
			{
				itemhtml += ">";
				itemhtml += "<img border=0 id='thumb" + x + "' src='" + pageData.basePath + "Thumbs/" + pageData.menuItems[x].slidePrefix  + ".jpg'>";
			}
			else
			{
				itemhtml += "onmouseover=\"swapSrc('thumb" + x + "', '" + pageData.basePath + "Thumbs/" + pageData.menuItems[x].slidePrefix  + "_roll.jpg')\" onmouseout=\"swapSrc('thumb" + x + "', '" + pageData.basePath + "Thumbs/" + pageData.menuItems[x].slidePrefix + "_off.jpg')\">";
				itemhtml += "<img border=0 id='thumb" + x + "' src='" + pageData.basePath + "Thumbs/" + pageData.menuItems[x].slidePrefix  + "_off.jpg'>";
			}

			itemhtml += "</img></a>";
		}
		else
		{
			itemhtml="<img border=0 id='thumb" + x + "' src='images/advertising/BLANK_off.jpg'>";
		}
		
		menuElem.innerHTML = itemhtml;

	}

	var imageArea = getElement( document, "spotMediaElem");
	var imghtml = "";
	if ( pageData.menuItems[(currentPage*6)+currentMenuItem].slideType == "flv" )
	{
		imghtml += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="422" height="349" id="flvplayer" align="left">';
		imghtml += '<param name="movie" value="'  + pageData.basePath +  'swf/adplayer.swf?_flvurl=' + pageData.menuItems[(currentPage*6)+currentMenuItem].slideURL + '&_bwdetecturl=' + pageData.basePath +  'swf/payload.swf" />';
		imghtml += '<param name="allowScriptAccess" value="*" /><param name="quality" value="high" /><param name="bgcolor" value="#EAE7E6" />';
		imghtml += '<embed src="'  + pageData.basePath +  'swf/adplayer.swf?_flvurl=' + pageData.menuItems[(currentPage*6)+currentMenuItem].slideURL + '&_bwdetecturl=' + pageData.basePath +  'swf/payload.swf" quality="high" bgcolor="#EAE7E6" width="422" height="349" name="flvplayer" align="middle" allowScriptAccess="*" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
//		imghtml += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="422" height="349" id="flvplayer" align="left">';
//		imghtml += '<param name="movie" value="swf/adplayer.swf?_flvurl=' + pageData.menuItems[(currentPage*6)+currentMenuItem].slideURL + '&_bwdetecturl=swf/payload.swf" />';
//		imghtml += '<param name="allowScriptAccess" value="*" /><param name="quality" value="high" /><param name="bgcolor" value="#EAE7E6" />';
//		imghtml += '<embed src="swf/adplayer.swf?_flvurl=' + pageData.menuItems[(currentPage*6)+currentMenuItem].slideURL + '&_bwdetecturl=swf/payload.swf" quality="high" bgcolor="#EAE7E6" width="422" height="349" name="flvplayer" align="middle" allowScriptAccess="*" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	}
	else
	{
		var nextitem = currentMenuItem+1;
		if ( (nextitem >= 6) || ((nextitem + (6*currentPage)) >= pageData.menuItems.length) )
		{
			nextitem = 0;
		}
		imghtml = "<a href=\"javascript:chooseAdMenuItem(" + nextitem + ");\"><img border=0 src='" + pageData.basePath + "Screens/" + pageData.menuItems[currentMenuItem + (6*currentPage)].slidePrefix  + "." + pageData.menuItems[currentMenuItem + (6*currentPage)].slideType + "'></a>";
	}
	imageArea.innerHTML = imghtml;
	
}

function parseQueryString (str) {
  str = str ? str : location.search;
  var query = str.charAt(0) == '?' ? str.substring(1) : str;
  var args = new Object();
  if (query) {
    var fields = query.split('&');
    for (var f = 0; f < fields.length; f++) {
      var field = fields[f].split('=');
      args[unescape(field[0].replace(/\+/g, ' '))] = unescape(field[1].replace(/\+/g, ' '));
    }
  }
  return args;
}


