var xmlHttp; //Entire Image Biref
var xmlHttp2;  //Image Title
var xmlHttp3;  //Image Subtitle
var title = document.title;
var html_meta_keywords;
var html_meta_description;


//Vars for share popup, email this page
var emailThis = "http://www.slv.vic.gov.au/script/emailThis.php";
var emailThisUrl = window.location.href;

var copyUrl;
//update URL
if(getServerBaseUrl()=="http://www.slv.vic.gov.au" || getServerBaseUrl()=="http://slv.vic.gov.au"){
	copyUrl = getServerBaseUrl() + "/gallery/copy_url.php";
}
else{
	copyUrl = getServerBaseUrl() + "/copy_url.php";
}



function showImage(id)
{ 
	xmlHttp=GetXmlHttpObject();
	xmlHttp2=GetXmlHttpObject();
	xmlHttp3=GetXmlHttpObject();
	xmlHttp4=GetXmlHttpObject();
	xmlHttp5=GetXmlHttpObject();
	
	//browserTitle = " - The Anzac Spirit Slideshow, State Library of Victoria";

	html_meta_keywords = document.getElementsByName('keywords')[0].content;
	html_meta_description = document.getElementsByName('description')[0].content;
	
	//update URL
	if(getServerBaseUrl()=="http://www.slv.vic.gov.au" || getServerBaseUrl()=="http://slv.vic.gov.au"){
		emailThisUrl = getServerBaseUrl() + "/gallery/image.php?id=" + id;
		
		//live
		var url="/gallery/ajax/getImage.php";
	}
	else{
		emailThisUrl = getServerBaseUrl() + "/image.php?id=" + id;
		
		//dev
		var url="/ajax/getImage.php";
	}
	

	
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	
	//document.getElementById("ajax_load_icon").style.display = "block";
	

	url=url+"?id="+id;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=contentChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	url2=url+"&action=title";
	xmlHttp2.onreadystatechange=changeBrowserTitle;
	xmlHttp2.open("GET",url2,true);
	xmlHttp2.send(null);
	
	url3=url+"&action=metadescription";
	xmlHttp3.onreadystatechange=changeMetaDescription;
	xmlHttp3.open("GET",url3,true);
	xmlHttp3.send(null);
	
	obj = document.getElementById('img' + id);
	changeClassName(obj,"gallery_img_thumb_current");
	//document.getElementById("img" + id).scrollIntoView();
	
	url4=url+"&action=breadcrumb";
	xmlHttp4.onreadystatechange=changeBreadcrumb;
	xmlHttp4.open("GET",url4,true);
	xmlHttp4.send(null);
	
	url5=url+"&action=emailthispage";
	xmlHttp5.onreadystatechange=changeEmailThisPageLink;
	xmlHttp5.open("GET",url5,true);
	xmlHttp5.send(null);
	
}

function contentChanged() 
{
	if(xmlHttp.readyState == 3) // Loading Request (Server -> Client)
	{
		//*** Loading ***//
		//document.getElementById("ajax_load_icon").style.display = "block";
		//alert("loading");
	}

	if(xmlHttp.readyState == 4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("leftNavCol").innerHTML=xmlHttp.responseText;
		//document.getElementById("ajax_load_icon").style.display = "none";
		//alert("finished");
	} 
}

//Changes browser title
function changeBrowserTitle() 
{
	if(xmlHttp2.readyState == 3) // Loading Request (Server -> Client)
	{
		//*** Loading ***//
		//document.getElementById("ajax_load_icon").style.display = "block";
		//alert("loading");
	}

	if(xmlHttp2.readyState == 4 || xmlHttp2.readyState=="complete")
	{ 
		//alert(html_meta['keywords']);
		
		//document.title=xmlHttp2.responseText + browserTitle;
		title=convertSpecialChar(xmlHttp2.responseText);
		document.title=convertSpecialChar(xmlHttp2.responseText);
		//alert(xmlHttp2.responseText);
		//document.getElementById("ajax_load_icon").style.display = "none";
		//alert("finished");
		
		var keywordsArray = html_meta_keywords.split(", ");
		keywordsArray.shift();
		html_meta_keywords = keywordsArray;
		
	
		document.getElementsByName('keywords')[0].content=xmlHttp2.responseText + ", " + html_meta_keywords;
		
		//alert(xmlHttp2.responseText + ", " + html_meta_keywords);
		
		//alert(xmlHttp2.responseText);
		//alert(html_meta['keywords']);
		//alert(html_meta['description']);
	} 
}


function changeMetaDescription() 
{
	if(xmlHttp3.readyState == 3) // Loading Request (Server -> Client)
	{
		//*** Loading ***//
		//document.getElementById("ajax_load_icon").style.display = "block";
		//alert("loading");
	}

	if(xmlHttp3.readyState == 4 || xmlHttp3.readyState=="complete")
	{ 
		//alert(html_meta['description']);
		
		var descriptionArray = html_meta_description.split(", ");
		descriptionArray.shift();
		html_meta_description = descriptionArray;
		
	
		document.getElementsByName('description')[0].content=xmlHttp3.responseText + ", " + html_meta_description;
		//alert(xmlHttp3.responseText + ", " + html_meta_description);
		
	} 
}



//Changes breadcrumb
function changeBreadcrumb() 
{
	if(xmlHttp4.readyState == 3) // Loading Request (Server -> Client)
	{
		//*** Loading ***//
		//document.getElementById("ajax_load_icon").style.display = "block";
		//alert("loading");
	}

	if(xmlHttp4.readyState == 4 || xmlHttp4.readyState=="complete")
	{ 
		document.getElementById("breadcrumb_image_title").innerHTML=xmlHttp4.responseText;

	} 
}

//Changes breadcrumb
function changeEmailThisPageLink() 
{
	if(xmlHttp5.readyState == 3) // Loading Request (Server -> Client)
	{
		//*** Loading ***//
		//document.getElementById("ajax_load_icon").style.display = "block";
		//alert("loading");
	}

	if(xmlHttp5.readyState == 4 || xmlHttp5.readyState=="complete")
	{ 
		//innerHTML automatically convert &#039 to a ' and causing JS problem, 
		//use the following live to convert &#039 to \\' and it will show \' in t he final rendered HTML/JS code
		
		//var linkCode = xmlHttp5.responseText.replace("&#039;","\\'"); 
		//linkCode = linkCode.replace("&#039;","\\'"); 
		
		var linkCode = convertSpecialCharAjax(xmlHttp5.responseText);
		//var linkCode = xmlHttp5.responseText.replace("&#039;","\\'");  
		
		document.getElementById("share_option_email").innerHTML=linkCode;
		//alert(linkCode);
	} 
}


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 changeClassName(obj, classname){
		//gallery_img_thumb_current
		
		for(i=629; i<650; i++){
			var element_id = 'img'+i;
			//alert(element_id);

			if(document.getElementById(element_id) != null){
				document.getElementById(element_id).className = "gallery_img_thumb";
			}
		}
		obj.className = classname;
		
	}
*/


