// JavaScript Document

var NCL_Collections = {
	
	setImage : function(UNID) {
		if(document.getElementById("collectionImage")) {
			document.getElementById("collectionImage").src= "/newcastlecollection/if/loader_large.gif";
		}
		var server_name = "www.newcastle.gov.uk";
		var url = '/collections.nsf/AJAX_getImage?openagent';
		var params = '&unid=' + UNID;
		// notice the use of a proxy to circumvent the Same Origin Policy.
		var ajax = new Ajax.Request(url, {
			parameters: params,
			method: 'get',
			onSuccess: function(xm){
				xmr = xm.responseXML;
				tPath = xmr.getElementsByTagName("path")[0].firstChild.nodeValue;
				tPathThumb = "/newcastlecollection/optimised/thumbs"+tPath;
				tPathFullsize = "/newcastlecollection/optimised/fullsize"+tPath;

				tNextImage = xmr.getElementsByTagName("nextimage")[0].firstChild.nodeValue;
				tPreviousImage = xmr.getElementsByTagName("previousimage")[0].firstChild.nodeValue;
				tCurrentIndex = xmr.getElementsByTagName("currentindex")[0].firstChild.nodeValue;
				tCurrentUNID = xmr.getElementsByTagName("unid")[0].firstChild.nodeValue;
				tTitle = xmr.getElementsByTagName("title")[0].firstChild.nodeValue;
				
				URL_Path = escape("http://"+server_name+"/collections.nsf/display?readform&id="+tCurrentUNID);
		
				if(tNextImage != "NULL"){
					//if Flash version being used then update the params
		
					if(!document.getElementById("nextimglink")){
						var a_href = document.createElement("a");
						a_href.id="nextimglink";
						a_href.href="#";
						a_href.innerHTML = document.getElementById("nextimg").innerHTML;
						document.getElementById("nextimg").innerHTML = "";
						document.getElementById("nextimg").appendChild(a_href);
					}
					document.getElementById("nextimglink").onclick=new Function ("onclick","NCL_Collections.setImage('"+tNextImage+"'); return false;");
					document.getElementById("nextimglink").href="/collections.nsf/display?readform&id="+tNextImage;
					document.getElementById("currentindex").innerHTML = tCurrentIndex;
				
				} else {
					document.getElementById("nextimg").innerHTML = "Next &#187;";
					//document.getElementById("nextimg").class = "prevnolink";
				}
				
				if(tPreviousImage != "NULL"){
					if(!document.getElementById("previousimglink")){
						var a_href = document.createElement("a");
						a_href.id="previousimglink";
						a_href.href="#";
						a_href.innerHTML = document.getElementById("previousimg").innerHTML;
						document.getElementById("previousimg").innerHTML = "";
						document.getElementById("previousimg").appendChild(a_href);
					}
					document.getElementById("previousimglink").onclick = new Function ("evt","NCL_Collections.setImage('"+tPreviousImage+"'); return false;");
					document.getElementById("previousimglink").href="/collections.nsf/display?readform&id="+tPreviousImage;
					document.getElementById("currentindex").innerHTML = tCurrentIndex;
				} else {
					document.getElementById("previousimg").innerHTML = "&#171; Previous";
					//document.getElementById("previousimg").class = "prevnolink";
				}
				
				if((document.getElementById("imagezoomIE")) || (document.getElementById("imagezoomFF"))){
					
					var HTMLStr = "";
					var thumbwidth = "375";
					var fullwidth = "580";
					var flashwidth = "375";
					var flashheight = "580";
					
					thumbimage = tPathThumb;
					fullimage = tPathFullsize;
					
					HTMLStr = HTMLStr + '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="'+flashwidth+'" HEIGHT="'+flashheight+'" id="imagezoomIE" ALIGN="middle">';
					HTMLStr = HTMLStr + '<param id="movie" name="movie" value="/newcastlecollection/flash/imagezoom.swf?image='+thumbimage+'&imagebig='+fullimage+'&imgW='+flashwidth+'&imgH='+flashheight+'">';
					HTMLStr = HTMLStr + '<param id="loop" name="loop" value="false">';
					HTMLStr = HTMLStr + '<param id="menu" name="menu" value="false">';
					HTMLStr = HTMLStr + '<param id="quality" name="quality" value="high">';
					HTMLStr = HTMLStr + '<param id="bgcolor" name="bgcolor" value="#ffffff">';
					HTMLStr = HTMLStr + '<param id="thumbnail" name="thumbnail" value="'+thumbimage+'">';
					HTMLStr = HTMLStr + '<param id="fullimage" name="fullimage" value="'+fullimage+'">';
					HTMLStr = HTMLStr + '<EMBED id="imagezoomFF" src="/newcastlecollection/flash/imagezoom.swf?image='+thumbimage+'&amp;imagebig='+fullimage+'&amp;imgW='+flashwidth+'&amp;imgH='+flashheight+'" loop="false" menu="false" quality="high" bgcolor="#ffffff"  WIDTH="'+flashwidth+'" HEIGHT="'+flashheight+'" NAME="imagezoom" ALIGN="middle" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>';
					HTMLStr = HTMLStr + '</OBJECT>';
					document.getElementById("imageHolder").innerHTML = HTMLStr;
				} else {
					//Set the image
					img_src = tPathThumb;
					document.getElementById("collectionImage").src = img_src;
				}

				//Set the social bookmarks for currently displayed image
				title_QS = "&title="+tTitle;
				document.getElementById("email_link").href="emailfriend?openform&id="+tCurrentUNID;
				document.getElementById("google_link").href="http://www.google.com/bookmarks/mark?op=add&bkmk="+URL_Path+title_QS;				
				document.getElementById("delicious_link").href="http://del.icio.us/post?url="+URL_Path+title_QS;
				document.getElementById("reddit_link").href="http://reddit.com/submit?url="+URL_Path+title_QS;
				document.getElementById("digg_link").href="http://digg.com/submit?url="+URL_Path+title_QS;
				document.getElementById("facebook_link").href="http://www.facebook.com/sharer.php?u="+URL_Path;
				document.getElementById("stumbleupon_link").href="http://www.stumbleupon.com/submit?url="+URL_Path+title_QS;
			}
		});
	},
	
	reportImageError : function(request) {
		$F('collectionImageFail') = "Error";
	}
}