//<![CDATA[
var useridtmp = "";
var ismypagetmp = "False";
var owneridtmp = "";
var editinplacephotolatestname = new Array;
var countphotolatest = 0;
var objectselect= null;
var photopageno = 0;
var commentpageno = 0;
var imgdeleting = false;
var commentdeleting = false;
var commentposting = false

function deletecomment_click(itemtype,itemid)
{
	if(itemid && !commentdeleting)
	{
		if(confirm(_('Do you want to delete this message?')))
		{
			deleting=true;
			var opt = {
				method: 'post',
				postBody: 'type=' + itemtype + '&id=' + itemid,
				onSuccess: function(t) {
					commentdeleting = false;
					showcomments(useridtmp,commentpageno);
				},
				onFailure: function(t) {
					commentdeleting = false;
					alert(_("Sorry, the message cannot be deleted. Please try again"));
				}
			}
			new Ajax.Request('/photos/deletePhotoComment', opt);
		}
	}
	else
	{
		if(commentdeleting)
		{
			alert(_("Sorry, the message is deleting. Please wait"));
		}
	}
}

function CutTitle()
{
	var TitleIdList = document.getElementsByTagName('h3');
	for(var i=0;i < TitleIdList.length;i++)
	{	var itemid = TitleIdList[i].id
		if(itemid.indexOf('photolatesttitle')==0)
		{
			 var output = CutString(TitleIdList[i].innerHTML,14);
			 TitleIdList[i].innerHTML = output;
		}
	}

}

function swaptitle(itemid,reverse)
{
	if(itemid != -1)
	{
		if(reverse)
			 $('hiddentitle'+itemid).value = $("photolatesttitle"+itemid).innerHTML;
		else
			$("photolatesttitle"+itemid).innerHTML = $('hiddentitle'+itemid).value;
	}
}

function SearchInArray(array,value)
{
	for(var i = 0;i<array.length;i++)
	{
		if(array[i]==value)
			return true;
	}
	return false;
}

function showelement(evt)
{
	var evt = (evt)?evt:((window.event)?window.event:null);
	if(evt)
	{
		var objecttarget = (evt.target)?evt.target:((evt.srcElement)?evt.srcElement:null);
		var str = objecttarget.id;
		if(str)
		{
			var itemid2 = "";
			if(str.indexOf("item")==0)
				itemid2 = str.substring(4);
			else if(str.indexOf("photo_")==0)
				itemid2 = str.substring(6);
			else if(str.indexOf("photolatesttitle")==0)
			{
				itemid2 = str.substring(16);
				var index = itemid2.indexOf("-");
				if(index != -1)
					itemid2 = itemid2.substring(0,index);
			}
			else if(str.indexOf("date")==0)
				itemid2 = str.substring(4);
			else if(str.indexOf("commentno")==0)
				itemid2 = str.substring(9);
			else if(str.indexOf("detail")==0)
				itemid2 = str.substring(6);
			else if(str.indexOf("location")==0)
				itemid2 = str.substring(8);
			else if(str.indexOf("delete")==0)
				itemid2 = str.substring(6);

			if(itemid2!="")
			{
				showElement("delete"+itemid2);
			}
		}
	}
}


function hideelement(evt)
{
	var evt = (evt)?evt:((window.event)?window.event:null);
	if(evt)
	{
		var objecttarget = (evt.target)?evt.target:((evt.srcElement)?evt.srcElement:null);
		var str = objecttarget.id;
		if(str)
		{
			var itemid2 = "";

			if(str.indexOf("item")==0)
				itemid2 = str.substring(4);
			else if(str.indexOf("photo_")==0)
				itemid2 = str.substring(6);
			else if(str.indexOf("photolatesttitle")==0)
			{
				itemid2 = str.substring(16);
				var index = itemid2.indexOf("-");
				if(index != -1)
					itemid2 = itemid2.substring(0,index);
			}
			else if(str.indexOf("date")==0)
				itemid2 = str.substring(4);
			else if(str.indexOf("commentno")==0)
				itemid2 = str.substring(9);
			else if(str.indexOf("detail")==0)
				itemid2 = str.substring(6);
			else if(str.indexOf("location")==0)
				itemid2 = str.substring(8);
			else if(str.indexOf("delete")==0)
				itemid2 = str.substring(6);

			if(itemid2!="")
			{
				hideElement("delete"+itemid2);
			}
		}
	}
}


function CreateEditTitle()
{
	var TitleIdList = document.getElementsByTagName('h3');
	for(var i=0;i < TitleIdList.length;i++)
	{	var itemid = TitleIdList[i].id
		if(itemid.indexOf('photolatesttitle')==0)
		{
			var selectid = itemid.substring(16);
			if(!SearchInArray(editinplacephotolatestname,itemid))
			{
				var TitleEditor = new Ajax.InPlaceEditor(itemid,'/photo/edit/'+selectid+'/title',
				{
					okControl: 'link',okText:'Save',cancelText:'Cancel',textBeforeControls:'  ',textBetweenControls:' ',clickToEditText:'Click to Edit Title', callback: function(form, value) {
					return 'value='+ encodeURIComponent(value)}, 
						onEnterEditMode:function(t)
						{
							var itemid2 = t.element.id.substring(16);
							hideElement('delete'+itemid2);
							if(isIE || isIE7)
							{
								deleteEvent($('item'+itemid2), 'mouseover',showelement);
								addEvent($('item'+itemid2), 'mouseover', hideelement);
							}
							else
							{
								addEvent($('item'+itemid2), 'mouseover',function(){hideElement('delete'+itemid2);});
							}
						},
						onLeaveEditMode:function(t)
						{
							if($(t.element.id))
							{
								var itemid2 = t.element.id.substring(16);
								swaptitle(itemid2,true);
								if(Trim($(t.element.id).innerHTML) != '')
									$(t.element.id).innerHTML = CutString($(t.element.id).innerHTML,14);
								else
									$(t.element.id).innerHTML = _('Edit title here');

								if(isIE || isIE7)
								{
									deleteEvent($('item'+itemid2), 'mouseover', hideelement);
									addEvent($('item'+itemid2), 'mouseover',showelement);
								}
								else
								{
									addEvent($('item'+itemid2), 'mouseover', function(){showElement('delete'+itemid2);});
								}
							}
						}
				});
				editinplacephotolatestname[countphotolatest++] = itemid;
			}
		}
	}
}


function delete_click(photoid)
{
	if(photoid && !imgdeleting)
	{
		if(confirm(_('Do you want to delete this photo?')))
		{
			imgdeleting = true;
			showElement('loading');
			var opt = {
				method: 'post',
				postBody: 'photoid=' + photoid,
				onSuccess: function(t) {
					imgdeleting = false;
					hideElement('loading');
					showlatestphotos(useridtmp,photopageno);
				},
				onFailure: function(t) {
					imgdeleting = false;
					hideElement('loading');
					alert(_("Sorry, Can't delete photo. Please, try again."));
				}
			}
			new Ajax.Request('/photo/deletephoto', opt);
		}
	}
}

function editphotoname(id)
{
	if(isMyPagetmp == 'True')
	{
		var found = false;
		if(editinplacephotolatestname.length >0)
		{
			for(var i =0 ;i< editinplacephotolatestname.length;i++)
			{
				if(editinplacephotolatestname[i] == id)
				{
					found = true;
					break;
				}
			}
		}

		if(!found)
		{
			editinplacephotolatestname[countphotolatest++] = id;
			new Ajax.InPlaceEditor('phototitle' + id , '/edit/' + id + '/photo/title');
		}
      }
}

function showlatestphotos(userid,photopage)
{
	//$('update').innerHTML  = loadingText;
	var opt = {
			method: 'post',
			postBody: 'userid=' + userid + '&photopage='+ photopage,
			onSuccess: function(t) {
					if(t.responseText.indexOf('updateitem')!=-1  || photopageno == 0)
					{
						photopageno = photopage;
						if($('latestphotos'))
						{
							$('latestphotos').update(t.responseText);				
						}
						else
						{
							$('update').update(t.responseText);
							if(ismypagetmp == "True")
								CreateEditTitle();
							CutTitle();

							var xxx = document.getElementsByClassName("updateitem");

							for(var i = 0;i<xxx.length;i++)
							{
								    if(xxx[i].id.indexOf("item")==0)
									{
										    if(isIE || isIE7)
											{
												xxx[i].onmouseover = "";
												xxx[i].onmouseout = "";
												addEvent(xxx[i], 'mouseover',showelement);
												addEvent(xxx[i], 'mouseout', hideelement);													   										
											}
									}
							}
						}
					}
					else
					{
						photopageno = (parseInt(photopage)-1).toString();
						showlatestphotos(userid,photopageno);
					}
					editinplacephotolatestname = null;
					editinplacephotolatestname = new Array;
					countphotolatest = 0;
				
			},
			onFailure: function(t) {
				if($('latestphotos'))
					$('latestphotos').innerHTML = t.responseText;
				else
					$('update').innerHTML = t.responseText;
			}
		  }
	new Ajax.Request('/photos/showLatestPhotoList', opt);
}


function showcomments(userid,commentspage)
{
	var opt = {
			method: 'post',
			postBody: 'userid=' + userid + '&commentspage='+ commentspage,
			onSuccess: function(t) {
				if(t.responseText.indexOf('commentitem')!=-1  || commentpageno == 0)
				{
					commentposting = false;
					commentpageno = commentspage;
					hideElement('commentloading');
					$('commentlist').innerHTML = t.responseText;
				}
				else
				{
						commentpageno = (parseInt(commentspage)-1).toString();
						showcomments(userid,commentpageno);
				}
				ReplacePgLinkComment();
			},
			onFailure: function(t) {
				commentposting = false;
				hideElement('commentloading');
				$('commentlist').innerHTML = t.responseText;
			}
		  }
	new Ajax.Request('/photos/showPhotosComment', opt);
}


function commentsubmit()
{
	if(commentposting)
	{
		alert(_('Posting comment. Please wait'));
		return;
	}
	if(Trim($('txtcomment').value)=='')
		return;
	showElement('commentloading');
	commentposting = true;
	var posttext = encodeURIComponent($('txtcomment').value);
	var opt = {
			method: 'post',
			postBody: 'ownerid=' + useridtmp + '&userid='+owneridtmp+'&text=' + posttext,
			onSuccess: function(t) {
				showcomments(useridtmp,'0')
				$('txtcomment').value="";
				showrecentupdate(useridtmp);
			},
			onFailure: function(t) {
				commentposting = false;
				hideElement('commentloading');
				$('showcommentlist').innerHTML = t.responseTest;//'Sorry, could not load  your photos'
			}
		  }
	new Ajax.Request('/photos/PostPhotosComment', opt);
}


function quotesubmit(qid,types,itemid,commentid)
{
	if(commentposting)
	{
		alert("Posting comment. Please wait");
		return;
	}
	if(Trim($(qid).value)=='')
		return;
	showElement('loading'+qid.substring(4));
    commentposting = true;
	var ownerid = owneridtmp;
	var posttext = encodeURIComponent($(qid).value);

	var posturl = '/photos/PostPhotosComment';
	var postdata = 'ownerid=' + useridtmp + '&userid='+ownerid+'&text=' + posttext + "&commentid=" + commentid;

	if (types=="photo")
	{
		posturl = '/photo/PostPhotoComment';
		postdata = 'photoid=' + itemid + '&userid=' + ownerid + '&text=' + posttext + "&commentid=" + commentid;
	}
	else if(types=="album")
	{
		posturl = '/photos/PostAlbumComment';
		postdata = 'albumid=' + itemid + '&userid=' + ownerid + '&text=' + posttext + "&commentid=" + commentid;
	}

	var opt = {
			method: 'post',
			postBody: postdata,
			onSuccess: function(t) {
				showcomments(useridtmp,'0');
				txtcomment.value="";
				showrecentupdate(useridtmp);
			},
			onFailure: function(t) {
				commentposting = false;
				hideElement('loading'+qid.substring(4));
				$('showcomments').innerHTML = t.responseTest;
			}
		  }
	new Ajax.Request(posturl, opt);
}


function replysubmit(rid,types,itemid,commentid)
{
	if(commentposting)
	{
		alert(_('Posting comment. Please wait'));
		return;
	}
	if(Trim($(rid).value)=='')
		return;
	showElement('replyloading'+rid.substring(4));
    commentposting = true;
	var ownerid = owneridtmp;
	var posttext = encodeURIComponent($(rid).value);

	var posturl = '/photos/PostPhotosComment';
	var postdata = 'ownerid=' + useridtmp + '&userid='+ownerid+'&text=' + posttext + "&commentid=" + commentid + "&isreply=True";

	if (types=="photo")
	{
		posturl = '/photo/PostPhotoComment';
		postdata = 'photoid=' + itemid + '&userid=' + ownerid + '&text=' + posttext + "&commentid=" + commentid  + "&isreply=True";
	}
	else if(types=="album")
	{
		posturl = '/photos/PostAlbumComment';
		postdata = 'albumid=' + itemid + '&userid=' + ownerid + '&text=' + posttext + "&commentid=" + commentid  + "&isreply=True";
	}

	var opt = {
			method: 'post',
			postBody: postdata,
			onSuccess: function(t) {
				showcomments(useridtmp,'0');
				txtcomment.value="";
				showrecentupdate(useridtmp);
			},
			onFailure: function(t) {
				commentposting = false;
				hideElement('replyloading'+rid.substring(4));
				$('showcomments').innerHTML = t.responseTest;
			}
		  }
	new Ajax.Request(posturl, opt);
}


function moveobject(evt)
{
	var object = $('deleteicon');
	object.style.zIndex=100;
	var evt = (evt)?evt:((window.event)?window.event:null);
	if(evt)
	{
		var objecttarget = (evt.target)?evt.target:((evt.srcElement)?evt.srcElement:null);
		var id = objecttarget.id.substr(0,9);
		if(id == 'photoimg_')
		{
			showElement('deleteicon');

			object.style.left = findPosX(objecttarget) + 155 + 'px';
			object.style.top =  findPosY(objecttarget) + 110 + 'px';

			objectselect = objecttarget;
		}
		else
		{
			if(objecttarget.id != 'deleteicon')
			{
				object.style.display = 'none';
				objectselect = null;
			}
		}
	}
}

function URLEncode(plaintext )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.!~*'()";
	var HEX = "0123456789ABCDEF";
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += ch;
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
}
function sendPM(id)
{
	var topic = $('topic'+id).value;
	var message = $('msg'+id).value;
	var targetid = $('huid'+id).value;
	var postdata = "userid=" + owneridtmp + "&targetid=" + targetid + "&topic="+ encodeURIComponent(topic) + "&message=" + encodeURIComponent(message);

	var posturl = "/sendPrivateMessage";
	hideElement('p'+id);
	var opt = {
			method: 'post',
			postBody: postdata,
			onSuccess: function(t) {
				$('topic'+id).value = "";
				$('msg'+id).value = "";
				if (t.responseText=="Success")
				{
					$('infotext').innerHTML = _('Send message completed');
					showElement('infobox');
					$('infobox').focus();	
				}
			},
			onFailure: function(t) {
				$('topic'+id).value = "";
				$('msg'+id).value = "";
				$('infotext').innerHTML = _("Cannot send message");
				showElement('infobox');
				$('infobox').focus();
			}
		  }
	new Ajax.Request(posturl, opt);
}

function setphotoprivacy(privacy,pid)
{
		var opt = {
			method: 'post',
			postBody: 'pid=' + pid + '&value=' + privacy,
			onSuccess: function(t) {
				if(t.responseText != "")
				{
					$('item'+pid).className='updateitem '+privacy+'-item';

					var privacylist = new Array("public","friend","topfriend","private");
					for(var i=0;i<privacylist.length;i++)
					{
						$('privacy_'+privacylist[i]+'_'+pid).className = "";
					}
					$('privacy_'+privacy+'_'+pid).className = "selected";
					var eid = 'privacy_'+pid;

					window.setTimeout("hideElement('"+eid+"')",500);
				}
				else
				{
					hideElement('privacy_'+pid);
					$('msgErr').innerHTML = _("Can't set album privacy");
					showElement('infobox');
				}
			},
			onFailure: function(t) {
				if(t.responseText!="No Permission")
				{
					$('msgErr').innerHTML = t.responseText;
					showElement('infobox');
				}
				hideElement('privacy_'+pid);
			}
		  }
		new Ajax.Request('/photo/setphotoprivacy', opt);
}

function showprivacysetting(id)
{
	var eid = 'privacy_'+id;
	var e = document.getElementsByClassName('privacy_setting');
	for(var i=0;i<e.length;i++)
	{
		if (e[i].id != eid)
			hideElement(e[i].id);
	}
	
	if($(eid).visible())
		hideElement(eid);
	else
		showElement(eid);
}

function showdelete_edit(id,value)
{
	if(value)
	{
		showElement('delete'+id);
		if($('editprivacy_'+id))
			showElement('editprivacy_'+id);
		if($('edit_'+id))
			showElement('edit_'+id);
	}
	else
	{
		hideElement('delete'+id);
		if($('editprivacy_'+id))
			hideElement('editprivacy_'+id);
		if($('edit_'+id))
			hideElement('edit_'+id);
	}
}

//]]>
