﻿
$(document).ready(function() {

	    $('a.comment_video').bind('click', toggleContentVideo);
        $('a.comment_image').bind('click', toggleContentImage);
        	}
);
function getPagedResults(resultType)
{
   var activityDiv=document.getElementById("insertContent");
   window.location.href="#topComment";
   //$("#commentList").slideUp(600);
 
   var urlToSend='/ajax/addNewComment.aspx?add=1';
   var parentID =  $("input[@id*=hdnParentID]").val();
   var currentPage = "";
   if(resultType=="previous")
   {
     var amountToSubtract=2;
     if($("div[@id*=divNext]").length == 0)
     {
        amountToSubtract =  1;
     }
     currentPage =  $("input[@id*=hdnCurrentPage]").val() - amountToSubtract;
   }
   else
   {
     currentPage =  $("input[@id*=hdnCurrentPage]").val();
   }
   var commentType = $("input[@id*=hndCommentType]").val();
   activityDiv.innerHTML = "<div align='center' style='margin-top:100px;'><img align='center' src='/images/indicator.gif' /></div>";
   $.ajax({
       type: "POST",
       dataType: "xml",
       url: urlToSend,
       data: "pagedComments=1&parentID=" + parentID + "&commentType=" + commentType + "&currentPage=" + currentPage,
       success: function (msg) {

           var bigDescription = "";
           var xmlDescription = "";

           var item_message_nodes = msg.getElementsByTagName("item");
           
           if (item_message_nodes.length > 0) {

               var n_messages = item_message_nodes.length;
               var message_nodes = "";
               message_nodes = item_message_nodes[0].getElementsByTagName("description");

               for (j = 0; j < message_nodes[0].childNodes.length; j++) {
                   xmlDescription = message_nodes[0].childNodes[j].nodeValue;
                   xmlDescription = xmlDescription.replace(/&lt;/g, '<').replace(/&gt;/g, ">").replace(/&amp;/g, "&");
                   bigDescription += xmlDescription;
               }

               activityDiv.innerHTML = bigDescription;
               $("#commentList").slideDown(600);
           }
       }
   });
	
	return false;
}
function addComment()
{
  
     var wallPic=$("input[@id*=wallPic]").attr("id");
     var wallPicValue=$("input[@id*=wallPic]").val();
     var wallPicValueDB = "";
     
     var urlToSend='/ajax/addNewComment.aspx?add=1';
     document.getElementById("loadingDiv").style.display="block";
	 if(wallPicValue!="")
	 {
	    $.ajaxFileUpload
        (
            {
                url:urlToSend,
                secureuri:false,
                fileElementId:wallPic,
                dataType: 'json',
                success: function (data, status)
                {  
                    wallPicValueDB=data.msg;
                    postComment(wallPicValueDB);
                },
                error: function (data, status, e) {
                    alert(e);
                }
            }
        )
	 }
	 else
	 {
	    postComment("");
	 }
	 return false;
	// 
}

function postComment(wallPicValueDB)
{
     var postData="";
     var parentID =  $("input[@id*=hdnParentID]").val();
     var commentType = $("input[@id*=hndCommentType]").val();
     var comment = encodeURIComponent($("textarea[@id*=txtComment]").val());
     
     var feedUrl="/ajax/addNewComment.aspx";
     postData="comment="+comment+"&commentType="+commentType+"&parentID="+parentID;
     
     var videoUrl="";
     var imageUrl="";
   
     videoUrl=$("textarea[@id*=videoUrl]").val();
	 imageUrl=$("input[@id*=imageUrl]").val();
	 user=$("input[@id*=hdnUsernameOwner]").val();
     if(videoUrl!="")
     {
        postData+="&videoUrl="+encodeURIComponent(videoUrl);
     }
     if(imageUrl!="")
     {
        postData+="&imageUrl="+encodeURIComponent(imageUrl);
     }
     if(wallPicValueDB!="")
     {
        postData+="&wallPic="+encodeURIComponent(wallPicValueDB);
     }

     $.ajax({
         type: "POST",
         url: feedUrl,
         data: postData,
         success: function(msg) {
             document.getElementById("loadingDiv").style.display = "none";
             var commentDiv = document.getElementById("commentList");
             var bigDescription = "";
             var resultDiv = document.getElementById("resultDiv");
             var message_nodes = msg.getElementsByTagName("comment");
             var n_messages = message_nodes.length;
             var description = "";
             if (n_messages > 0) {
                 description = message_nodes[0].firstChild.data;
                 if (description != "") {
                     resultDiv.className = "success";
                     resultDiv.style.display = "block";
                     resultDiv.innerHTML = "Comment posted successfully";
                     $("#resultDiv").fadeOut(6000);
                     $("textarea[@id*=videoUrl]").val("");
                     $("textarea[@id*=txtComment]").val("");
                     $("input[@id*=imageUrl]").val("");
                     $("input[@id*=wallPic]").val("");
                 }

                 bigDescription += description;
                 commentDiv.innerHTML = bigDescription + commentDiv.innerHTML;
                 //window.location="#more";
                 pulish_to_FB();
             }
             else {
                 var errorNode = msg.getElementsByTagName("error");
                 if (errorNode[0].firstChild.data != "") {
                     resultDiv.style.display = "block";
                     resultDiv.innerHTML = errorNode[0].firstChild.data;
                     resultDiv.className = "error";
                     $("#resultDiv").fadeOut(10000);
                 }

             }
         }
     });
     
	 return false;
	// 
}
function deleteComment(commentID,user)
{
    if (confirm("Are you sure you want to remove this comment?"))
     {
	      var feedUrl="/ajax/addNewComment.aspx?del=1";
	      var postData="del=1&user="+user+"&commentID="+commentID;
    	  
	    $.ajax({
	       type: "POST",
	       url: feedUrl,
	       data: postData,
	       success: function(msg)
	       {
	            var bigDescription="";
	            var resultDiv = document.getElementById("resultDiv");
    	        
	            var message_nodes = msg.getElementsByTagName("comment");
                var n_messages = message_nodes.length;
                var description="";
                if(n_messages > 0)
                {
    	     
                  description = message_nodes[0].firstChild.data;
                  if(description != "")
                   {
                      var tagLI=document.getElementById("commentLI"+commentID);
                      tagLI.parentNode.removeChild(tagLI);
                          
                      resultDiv.className="success";
                      resultDiv.style.display="block";
                      resultDiv.innerHTML = "Comment deleted successfully";
                       $("#resultDiv").fadeOut(6000);
                   }
	           }
	           else
	           {
	             var errorNode=msg.getElementsByTagName("error");
	             if(errorNode[0].firstChild.data != "")
	             {
	               resultDiv.style.display="block";
                   resultDiv.innerHTML = errorNode[0].firstChild.data;
                   resultDiv.className="error";
                   $("#resultDiv").fadeOut(10000);
	             }
    	       
	           }
	       } 
	     });
    	

    }
    else
    {

    }
}
var toggleContentVideo = function(e)
{
	var targetContent = $('div.comment_video_popup');
	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(300);
		$(this).html('Hide Video Attach');
	} else {
		targetContent.slideUp(300);
		$(this).html('Attach Video');
		$('div.comment_video_form textarea').attr("value","");
	}
	return false;
};

var toggleContentImage = function(e)
{
	var targetContent = $('div.comment_image_popup');
	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(300);
		$(this).html('Hide Image Attach');
	} else {
		targetContent.slideUp(300);
		$(this).html('Attach Image');
		$('div.comment_image_form input').attr("value","");
	}
	return false;
};
