var curLocation, newLocation, checkinterval, interval;
function GetFileData(url, obj, cond)
{	
	var getrequest = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		getrequest = new XMLHttpRequest();
		if (getrequest.overrideMimeType) {		
		getrequest.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
	 try {
		getrequest = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   getrequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
	}
	 

	getrequest.open('GET', url, true);
	getrequest.onreadystatechange = function() {
		if(getrequest.readyState == 4) {
		  	result = getrequest.responseText;
			if(cond == "img")
				obj.src = result;
			else if(cond == "add"){
				
				obj.innerHTML += result;
			}
		}
	};	
	getrequest.send(null);
}
function ViewComments(photoId,total)
{
	var image = SiteName+'/images/loading.gif';
	var url = SiteName+"/people/files/getAllPhotoComments.php?photoId="+photoId+"&total="+total;
	document.getElementById("moreComment").innerHTML = "<img src='"+image+"' />";
	GetFormData(url, "moreComment");
}
function AddComment(pnt, photo_id)
{
	document.getElementById('PhotoComment').innerHTML = '<textarea id="photo_comment" rows="2" cols="70">'+pnt.innerHTML+'</textarea><br /><div class="space"></div><input type="button" value="Save Comment" onclick="SaveComment('+photo_id+');" /><input type="button" value="Cancel" onclick="CancelButton('+photo_id+')">';
}

function SaveComment(photo_id)
{
	var save_val = document.getElementById('photo_comment').value;	
	if(save_val.length <= 255 &&  save_val.length > 0)
	{
		var url = SiteName+"/people/files/photo_comment_save.php?photoId="+photo_id+'&desc='+encodeURI(escape(save_val));
		document.getElementById('PhotoComment').innerHTML = 'Saving Photo Comment ...';
		GetFormData(url, 'PhotoComment');
		IncreaseInNo(photo_id);
	}
	else
	{
		alert('Photo Comment should be not be empty and longer than 255 characters.');
		document.getElementById('photo_comment').select();
	}		
}
function IncreaseInNo(photo_id)
{
	var url = SiteName+"/people/files/getnewcomment.php?photoId="+photo_id;
	var list = document.getElementById('getNewComment');
	GetFileData(url, list , "add");
}
function CancelButton(photoId)
{
	var save_val = 'Click here to add comment';
	document.getElementById('PhotoComment').innerHTML = '<div class="comment" style=" width:90%;"><a href="#Photo'+photoId+'" onclick="javascript:AddComment(this, '+photoId+');" class="more">'+save_val+'</a></div>';	
		
}
function StartScroll(lr)
{
	clearTimeout(interval);
	var bigelm = document.getElementById("BreadCrumbs");
	var elm = document.getElementById("AllThumbnails");
	var maxscroll = eval(parseInt(elm.style.width) - 600);
	if(lr == "left")
	{
		if(bigelm.scrollLeft > 0)
			bigelm.scrollLeft -= 10;
		interval = setTimeout("StartScroll('left')", 11);
	}
	
	if(lr == "right")
	{
		if(bigelm.scrollLeft < maxscroll)
			bigelm.scrollLeft += 10;
		interval = setTimeout("StartScroll('right')", 11);
	}
}

function StopScroll()
{
	clearTimeout(interval);
}

function ShowDetails(photoId)
{
	
	clearInterval(checkinterval);	
	var elm = document.getElementById("AllThumbnails");
	var bigelm = document.getElementById("BreadCrumbs");
	var x = false;
	location.href = '#Photo'+photoId;	
	newLocation = location.hash.slice(1);
	
	if(elm.hasChildNodes())
	{
		var child = elm.childNodes;		
		for(var i=0; i<child.length; i++)
		{
			if(child[i].nodeName == "img" || child[i].nodeName == "IMG")
			{
				if(isNaN(photoId) || photoId == '')
				{
					photoId = child[i].getAttribute('photoid');
				}
				if(child[i].getAttribute('photoid') == photoId)
				{
					
					child[i].setAttribute("class", "imgnofilter");
					child[i].className =  "imgnofilter";
					var imgsrc = child[i].src.substr(child[i].src.indexOf("ico_") + 4);
					document.getElementById('ZoomedImage').innerHTML = '<img src="'+PARENT_SITENAME+'/uploads/userimages/'+imgsrc+'" alt="Loading..." />';
					document.getElementById("currentSerial").innerHTML = child[i].getAttribute("serial");
					
					
					if(i>=1 && child[i-1])
					{
						
						if(child[i-1].nodeName == "img" || child[i-1].nodeName == "IMG")						
							document.getElementById("PreviousLink").innerHTML = '<a href="#Photo'+child[i-1].getAttribute('photoid')+'"  class="more"><b>Previous</b></a>';
						else
						if(i>2 && child[i-2] && (child[i-2].nodeName == "img" || child[i-2].nodeName == "IMG"))
							document.getElementById("PreviousLink").innerHTML = '<a href="#Photo'+child[i-2].getAttribute('photoid')+'"  class="more"><b>Previous</b></a>';
					}
					else
					{
						document.getElementById("PreviousLink").innerHTML = '&nbsp;';						
					}
					
					if(child[i+1])
					{
						if(child[i+1].nodeName == "img" || child[i+1].nodeName == "IMG")
							document.getElementById("NextLink").innerHTML = '<a href="#Photo'+child[i+1].getAttribute('photoid')+'" class="more"><b>Next</b></a>';
						else
						if(child[i+2] && (child[i+2].nodeName == "img" || child[i+2].nodeName == "IMG"))
						document.getElementById("NextLink").innerHTML = '<a href="#Photo'+child[i+2].getAttribute('photoid')+'"  class="more"><b>Next</b></a>';	
					}
					else
					{
						document.getElementById("NextLink").innerHTML = '&nbsp;';
					}
					
				
					var val = eval(parseInt(child[i].getAttribute("serial")) * 60) - 60;
					if(val < 240)
						val = 0;
					else
						val = val - 240;
					
					
					bigelm.scrollLeft = val;
					
				if(user_session==1)
				{
				document.getElementById("PhotoComment").innerHTML = '<div class="comment" style="width:90%;"><a href="#Photo'+photoId+'" onclick="AddComment(this, '+photoId+');" class="more">Click here to add comment</a></div>';
				}
					
					var url = SiteName+"/people/files/process_photo_detail.php?photoId="+photoId;
					GetFormData(url, "photo_desc_comm");
					x = true;
				}
				else
				{
					child[i].setAttribute("class", "imgfilter");
					child[i].className =  "imgfilter";					
				}
			}
		}
	}
	if(!x)
		ShowDetails("");
		
	
	curLocation = newLocation;
	checkinterval = setInterval("checkLinkChange()", 200);
}

function LoadDefaults()
{
	clearInterval(checkinterval);
	var elm = document.getElementById("AllThumbnails");
	if(elm.hasChildNodes())
	{
		var child = elm.childNodes;		
		for(var i=0; i<child.length; i++)
		{
			if(child[i].nodeName == "img" || child[i].nodeName == "IMG")
			{
				child[i].onclick = function(){location.href = '#Photo' + this.getAttribute('photoid');}
			}
		}
	}
	curLocation = location.hash.slice(1);
	photoId = curLocation.substr(5);	
	ShowDetails(photoId);	
}

function checkLinkChange()
{	
	newLocation = location.hash.slice(1);
	if(newLocation != curLocation)
		LoadDefaults();	
}

