
// these vars are written by topnav.cs
//	g_ActiveTopIdx
//	g_ActiveSubIdx
//	g_lbl_lightboxsaveconfirm 
//	g_NSHomePath
//	g_NSFilmDetectPluginPath
//	g_NSFilmDownloadClipPath
//	g_sImgHunID 
//	g_sImgTenID
//	g_sImgOneID

var g_menuColor = "#FFFFFF";

var g_fontColorOff	  = "#000000";
var g_menuBorderColor = "#CCCCCC";
var g_PrimaryColor	  = "#FF9900";

var g_BrowserWidth	= window.innerWidth;
var g_hasLayers		= (document.layers);
var g_hasAll		= (document.all);
var g_hasGetElement = (document.getElementById);
var undefined;

var IE40 = (navigator.appVersion.indexOf("MSIE 4.0") >= 0);
var IE45 = (navigator.appVersion.indexOf("MSIE 4.5") >= 0);
var NS7  = ((navigator.userAgent.indexOf("Netscape/7") >= 0) || (navigator.userAgent.indexOf("Safari") >= 0));
var NS6  = (!g_hasAll && g_hasGetElement);	// this test is positive for NS7 also

var g_sepText;
if (g_hasGetElement || g_hasAll) {
	if (IE40)
		g_sepText = "<font color='#999999' size='1'>&nbsp;|&nbsp;</font>";
	else
		g_sepText ="<span class='subNav' style='color:#999999'>&nbsp;|&nbsp;</span>";
}
else if (g_hasLayers) 
	g_sepText = "&nbsp;|&nbsp;";

function outputMenusNow()
{
	if (g_hasLayers)	{
		document.write("<layer id='topNav' position=relative; top=6; left=0; visibility=hide;>");
		writeMenuItems();
		document.write("</layer>");
	} else if (g_hasGetElement || g_hasAll) {
		document.write("<DIV id='topNav' style='position:relative; top:6; left:0'>")
		writeMenuItems()
		document.write("</DIV>")
	}

	if (g_hasAll)
		centerDiv();
	else if (g_hasGetElement)
         centerNS6();
	else if (g_hasLayers)
		centerLayers();
}

function globalnavStart()
{
	window.onresize = centerDiv;
}

function showNavTip(showit)
{
	if (showit)
		showDDNav('globalNavTip');
	else
		hideDDNav('globalNavTip');
}

function showDDNav(itemId) { 
// DD means DropDown
	var obj;
	if (g_hasGetElement || g_hasAll) {
		if (g_hasGetElement)
			obj = document.getElementById(itemId);
		else
			obj = document.all[itemId];

		if (obj) {
			if(obj.style.visibility == 'hidden') {
				obj.style.visibility = 'visible';
			}
		}
	} else if (g_hasLayers) {
		obj = document.layers[itemId];
		if (obj) 
			if(obj.visibility == 'hide')
				obj.visibility = 'show';
	}

}

function hideDDNav(itemId) {
// DD means DropDown
	var obj;
	if (g_hasGetElement || g_hasAll) {
		if (g_hasGetElement)
			obj = document.getElementById(itemId);
		else
			obj = document.all[itemId];
		if (obj) {
			if(obj.style.visibility == 'visible') {
				obj.style.visibility = 'hidden';
			}
		}
	} else if (g_hasLayers) {
		obj = document.layers[itemId];
		if (obj) 
			if(obj.visibility == 'show')
				obj.visibility = 'hide';
	}
	
}

function menuOver(layer) {
	var menu = eval(layer);
	if (menu == null) return;	
	var MenuURL = menu.document.links[0].href;
	var MenuText = menu.document.links[0].text;
	menu.document.open();
	menu.document.write("<a href="+MenuURL +"><font class='menuFonts' face='arial' color='" + g_PrimaryColor + "'>"+ MenuText + "</font></a>");
	menu.document.close();
}

function menuOut(layer) {
	var menu = eval(layer);
	if (menu == null) return;
	var MenuURL = menu.document.links[0].href;
	var MenuText = menu.document.links[0].text;
	menu.document.open();
	menu.document.write("<a href="+MenuURL+"><font class='menuFonts' face='arial' color='#000000'>"+ MenuText + "</font></a>");
	menu.document.close();
}

function subOver(layer, itemStr, sepStr)
{    
	var menu = eval(layer);
	if (menu == null) return;
	var MenuURL = menu.document.links[0].href;
	var MenuText = menu.document.links[0].text;
	menu.document.open();
	menu.document.write("<a href=" + MenuURL + "><font class='menuFonts' size='2' face='arial' color='" + g_PrimaryColor + "'>" + itemStr + "<font color='#999999'>" + sepStr + "</font></font></a>");
	menu.document.close();
}

function subOut(layer, itemStr, sepStr) {
	var menu = eval(layer);
	if (menu == null) return;
	var MenuURL = menu.document.links[0].href;
	var MenuText = menu.document.links[0].text;
	menu.document.open();
	menu.document.write("<a href="+MenuURL +"><font class='menuFonts' size='2' face='arial' color='#000000'>"+ itemStr + "<font color='#999999'>" + sepStr + "</font></font></a>");
	menu.document.close();
}

function writeDiv(menuID, left, divTop, tdID, width) {

	var divID = "menu" + menuID;
	var menuData = eval("menuData" + menuID);
	if (menuData == null) return 0;
	
	var divLeft = parseInt(left);
	divLeft = divLeft + (menuID % 7);

	divTop = divTop - 1;
	var mousey = " onMouseover=\"showDDNav('"+ divID + "');\" onMouseout=\"hideDDNav('" + divID + "');\" ";
	if (g_hasGetElement) {
		if (NS6) {
			objDiv = "<div class=\"dropDownContainer\" id=\"" + divID + "\" style=\"left: " + divLeft + "; width: " + (width+2) + "; visibility:hidden; top: " + divTop + "; z-index:2; background-color: " + g_menuBorderColor + "; \" " + mousey + " >";
			objDiv += "<div style=\"background-color:" + g_menuBorderColor + "; z-index:3; width:" + (width+2) + "; height:1;\"></div>";
		} else {
			objDiv = "<div class=\"dropDownContainer\" id=\"" + divID + "\" style=\"left: " + divLeft + ";visibility: hidden;top: " + divTop + "; z-index:2; \" " + mousey + " >";
		}
	} else if (g_hasAll) {
		objDiv = "<div class=\"dropDownContainer\" id=\"" + divID + "\" style=\"left: " + divLeft + ";visibility: hidden;top: " + divTop + "; z-index:2; \"  " + mousey + " >";
	}

	var leftText = (g_hasAll) ? "" : "left:1;";
	
	for (y = 0; y < menuData.length / 2; y++) {
		var strItem = menuData[(y * 2)];
		var strUrl  = menuData[(y * 2) + 1];
		var strItemTip = strItem.replace("'","\\'");

		if (strUrl.search(/^javascript:/i) == -1)
			strUrl = "doJump('" + strUrl + "')"
		
		if (g_hasAll || NS6)
			objDiv += "<div id=\"x" + tdID + "\" style=\"background-color:"+ g_menuColor +"; z-index:3; width:" + width + ";" + leftText + "\" onMouseover=\"this.style.color='" + g_PrimaryColor + "';onText('"+ strItemTip +"');\" onMouseout=\"this.style.color='#000000';StatusOff();\" class=\"dhtmlNav\" onClick=\""+strUrl+"\">&nbsp;"+ strItem +"&nbsp;</div>";
		tdID = tdID + 1;
	}

	if (NS6) {
		objDiv += "<div style=\"background-color:" + g_menuBorderColor + "; z-index:3; width:" + (width+1) + "; height:1;\"></div>";
	}
	objDiv += "</div>";
	document.write(objDiv);

	return tdID;
}

function PrimaryNewImageSuffix(menuImg, suffix)
{
	var imgName = eval("document.img" + menuImg);
	if (imgName == null) return;
	
	var currentSrc = imgName.src;
	var ix = currentSrc.lastIndexOf('_');
	imgName.src = currentSrc.slice(0, ix) + suffix;

}
function PrimarySwapImg(menuImg) {
	PrimaryNewImageSuffix(menuImg, "_sel.gif");
}

function PrimarySwapImgBack(menuImg, menuNumber) {   
	// These are the primary 'buttons'
	if (menuNumber == g_ActiveTopIdx)
		PrimaryNewImageSuffix(menuImg, "_sel.gif");
	else
		PrimaryNewImageSuffix(menuImg, "_reg.gif");
}

function writeLayer(x, left, top, width) {
	var menuData = eval("menuData" + x);
	if (menuData == null) return;
	if (menuData.length ==0 ) return;

	var nestLayerID = 0;
	var startLeftPos = getLeftPos();
	var layerLeft = parseInt(left) - 9;	
	layerLeft = layerLeft + (startLeftPos) + (x % 7);	
	var layerTop = parseInt(top)-11;
	var layerID = "menu" + x;
	var layerStyle;
	
	if (g_JS_isWin)
		layerStyle="netMenu";
	else
		layerStyle="netMenuMac";
	
	var layerInc = 11;
	var mousey = " onMouseover=\"showDDNav('"+ layerID + "');\" onMouseout=\"hideDDNav('" + layerID + "');\" ";

	var objDiv = "<layer id=\""+ layerID +"\" Z-INDEX=30 BGCOLOR=\""+ g_menuBorderColor +"\" WIDTH="+ (width+2) +" LEFT="+ layerLeft +" TOP="+ (layerTop +40) +" VISIBILITY=HIDE " + mousey + ">;";

	var nestTop = 10;
	for (var y = 0; y < menuData.length / 2; y++)
	{
		var strItem = menuData[(y * 2)];
		var strUrl = menuData[(y * 2) + 1];
		if (strUrl.search(/^javascript:/i) == -1)
			strUrl = "doJump('" + strUrl + "')";
		objDiv += "<layer id=\"x"+(nestLayerID +"a")+"\" class=\""+ layerStyle +"\" BGCOLOR=\""+ g_menuColor +"\" HEIGHT=13 WIDTH="+ width +" LEFT=1 TOP="+ (nestTop-9) + "></layer>";
		objDiv += "<layer id=\"x"+ nestLayerID      +"\" class=\""+ layerStyle +"\" Z-INDEX=37 HEIGHT=13 WIDTH="+ (width-3) +" LEFT=5 TOP="+ (nestTop-9) +" onmouseover=\"menuOver('document.layers[\\'"+ layerID +"\\'].document.layers[\\'x"+nestLayerID+"\\']')\" onmouseout=\"menuOut('document.layers[\\'"+ layerID +"\\'].document.layers[\\'x"+nestLayerID+"\\']')\" onClick=\""+strUrl+"\"><a id=navstyle  href=JavaScript:"+strUrl+"><font class='menuFonts' size='2' face='arial' color='" + g_fontColorOff + "'>" + strItem;
		objDiv += "</font></a></layer>";						
		nestLayerID=nestLayerID+1
		nestTop=nestTop+layerInc+1
	}
	objDiv +="<layer id=\"lineEnd\" BGCOLOR='" + g_menuBorderColor + "' WIDTH=160 HEIGHT=0 Z-INDEX=37 TOP="+ (nestTop-7) +"></layer>";
	objDiv +="</layer>";
	document.write(objDiv);
} 

function writeMenuItem(menuIndex){

	var menuName = "menu" + menuIndex;

	var menuDataIdx = menuIndex*7;
	
	var href	 = mainMenuData[menuDataIdx ];
	var imgSrc	 = mainMenuData[menuDataIdx + 1];
	var imgWidth = mainMenuData[menuDataIdx + 3];
	var imgHeight= mainMenuData[menuDataIdx + 4];
	var altTag   = mainMenuData[menuDataIdx + 5];

	if (menuIndex == g_ActiveTopIdx) {
		imgSrc=imgSrc.slice(0, imgSrc.length-8);
		imgSrc=imgSrc + "_sel.gif";
	}

	var imgClass = (menuIndex == 0) ? "" : "navImage";

	var mousey = " onMouseover=\"showDDNav('"+ menuName+ "');PrimarySwapImg('" + menuName + "');return onText('" + altTag + "');\" onMouseout=\"hideDDNav('" + menuName+ "');PrimarySwapImgBack('" + menuName +"'," + menuIndex + ");StatusOff();\" ";
	var str = "";
	
	if (g_hasGetElement){
		str += "<img name='img" + menuName + "' src='" + imgSrc + "' width='" + imgWidth + "' height='" + imgHeight +"' border='0' " + mousey + ">";
		str += "<img src='/source/images/clear.gif' height='1' width='7'>";
	}
	else if (g_hasAll){
		str += "<a " + mousey + " href=\"#\"  TARGET=\"_top\">";
		str += "<img name='img" + menuName + "' src='" + imgSrc + "' width='" + imgWidth + "' height='" + imgHeight +"' border='0' class='" + imgClass + "'></a>";
	}
	else if (g_hasLayers)
	{
		var leftPos = ((menuIndex % 7) * 121);
		str += "<layer class='navImage' left='" + leftPos + "'>";
		str += "<a " + mousey + " href=\"JavaScript:return null;\" TARGET=\"_top\">";
		str += "<img name=img" + menuName + " src='" + imgSrc + "' width='" + imgWidth + "' height='"+ imgHeight +"' border=0></a>";
		str += "</layer>";

	}

	document.write(str);
}

function writeMenuItems() 
{
	if (g_hasGetElement || g_hasAll)
		initMenus();

	for (var i = 0; i < g_TopMenuCount; i++)
	  	writeMenuItem(i);
	
	if (g_hasGetElement || g_hasAll)
		writeHorizSubNav();
}

function writeHorizSubNav() {

	var subMenuItem = g_ActiveTopIdx;
	var menuData = eval("menuData" +subMenuItem);
	if (menuData == null) return;
	if (menuData.length ==0) return;
		
	var subtdID = 0;
	var sepText = g_sepText;
	var objSubDiv ="";

	if (g_hasGetElement || g_hasAll)
		objSubDiv +="<br />";

	if (g_hasLayers)
		objSubDiv += "<layer id='subxContainer' visibility=hide left=20 top=50 width=759 height=32 z-index=0>"; 

	for (var i = 0; i < menuData.length / 2; i++) {	 
		if (i == menuData.length / 2 - 1)
			sepText="";

		var strItem = menuData[(i * 2)];
		var strUrl  = menuData[(i * 2) + 1];

		if (strUrl.search(/^javascript:/i) == -1)
			strUrl = "javascript:doJump('" + strUrl + "'); "

		var ix = strUrl.indexOf("void ");
		if (ix != -1) {
			var px = "";
			if (ix > 0)
				px += strUrl.substr(0, ix);
			px += strUrl.substr(ix+5);
			strUrl = px;
		}
		var hrefUrl  = " href=\"" + strUrl + "\"";
		var clickUrl =  " onClick=\"" + strUrl+ "\"";
		var strItemTip = strItem.replace("'","\\'");
		
		if (g_hasGetElement || g_hasAll) {

			if (i == g_ActiveSubIdx)
			{
				if (IE40)
					objSubDiv += "<a " + hrefUrl + " style=\"color:'" + g_PrimaryColor + "';\" class=\"subNav40\"							 onmouseover=\"return onText('" + strItemTip + "');\">"+ strItem + "</a>";
				else if (NS6)
					objSubDiv +="<span id=\"subx" + subtdID + "\"" + clickUrl + " class=\"subNavOr\""+ "									 onmouseover=\"return onText('" + strItemTip + "');\">"+ strItem + "</span>" ;
				else
					objSubDiv +="<span id=\"subx" + subtdID + "\"" + clickUrl + " style=\"color:'" + g_PrimaryColor + "';\" class=\"subNav\" onmouseover=\"return onText('" + strItemTip + "');\">"+ strItem + "</span>";

			} else {
				if (IE40)
					objSubDiv += "<a " + hrefUrl + " class=\"subNav40\"	+						   onmouseover=\"this.style.color='" + g_PrimaryColor + "';return onText('" + strItemTip + "');\" onmouseout=\"this.style.color='#000000';StatusOff();\" >"+ strItem + "</a>";
				else
					objSubDiv +="<span id=\"subx" + subtdID + "\" class=\"subNav\"" + clickUrl + " onmouseover=\"this.style.color='" + g_PrimaryColor + "';return onText('" + strItemTip + "');\" onmouseout=\"this.style.color='#000000';StatusOff();\" >"+ strItem +"</span>";
			}
			objSubDiv += sepText;
		}	
		else if (g_hasLayers) {
			   			
			objSubDiv +="<layer id=\"subx" + subtdID + "\" class=\"subNav\" height=16 top=0 " + clickUrl;			   			
			if (i == g_ActiveSubIdx)
				objSubDiv += "\"><a " + hrefUrl + " onmouseover=\"return onText('" + strItemTip + "');\" onmouseout=\"StatusOff();\">";
			else
				objSubDiv += " onmouseover=\"subOver('document.layers[\\'subxContainer\\'].document.layers[\\'subx"+subtdID+"\\']','" + strItemTip + "', '" + sepText + "');\" onmouseout=\"subOut('document.layers[\\'subxContainer\\'].document.layers[\\'subx"+subtdID+"\\']','" + strItemTip + "', '" + sepText + "');\"><a href=javascript:doJump('"+strUrl+"') onmouseover=\"return onText('" + strItemTip + "');\" onmouseout=\"StatusOff()\">";
			
			var colora = (i == g_ActiveSubIdx) ? g_PrimaryColor : "#000000";
			var colorx = (i == g_ActiveSubIdx) ? "#000000" : "#999999";

			objSubDiv += "<font class='menuFonts' size='2' face='arial' color='" + colora + "'>";
			objSubDiv += strItem + "</font>" + "<font class=menuFonts color='" + colorx + "'>" + sepText +"</font></a></layer>";
		}
		subtdID = subtdID + 1;
	}
	
	if (g_hasLayers)
		objSubDiv +="</layer>"

	document.write(objSubDiv);
	
	if (g_hasLayers)
	{
		bottomColumnStartIndex = setSubNavWidths();
		if(bottomColumnStartIndex >0){
			setSubNavHeights(bottomColumnStartIndex);
		}
	}
}

function parseSubArrays(TopMenuIndex) {
	var menuArray = eval("menuData" + TopMenuIndex);
	if (menuArray == null) return;
	return g_ActiveTopIdx == TopMenuIndex;
}

function initMenus() { 
	var top = mainMenuData[4];
	var tdID = 0;

	var offset = 0;
	for (var y = 0; y < g_TopMenuCount; y++)  {  
		var left = mainMenuData[y * 7 + 2] - offset;
		var width = parseInt(mainMenuData[y * 7 + 6]);
		
		if (g_hasGetElement || g_hasAll) {			// if IE or NS6
			tdID = writeDiv(y, left, top, tdID, width);
		} else if (g_hasLayers) {					// if NS<6
			writeLayer(y, left, top, width);
			if (g_ActiveTopIdx == y)
				writeHorizSubNav();
		}
		offset++;
	}
}

function setSubNavWidths() { 

	var ourLayers = document.layers.subxContainer.layers;
	var subItemWidth = 0;
	var subItemLeft = 0;
	var b = 0;
	for (var i=0; i < ourLayers.length; i++) {

		if (subItemLeft > 600){
			subItemLeft = 0;
			b = i;
		}
		subItemWidth = ourLayers[i].clip.width;
		ourLayers[i].left = subItemLeft;
		subItemLeft = subItemLeft + subItemWidth;
	}
	return b;
}

function setSubNavHeights(startIndex) { 

	var ourLayers = document.layers.subxContainer.layers;
	var secondRowHeight = ourLayers[0].clip.top + 16;
	for (var i=startIndex; i < ourLayers.length; i++)
		ourLayers[i].top = secondRowHeight;
}


function centerDiv()
{
	var elGlobalNav = FindElement("globalNav");
	if (elGlobalNav == null) return;
	var browserWidth = GetBrowserWidth();
	var globalNavWidth = GetElementWidth("globalNav");
	
	if (this.parent.frames.length > 1)
		browserWidth -=16;
	
	var leftMarg = (browserWidth-globalNavWidth) / 2;
	if (leftMarg < 5) leftMarg = 5;
	
	SetLeftPos("globalNav", leftMarg);
	elGlobalNav.style.visibility = "visible";
}

function centerNS6() {
	var elGlobalNav = document.getElementById("globalNav");
	if (elGlobalNav == null) return;

	elGlobalNav.style.left = getLeftPos();
	elGlobalNav.style.visibility = "visible";
}

function centerLayers() {
	var doc = document.layers;
	var leftPos = getLeftPos();

	if (doc.subxContainer != undefined) {
		
		if (g_JS_isWin) {
			if (location.href == parent.location.href)
				doc.subxContainer.left=leftPos - 10;
			else
				doc.subxContainer.left=leftPos - 8;
		} else
			doc.subxContainer.left=leftPos - 9;
		
		doc.subxContainer.visibility="show"; 
	}
	doc.Cart.left = leftPos + 575;
	doc.Cart.visibility="show";
	doc.Logo.left = leftPos - 9;
	doc.Logo.visibility="show";
	doc.WelcomeBack.left = leftPos + 195;
	doc.WelcomeBack.visibility="show";
	doc.topNav.left = leftPos - 9;
	doc.topNav.visibility="show";
}

function getLeftPos() {
	var browserWidth = window.innerWidth;
	var subNavWidth = 759;
	if (g_BrowserWidth > browserWidth) {
		subNavWidth = subNavWidth - 15;
	}
	var subNavLeft= (browserWidth - subNavWidth) / 2;
	subNavLeft = Math.round(subNavLeft);
	
	// quirky offset for gecko
	if (NS6 || NS7) {
		subNavLeft -= 8;
		if (subNavLeft < 5) subNavLeft = 5;
	} else {
		if (subNavLeft < 15) subNavLeft = 15;
	}

	return subNavLeft;
}

function doJump(url)
{
	if (url != "")
		top.location.href = url;	
}

function openClipPreviewWindow(clipID,brandID) {
	openClipShowreelPreviewWindow(clipID,brandID,"Clip");
}

function openShowreelPreviewWindow(clipID,brandID) {
	openClipShowreelPreviewWindow(clipID,brandID,"Showreel");
}
 
function openClipShowreelPreviewWindow(clipID,brandID, ourType)
{
	window.open(g_NSFilmDetectPluginPath + "?Type=" + ourType + "&clipID=" + clipID + "&brandID=" + brandID,"previewWin", "width=500,height=500,top=50,left=420,alwaysRaised=yes,toolbar=0,directories=0,menubar=0,status=1,resizable=yes,location=0,scrollbars=1,copyhistory=0");
}

// Only need handle the .net front doors.

var g_arrFrontDoors = new Array(
	"/archivefilms/",
	"/bavariabild/",
	"/brandx/",
	"/botanica/",
	"/digitalvision/",
	"/photodisc/",
	"/photographerchoice/",
	"/pix/",
	"/rubberball_stills/",
	"/stone/",
	"/taxi/",
	"/tcl/",
	"/thinkstock/",
	"/tibfilm/",
	"/timelife/");

var g_arrFrontDoorsUrl = new Array(
	"/frontdoor/default_archivefilms.aspx",
	"/frontdoor/default_taxi.aspx",  // bavaria
	"/frontdoor/default_brandx.aspx",
	"/frontdoor/default_botanica.aspx",
	"/frontdoor/default_digitalvision.aspx",
	"/frontdoor/default_photodisc.aspx",
	"/frontdoor/default_choice.aspx",
	"/frontdoor/default_taxi.aspx",	// pix
	"/frontdoor/default_rubberball.aspx",
	"/frontdoor/default_stone.aspx",
	"/frontdoor/default_taxi.aspx",
	"/frontdoor/default_taxi.aspx",	// tcl
	"/frontdoor/default_thinkstock.aspx",
	"/frontdoor/default_tibfilm.aspx",
	"/frontdoor/default_timelife.aspx"
	);

function SetCountry(country)
{
	var url = top.document.location.href.toLowerCase();
	var countryParam = "country="; 

	if (url.indexOf("/frontdoor/") == -1)
	{
		url = g_NSHomePath;
		for (var i = 0; i < g_arrFrontDoors.length; i++)
		{
			if (url.indexOf(g_arrFrontDoors[i]) != -1) 
			{
				url = g_arrFrontDoorsUrl[i];
				break;
			}
		}
	}

	// If there's already a country parameter tweak it. Otherwise, add it
	if (url.indexOf(countryParam) != -1) {
		var pos1 = url.indexOf(countryParam) + countryParam.length;
		url = url.slice(0,pos1) + country + url.slice(pos1 + country.length);
	} 
	else if (url.indexOf('?') == -1)
		url += "?" + countryParam + country;
	else
		url += "&" + countryParam + country;
	doJump(url);
}

