//<![CDATA[
var useridtmp = "";
var ownerid = "";
var pageno = 0;
var editinplacephotopname = new Array;
var countphotop = 0;
var editinplacevideopname = new Array;
var countvideop = 0;
var ismypagetmp = "False";

var commentpageno = 0;
var commentdeleting = false;
var commentposting = false

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;
					alert(_("Sorry, the message cannot be deleted. Please try again"));
				}
			}
			new Ajax.Request('/profiles/deleteProfilesComment', 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('photoptitle')==0 || itemid.indexOf('videoptitle')==0)
		{
			 var output = CutString(TitleIdList[i].innerHTML,14);
			 TitleIdList[i].innerHTML = output;
		}
	}

}

function swaptitle(itemid,reverse,page)
{
	if(itemid != -1)
	{
		if(page == 'photop')
		{
			if(reverse)
				$('hiddenphotoptitle'+itemid).value = $("photoptitle"+itemid).innerHTML;
			else
				$("photoptitle"+itemid).innerHTML = $('hiddenphotoptitle'+itemid).value;
		}
		else if(page == 'videop')
		{
			if(reverse)
			    $('hiddenvideoptitle'+itemid).value = $("videoptitle"+itemid).innerHTML;
			else
				$("videoptitle"+itemid).innerHTML = $('hiddenvideoptitle'+itemid).value;
		}
	}
}

function SearchInArray(array,value)
{
	for(var i = 0;i<array.length;i++)
	{
		if(array[i]==value)
			return true;
	}
	return false;
}

function CreateEditTitle()
{
	var TitleIdList = document.getElementsByTagName('h3');
	for(var i=0;i < TitleIdList.length;i++)
	{	var itemid = TitleIdList[i].id
		if(itemid.indexOf('photoptitle')==0 || itemid.indexOf('videoptitle')==0)
		{
		   var selectid = itemid.substring(11);
		   var src = '/photo/edit/'+selectid+'/title';
		   var found = false;
			if (itemid.indexOf('videoptitle')==0)
			{
				src = '/video/edit/'+selectid+'/title';
				found = SearchInArray(editinplacevideopname,itemid);
			}
			else
			{
				found = SearchInArray(editinplacephotopname,itemid);
			}
			
			if(!found)
			{
				//alert("OK");
				var TitleEditor = new Ajax.InPlaceEditor(itemid,src,
				{
					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(11);
							addEvent($('item'+itemid2), 'mouseover', function(){hideElement('delete'+itemid2);hideElement('edit_'+itemid2);});
						},
						onLeaveEditMode:function(t)
						{
							if($(t.element.id))
							{
								var itemid2 = t.element.id.substring(11);
							    if(t.element.id.indexOf('photoptitle')==0)
									swaptitle(itemid2,true,'photop');
								else if(t.element.id.indexOf('videoptitle')==0)
									swaptitle(itemid2,true,'videop');
								if(Trim($(t.element.id).innerHTML) != '')
									$(t.element.id).innerHTML = CutString($(t.element.id).innerHTML,14);
								else
									$(t.element.id).innerHTML = "Edit title here";
								addEvent($('item'+itemid2), 'mouseover', function(){showElement('delete'+itemid2);showElement('edit_'+itemid2);});
							}
						}
				});
				if (itemid.indexOf('videoptitle')==0)
				{
					editinplacevideopname[countphotop++] = itemid;
				}
				else
				{
					editinplacephotopname[countvideop++] = itemid;
				}
			}
		}
	}
}

function showActivityList(userid,contentpage)
{	
	var opt = {
			method: 'post',
			postBody: 'userid=' + userid +'&contentpage='+ contentpage,
			onSuccess: function(t) {
					if(t.responseText.indexOf('updateitem')!=-1  || pageno == 0)
					{
						pageno = contentpage;
						var update = $('update');
						update.update(t.responseText);
						if(ismypagetmp == "True")
						{
							CreateEditTitle();
						}
						CutTitle();
					}
					else
					{
						pageno = (parseInt(contentpage)-1).toString();
						showActivityList(userid,pageno);
					}
					editinplacevideopname = null;
					editinplacevideopname = new Array;
					countphotop = 0;
					editinplacephotopname = null;
					editinplacephotopname = new Array;
					countphotop=0;
					countphotolatest = 0;
			},
			onFailure: function(t) {
				$('update').innerHTML = t.responseText;
			}
		  }
	new Ajax.Request('/profiles/showActivityList', 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;
					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('/profiles/showComment', opt);
}
function commentsubmit()
{
	if(commentposting)
	{
		alert(_("Posting comment. Please wait"));
		return;
	}
	if(Trim($('txtcomment').value)=='')
		return;	
	showElement('commentloading');
    commentposting = true;
	var ownerid = owneridtmp;
	var posttext = encodeURIComponent($('txtcomment').value);
	var opt = {
			method: 'post',
			postBody: 'ownerid=' + useridtmp + '&userid=' + ownerid + '&text=' + posttext,
			onSuccess: function(t) {
				showcomments(useridtmp,'0');
				$('txtcomment').value="";
				showrecentupdate(useridtmp);
			},
			onFailure: function(t) {
				hideElement('commentloading');
				commentposting = false;
				$('showcommentlist').innerHTML = t.responseTest;
			}
		  }
	new Ajax.Request('/profiles/postComment', 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 opt = {
			method: 'post',
			postBody: 'ownerid=' + useridtmp + '&userid=' + ownerid + '&text=' + posttext + '&commentid=' + commentid,
			onSuccess: function(t) {
				showcomments(useridtmp,'0');
				$(qid).value="";
				showrecentupdate(useridtmp);
			},
			onFailure: function(t) {
				commentposting = false;
				hideElement('loading'+qid.substring(4));
				$('showcommentlist').innerHTML = t.responseTest;
			}
		  }
	new Ajax.Request('/profiles/postComment', 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 opt = {
			method: 'post',
			postBody: 'ownerid=' + useridtmp + '&userid=' + ownerid + '&text=' + posttext + '&commentid=' + commentid + "&isreply=True",
			onSuccess: function(t) {
				showcomments(useridtmp,'0');
				$(rid).value="";
				showrecentupdate(useridtmp);
			},
			onFailure: function(t) {
				commentposting = false;
				hideElement('replyloading'+rid.substring(4));
				$('showcommentlist').innerHTML = t.responseTest;
			}
		  }
	new Ajax.Request('/profiles/postComment', opt);
}

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 loadphotoview(userid,contentpage){
	if (!contentpage)
	{
		contentpage = 0;
	}
	var posturl = "/profiles/getallphotoview/" + ourid + "/" + contentpage;
	var opt = {
			method: 'post',
			onSuccess: function(t) {
				$('photoavatarbox').update(t.responseText);
				hideElement('profileedit');
				showElement('viewphotosetavatar');
			},
			onFailure: function(t) {
				$('infotext').innerHTML = "Can't load photo.";
				showElement('infobox');
				$('infobox').focus();	
			}
		  }
	new Ajax.Request(posturl, opt);
}

function showAvatarcrop(id,photofilename){
	 showElement('avatarcropper');
	 photoidtmp = id;
	 var imgsrc = staticpath+'/asset/photo/medium/'+getPhotoPath(ourid)+'/'+ photofilename;
	 hideElement('viewphotosetavatar');
	 hideElement('profileedit');
	 setCropAvatar(imgsrc);
}

// set crop avatar
function setCropAvatar(src){
    var wrapper = $("testWrap");
    wrapper.update("");
    var imgCrop = new Image();
    imgCrop.id = 'testImage';
    imgCrop.src = src;
    imgCrop.onload = function(){
        isInitCropper = undefined;
        wrapper.update(this)
	initCropper(150,300);
    };
}

//addTopFriend
function addTopFriend(){
	var opt = {
			method: 'post',
			onSuccess: function(t) {
				if (t.responseText == "OK") {
					hideElement("addtopfriend_button");
					$("infotext1").innerHTML = _("add top friend completed.");
					showElement("infobox1");
				}
				else 
				{
					$("infotext1").innerHTML = _("Can't add top friend.");
					showElement("infobox1");
				}
			},
			onFailure: function(t) {
				$("infotext1").innerHTML = _("Can't add top friend. Please, try again.");
				showElement("infobox1");
			}
		  }
	new Ajax.Request('/friends/addtopfriends/'+ friend, opt);
	
}

function checkimg(){
    var avatar_file = document.form1.avatar_file.value.toLowerCase();
    if (avatar_file.indexOf(".png")>-1 || avatar_file.indexOf(".jpg")>-1 || avatar_file.indexOf(".gif")>-1){
        if($('avatar_file').files){
            var filesize = $('avatar_file').files.item(0).fileSize;
            if(filesize < 512000){
                $('avatar_file_result2').innerHTML = _('The accepted file types are jpg, png and gif. The file size does not exceed')+" 500KB";
                $("avatar_file_result2").style.color='#666666';
                return true;
            }else{
                $('avatar_file_result2').innerHTML = _("The uploaded picture's size is exceed");
                $("avatar_file_result2").style.color='#FF0000';
                return false;
            }
        }else{
            return true;
        }
    }else{
        $('avatar_file_result2').innerHTML = _('Image filetype must be .jpg, .png or .gif');
        $("avatar_file_result2").style.color='#FF0000';
        return false;
    }
}
//]]>