	var thecomments;	
	var videocomments;	
	var nextid = 0;	
	var previd = 0;	
	var nextimgid = 0;
	var previmgid = 0;
	var curid = 0;
	var curlocationid = 1;
	var lon_imagelist = [];
	var lon_totalimages = 0;
	var lon_show='';
	var edin_imagelist = [];
	var edin_totalimages = 0;
	var edin_show='';
	var other_imagelist = [];
	var other_totalimages = 0;
	var other_show='';
	var showstore=new Object;
	var imagelisthold = new Object;
	var holder = [];
	jQuery.preloadImages = function(){
  		for(var i = 0; i<arguments.length; i++){
    		jQuery("<img>").attr("src", arguments[i]);
  		}
	}
	
	$.preloadImages("images/trans-loader.gif","images/blue-loader.gif","images/mainbg-long-new.gif");
	function drawImage(imagedata){
		$('#imageloader').show();
		$('#imageholder-inner').hide()
		$('#imageholder-inner').empty();
		$('#imageholder-inner').html('<img style="display:none;" onload="showPic()" src="http://'+imagedata.url+imagedata.showimage+'" id="showimage" />');
		$('#artistinfo').empty();
		$('#artistinfo').append(imagedata.caption);
		curid = parseInt(imagedata.pos);
		showImageNav(curid);
	}
	function updateImage(imageid){
		var theform =$('#thecommentform input[@name=imageid]');
		theform.attr('value',imagelisthold["id"+curlocationid][imageid].imageid);
		drawImage(imagelisthold["id"+curlocationid][imageid]);
	}
	
	function showPic(){
		$('#imageloader').hide();
		$('#showimage').show();
		$('#imageholder-inner').show();
	}
	
	function showThumb(imageid){
		$('#loader'+imageid).hide();
		$('#thumb'+imageid).show();
		var nextone = parseInt(imageid)+1;
		showtheImage(nextone);
	}
	function showImageNav(imageid){
		if(curlocationid==1){
			var totalimages = lon_totalimages;
		}else if(curlocationid==2){
			var totalimages = edin_totalimages;
		}else if(curlocationid==3){
			var totalimages = other_totalimages;
		}
		nextimgid = imageid+1;
		previmgid = imageid-1;
		var currimg = $('#currentimage');
		if(imagelisthold["id"+curlocationid][nextimgid]){
			$('#nextimg').show();
			currimg.css('margin-right','');
		}else{
			$('#nextimg').hide();
			currimg.css('margin-right',35);
		}
		if(imagelisthold["id"+curlocationid][previmgid]){
			$('#previmg').show();
			currimg.css('margin-left','');
		}else{
			$('#previmg').hide();
			currimg.css('margin-left',35);
		}
		currimg.empty();
		currimg.append(imagelisthold["id"+curlocationid][imageid].pos+'&nbsp;of&nbsp;'+totalimages);
		var lon = $('#londoncount');
		lon.empty();
		lon.append("("+lon_totalimages+")");
		var edin = $('#edincount');
		edin.empty();
		edin.append("("+edin_totalimages+")");
		var other = $('#othercount');
		other.empty();
		other.append("("+other_totalimages+")");
	}
	function updateImage2(imageid){
		$.getJSON( 'imageinfo.php',{'imageid':imageid},
				function(imagedata){
					$('#imageloader').show();
					$('#imageholder-inner').hide().empty().html('<img style="display:none;" onload="showPic()" src="http://'+imagedata.url+imagedata.showimage+'" id="showimage" />');
					$('#artistinfo').empty().append(imagedata.caption);
				}
		);
	}
	function showComment(commentid){
		if(thecomments[commentid]){
			var thecomment=$('#thecomment');
			//If a comment is already in place.
			if(thecomment){
				//fade it out
				
				thecomment.fadeOut(300,function(){
						//empty the comment of content.
						thecomment.empty();
						//append the new comment to the comment area
						var thumbnail = ''
						if(thecomments[commentid].thumbnail) thumbnail = "http://"+thecomments[commentid].Imageurl+thecomments[commentid].Imagethumbnail;
						else thumbnail="http://"+thecomments[commentid].Imageurl+thecomments[commentid].Imagethumbnail_org;
						
						thecomment.append('<div> <a href="#1" onclick="updateImage2('+thecomments[commentid].imageid+')"><img class="slideshowimages2" src="'+thumbnail+'" alt="image" id="thumbcomment" align="right"/></a><img src="images/right_speak.png" alt="right speach mark"/> '+thecomments[commentid].comment+' <img src="images/left_speak.png" alt="left speach mark"/></div>');

						thecomment.append('<span id="thecommentauthor" class="clear">'+thecomments[commentid].author+',&nbsp;'+thecomments[commentid].authorlocation+'</span><div class="clear"></div>');
						//fade the new comment in
						thecomment.fadeIn(300);
					}
				);
			}
		}
		nextid = commentid+1;
		previd = commentid-1;
		if(thecomments[nextid]){
			$('#nextbutton').show();
		}else{
			$('#nextbutton').hide();
		}
		if(thecomments[previd]){
			$('#prevbutton').show();
		}else{
			$('#prevbutton').hide();
		}
		showDirectlinks(commentid);
	}
	function showNext(){
		showComment(nextid);
	}
	function showPrev(){
		showComment(previd);
	}
	function showNextImg(){
		updateImage(nextimgid);
	}
	function showPrevImg(){
		updateImage(previmgid);
	}
	function showDirectlinks(curcomment){
		var paging = $('#commentpaging');
		paging.empty();
		for(commentid in thecomments){
			if(commentid == curcomment){
				paging.append('&nbsp;'+commentid+'&nbsp;');
			}else{
				paging.append('&nbsp;<a href="#1" onclick="showComment('+commentid+')">'+commentid+'</a>&nbsp;');
			}
			if(commentid==15) paging.append('<br />');
		}
	}
	
	function showCommentPost(){
		$('#infobox').hide();
		$('#commentform').toggle();
		$('#showpostlink').toggle();
	}
	function getComments(){
		$.getJSON( 'listcomments.php', 
				function(json){
					thecomments=json;
					showComment(1);
				}
		);
	}
	function switchlocations(locid){
		var lontab=$('#lontab');
		var edintab=$('#edintab');
		var othertab=$('#othertab');
		if(locid==1){
			curlocationid=1;
			curid=1;
			lontab.attr('class','on');
			edintab.attr('class','off');
			othertab.attr('class','off');
			initShow(imagelisthold["id"+curlocationid],curid);
		}else if (locid==2){
			curlocationid=2;
			curid=1;
			lontab.attr('class','off');
			edintab.attr('class','on');
			othertab.attr('class','off');
			initShow(imagelisthold["id"+curlocationid],curid);
		}else if (locid==3){
			curlocationid=3;
			curid=1;
			lontab.attr('class','off');
			edintab.attr('class','off');
			othertab.attr('class','on');
			initShow(imagelisthold["id"+curlocationid],curid);
		}
	}
	function switchontab(){
		var lontab=$('#lontab');
		var edintab=$('#edintab');
		var othertab=$('#othertab');
		
		if(curlocationid==1){		
			lontab.attr('class','on');
			edintab.attr('class','off');
			othertab.attr('class','off');
		}else if (curlocationid==2){		
			lontab.attr('class','off');
			edintab.attr('class','on');
			othertab.attr('class','off');
			
		}else if (curlocationid==3){		
			lontab.attr('class','off');
			edintab.attr('class','off');
			othertab.attr('class','on');		
		}
	}
	function showtheImage(imageid){
		imageid=parseInt(imageid);
		if(imagelisthold["id"+curlocationid][imageid] && holder[imageid]!=1){
			if(imagelisthold["id"+curlocationid][imageid].thumbnail) thumbnail = "http://"+imagelisthold["id"+curlocationid][imageid].url+imagelisthold["id"+curlocationid][imageid].thumbnail;
			else thumbnail="http://"+imagelisthold["id"+curlocationid][imageid].url+imagelisthold["id"+curlocationid][imageid].thumbnail_org;
			holder[imageid]=1;
	  		$('#loader'+imageid).after('<a href="#1" onclick="updateImage('+imageid+')"><img class="slideshowimages" src="'+thumbnail+'" alt="image" style="display:none;" onload="showThumb('+imageid+')" id="thumb'+imageid+'"/></a>');
		}
	}
	function doShow(imagedata){
		var showarea = $('#sliderwidth');
		var count=1;
		var startwidth=69;
		var holdstr = '';
		var firstid =0;
		var thisid = 0;
		holder = [];
		for(imageid in imagedata){
			var thumbnail = '';
			if(firstid==0){
				firstid = imageid;
				if(curid==0) {
					updateImage(firstid);
				}
				var firstrow=1;
			}else{
				firstrow =0;
			}
			if(imagedata[imageid].thumbnail) thumbnail = "http://"+imagedata[imageid].url+imagedata[imageid].thumbnail;
			else thumbnail="http://"+imagedata[imageid].url+imagedata[imageid].thumbnail_org;
			if(count==1){
				holdstr='<div class="slideshowimageshold">';
			}
			if(firstrow==1){
				holdstr=holdstr+'<div id="loader'+imageid+'" class="thumbloader"><img src="images/tiny.gif"  onload="showtheImage('+imageid+',\''+thumbnail+'\')" /></div>';
			}else{
				holdstr=holdstr+'<div id="loader'+imageid+'" class="thumbloader"></div>';
			}
			if(count==2){
				holdstr=holdstr+'</div>';
				showarea.append(holdstr);
				count=1;
				showarea.css("width",startwidth);
				startwidth = startwidth+69;
			}else{
				count=2;
			}
		}
		if(count==2){
			holdstr=holdstr+'<img src="images/tiny.gif" class="slideshowimages" width="64" height="1"/></div>';
			showarea.append(holdstr);
			count=1;
			showarea.css("width",startwidth);
		}
		
	}
	function initShow(imagedata,theimageid){
		var showarea = $('#sliderwidth');
		var count=1;
		var startwidth=69;
		var holdstr = '';
		var firstid =0;
		var thisid = 0;
		if(theimageid>0)
			curid = theimageid;
		showarea.empty();
		if(imagedata[theimageid]){
			updateImage(theimageid);
		}
		doShow(imagedata);		
	}
	function buildShow(json){
		imagelisthold["id1"]=[];
		imagelisthold["id1"]=json.london.imagedata;
		lon_totalimages=parseInt(json.london.imagecount);
		imagelisthold["id2"]=[];
		imagelisthold["id2"]=json.edinburgh.imagedata;
		edin_totalimages=parseInt(json.edinburgh.imagecount);
		imagelisthold["id3"]=[];
		imagelisthold["id3"]=json.other.imagedata;
		other_totalimages=parseInt(json.other.imagecount);
		var lontab=$('#lontab');
		var edintab=$('#edintab');
		var othertab=$('#othertab');
		if(lon_totalimages>0) lontab.show();
		if(edin_totalimages>0) edintab.show();
		if(other_totalimages>0) othertab.show();
		
		initShow(imagelisthold["id"+curlocationid],curid);
		
		getComments();
		switchontab();
	}
	function getVideoComments(){
		$.getJSON( 'listcomments.php', {imageid:1000000},
				function(json){
					videocomments=json;
					showVideoComment(1);
				}
		);
	}
	function showVideoComment(commentid){
		if(videocomments[commentid]){
			var thecomment=$('#thecomment');
			//If a comment is already in place.
			if(thecomment){
				//fade it out
				
				thecomment.fadeOut(300,function(){
						//empty the comment of content.
						thecomment.empty();
						//append the new comment to the comment area
						
						thecomment.append('<img src="images/right_speak.png" alt="right speach mark"/> '+videocomments[commentid].comment+' <img src="images/left_speak.png" alt="left speach mark"/>');

						thecomment.append('<span id="thecommentauthor">'+videocomments[commentid].author+',&nbsp;'+videocomments[commentid].authorlocation+'</span><div class="clear"></div>');
						//fade the new comment in
						thecomment.fadeIn(300);
					}
				);
			}
		}
		nextid = commentid+1;
		previd = commentid-1;
		if(videocomments[nextid]){
			$('#nextbutton').show();
		}else{
			$('#nextbutton').hide();
		}
		if(videocomments[previd]){
			$('#prevbutton').show();
		}else{
			$('#prevbutton').hide();
		}
		showVideoDirectlinks(commentid);
	}
	function showVideoDirectlinks(curcomment){
		var paging = $('#commentpaging');
		paging.empty();
		for(commentid in videocomments){
			if(commentid == curcomment){
				paging.append('&nbsp;'+commentid+'&nbsp;');
			}else{
				paging.append('&nbsp;<a href="#1" onclick="showVideoComment('+commentid+')">'+commentid+'</a>&nbsp;');
			}
			if(commentid==15) paging.append('<br />');
		}
	}