
//var url = 'http://tb-solcis/glossary/admin/xml/control.jsp?entry=';
var url = '/collections/glossary/xml/control.jsp?entry=';
var isGlossaryInit = false, loadedEntryId = -1, xmlDoc, theTitle, theText, theLink, theBSL, theBSLLink, bottomNav, bottomNav2, bottomNav3;
var BSLentry;
var HTMLinserted = false;
var bandwidth, popupType, mode, popupLeft, popupTop, definitionId, callingPage, TateGlossaryParent;

function glossaryInit() {
	if (document.getElementById) {
		theTitle = document.getElementById('GlossaryPopupTitle');
		theText = document.getElementById('GlossaryPopupDefinition');
		theLink = document.getElementById('GlossaryPopupLink');
		theBSL = document.getElementById('GlossaryPopupBSL');
		theBSLLink = document.getElementById('GlossaryPopupBSLLink');
		isGlossaryInit = theTitle != null && theText != null && theLink != null && theBSL != null;
	}
}

function setDisplay(id, title, text, bsl) {
	glossaryInit();
	if (theLink != null && id != null) {
		theLink.href = '/collections/glossary/' + (id >= 0 ? 'definition.jsp?entryId=' + id : '');
		loadedEntryId = id;
	}
	if (theTitle != null && title != null) theTitle.innerHTML = title;
	if (theText != null && text != null) theText.innerHTML = '<div class="glossarypopupdef">' + text + '</div>';
	BSLentry = (bsl == 'Y');
	theBSL.style.visibility = 'hidden';
	if (theBSL != null) theBSL.style.visibility = BSLentry ? 'visible' : 'hidden';
	if (theBSLLink != null) theBSLLink.href = '/collections/glossary/bsl-definition.jsp?entryId=' + id;
}

function resetText() {
	if (document.glossaryclip) {
		document.glossaryclip.DoPause(); 
	}
	displayEntry();
	normalButtons();
}

function displayEntry() {
	var def = xmlDoc.getElementsByTagName('definition');
	if (def.length == 1) {
		def = def[0];
		var id = def.getAttribute('id');
		var title = def.getAttribute('name');
		var text = def.getElementsByTagName('text');
		var bsl = def.getAttribute('bsl');
		if (text != null) {
			text = text[0].firstChild.nodeValue;
		} else {
			text = '';
		}
		setDisplay(id, title, text, bsl);
	} else {
		setDisplay(-1, 'Error loading glossary definition', 'Please try again later');
	}
}

function getDefinition(id) {
	// setState('layer', 'text', popupLeft, popupTop, id, window.location.href);
	normalButtons();
	var notSupported = false;
	//if (loadedEntryId != id) {
		// Internet Explorer 5 or above
		if (window.ActiveXObject) {
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.onreadystatechange = function () {
				if (xmlDoc.readyState == 4) displayEntry();
			};
		}
		// Netscape 6 or above
		else if (document.implementation && document.implementation.createDocument) {
			xmlDoc = document.implementation.createDocument("", "", null);
			xmlDoc.onload = displayEntry;
		}
		else {
			//alert('your browser can\'t handle this script');
			notSupported = true;
			glossaryPopup(id);
		}
		if (!notSupported) {
			setDisplay(null, 'Loading...', '<div style="height: 350px;">&nbsp;</div>');
			//try {
			xmlDoc.load(url + id);
			//} catch (e) {	window.alert(e.message);	}
			popupLayer = document.getElementById("glossaryPopupLayer");
			popupLayer.style.visibility="visible";
		}
	//}
	return notSupported;
}

// Script Source: CodeLifter.com
// Copyright 2003
// Do not remove this header

isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;
isHot=false;

function ddInit(e){
  if (document.getElementById) {
	  topDog=isIE ? "BODY" : "HTML";
	  whichDog=isIE ? document.all.glossaryPopupLayer : document.getElementById("glossaryPopupLayer");  
	  hotDog=isIE ? event.srcElement : e.target;  
	  while (hotDog.id!="titleBar"&&hotDog.tagName!=topDog){
		hotDog=isIE ? hotDog.parentElement : hotDog.parentNode;
	  }  
	  if (hotDog.id=="titleBar"){
		offsetx=isIE ? event.clientX : e.clientX;
		offsety=isIE ? event.clientY : e.clientY;
		nowX=parseInt(whichDog.style.left);
		nowY=parseInt(whichDog.style.top);
		ddEnabled=true;
		document.onmousemove=dd;
	  }
  }
}

function dd(e){
  if (!ddEnabled) return;
  whichDog.style.left=isIE ? nowX+event.clientX-offsetx : nowX+e.clientX-offsetx; 
  whichDog.style.top=isIE ? nowY+event.clientY-offsety : nowY+e.clientY-offsety;
  return false;  
}

function ddN4(whatDog){
  if (!isN4) return;
  N4=eval(whatDog);
  N4.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
  N4.onmousedown=function(e){
    N4.captureEvents(Event.MOUSEMOVE);
    N4x=e.x;
    N4y=e.y;
  }
  N4.onmousemove=function(e){
    if (isHot){
      N4.moveBy(e.x-N4x,e.y-N4y);
      return false;
    }
  }
  N4.onmouseup=function(){
    N4.releaseEvents(Event.MOUSEMOVE);
  }
}

function hideMe(){
  //alert("hello");
  setDisplay(null, 'Loading...', '');
  if (isIE||isNN) {
  	whichDog.style.visibility = "visible";
	if (theBSL != null) {
		theBSL.style.visibility = "hidden";
	}
	if (document.glossaryclip) {
		document.glossaryclip.DoPause(); 
	}
  } 
}

function hideMe2(){
  //alert("hello");
  setDisplay(null, 'Loading...', '');
  if (isIE||isNN) {
  	whichDog.style.visibility = "hidden";
	if (theBSL != null) {
		theBSL.style.visibility = "hidden";
	}
	if (document.glossaryclip) {
		document.glossaryclip.DoPause(); 
	}
  } 
}

function showMe(){
  if (isIE||isNN) {
  	whichDog.style.visibility="visible";
	if (isIE) {
		whichDog.focus();
	}
  }
}

document.onmousedown=ddInit;
document.onmouseup=Function("ddEnabled=false");

// Positioning code

function setLyr(obj,lyr)
{
	var xOffset = 180;
	var yOffset = -300;
	
	var newX = findPosX(obj) + xOffset;
	var newY = findPosY(obj) + yOffset;
	var x = new getObj(lyr);
	if (x.style.visibility == "hidden") {
		x.style.top = newY + 'px';
		x.style.left = newX + 'px';
	}
}

function setLayer(obj,lyr,xDesiredOffset,yDesiredOffset,xDesiredAbs,yDesiredAbs)
{
	var xOffset, yOffset;
	
	if (xDesiredOffset == null) {
		xOffset = 120;
	} else {
		xOffset = xDesiredOffset;
	}
	
	if (yDesiredOffset == null) {
		yOffset = -35;
	} else {
		yOffset = yDesiredOffset;
	}
	
	var newX = findPosX(obj) + xOffset;
	var newY = findPosY(obj) + yOffset;
	
	if (xDesiredAbs != null) {
		newX = xDesiredAbs;
	}
	if (yDesiredAbs != null) {
		newY = yDesiredAbs;
	}
	
	var x = new getObj(lyr);
	if (x.style.visibility == "hidden") {
		x.style.top = newY + 'px';
		x.style.left = newX + 'px';
	}
	
	popupLeft = newX;
	popupTop = newY;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getObj(name)
{
 if (document.getElementById)
 {
	   this.obj = document.getElementById(name);
	   this.style = document.getElementById(name).style;
 }
 else if (document.all)
 {
	   this.obj = document.all[name];
	   this.style = document.all[name].style;
 }
 else if (document.layers)
 {
	   if (document.layers[name])
	   {
	   	this.obj = document.layers[name];
	   	this.style = document.layers[name];
	   }
	   else
	   {
	    this.obj = document.layers.testP.layers[name];
	    this.style = document.layers.testP.layers[name];
	   }
 }
}

function insertPopupHTML()
{
	/*// getState();
	if (state == null) {
		// setState('n');
	}*/
	if (!HTMLinserted) {
	document.write('<div id="glossaryPopupLayer" style="position:absolute;width:350px;left:236px;top:218px;visibility:hidden;"><table border="0" width="350" bgcolor="#FFFFFF" cellspacing="0" cellpadding="0"><tr><td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0"><tr height="23"><td valign="middle" bgcolor="#999999" class="glossarytitlebar" id="titleBar" style="cursor:move" width="321"><ilayer width="100%" onSelectStart="return false"><layer width="100%" onMouseover="isHot=true;if (isN4) ddN4(glossaryPopupLayer)" onMouseout="isHot=false"><table width="181" border="0" cellpadding="0" cellspacing="0" background="/collections/glossary/images/popup_title.gif"><tr><td><img src="/images/transparent.gif" alt="Tate Glossary" name="glossarytitle" width="1" height="23" id="glossarytitle"></td></tr></table></layer></ilayer></td><td width="29" align="right" valign="middle" class="glossarytitlebar" style="cursor:hand"><a href="#close" onClick="hideMe2(); return false;" onMouseOver="return rollover(\'glossary_popup_close\',1)" onMouseOut="return rollover(\'glossary_popup_close\',0)" name="glossary_popup_closelink" id="glossary_popup_closelink"><img src="/collections/generic/white/glossary_popup_close_norm.gif" alt="Exit glossary definition and return to text" name="glossary_popup_close" id="glossary_popup_close" width="29" height="23" border="0"></a></td><td rowspan="2" valign="top" background="/collections/glossary/images/popup_shadr.gif" bgcolor="#555555"><img src="/images/transparent.gif" width="1" height="1" alt=""></td></tr><tr><td width="100%" bgcolor="#FFFFFF" colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="1" rowspan="3" class="glossarydarkbg"><img src="/images/transparent.gif" width="1" height="1"></td><td height="20" colspan="3" class="glossarypopuptitle" id="GlossaryPopupTitle">Glossary term</td><td width="1" rowspan="3" class="glossarydarkbg"><img src="/images/transparent.gif" width="1" height="1"></td></tr><tr><td colspan="3" id="GlossaryPopupDefinition">Definition</td></tr><tr><td colspan="3" id="glossarybottomnav">&nbsp;</td></tr><tr><td colspan="5" class="glossarydarkbg" height="1"><img src="/images/transparent.gif" width="1" height="1"></td></tr></table></td></tr><tr><td height="5" colspan="2" valign="top" background="/collections/glossary/images/popup_shadb.gif" bgcolor="#555555"><img src="/collections/glossary/images/popup_shadbl.gif" alt=""></td><td height="5" valign="top" bgcolor="#555555"><img src="/collections/glossary/images/popup_shadbr.gif" alt=""></td></tr></table></td></tr></table></div>');
	}
	HTMLinserted = true;
	normalButtons();
	/*if (state.charAt(0) == 't') {
		
	}*/
}

function normalButtons()
{
	if ((window.ActiveXObject) || (document.implementation && document.implementation.createDocument)) {
		bottomNav = document.getElementById('glossarybottomnav');
	}
	if (bottomNav != null) bottomNav.innerHTML = '<table class="glossarymedbg" width="100%" cellspacing="0" cellpadding="0"><tr><td width="100" valign="bottom" class="glossarypopuplinks"><a id="GlossaryPopupLink" href="definition.jsp" class="glossarypopuplinks" title="Read this definition with examples of related works"><img src="/collections/glossary/images/imagesbutton.gif" alt="Read this definition with examples of related works" width="32" height="30" border="0" align="left">View with images &gt;</a> &nbsp;&nbsp;</td><td width="100" valign="bottom" class="glossarypopuplinks"><a href="/collections/glossary/" class="glossarypopuplinks" title="Explore hundreds of terms in the Tate Glossary"><img src="/collections/glossary/images/fullglossary_button.gif" alt="Explore hundreds of terms in the Tate Glossary" width="30" height="30" border="0" align="left">Full<br>glossary &gt;</a> <a href="#close" onClick="hideMe(); return false;" name="glossary_popup_closelink" id="glossary_popup_closelink"><img src="/images/transparent.gif" alt="Exit glossary definition and return to text" width="1" height="1" border="0"></a><td id="GlossaryPopupBSL" width="100" valign="bottom" class="glossarypopuplinks"><a id="GlossaryPopupBSLLink" href="" !onclick="if ((navigator.appVersion.indexOf(\'Win\')!=-1) && (isIE)) { checkBandwidth(); return false; }" class="glossarypopuplinks" title="Watch BSL signed version"><img src="/collections/glossary/images/bslbutton.gif" alt="Watch BSL signed version" border="0" align="left">BSL signed &gt;</a></td></tr></table>';

/*
	} else {
		if (bottomNav != null) bottomNav.innerHTML = '<table width="100%" cellspacing="0" cellpadding="0"><tr><td width="100" valign="bottom" class="glossarypopuplinks"><a href="definition.jsp" class="glossarypopuplinks" title="Read this definition with examples of related works"><img src="/collections/glossary/images/imagesbutton.gif" alt="Read this definition with examples of related works" width="32" height="30" border="0" align="left">View images &gt;</a> &nbsp;&nbsp;</td><td width="100" valign="bottom" class="glossarypopuplinks"><a href="/collections/glossary/" class="glossarypopuplinks" title="Explore hundreds of terms in the Tate Glossary"><img src="/collections/glossary/images/fullglossarybutton.gif" alt="Explore hundreds of terms in the Tate Glossary" width="30" height="30" border="0" align="left">Full<br>glossary &gt;</a> <a href="#glossary" onClick="hideMe(); return false;" name="glossary_popup_closelink" id="glossary_popup_closelink"><img src="/images/transparent.gif" alt="Exit glossary definition and return to text" width="1" height="1" border="0"></a><td width="100" valign="bottom" class="glossarypopuplinks" id="glossarybottomnav3"><img src="/images/transparent.gif" alt="" border="0" width="90" height="1"></td></tr></table>';
	}
*/
}

function BSLVideoButtons() {
	bottomNav = document.getElementById('glossarybottomnav');
	if (bottomNav != null) {
		bottomNav.innerHTML = '<table width="100%" cellspacing="0" cellpadding="0"><tr><td width="165" valign="bottom" class="glossarymedbg" id="bslvideocontrols">&nbsp;</td><td width="80" valign="bottom" class="glossarypopuplinks" style="padding-right: 7px;" id="bslvideolink1">&nbsp;</td><td width="100" valign="bottom" class="glossarypopuplinks" style="padding-right: 4px;" id="bslvideolink2"><a href="#text" onClick="resetText(); return false;" class="glossarypopuplinks" title="Back to text"><img src="/collections/glossary/images/backtotextbutton.gif" alt="Back to text" border="0" align="left">Back<br>to text &gt;</a></td></tr></table>';
		if (bandwidth != null) {
			bslVideoControls = document.getElementById('bslvideocontrols');
			bslVideoControls.innerHTML = '<OBJECT ID=RVOCX CLASSID="clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" WIDTH="155" HEIGHT="36"><PARAM NAME="CONSOLE" VALUE="MOVIE1"><PARAM NAME="CONTROLS" VALUE="ControlPanel"><PARAM NAME="NOJAVA" VALUE="TRUE"><EMBED CONTROLS="ControlPanel" TYPE="AUDIO/X-PN-REALAUDIO-PLUGIN" WIDTH="155" HEIGHT="36" NOJAVA="TRUE" CONSOLE="MOVIE1"></EMBED></OBJECT>';
			bslVideoLink = document.getElementById('bslvideolink1');
			bslVideoLink.innerHTML = '<a href="bsl-help.htm" class="glossarypopuplinks" title="Get help for BSL video or change your preferences"><img src="/collections/glossary/images/bslhelp_button.gif" alt="Get help for BSL video or change your preferences" border="0" align="left">Options <br>&amp; Help &gt;</a>';
		}
	}
	
}

function checkBandwidth() {
	bandwidth = getCookie('tatebandwidth');
	//alert(bandwidth);
	if (bandwidth == null) {
		decideBandwidth();
	} else {
		playBSL();
	}
}

function decideBandwidth() {
	// getState();
	if (popupType == 'layer') {
		theText = document.getElementById('GlossaryPopupDefinition');
		theText.innerHTML = '<div class="glossarypopupbandwidthchoice"><table cellpadding="0" cellspacing="0" border="0"><tr><td colspan="2" style="padding-bottom: 15px;">To watch the BSL signed video, please choose your connection speed:</td></tr><tr><td><a href="#narrowband" onClick="setBandwidth(\'lb\'); playBSL(); return false;"><img src="/collections/glossary/images/narrowband.jpg" alt="Narrowband: smaller picture, for slower connections" width="147" height="160" border="0"></a></td><td><a href="#broadband" onClick="setBandwidth(\'hb\'); playBSL(); return false;"><img src="/collections/glossary/images/broadband.jpg" alt="Broadband: larger picture, needs faster connection" width="147" height="160" border="0"></a></td></tr><tr><td colspan="2" style="padding-top: 10px;">(Your choice will be saved, you can also change it later)</td></tr></table></div>';
		BSLVideoButtons();
	}
	if (popupType == 'window') {
		window.location = 'popup_decidebandwidth.jsp?entryId=' + definitionId;
	}
}

function setBandwidth(speed) {
	bandwidth = speed;
	setCookie('tatebandwidth', speed, threemonths, '/');
}

function playBSL(){
	//alert('hi');
	// getState();
	if (popupType == 'layer') {
		theText = document.getElementById('GlossaryPopupDefinition');
		theText.innerHTML = '<div class="glossarypopupvideo"><OBJECT ID=RVOCX CLASSID="clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" NAME="glossaryclip" WIDTH="340" HEIGHT="180"><EMBED NAME="glossaryclip" SRC="" TYPE="AUDIO/X-PN-REALAUDIO-PLUGIN" CONTROLS="ImageWindow" WIDTH="320" HEIGHT="180" NOJAVA="TRUE" CONSOLE="MOVIE1" BACKGROUNDCOLOR="#000000" CENTER="TRUE" MAINTAINASPECT="TRUE" NOLOGO="TRUE" AUTOSTART="TRUE"></EMBED><PARAM NAME="SRC" VALUE=""><PARAM NAME="CONSOLE" VALUE="MOVIE1"><PARAM NAME="CONTROLS" VALUE="ImageWindow"><PARAM NAME="BACKGROUNDCOLOR" VALUE="#000000"><PARAM NAME="CENTER" VALUE="TRUE"><PARAM NAME="MAINTAINASPECT" VALUE="TRUE"><PARAM NAME="NOLOGO" VALUE="TRUE"><PARAM NAME="AUTOSTART" VALUE="TRUE"></OBJECT></div>';
		BSLVideoButtons();
		//normalButtons();
	}
	if (popupType == 'window') {
		window.location.href = 'popup_bslvideo.jsp?entryId=' + definitionId;
	}
}

function setState(thePopupType, theMode, thePopupLeft, thePopupTop, theDefinitionId, theCallingPage) {
	popupType = thePopupType;
	mode = theMode;
	popupLeft = thePopupLeft;
	popupTop = thePopupTop;
	definitionId = theDefinitionId;
	callingPage = theCallingPage;
	var theCookieValue = '*POPUPTYPE:' + thePopupType + '*MODE:' + theMode + '*POPUPLEFT:' + thePopupLeft + '*POPUPTOP:' + thePopupTop + '*DEFINITIONID:' + theDefinitionId + '*CALLINGPAGE:' + theCallingPage + '**';
	setCookie('tateglossarypopup', theCookieValue, onehour, '/');
	// getState();
}

function getState() {
	var theCookieValue = getCookie('tateglossarypopup');
	if (theCookieValue != null) {
		popupType = theCookieValue.substring(theCookieValue.indexOf('*POPUPTYPE:') + 11, theCookieValue.indexOf('*MODE:'));
		mode = theCookieValue.substring(theCookieValue.indexOf('*MODE:') + 6, theCookieValue.indexOf('*POPUPLEFT:'));
		popupLeft = theCookieValue.substring(theCookieValue.indexOf('*POPUPLEFT:') + 11, theCookieValue.indexOf('*POPUPTOP:'));
		popupTop = theCookieValue.substring(theCookieValue.indexOf('*POPUPTOP:') + 10, theCookieValue.indexOf('*DEFINITIONID:'));
		definitionId = theCookieValue.substring(theCookieValue.indexOf('*DEFINITIONID:') + 14, theCookieValue.indexOf('*CALLINGPAGE:'));
		callingPage = theCookieValue.substring(theCookieValue.indexOf('*CALLINGPAGE:') + 13, theCookieValue.indexOf('**'));
		//alert(popupType + ', ' + mode + ', ' + popupLeft + ', ' + popupTop + ', ' + definitionId + ', ' + callingPage);
	}
	//alert(theCookieValue);
	
}

/*function checkGlossaryPopup() {
	// getState();
	if ((popupType == 'layer') && (mode == 'text') && (window.location.href == callingPage)) {
		var glossaryPopupLayer = new getObj('glossaryPopupLayer');
		glossaryPopupLayer.style.top = popupTop + 'px';
		glossaryPopupLayer.style.left = popupLeft + 'px';
		glossaryPopupLayer.style.visibility="visible";
		//ddInit();
		//setLayer(window,'glossaryPopupLayer',1,1,popupLeft,popupTop);
		//showMe();
		//alert('hello');
		getDefinition(definitionId);
	}
}*/


// Macromedia image rollover scripts

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

MM_preloadImages('/collections/glossary/images/popup_close_over.gif');