var responseText;
var globNo;
var globtypeId;
var globtypeName;
var globSize;
var globFrame;
var numberofButtons = 6;
var globDataType;
var globDataSize;
var globDataImageNo;
var globDataFrame;
var globPrevDataFrame;
var globPrevButton;
var globFrameRef;
var globCanvasType;

function GetXmlHttpObject()
{
	var xmlHttp=null;

	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlHttp;
}

function changeImage(no,data,typeName,typeId,size,frame) 
{
	globtypeName = typeName;
	globtypeId = typeId;
	globNo = no;
	globSize = size;
	globFrame = frame;
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	xmlHttp.onreadystatechange=function ()
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			if(data=="image")
			{
				responseText=xmlHttp.responseText;
				
				if(globPrevButton)
					fadeButton(globPrevButton);
					
				var button=document.getElementById(typeId);
				globPrevButton=button;
				highlightButton(button);
				
				changeImage(globNo,"info",globtypeName,globtypeId,globSize,globFrame);
				document.getElementById('imageContainer').innerHTML =responseText;
				//document.getElementById('titleContainer').innerHTML = globtypeName;
		
				//Uncomment this and comment the above 3 lines if a delay is necessary to load the images.
				//setTimeout(introduceDelay, 300);
		
			}
			else
			{
				if(xmlHttp.responseText!="NA")
				{
					document.getElementById('infoContainer').innerHTML =xmlHttp.responseText;
					if(globFrame!="0")
						getData('info',globtypeName,globSize,globFrame,globNo,globFrameRef,globCanvasType);
				}
				else
				{
					window.location="/index.php";
				}
			}
		}
	}
	
	xmlHttp.open("GET","image_info.php?data="+data+"&type="+typeName+"&type_id="+typeId+"&size="+size+"&frame="+frame+"&image_no="+no+"&sid="+Math.random(),true);

	xmlHttp.send(null);	
}

function introduceDelay()
{
	changeImage(globNo,"info",globtypeName,globtypeId,globSize,globFrame);
	document.getElementById('imageContainer').innerHTML =responseText;
	//document.getElementById('titleContainer').innerHTML = globtypeName;
}

function checkOut(form)
{
	form.submit();
	
}

function highlightButton(button)
{
	if(navigator.appName=="Microsoft Internet Explorer")
		button.filters.alpha.opacity=100;
	else
		button.style.opacity=1;	
}

function fadeButton(button)
{	
	if(button.id!=globtypeId)
	{	
		if(navigator.appName=="Microsoft Internet Explorer")
			button.filters.alpha.opacity=60;
		else
			button.style.opacity=0.6;			
	}
}

function highlightImage(image)
{
	globPrevDataFrame=image;
	if(navigator.appName=="Microsoft Internet Explorer")
		image.filters.alpha.opacity=100;
	else
		image.style.opacity=1;
}

function fadeImage(image)
{	
	if(image.id!=globDataFrame)
	{
		if(navigator.appName=="Microsoft Internet Explorer")
			image.filters.alpha.opacity=20;
		else
			image.style.opacity=0.2
	}
}

function getData(data,type,size,frame,imageNo,frameRef)
{
	globDataType = type;
	globDataSize = size;
	globDataImageNo = imageNo;
	globDataFrame = frame;
	globFrameRef = frameRef;
	
	if(frame!=0)
	{
		if(navigator.appName=="Microsoft Internet Explorer")
			document.getElementById(frame).filters.alpha.opacity=100;
		else
			document.getElementById(frame).style.opacity=1;
	}
		
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	xmlHttp.onreadystatechange=function ()
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			if(data=="image")
			{
				document.getElementById('imageContainer').innerHTML =xmlHttp.responseText;
			}
			else
			{
				if(frame!="0")
				{
					var frame_obj=document.getElementById(frame);
					highlightImage(frame_obj);
				}
				
				document.getElementById('infoContainer').innerHTML =xmlHttp.responseText;
				getData("image",globDataType,globDataSize,globDataFrame,globDataImageNo,globFrameRef)
			}
		}
	}
	
	xmlHttp.open("GET","image_info.php?data="+data+"&type="+type+"&size="+size+"&frame="+escape(frame)+"&image_no="+imageNo+"&frame_ref="+frameRef+"&sid="+Math.random(),true);

	xmlHttp.send(null);	
}
