 //<![CDATA[
var photopageno = 0;
var countphoto = 0;
var editinplacephotoname = new Array;
var objectselect= null;
var object = null;
var objecttmp = null;
var mousex = 0;
var mousey = 0;
var grabx = 0;
var graby = 0;
var orix = 0;
var oriy = 0;
var elex = 0;
var eley = 0;
var algor = 0;
var useridtmp = "";
var albumidtmp = "";
var owneridtmp = "";
var itemselectid;
var ismypagetmp = "False";
var commentpageno = 0;
var imgdeleting = false;
var commentdeleting = false;
var commentposting = false
var newcoverid = -1;
var newcover = "";
var albumprivacy = "";

function setCover(itemid,e)
{
	newcoverid = itemid;
	newcover = e.src;
	$('cover-preview-image').src = e.src;
}

function deletecomment_click(itemtype,itemid)
{
	if(itemid && !commentdeleting)
	{
		if(confirm(_('Do you want to delete this message?')))
		{
			commentdeleting = true;
			var opt = {
				method: 'post',
				postBody: 'type=' + itemtype + '&id=' + itemid,
				onSuccess: function(t) {
					commentdeleting = false;
					showcomments(useridtmp,commentpageno);
				},
				onFailure: function(t) {
					commentdeleting = false;
					$('msgErr').innerHTML = _("Sorry, the message cannot be deleted. Please try again");
					showElement('infobox');
				}
			}
			new Ajax.Request('/photos/deletePhotoComment', opt);
		}
	}
	else
	{
		if(commentdeleting)
		{
			$('msgErr').innerHTML = _("Sorry, the message is deleting. Please wait");
			showElement('infobox');
		}
	}
}

function CutTitle()
{
	var TitleIdList = document.getElementsByTagName('h3');
	for(var i=0;i < TitleIdList.length;i++)
	{	var itemid = TitleIdList[i].id
		if(itemid.indexOf('phototitle')==0)
		{
			 var output = CutString(TitleIdList[i].innerHTML,14);
			 TitleIdList[i].innerHTML = output;
		}
	}

}

function swaptitle(itemid,reverse)
{
	if(itemid!=-1)
	{
		if(reverse)
			$('hiddentitle'+itemid).value = $('phototitle'+itemid).innerHTML;
		else
			$('phototitle'+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("phototitle")==0)
			{
				itemid2 = str.substring(10);
				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);
			else if(str.indexOf("editprivacy_")==0)
				itemid2 = str.substring(12);
			else if(str.indexOf('showmovephoto_') == 0)
				itemid2 = str.substring(14);
			
			if(itemid2!="")
			{
				showElement('editprivacy_'+itemid2);
				if($('showmovephoto_'+itemid2))
					showElement('showmovephoto_'+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("phototitle")==0)
			{
				itemid2 = str.substring(10);
				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);
			else if(str.indexOf("editprivacy_")==0)
				itemid2 = str.substring(12);
			else if(str.indexOf("showmovephoto_")==0)
				itemid2 = str.substring(14);
			if(itemid2!="")
			{
				if($('showmovephoto_'+itemid2))
				{
					if($('movephoto_'+itemid2))
					{
						if(!$('movephoto_'+itemid2).visible())
							hideElement("showmovephoto_"+itemid2);
					}
					else
						hideElement("showmovephoto_"+itemid2);
				}

				if($('privacy_'+itemid2))
				{
					if($('hprivacy_'+itemid2) && $('hprivacy_'+itemid2).value == albumprivacy)
					{
						if(!$('privacy_'+itemid2).visible())
							hideElement("editprivacy_"+itemid2);
					}
				}
				else
					hideElement("editprivacy_"+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('phototitle')==0)
		{
			var selectid = itemid.substring(10);
			if(!SearchInArray(editinplacephotoname,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(10);
							hideElement('delete'+itemid2);
							if($('privacy_'+itemid2))
							{
								if($('hprivacy_'+itemid2) && $('hprivacy_'+itemid2).value == albumprivacy)
								{
									if(!$('privacy_'+itemid2).visible())
										hideElement("editprivacy_"+itemid2);
								}
							}
							else
								hideElement("editprivacy_"+itemid2);

							if($('showmovephoto_'+itemid2))
							{
								if($('movephoto_'+itemid2))
								{
									if(!$('movephoto_'+itemid2).visible())
										hideElement("showmovephoto_"+itemid2);
								}
								else
									hideElement("showmovephoto_"+itemid2);
							}

							if(isIE || isIE7)
							{
								deleteEvent($('item'+itemid2), 'mouseover',showelement);
								addEvent($('item'+itemid2), 'mouseover', hideelement);
							}
							else
							{
								addEvent($('item'+itemid2), 'mouseover', function(){
									hideElement('delete'+itemid2);
									if($('privacy_'+itemid2))
									{
										if($('hprivacy_'+itemid2) && $('hprivacy_'+itemid2).value == albumprivacy)
										{
											if(!$('privacy_'+itemid2).visible())
												hideElement("editprivacy_"+itemid2);
										}
									}
									else
										hideElement("editprivacy_"+itemid2);

									if($('showmovephoto_'+itemid2))
									{
										if($('movephoto_'+itemid2))
										{
											if(!$('movephoto_'+itemid2).visible())
												hideElement("showmovephoto_"+itemid2);
										}
										else
											hideElement("showmovephoto_"+itemid2);
									}
									});
							}
						},
						onLeaveEditMode:function(t)
						{
							if($(t.element.id))
							{
								var itemid2 = t.element.id.substring(10);
								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);
										showElement('editprivacy_'+itemid2);
										if($('showmovephoto_'+itemid2))
											showElement('showmovephoto_'+itemid2);});
								}
							}
						}
				});
				editinplacephotoname[countphoto++] = itemid;
			}
		}
	}
}

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++)
					{
						if($('privacy_'+privacylist[i]+'_'+pid))
							$('privacy_'+privacylist[i]+'_'+pid).className = "";
					}
					$('privacy_'+privacy+'_'+pid).className = "selected";
					$('hprivacy_'+pid).value = privacy;
					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 = 'movephoto_'+id;
	var e = document.getElementsByClassName('movephoto_setting');
	for(var i=0;i<e.length;i++)
		hideElement(e[i].id);

	eid = 'privacy_'+id;
	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 showmovephoto(id)
{
	var eid = 'privacy_'+id;
	var e = document.getElementsByClassName('privacy_setting');
	for(var i=0;i<e.length;i++)
		hideElement(e[i].id);

	eid = 'showmovephoto_'+id
	e = document.getElementsByClassName('movephoto_status');
	for(var i=0;i<e.length;i++)
	{
		if (e[i].id != eid)
		{
			if ($('hprivacy_'+id).value!=albumprivacy)
				hideElement(e[i].id);
		}
	}

	eid = 'movephoto_'+id;
	e = document.getElementsByClassName('movephoto_setting');
	for(var i=0;i<e.length;i++)
	{
		if (e[i].id != eid)
			hideElement(e[i].id);
	}

	if($(eid).visible())
		hideElement(eid);
	else
	{
		$('ItemSetting_'+id).value = albumidtmp;
		showElement(eid);
	}
}


function SetMovePhoto(pid) 
{
	var opt = {
			method: 'post',
			postBody: 'albumid=' + $('ItemSetting_'+pid).value + '&photoid=' + pid + '&calbumid=' + albumidtmp,
			onSuccess: function(t) {
				$('msgErr').innerHTML = "";
				showPhotosList(useridtmp,photopageno);
			},
			onFailure: function(t) {
				$('msgErr').innerHTML = _("Sorry, the photo is not moved. Please try again");
				showElement('infobox');
			}
		  }
	new Ajax.Request('/photos/album/movephoto', opt);
}

function showPhotosList(userid,photopage)
{
	var opt = {
			method: 'post',
			postBody: 'userid=' + userid + '&albumid='+ albumidtmp +'&photopage='+ photopage,
			onSuccess: function(t) {
					if(t.responseText.indexOf('updateitem')!=-1 || photopageno == 0)
					{
						photopageno = photopage;
						$('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();
						showPhotosList(userid,photopageno);
					}
					editinplacephotoname = null;
					editinplacephotoname = new Array;
					countphoto = 0;
			},
			onFailure: function(t) {
				$('update').innerHTML = t.responseText;
			}
		  }
	new Ajax.Request('/photos/showPhotoList', opt);
}


function showPhotosCoverList(userid,photopage)
{
	$('coverlist').innerHTML = '<div class="big-loading center pushtop10 pushbottom10"></div>';
	var opt = {
			method: 'post',
			postBody: 'userid=' + userid + '&albumid='+albumidtmp+'&photopage='+ photopage,
			onSuccess: function(t) {
					if(t.responseText.indexOf('cover-thumbnail')!=-1 || photopageno == 0)
					{
						photopageno = photopage;
						$('coverlist').innerHTML = t.responseText;
						if (newcover!="")
							$('cover-preview-image').src = newcover;
					}
					else
					{
						photopageno = (parseInt(photopage)-1).toString();
						showPhotosCoverList(userid,photopageno);
					}
					countphoto = 0;
			},
			onFailure: function(t) {
				$('coverlist').innerHTML = t.responseText;
			}
		  }
	new Ajax.Request('/photos/showPhotoCoverList', opt);
}


function showcomments(userid,commentspage)
{
	var opt = {
			method: 'post',
			postBody: 'userid='+userid+'&albumid=' + albumidtmp + '&commentspage='+ commentspage,
			onSuccess: function(t) {
				if(t.responseText.indexOf('commentitem')!=-1  || commentpageno == 0)
				{
					commentposting = false;
					hideElement('commentloading');
					commentpageno = commentspage;
					$('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/showPhotosAlbumComment', opt);
}


function commentsubmit()
{
	if(commentposting)
	{
		$('msgErr').innerHTML = _('Posting comment. Please wait');
		showElement('infobox');
		return;
	}
	
	if(Trim($('txtcomment').value)=='')
		return;
	showElement('commentloading');
    commentposting = true;
	var ownerid = owneridtmp;

	var posttext = encodeURIComponent($('txtcomment').value);

	var opt = {
			method: 'post',
			postBody: 'albumid=' + albumidtmp + '&userid=' + ownerid + '&text=' + posttext,
			onSuccess: function(t) {
				showcomments(useridtmp,'0');
				$('txtcomment').value="";
				showrecentupdate(useridtmp);
			},
			onFailure: function(t) {
				hideElement('commentloading');
				commentposting = false;
				$('showcomments').innerHTML = t.responseTest;
			}
		  }
	new Ajax.Request('/photos/PostAlbumComment', opt);
}

function quotesubmit(qid,types,itemid,commentid)
{
	if(commentposting)
	{
		$('msgErr').innerHTML = _('Posting comment. Please wait');
		showElement('infobox');
		return;
	}
	if(Trim($(qid).value)=='')
		return;
	showElement('loading'+qid.substring(4));
    commentposting = true;
	var ownerid = owneridtmp;
	var posttext = encodeURIComponent($(qid).value);

	var posturl = '/photos/PostAlbumComment'
	var postdata = 'albumid=' + albumidtmp + '&userid=' + ownerid + '&text=' + posttext + "&commentid=" + commentid;

	if (types=="photo")
	{
		posturl = '/photo/PostPhotoComment'
		postdata = 'photoid=' + 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)
	{
		$('msgErr').innerHTML = _('Posting comment. Please wait');
		showElement('infobox');
		return;
	}
	if(Trim($(rid).value)=='')
		return;
	showElement('replyloading'+rid.substring(4));
    commentposting = true;
	var ownerid = owneridtmp;
	var posttext = encodeURIComponent($(rid).value);

	var posturl = '/photos/PostAlbumComment'
	var postdata = 'albumid=' + albumidtmp + '&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";
	}

	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 editphotoname(id)
{
      var found = false;
      if(editinplacephotoname.length >0)
      {
		for(var i =0 ;i< editinplacephotoname.length;i++)
		{
			if(editinplacephotoname[i] == id)
			{
				found = true;
				break;
			}
		}
      }

      if(!found)
      {
		editinplacephotoname[countphoto++] = id;
		new Ajax.InPlaceEditor('phototitle' + id , '/edit/' + id + '/photo/title');
      }
}

function CloseChangeCover()
{
	newcoverid=-1;
	newcover = "";
	hideElement('photo-coverlist');
}

function SaveCover()
{
	if (newcoverid==-1)
	{
		newcover = "";
		hideElement('photo-coverlist');
		return;
	}
	var opt = {
			method: 'post',
			postBody: 'albumid='+ albumidtmp +'&photoid='+ newcoverid,
			onSuccess: function(t) {
				if(t.responseText != "Cannot change album cover.")
				{
					if($('albumcover'))
						$('albumcover').src = staticpath+"/asset/photo/48x48/"+getPhotoPath(useridtmp)+"/"+t.responseText;
				}
				newcoverid = -1;
				newcover = "";
				hideElement('photo-coverlist');
			},
			onFailure: function(t) {
				$('msgErr').innerHTML = _("Cannot set cover, please try again later");
				showElement('infobox');
			}
		  }
	new Ajax.Request('/photos/setCoverAlbum', opt);
}

function delete_click(e)
{
	if(e!=null && !imgdeleting)
	{
		if(confirm(_('Do you want to delete this photo?')))
		{
			imgdeleting = true;
			showElement('loading');
			var photoid = e;
			var opt = {
				method: 'post',
				postBody: 'photoid=' + photoid,
				onSuccess: function(t) {
					imgdeleting = false;
					hideElement('loading');
					showPhotosList(useridtmp,photopageno);
				},
				onFailure: function(t) {
					imgdeleting = false;
					hideElement('loading');
					$('msgErr').innerHTML = _("Sorry, Can't delete photo. Please, try again.");
					showElement('infobox');
				}
			}
			new Ajax.Request('/photo/deletephoto', opt);
		}
	}
}

function showphotoscover()
{
	showElement('photo-coverlist');
	showPhotosCoverList(useridtmp,'0');
}

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 showdelete_edit(id,value)
{
	if(value)
	{
		showElement('delete'+id);
		if($('editprivacy_'+id))
			showElement('editprivacy_'+id);
		if($('edit_'+id))
			showElement('edit_'+id);
		if($('showmovephoto_'+id))
			showElement('showmovephoto_'+id);
	}
	else
	{
		hideElement('delete'+id);
		if($('editprivacy_'+id))
		{
			if($('privacy_'+id))
			{
				if($('hprivacy_'+id) && $('hprivacy_'+id).value == albumprivacy)
				{
					if(!$('privacy_'+id).visible())
						hideElement('editprivacy_'+id);
				}
			}
			else
				hideElement('editprivacy_'+id);
		}
		if($('edit_'+id))
			hideElement('edit_'+id);
		if($('showmovephoto_'+id))
		{
			if($('movephoto_'+id))
			{
				if(!$('movephoto_'+id).visible())
					hideElement('showmovephoto_'+id);

			}
			else
				hideElement('showmovephoto_'+id);
		}
	}
}
//]]>
