//<![CDATA[
var owneridtmp = "";
var useridtmp = "";
var ismypagetmp = "False";
var editinplacevideolatestname = new Array;
var countvideolatest = 0;
var objectselect= null;
var videopageno = 0;
var itemselectid2=-1;
var commentpageno = 0;
var videodeleting = false;
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;
					refreshcomments(useridtmp,commentpageno);
				},
				onFailure: function(t) {
					commentdeleting = false;
					alert(_("Sorry, the message cannot be deleted. Please try again"));
				}
			}
			new Ajax.Request('/videos/deleteVideosComment', 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('videolatesttitle')==0)
		{
			 var output = CutString(TitleIdList[i].innerHTML,14);
			 TitleIdList[i].innerHTML = output;
		}
	}

}


function swaptitle(itemid,reverse)
{
	if(itemid!=-1)
	{
		if(reverse)
			$('hiddentitle'+itemid).value = $('videolatesttitle'+itemid).innerHTML;
		else
			$('videolatesttitle'+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("videoimg_")==0)
			itemid2 = str.substring(9);
		else if(str.indexOf("videolatesttitle")==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("videoimg_")==0)
				itemid2 = str.substring(9);
			else if(str.indexOf("videolatesttitle")==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('videolatesttitle')==0)
		{

			var selectid = itemid.substring(16);
			if(!SearchInArray(editinplacevideolatestname,itemid))
			{
				var TitleEditor = new Ajax.InPlaceEditor(itemid,'/video/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);});
								}							}
						}
				});
				editinplacevideolatestname[countvideolatest++] = itemid;
			}
		}
	}
}


function refreshcomments(userid,commentspage)
{
	var opt = {
			method: 'post',
			postBody: 'pagesname=videos&userid=' + userid + '&commentspage='+ commentspage+'&videoid=-1' +'&categoryid=0' ,
			onSuccess: function(t) {
				if(t.responseText.indexOf('commentitem')!=-1  || commentpageno == 0)
				{
					commentposting = false;
					hideElement('commentloading');
					$('commentlist').innerHTML = t.responseText;
					commentpageno = commentspage;
				}
				else
				{
						commentpageno = (parseInt(commentspage)-1).toString();
						refreshcomments(userid,commentpageno);
				}
				ReplacePgLinkComment();			
			},
			onFailure: function(t) {
				commentposting = false;
				hideElement('commentloading');
				$('commentlist').innerHTML = t.responseTest;//'Sorry, could not load  your photos'
			}
		  }
	new Ajax.Request('/videos/showVideosComment', opt);
}
function commentsubmit()
{
	if(commentposting)
	{
		alert(_('Posting comment. Please wait'));
		return;
	}
	var ownerid = owneridtmp;
	if(Trim($('txtcomment').value)=='')
		return;	
	showElement('commentloading');
	commentposting = true;
	var posttext = encodeURIComponent($('txtcomment').value);
	var opt = {
			method: 'post',
			postBody: 'ownerid=' + useridtmp + '&userid=' + ownerid + '&text=' + posttext + '&categoryid=0',
			onSuccess: function(t) {
				refreshcomments(useridtmp,'0');
				$('txtcomment').value="";
				showrecentupdate(useridtmp);
			},
			onFailure: function(t) {
				commentposting = false;
				hideElement('commentloading');
				$('showcommentlist').innerHTML = t.responseTest;
			}
		  }
	new Ajax.Request('/videos/PostVideosComment', 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 = '/videos/PostVideosComment';
	var postdata = 'ownerid=' + useridtmp + '&userid=' + ownerid + '&text=' + posttext + '&categoryid=0' + "&commentid=" + commentid;

	if (types=="video")
	{
		posturl = '/video/PostVideoComment';
		postdata = 'videoid=' + itemid + '&text=' + posttext + '&userid=' + ownerid + "&commentid=" + commentid;
	}
	else if(types=="category")
	{
		posturl = '/videos/PostVideosComment';
		postdata = 'ownerid=' + useridtmp + '&userid=' + ownerid + '&text=' + posttext + '&categoryid=' + itemid + "&commentid=" + commentid;
	}
	
	var opt = {
			method: 'post',
			postBody: postdata,
			onSuccess: function(t) {
				refreshcomments(useridtmp,'0');
				$(qid).value="";
				showrecentupdate(useridtmp);
			},
			onFailure: function(t) {
				commentposting = false;
				hideElement('loading'+qid.substring(4));
				$('showcommentlist').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 = '/videos/PostVideosComment';
	var postdata = 'ownerid=' + useridtmp + '&userid=' + ownerid + '&text=' + posttext + '&categoryid=0' + "&commentid=" + commentid + "&isreply=True";

	if (types=="video")
	{
		posturl = '/video/PostVideoComment';
		postdata = 'videoid=' + itemid + '&text=' + posttext + '&userid=' + ownerid + "&commentid=" + commentid + "&isreply=True";
	}
	else if(types=="category")
	{
		posturl = '/videos/PostVideosComment';
		postdata = 'ownerid=' + useridtmp + '&userid=' + ownerid + '&text=' + posttext + '&categoryid=' + itemid + "&commentid=" + commentid + "&isreply=True";
	}
	
	var opt = {
			method: 'post',
			postBody: postdata,
			onSuccess: function(t) {
				refreshcomments(useridtmp,'0');
				$(rid).value="";
				showrecentupdate(useridtmp);
			},
			onFailure: function(t) {
				commentposting = false;
				hideElement('replyloading'+rid.substring(4));
				$('showcommentlist').innerHTML = t.responseTest;
			}
		  }
	new Ajax.Request(posturl, opt);
}

function editvideoname(id)
{
      var found = false;
      if(editinplacevideolatestname.length >0)
      {
			for(var i =0 ;i< editinplacevideolatestname.length;i++)
			{
				if(editinplacevideolatestname[i] == id)
				{
					found = true;
					break;
				}
			}
      }

      if(!found)
      {
		editinplacevideolatestname[countvideolateest++] = id;
		new Ajax.InPlaceEditor('videotitle' + id , '/video/edit/' + id + '/title');
      }
}

function showlatestvideos(userid,videopage)
{
	var opt = {
			method: 'post',
			postBody: 'userid=' + userid + '&videopage='+ videopage,
			onSuccess: function(t) {
					if(t.responseText.indexOf('updateitem')!=-1  || videopageno == 0)
					{
						videopageno = videopage;
						$('update').update(t.responseText);
						if(ismypagetmp == "True")
							CreateEditTitle();
						CutTitle();
					}
					else
					{
						videopageno = (parseInt(videopage)-1).toString();
						showlatestvideos(userid,videopageno);
					}
					editinplacevideolatestname = null;
					editinplacevideolatestname = new Array;
					countvideolatest = 0;
				
			},
			onFailure: function(t) {
				$('latestvideos').innerHTML = t.responseText;
			}

		  }
	new Ajax.Request('/videos/showLatestVideoList', 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 == 'videoimg_')
		{
			object.style.display = 'block';
			
				//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 delete_click(videoid)
{
	if(videoid!='' && !videodeleting)
	{	if(confirm(_('Do you want to delete this video?')))
		{
			videodeleting = true;
			showElement('loading');
			var opt = {
				method: 'post',
				postBody: 'videoid=' + videoid,
				onSuccess: function(t) {
					videodeleting = false;
					hideElement('loading');
					showlatestvideos(useridtmp,videopageno);
				},
				onFailure: function(t) {
					videodeleting = false;
					hideElement('loading');
					alert(_("Sorry! the video clip is not deleted. Please try again"));
				}
			}
			new Ajax.Request('/video/delete/', 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 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);
	}
}
//]]>
