//////////////begin////////////////////////////////
//functions governing appearance and screen effects
///////////////////////////////////////////////////
var pageName;
var restoreData;
var isIE=window.ActiveXObject;

var arrData=new Array();
//var arrMenu=new Array('&nbsp;resume&nbsp;','&nbsp;biography&nbsp;','&nbsp;theatre&nbsp;','&nbsp;paintings&nbsp;','&nbsp;posters&nbsp;','&nbsp;costumes&nbsp;','&nbsp;contact&nbsp;','&nbsp;home&nbsp;');
var arrMenu=new Array('&nbsp;home&nbsp;','&nbsp;theatre&nbsp;','&nbsp;posters&nbsp;','&nbsp;costumes&nbsp;','&nbsp;painting&nbsp;','&nbsp;extra&nbsp;','&nbsp;contact&nbsp;','&nbsp;biography&nbsp;','&nbsp;resume&nbsp;');

function doActivate(img)
{
	img.className='thumbActive';
	//debug();
	//alert(img.idx);
	var idx;
	if(img.idx){idx=img.idx;}//IE
	else if(img.attributes['idx']){idx=img.attributes['idx'].value;}//FF
	doDisplay(idx);
}

function doDesActivate(img)
{
	img.className='thumb';restoreDisplay();
}


function doDisplay(idx)
{
	//debug();
	var folderName=pageName.split('.')[0];
	var content;
	var extra='';
	restoreData=document.getElementById('leftDisplay').innerHTML;

	if (restoreData.indexOf(arrData[idx][0])>0){return;}//to avoid blink
	
	if(arrData[idx][3])
	{extra='cursor:pointer;" url="'+arrData[idx][3]+'" onclick="dscCell_onclick(this);';}

	content='<TABLE cellspacing=0 cellpadding=0 border=0><TR>\n';
	content+='<TD class="description" valign=top style="width:50%;">';
	//if(document.location.href.indexOf('painting.htm')>0) content+='Price-list is available by request.<br><br>All paintings are Oil on Canvas.';
	content+='&nbsp;</TD>\n'; 
	content+='<TD align=right><IMG SRC="'+folderName+'/'+arrData[idx][0]+'"></TD>\n';
	content+='<TD id="dscCell" align=left class="description" valign=top style="width:50%;color:#000000;FONT-SIZE:12px;'+extra+'">'+arrData[idx][2]+'</TD></TR></TABLE>\n';

	

	document.getElementById('leftDisplay').innerHTML=content;
/**/
	if(arrData[idx][3])
	{
	var cell=document.getElementById('dscCell');
	cell.style.cursor='pointer';
	if(cell.style.cursor==''){cell.style.cursor='pointer';}//FF
	cell.url=arrData[idx][3];
	//alert(cell.url);
	}
}
function dscCell_onclick(cell)
{
//alert();
var url=cell.url;
if(!url)
	{url=cell.attributes['url'].nodeValue;}//url.nodeValue
document.location=url;
}

function doStick(img)
{
restoreData=document.getElementById('leftDisplay').innerHTML;
}

function restoreDisplay()
{
	if(document.getElementById('leftDisplay').innerHTML!=restoreData)//to avoid blink
	{document.getElementById('leftDisplay').innerHTML=restoreData;}
}

function body_onload()
{
if(!isIE){document.getElementById('mainTable').rows[2].cells[0].style.height='80px';}
getXML();
drawMenu();
}

function drawMenu()
{
	var tr=document.getElementById('topMenu').rows[0];

	for(var i=0;i<arrMenu.length;i++)
	{
	td=tr.insertCell(i);//cell insertred
	td.innerHTML=arrMenu[i];
	td.className='tdMenu';
	td.onmouseover=function(){menuMouseOver(this);}
	td.onmouseout=function(){menuMouseOut(this);}
	td.onclick=function(){menuClick(this)}

	if(pageName=='home.htm'&&i==0)//don't display home on home page
		{td.style.display='none';}
	}
}
function menuMouseOver(td)
{
td.className='tdMenuActive';
if(!isIE){td.style.cursor='pointer';}
}
function menuMouseOut(td)
{
td.className='tdMenu';
}
function menuClick(td)
{
var url=td.innerHTML.split('&nbsp;')[1]+'.htm';
document.location=url;
}

function getXML()
{
		pageName=this.location.href.split('/');//getting file name
		pageName=pageName[pageName.length - 1]
		var xmlName=pageName.split('.')[0] + '.xml';//alert(xmlName);

		makeRequest(xmlName, 'GET', 'getXML');
}

function buildGallery()
{
	var folderName=pageName.split('.')[0];
	var tb=document.getElementById('listTable');
	if(!tb)return;
	var tr0=tb.rows[0];
	//var tr1=tb.rows[1];
	var td;

	//var totalWidth=0;
	var smallArr;var small;

	for(var i=0;i<arrData.length;i++)
	{
		//inserting '_small' into pic name
		smallArr=arrData[i][0].split('.');
		small=smallArr[0]+'_small.'+smallArr[1];

		td=tr0.insertCell(i);//cell insertred
		td.vAlign='top';tb.align='left';//aligned
		td.style.width='0%';
		td.style.position='relative';
		td.innerHTML='<IMG SRC="'+folderName+'/'+small+'" ALT="'+arrData[i][1]+'" idx="'+i
		+'" onMouseOver="doActivate(this);" onMouseOut="doDesActivate(this);" onClick="doStick(this);" class="thumb">';//image added
	}
	if(tb.clientWidth<document.getElementById('mainTable').clientWidth&&isIE)
	{//centering thumbnails
		var Width='50%';
		if(!isIE)
			{
				Width=((document.getElementById('mainTable').clientWidth-tb.clientWidth)/2);
				Width+='px';
			}
		td=tr0.insertCell(tr0.cells.length);//right spacer cell
		td.innerHTML='&nbsp;';
		td.style.position='relative';
		td.style.width=Width;
		td.width.value=Width;

		td=tr0.insertCell(0);//left spacer cell
		td.innerHTML='&nbsp;';
		td.style.position='relative';
		td.style.width=Width;
		td.width.value=Width;
	}
}

var tr_intro;
function doHomePage()
{
	tr_intro=document.getElementById('introTable').rows[0];
	for(i=0;i<tr_intro.cells.length-1;i++)
	{
		setTimeout('tr_intro.cells['+i+'].style.display="inline"',i*100);
	}
	//setTimeout('document.getElementById("keyTable").style.display="block";',5000);

	setTimeout('document.location="home.htm"',8000);

	setTimeout('displayKey();',1500);
}
function displayKey()
{
var td=tr_intro.cells[tr_intro.cells.length-1];
if(isIE)//MS IE
	{td.style.display="inline";}
else//FF
	{
	td.style.top=18;
	td.style.left=520;
	td.style.cursor='pointer';
	td.style.display="inline";
	}
}

function linkMouseOver(thing)
{
//debug();
thing.style.textDecoration='underline';
if(!isIE)thing.style.cursor='pointer';
return;
}
function linkMouseOut(thing)
{
thing.style.textDecoration='';
if(!isIE)thing.style.cursor='default';
return;
}
function swapThis(idx)
{
tb=document.getElementById('extraTable');
for(i=0;i<tb.rows.length;i++)
	{
	tb.rows[i].cells[tb.rows[i].cells.length-1].style.display='none';
	document.getElementById('extra_'+i).style.color='#000000';
	}
tb.rows[idx].cells[tb.rows[idx].cells.length-1].style.display='block';
document.getElementById('extra_'+idx).style.color='#0000FF';
}
//////////////end//////////////////////////////////
//functions governing appearance and screen effects
///////////////////////////////////////////////////

//////////////begin////////////////////////////////
//AJAX and data retrieval
///////////////////////////////////////////////////
var asynch=true;

var flag=false;

function makeRequest(url,Action,extra) 
{
	//extra - information related to caller that may be needed in processing response
	if(!extra)extra=false;

	var Method='POST';
	if(Action=='GET'){Method='GET';}

	var request;
	if(window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
		if(request.overrideMimeType)request.overrideMimeType('text/xml');
	}
	else if(window.ActiveXObject)
	{
		request = new ActiveXObject("MSXML2.XMLHTTP");
	}
	else{alert("Your browser is too obsolete to use this application.\n Please upgrade and come back.");return;}
	sendRequest(url,request,Action,Method,extra);
}

function sendRequest(url,request,Action,Method,extra)
{
	var arg=null;
	request.onreadystatechange = function(){onResponse(request,extra);};
	request.open(Method, url, asynch);
	if(Method=='POST')
	{
		request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		if(Action=='getDataFromService')
		{
			var arr=url.split('?');
			url=arr[0];
			arg=arr[1];
		}
		else
		{//default - grab form values			
			arg=getFormValues(document.forms[0]);		
		}
	}
	request.send(arg); 
}

function checkReadyState(obj)
{
	if(obj.readyState == 0){window.status = "Sending Request...";}
	if(obj.readyState == 1){window.status = "Loading Response...";}
	if(obj.readyState == 2){window.status = "Response Loaded...";}
	if(obj.readyState == 3){window.status = "Response Ready...";}
	if(obj.readyState == 4)
	{
		if(obj.status == 200){return true;}		
		else if(obj.status == 404){window.status = "File not found";}
		else if(obj.status == 403){window.status = "Forbidden - security violation";}
		else if(obj.status == 500){window.status = "Internal server error";}
		else if(obj.status == 12029){window.status = "A connection with the server could not be established";}
		else
			{
			//alert(obj.status);
			window.status = "There was a problem retrieving the XML.";
			}
	}
	return false;
}

function onResponse(request,extra) 
{//alert(checkReadyState(request));

	if(checkReadyState(request))
	{
//debug();

		var xmlNodes;
		var file_name;
		var short_dsc;
		var dsc;
		var url;

		if(window.ActiveXObject)//MS IE
		{
			xmlNodes=request.responseXML.documentElement.childNodes;
			for(var i=0;i<xmlNodes.length;i++)
			{
				var node=xmlNodes[i];
				file_name='';//clean old value
				file_name=node.childNodes[0].text;
				short_dsc='';
				if(node.childNodes[1])short_dsc=node.childNodes[1].text;
				dsc='';
				if(node.childNodes[2])
				{
					dsc=node.childNodes[2].text;
					//add encoded tags
					while (dsc.indexOf('~|')>=0)//compiler bug - replaces only first!!!
					{dsc=dsc.replace('~|','<');}
					while (dsc.indexOf('|~')>=0)
					{dsc=dsc.replace('|~','>');}
				}
				url='';
				if(node.childNodes[3])url=node.childNodes[3].text;

				if(file_name!='')
				{
				arrData[arrData.length]=new Array(file_name,short_dsc,dsc,url);
				}
			}
		}
		else//FireFox/Mozilla
		{
			xmlNodes=request.responseXML.getElementsByTagName('image_description');
			for(var i=0;i<xmlNodes.length;i++)
			{
				file_name='';//clean old value
				file_name=xmlNodes[i].getElementsByTagName('image_name')[0].textContent;
				dsc='';
				if(xmlNodes[i].getElementsByTagName('description')&&xmlNodes[i].getElementsByTagName('description')[0])
				{dsc=xmlNodes[i].getElementsByTagName('description')[0].textContent;}
					//add encoded tags
					while (dsc.indexOf('~|')>=0)//compiler bug - replaces only first!!!
					{dsc=dsc.replace('~|','<');}
					while (dsc.indexOf('|~')>=0)
					{dsc=dsc.replace('|~','>');}
				short_dsc='';
				if(xmlNodes[i].getElementsByTagName('short_description')&&xmlNodes[i].getElementsByTagName('short_description')[0])
				{short_dsc=xmlNodes[i].getElementsByTagName('short_description')[0].textContent;}
				url='';
				if(xmlNodes[i].getElementsByTagName('url')&&xmlNodes[i].getElementsByTagName('url')[0])
				{url=xmlNodes[i].getElementsByTagName('url')[0].textContent;}

				if(file_name!='')
				{
				arrData[arrData.length]=new Array(file_name,short_dsc,dsc,url);
				}

			}		
		}
		
		flag=false;//restore value
		asynch=true;//restore value

		window.status ='';

		buildGallery();//call to page building function
	}
}

function getFormValues(fobj,valFunc)
{
   var str = "";
   var valueArr = null;
   var val = "";
   var cmd = "";
   for(var i = 0;i < fobj.elements.length;i++)
   {
	   switch(fobj.elements[i].type)
	   {
		   case "text":
				if(valFunc)
				{
					//use single quotes for argument so that the value of
					//fobj.elements[i].value is treated as a string not a literal
					cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
					val = eval(cmd)
				}
				str += fobj.elements[i].name +"=" + escape(fobj.elements[i].value) + "&";
				 break;
		   case "select-one":
				str += fobj.elements[i].name +
				"=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
				break;
		   case "hidden":
				str += fobj.elements[i].name +"=" + escape(fobj.elements[i].value) + "&";
				 break;
		}
   }
   str = str.substr(0,(str.length - 1));
   return str;
}
//////////////end//////////////////////////////////
//AJAX and data retrieval
///////////////////////////////////////////////////

function test()
{alert(document.body.innerHTML);}

