// JavaScript Code for Tate Website
// (c) 2000 Nykris Digital Design Ltd.
// Engineer: Graham Bartram

// Browser ID Utilities

var gAgent = window.navigator.userAgent;
var gAgentVers = parseInt(gAgent.charAt(gAgent.indexOf("/")+1),10);


function is_explorer(){
	return gAgent.indexOf("MSIE") > 0;
}

/*Added By iBase Image Systems
 *Date: 9/11/2001
 *By: Peter Slater and Russell Wicks
 *
 */

//This function sets the default value for the browsers status bar
function statusSetter(){
	window.defaultStatus = "Tate Gallery";
	window.status = "Tate Gallery";
	return true;
}
function unset_selection(ID,CHCKOBJ,COOKIE){
	
	var isChecked = CHCKOBJ.checked;
	var cookieName = "selection";
	
switch (COOKIE){
	case 0:{
		cookieName = "selection";
		break;
	}
	case 1:{
		cookieName = "saved";
		break;
	}

}
		
	if(isChecked){
		
		DeleteCookie(cookieName,null,null,ID);		
	}
	else{
		SetCookie(cookieName,ID,null,null,null);
	}
}	
//function to detect selection or deselection of a particular work
function set_selection(ID,CHCKOBJ){
	
	var isChecked = CHCKOBJ.checked;
		
	if(isChecked){
		SetCookie("selection",ID,null,null,null);		
	}
	else{
		DeleteCookie("selection",null,null,ID);		
	}	
	
}


function FixCookieDate (date) {
     var base = new Date(0);
     var skew = base.getTime(); 
     if (skew > 0) 
         date.setTime (date.getTime() - skew);
}


function SetCookie(name, value, path, domain, secure) {
	 
	 
	 var newvalue = (GetCookie(name) != null)?GetCookie(name)+value+"-":value+"-";
	 document.cookie = name + "=" + newvalue +
         ((path) ? "; path=" + path : "") +
         ((domain) ? "; domain=" + domain : "") +
         ((secure) ? "; secure" : "");
	

}

function getCookieVal(offset) {
     var endstr = document.cookie.indexOf (";", offset);
     if (endstr == -1)
         endstr = document.cookie.length;
     return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name) {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen) {
     var j = i + alen;
     if (document.cookie.substring(i, j) == arg)
       return getCookieVal (j);
     i = document.cookie.indexOf(" ", i) + 1;
     if (i == 0) break; 
   }
   return null;
}

function CheckCookie(id){
 var ids = GetCookie("selection");
 
 if(ids.indexOf(id)>-1){
  return true;
 }
 else{
  return false;
 }
}

function DeleteCookie(name,path,domain,ID) {
   
   var ids = removeSelection(GetCookie(name),ID);	
   if (GetCookie(name)) {
    document.cookie = name + "=" + ids +
       ((path) ? "; path=" + path : "") +
       ((domain) ? "; domain=" + domain : "") +
       "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }
}	

function removeSelection(ids,id){
 var sId = new String(id);
 var startIndex = ids.indexOf(id);
 var endIndex = startIndex+sId.length;
 var newids;

 switch(startIndex){
  // The id being looked for is at the start of the string.
  case 0:{
   newids = ids.substring(endIndex+1, ids.length);
   break;
  }
  // The id being looked for is somewhere in the middle.
  default:{
   newids = ids.substring(0, startIndex) + ids.substring(endIndex+1, ids.length);  
   break;
  }
 } 

 return newids;
}
//End of iBase Image Systems code block.


// Flash and Image Utilities

// Check whether Flash is installed on the user's computer and create a cookie with the
// result. The cookie has four possible values which tell other routines what to do when
// asked to insert a Flash movie:
// "img" - write out <IMG> tag instead
// "swf" - write out <EMBED> tag and use a Flash movie
// "mov" - write out <EMBED> tag and use a QuickTime movie (QT4 onwards plays Flash)
// "none" - do nothing, <NOSCRIPT> will handle the image in Netscape 2

var gImageHandling = "";

function check_flash()
{
	var nextYear = new Date();
	
	gImageHandling = "none";
		
	if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] && navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin)
		{
		// Navigator and Explorer ver 5 onwards
		if (navigator.plugins && navigator.plugins["Shockwave Flash"])
			{
			var desc = navigator.plugins["Shockwave Flash"].description;
			var vers = parseInt(desc.charAt(16));
			if (vers >= 3) gImageHandling = "swf";
			}
		}
			
	if (gImageHandling == "none" && navigator.mimeTypes && navigator.mimeTypes["video/quicktime"] && navigator.mimeTypes["video/quicktime"].enabledPlugin)
		{
		// Navigator and Explorer ver 5 onwards
		var instPlugins = navigator.plugins
		for (var i = 0; i < instPlugins.length; i++)
			{
   			if (instPlugins[i].name.indexOf("QuickTime")>=0)
   				{
				var desc = navigator.plugins[i].name;
				var vers = parseInt(desc.charAt(18));
				if (vers >= 2) gImageHandling = "mov";
				i = instPlugins.length;
	   			}
			}
		}
			
	if (gImageHandling == "none" && navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
	   && (navigator.userAgent.indexOf("Windows 95")>=0 || navigator.userAgent.indexOf("Windows NT")>=0
	   || navigator.userAgent.indexOf("Windows 98")>=0 || navigator.userAgent.indexOf("Windows 2")>=0 || navigator.userAgent.indexOf("Win32")>=0))
	   	{
	   	// Explorer on a 32-bit PC
		document.write('<script language=VBScript\> \n');
		document.write('gImageHandling = "gif"\n');
		document.write('on error resume next \n');
		document.write('if (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) then\n');
		document.write('  gImageHandling = "swf"\n');
		document.write('end if\n');
		document.write('</scr' + 'ipt\> \n');
		}
		
	if (gImageHandling == "none" && !(navigator.appName && navigator.appName.indexOf("Netscape")>=0 && navigator.appVersion.indexOf("2.")>=0))
		{
		// Anything else, other than Navigator ver 2 which will use <NOSCRIPT>
		gImageHandling = "gif";
		}
	
	// Write out cookie
	nextYear.setFullYear(nextYear.getFullYear() + 1);
	document.cookie = "flashHandling=" +  gImageHandling + "; expires=" + nextYear.toGMTString() + "; domain=nykris.com; path=/";

//	document.write('<p>gImageHandling (cookie)=' + gImageHandling + '</p>');
}



function check_flash_nocookie()
{
	gImageHandling = "none";
		
	if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] && navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin)
		{
		// Navigator and Explorer ver 5 onwards
		if (navigator.plugins && navigator.plugins["Shockwave Flash"])
			{
			var desc = navigator.plugins["Shockwave Flash"].description;
			var vers = parseInt(desc.charAt(16));
			if (vers >= 3) gImageHandling = "swf";
			}
		}
			
	if (gImageHandling == "none" && navigator.mimeTypes && navigator.mimeTypes["video/quicktime"] && navigator.mimeTypes["video/quicktime"].enabledPlugin)
		{
		// Navigator and Explorer ver 5 onwards
		var instPlugins = navigator.plugins
		for (var i = 0; i < instPlugins.length; i++)
			{
   			if (instPlugins[i].name.indexOf("QuickTime")>=0)
   				{
				var desc = navigator.plugins[i].name;
				var vers = parseInt(desc.charAt(18));
				if (vers >= 2) gImageHandling = "mov";
				i = instPlugins.length;
	   			}
			}
		}
			
	if (gImageHandling == "none" && navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
	   && (navigator.userAgent.indexOf("Windows 95")>=0 || navigator.userAgent.indexOf("Windows NT")>=0
	   || navigator.userAgent.indexOf("Windows 98")>=0 || navigator.userAgent.indexOf("Windows 2")>=0 || navigator.userAgent.indexOf("Win32")>=0))
	   	{
	   	// Explorer on a 32-bit PC
		document.write('<script language=VBScript\> \n');
		document.write('gImageHandling = "gif"\n');
		document.write('on error resume next \n');
		document.write('if (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) then\n');
		document.write('  gImageHandling = "swf"\n');
		document.write('end if\n');
		document.write('</scr' + 'ipt\> \n');
		}
		
	if (gImageHandling == "none" && !(navigator.appName && navigator.appName.indexOf("Netscape")>=0 && navigator.appVersion.indexOf("2.")>=0))
		{
		// Anything else, other than Navigator ver 2 which will use <NOSCRIPT>
		gImageHandling = "gif";
		}
			
//	document.write('<p>gImageHandling (no cookie)=' + gImageHandling + '</p>');
}



// Insert a Flash movie or fallback GIF into the current page depending upon the setting
// of the imageHandling cookie.

function insert_flash(name, width, height, bgCol)
{
	var imageHandling = document.cookie;
	
	if (imageHandling.indexOf("flashHandling") == -1)
		{
		// No cookie exists - ie cookies are off, so do manual check
		check_flash_nocookie();
		imageHandling = gImageHandling
		}
		
	if (imageHandling.indexOf("none") != -1)
		{
		}
	else if (imageHandling.indexOf("swf") != -1)
		{
		document.write('<embed src="' + name + '.swf"');
		document.write(' swLiveConnect=false width=' + width + ' height=' + height);
		document.write(' loop=false quality=high scale=noborder bgcolor=' + bgCol);
		document.write(' type="application/x-shockwave-flash"\>');
		document.write('</embed\>');
		document.write('<noembed\><img src="' + name + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\></noembed\>');
		}
	else if (imageHandling.indexOf("mov") != -1)
		{
		document.write('<embed src="' + name + '.mov"');
		document.write(' autoplay="true" controller="false" width="' + width + '" height="' + height + '"');
		document.write(' loop=false bgcolor=' + bgCol);
		document.write(' type="video/quicktime"\>');
		document.write('</embed\>');
		document.write('<noembed\><img src="' + name + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\></noembed\>');
		}
	else
		{
		document.write('<img src="' + name + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\>');
		} 
	
}



// Insert a random Flash movie or random fallback GIF into the current page depending upon
// the setting of the imageHandling cookie.

function insert_random_flash(imgPath,width,height,bgCol,numImages)
{
	var imageHandling = document.cookie;
	
	if (imageHandling.indexOf("flashHandling") == -1)
		{
		// No cookie exists - ie cookies are off, so do manual check
		check_flash_nocookie();
		imageHandling = gImageHandling
		}
		
	imgNum = (Math.floor((new Date()).getTime()/10)%numImages) + 1;
	imgNumStr = (new Number(imgNum)).toString();
	if (imgNum < 10) imgNumStr = '0' + imgNumStr;

	if (imageHandling.indexOf("none") != -1)
		{
		}
	else if (imageHandling.indexOf("swf") != -1)
		{
		document.write('<embed src="' + imgPath + imgNumStr + '.swf"');
		document.write(' swLiveConnect=false width=' + width + ' height=' + height);
		document.write(' loop=false quality=high scale=noborder bgcolor=' + bgCol);
		document.write(' type="application/x-shockwave-flash"\>');
		document.write('</embed\>');
		document.write('<noembed\><img src="' + imgPath + imgNumStr + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\></noembed\>');
		}
	else if (imageHandling.indexOf("mov") != -1)
		{
		document.write('<embed src="' + imgPath + imgNumStr + '.mov"');
		document.write(' autoplay="true" controller="false" width="' + width + '" height="' + height + '"');
		document.write(' loop=false bgcolor=' + bgCol);
		document.write(' type="video/quicktime"\>');
		document.write('</embed\>');
		document.write('<noembed\><img src="' + imgPath + imgNumStr + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\></noembed\>');
		}
	else
		{
		document.write('<img src="' + imgPath + imgNumStr + '.gif" width=' + width + ' height=' + height + ' border=0 usemap="#' + name + '"\>');
		} 
	
}




// Insert a random GIF image into the current page depending upon the setting of the
// imageHandling cookie.

function insert_random_image(imgType,imgPath,imgWidth,imgHeight,imgHspace,imgVspace,numImages,border,alt)
{
	var imgNum,imgNumStr="";
	var imageHandling = document.cookie;
	
	if (imageHandling.indexOf("flashHandling") == -1)
		{
		// No cookie exists - ie cookies are off, so do manual check
		check_flash_nocookie();
		imageHandling = gImageHandling
		}
		
	if (imageHandling.indexOf("none") == -1)
		{
		imgNum = (Math.floor((new Date()).getTime()/10)%numImages) + 1;
		imgNumStr = (new Number(imgNum)).toString();
		if (imgNum < 10) imgNumStr = '0' + imgNumStr;
		document.write('<img' + ' border="0"'  + ' src="' + imgPath + imgNumStr + '.' + imgType + '" width=' + imgWidth + ' height=' + imgHeight + ' vspace=' + imgVspace + ' hspace=' + imgHspace + (alt ? ' alt="' + alt + '"' : '') + '\>');
		}

}


// Rollover Utilities


var gRollovers = new Object();
var gRolloversPath = "";

	
	
function find_element(name,layer)
{	
	if (gAgentVers < 4) return document[name];
	var currentLayer = layer ? layer.document : document;
	var element = currentLayer[name];
	if (!element)
		{
		for (var i=0;i<currentLayer.layers.length;i++)
			{
			element = find_element(name,currentLayer.layers[i]);
			if (element) return element;
			}
		}
	return element;
}



function set_rollovers_path(path) {
	gRolloversPath = path;
}

function load_rollover(button, source) {
	if (document.images && button && source) {
		var sourceOff, sourceOn;

		if (source.indexOf('_norm') >= 0) {
			sourceOff = source;
			sourceOn = source.replace(/_norm/, '_over');
		}
		else if (source.indexOf('_off') >= 0) {
			sourceOff = source;
			sourceOn = source.replace(/_off/, '_on');
		}

		if (sourceOff) {
			gRollovers[button] = new Object();
			gRollovers[button][0] = new Image();
			gRollovers[button][1] = new Image();

			gRollovers[button][0].src = sourceOff;
			gRollovers[button][1].src = sourceOn;
		}
	}
}


function load_all_rollovers()
{
	if (document.images) {
		var numImages = document.images.length;
		for (var i=0;i<numImages;i++) {
			if (document.images[i].id){
				load_rollover(document.images[i].id, document.images[i].src);
			}else if (document.images[i].name){
				load_rollover(document.images[i].name, document.images[i].src);
			}
		}
	}
}



function rollover(button,state) {
	if (document.images) {
		if (gRollovers[button]) {
			var img = (!is_explorer()) ? find_element(button,0) : document.images[button];
			if (img) img.src = gRollovers[button][state].src;
			return true;
		}
	}
	return false;
}


function rollover2(button1,state1,button2,state2) {
		if (document.images) {
		if (gRollovers[button1]) {
			var img = (!is_explorer()) ? find_element(button1,0) : document.images[button1];
			if (img) img.src = gRollovers[button1][state1].src;
			var img2 = (!is_explorer()) ? find_element(button2,0) : document.images[button2];
			if (img2) img2.src = gRollovers[button2][state2].src;
			return true;
		}
	}
	return false;
}


// Insert a random Flash movie or random fallback GIF and a random GIF into the current page
// depending upon the setting of the imageHandling cookie.

function insert_title_flash()
{
	var imageHandling = document.cookie;
	
	if (imageHandling.indexOf("flashHandling") == -1)
		{
		// No cookie exists - so try and create one
		check_flash();
		imageHandling = gImageHandling
		}
		
	imgNum = (Math.floor((new Date()).getTime()/10)%16);
	imgNum1 = imgNum%4;
	imgNum2 = ((imgNum - imgNum1)/4) + 1;
	imgNum1 = imgNum1 + 1;
	imgNumStr1 = (new Number(imgNum1)).toString();
	imgNumStr1 = '00' + imgNumStr1;
	imgNumStr2 = (new Number(imgNum2)).toString();

	if (imgNum1 == 1)
		{
		bgCol = '#ff6600';
		}
	else if (imgNum1 == 2)
		{
		bgCol = '#cc0066';
		}
	else if (imgNum1 == 3)
		{
		bgCol = '#9966cc';
		}
	else if (imgNum1 == 4)
		{
		bgCol = '#6699cc';
		}
		
	document.bgColor = bgCol;

	if (imageHandling.indexOf("none") != -1)
		{
		}
	else if (imageHandling.indexOf("swf") != -1)
		{
		document.write('<TD WIDTH=384 HEIGHT=70\>');
		document.write('<A HREF="home/default.htm">');
		document.write('<embed src="front/' + imgNumStr1 + '_front.swf"');
		document.write(' swLiveConnect=false width=384 height=70');
		document.write(' loop=false quality=high scale=noborder bgcolor=' + bgCol);
		document.write(' type="application/x-shockwave-flash"\>');
		document.write('</embed\>');
		document.write('<noembed\><img src="front/' + imgNumStr1 + '_front.gif" width=384 height=70 border=0\></noembed\>');
		document.write('</A></TD\><TD WIDTH=155 HEIGHT=70\>');
		document.write('<A HREF="home/default.htm">');
		document.write('<img src="front/images/' + imgNumStr1 + '_' + imgNumStr2 + '.gif" alt="enter the Tate website" width=155 height=70 border=0\>');
		document.write('</A></TD\>');
		}
	else if (imageHandling.indexOf("mov") != -1)
		{
		document.write('<TD WIDTH=384 HEIGHT=70\>');
		document.write('<A HREF="home/default.htm">');
		document.write('<embed src="front/' + imgNumStr1 + '_front.mov"');
		document.write(' autoplay="true" controller="false" width=384 height=70');
		document.write(' loop=false bgcolor=' + bgCol);
		document.write(' type="video/quicktime"\>');
		document.write('</embed\>');
		document.write('<noembed\><img src="front/' + imgNumStr1 + '_front.gif" alt="enter the Tate website" width=384 height=70 border=0\></noembed\>');
		document.write('</A></TD\><TD WIDTH=155 HEIGHT=70\>');
		document.write('<A HREF="home/default.htm">');
		document.write('<img src="front/images/' + imgNumStr1 + '_' + imgNumStr2 + '.gif" width=155 height=70 border=0\>');
		document.write('</A></TD\>');
		}
	else
		{
		document.write('<TD WIDTH=384 HEIGHT=70\>');
		document.write('<A HREF="home/default.htm">');
		document.write('<img src="front/' + imgNumStr1 + '_front.gif" width=384 height=70 border=0\>');
		document.write('</A></TD\><TD WIDTH=155 HEIGHT=70\>');
		document.write('<A HREF="home/default.htm">');
		document.write('<img src="front/images/' + imgNumStr1 + '_' + imgNumStr2 + '.gif"  alt="enter the Tate website" width=155 height=70 border=0\>');
		document.write('</A></TD\>');
		} 
	
}