// JavaScript Document
function LTrim(str){
    if (str==null){return null;}
    for(var i=0;str.charAt(i)==" ";i++);
    return str.substring(i,str.length);
}
function RTrim(str){
    if (str==null){return null;}
    for(var i=str.length-1;str.charAt(i)==" ";i--);
    return str.substring(0,i+1);
}
function Trim(str){
    return LTrim(RTrim(str));
}
function isEmpty(obj) { for(var i in obj) { return false; } return true; }
function clearvcomment() {
	var each = $(".eachcomment_video").get();
				for (var i=0;i<each.length;i++) {
					var ele = each[i];
					if ((i+1)!=each.length) {
						$(ele).remove();
					}
				}
}
function loadvideo(id) {
	$.ajax({
  		type: 'POST',
  		url: 'video.php',
  		data: "id=" + id,
  		success: function(json){
			myObject = JSON.parse(json);
			myCode = myObject.code;
			if (myCode.length==11) {
				swfobject.embedSWF("http://www.youtube.com/v/"+myCode+"&hl=en_US&fs=1&", "video_content", "425", "344", "9.0.0", "flash/expressInstall.swf", flashvars, params, attributes);
			} else {
				swfobject.embedSWF("http://vimeo.com/moogaloop.swf?clip_id="+myCode+"&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1", "video_content", "425", "344", "9.0.0", "flash/expressInstall.swf", flashvars, params, attributes);
			}
			var title = document.createElement("H3");
			title.innerHTML = myObject.title;
			
			var info = document.createElement("p");
			info.innerHTML = myObject.info;
			
			$("#video_detail").empty();
			$("#video_detail").append(title);
			$("#video_detail").append(info);
		}
	});
}
function loadvideocomment(id) {
	$.ajax({
   		type: "POST",
   		url: "Forums/vcomment.php",
   		data: "id="+id,
   		success: function(msg){
			commentObj = JSON.parse(msg);
			clearvcomment();
			if(commentObj.length>0){
				var comment = "";
				for (var i=0;i<commentObj.length;i++) {
					var eachcomment_video = document.createElement("div");
					eachcomment_video.className = "eachcomment_video";
							
					comment = '<div class="comment_wapper"><span class="comment_no">' + commentObj[i].no + commentObj[i].del + '</span><p>';
					comment += commentObj[i].text +'</p><span class="comment_date">' + commentObj[i].date + '</span><span class="comment_user">';
					comment += 'by <strong>' + commentObj[i].user + '</strong></span></div>';
					
					eachcomment_video.innerHTML = comment;
					$(eachcomment_video).insertBefore($(".eachcomment_video:last-child"));
				}
				
				$(".eachcomment_video:last-child").prev().find(".delc").bind("click",function(){
					var ins = $(this).get(0);
					$.ajax({
						type: "POST",
						url: "Forums/dcomment.php",
						data: "id="+$(this).attr("rel"),
						success: function(dcmsg){
										resultObj = eval('(' + dcmsg + ')');
										if (resultObj.result) {
											$(ins.parentNode.parentNode.parentNode).remove();
											arrange_video();
										}
									}
					});  
				});
				arrange_video();
			}
		}
 	});
}
function loadvideoform(id) {
	$.ajax({
		url: "Forums/dialog.php",
		success: function(dMsg){
			dialogObj = JSON.parse(dMsg);
			if (dialogObj.status=="guest") {
				form = '<div class="comment_form" style="text-align:center; display:block; font-size:13px">';
				form += '<p style="padding:10px 0 0 0">Please login before comment.</p></div>';
			} else {
				form = '<div class="comment_form"><span>Name : <strong class="username">'+dialogObj.username;
				form += '</strong></span><span>Comment</span><textarea name="txtvComment" id="txtvComment"></textarea>';
				form += '<a href="JavaScript:void(0);" id="btnvComment">Add comment</a></div>';
			}

			$(".eachcomment_video:last-child").html(form);
			arrange_video();
							
			$("#btnvComment").bind("click",function(){
				var no = $(".eachcomment_video").get().length;
				var username = dialogObj.username;
				var txtcomment = $("#txtvComment").val();
				$("#txtvComment").val("");
				if (Trim(txtcomment).length != 0) {
					$.ajax({
						type: "POST",
						url: "Forums/vpost.php",
						data: "id="+id+"&no="+no+"&username="+username+"&comment="+txtcomment,
						success: function(cmsg){
							newObj = JSON.parse(cmsg);
							if (!isEmpty(newObj)) {
								var eachcomment = document.createElement("div");
								eachcomment.className = "eachcomment_video";
							
								comment = '<div class="comment_wapper"><span class="comment_no">' + newObj.no + newObj.del + '</span><p>';
								comment += newObj.text +'</p><span class="comment_date">' + newObj.date + '</span><span class="comment_user">';
								comment += 'by <strong>' + newObj.user + '</strong></span></div>';
								eachcomment.innerHTML = comment;
								$(eachcomment).insertBefore($(".eachcomment_video:last-child"));
								
								$(".eachcomment_video:last-child").prev().find(".delc").bind("click",function(){
									var ins = $(this).get(0);														
									$.ajax({
									type: "POST",
									url: "Forums/dcomment.php",
									data: "id="+$(this).attr("rel"),
									success: function(smsg){
												resultObj = eval('(' + smsg + ')');
												if (resultObj.result) {
													$(ins.parentNode.parentNode.parentNode).remove();
													arrange_video();
												}
											}
									});  
								});
								arrange_video();
							}
						}
					});
				}
			});
		}
	});
}
