var postid = "";
var theComments = "";
var pluginPath = "wp-content/plugins/inlineComments/";
var currPostid = "";
var currParagraph = "";
var numParagraphs = "";

function countParagraphs(id) {
	var theDiv = document.getElementById("inline_" + id);
	//theDiv.style.border = "5px solid green"; //this was a test to let me know the div was selected
	//var innerNodes = theDiv.childNodes;
	var innerNodes = theDiv.getElementsByTagName('p');
	
	numParagraphs = innerNodes.length;
	
	for(var i = 0; i<innerNodes.length; i++ ) {
		//innerNodes[i].style.border = "1px solid grey";
		innerNodes[i].className = "paragraph";
		innerNodes[i].innerHTML += add_inline_div(id,i);
	}
	

}

function add_inline_div(postid,count) {
	var div = "<div class=\"comment_here\" id=\"inline_comment_" + postid + "_" + count + "\">";

	div += "<div class=\"inline_comment_boxes\" id=\"inline_comment_boxes_" + postid + "_" + count + "\">";
	//div += countComments(postid,count); //draw the boxes
	div += "</div>";
	
	div += "<div class=\"inline_view_link\" id=\"inline_view_link_" + postid + "_" + count + "\"><a href=\"javascript:inline_view(" +  postid + "," + count + ")\" id=\"inline_view_link_" + postid + "_" + count + "\"\">view</a></div>";
	div += "<div class=\"inline_form_link\"><a href=\"javascript:inline_form(" +  postid + "," + count + ")\" id=\"inline_form_link_" + postid + "_" + count + "\"\">add comment</a></div>";

	//insert the comment form div
	div += "<div class=\"inline_comment_form\" id=\"inline_comment_form_" + postid + "_" + count + "\"  style=\"display:none;\">";
	div += build_inline_form(postid,count);
	div += "</div>";
	
	//insert the comment view div
	div += "<div class=\"inline_comment_view\" id=\"inline_comment_view_" + postid + "_" + count + "\" style=\"display:none;\">";
	div += "</div>";
	
	div += "</div><br><br>";
	
	return div;

}


function countAllParagraphComments(postid){
	//alert("counting comments for " + postid);
	for (var i=0;i<numParagraphs;i++) {
		currPostid = postid;
		countComments(postid,i);
	}
}

/* open or close the inline comment form */

function inline_form(postid,count) {
	var inline_comment_area = document.getElementById("inline_comment_" + postid + "_" + count);
	var form = document.getElementById("inline_comment_form_" + postid + "_" + count);
	var link = document.getElementById("inline_form_link_" + postid + "_" + count);
	
	//toggle the form. and change the link text
	if (form.style.display == "none") {
		form.style.display = "block";
		link.innerHTML = "close form";
		inline_comment_area.style.border = "1px solid #E3DAC8";
	} else {
		form.style.display = "none";
		link.innerHTML = "add comment";
		inline_comment_area.style.border = "";
	}
}


//return the html for the form
function build_inline_form(postid,count) {
	var form;
	form = "<h3>add your comment, then click submit</h3>";
	form += "your name: <input type=\"text\" name=\"name\" class=\"inline_form_name\" id=\"inline_form_name_" + postid + "_" + count + "\"><br>";
	form += "comments: <textarea class=\"inline_form_comments\" name=\"comments\" id=\"inline_form_comments_" + postid + "_" + count + "\"></textarea>";
	form += "<br><input type=\"button\" name=\"submit\" id=\"inline_form_submit_" + postid + "_" + count + "\" class=\"inline_form_submit\" value=\"submit\" onclick=\"saveit(" + postid + "," + count + ")\">";
	form += " <input type=\"button\" name=\"cancel\" id=\"inline_form_cancel_" + postid + "_" + count + "\" class=\"inline_form_cancel\" value=\"cancel\" onclick=\"cancelit(" + postid + ","  + count + ")\">";
	return form;
}

function inline_view(postid,count) {
	var inline_comment_area = document.getElementById("inline_comment_" + postid + "_" + count);
	var comment_viewer = document.getElementById("inline_comment_view_" + postid + "_" + count);
	var comments;
	
	if  (comment_viewer.style.display == "none") {
		comment_viewer.style.display = "block";
		getComments(postid,count);
		
		inline_comment_area.style.border = "1px solid #E3DAC8";
	} else {
		comment_viewer.style.display = "none";
		comment_viewer.innerHTML = "";
		inline_comment_area.style.border = "";
	}
	
	
}


/* clear form and close */
function cancelit(postid,count) {
	
	//clear variables
	var name = document.getElementById("inline_form_name_" + postid + "_" + count);
	var comments = document.getElementById("inline_form_comments_" + postid + "_" + count);
	
	name.value = "";
	comments.value = "";
	
	//close form
	inline_form(postid,count);
	
}

function saveit(postid,count) {
	var name = document.getElementById("inline_form_name_" + postid + "_" + count).value;
	var comments = document.getElementById("inline_form_comments_" + postid + "_" + count).value;
	
	var url = pluginPath + "addComment.php?postid=" +postid + "&paragraph=" + postid + "_" + count + "&name=" +name + "&comment=" + comments;	

	currPostid = postid;
	currParagraph = count;
	var cObj = YAHOO.util.Connect.asyncRequest('GET',url,new_comment_callback,null);
}

function getComments(postid,paragraph) {
	var url = pluginPath +'getComments.php?post_id=' + postid + '&paragraph=' + paragraph;
		currPostid = postid;
		currParagraph = paragraph;
	var cObj = YAHOO.util.Connect.asyncRequest('GET',url,comments_callback,null);
}

function countComments(postid,paragraph) {
	var url = pluginPath + 'getComments.php?post_id=' + postid + '&paragraph=' + paragraph + "&count=true";
	currPostid = postid;
	currParagraph = paragraph;	
	var cObj = YAHOO.util.Connect.asyncRequest('GET',url,countComments_callback,null);
}

//**********************************************************
//  call back functions for comment request
//**********************************************************
var comments_Success = function(o){ //transaction success case logic
	var comment_viewer = document.getElementById("inline_comment_view_" + currPostid + "_" + currParagraph);
	theComments = o.responseText;
	
	var comments = theComments.split("#@!");
		for (var i=1; i<comments.length; i++) {
		
			var comment = comments[i].split("|&|");
			var name = comment[0];
			var date = comment[1];
			var content = comment[2];
			
			comment_viewer.innerHTML += date + " <b>" + name + "</b>: " + content + "<br><br>";	
		}
}

var comments_Failure = function(o){ //transaction failure case logic 
	//alert("failure");
}

var comments_callback = 
{
	success:comments_Success,
	failure:comments_Failure
}


//**********************************************************
//  call back functions for comment count request
//**********************************************************
var countComments_Success = function(o){ //transaction success case logic
	var results = o.responseText;
	//box_holder.innerHTML = numComments;
	var tempArray = results.split("&");
	var tempPostid = tempArray[0];
	var numComments = tempArray[1];
	//alert("there are " + numComments + " comments for post " + currPostid);
	var box_holder = document.getElementById("inline_comment_boxes_"  + tempPostid + "_0");
	var view_link = document.getElementById("inline_view_link_"  + tempPostid + "_0");
	if (numComments > 0) {
	
		//display view link
		view_link.style.display = "block";
		box_holder.innerHTML = "";
		//display comment boxes
		for (var x=0;x<numComments;x++) {
			box_holder.innerHTML += "<div class=\"inline_comment_marker\"></div>\n";	
		}
	}
}

var countComments_Failure = function(o){ //transaction failure case logic 
	alert("failure");
}

var countComments_callback = 
{
	success:countComments_Success,
	failure:countComments_Failure
}

//**********************************************************
//  call back functions for new comment
//**********************************************************
var new_comment_Success = function(o){ //transaction success case logic
	var results = o.responseText;
	//alert(currPostid + " & " + currParagraph);

	inline_form(currPostid,currParagraph); //close the form
	
	//inline_view(currPostid,currParagraph); //display the comments
	countComments(currPostid,currParagraph); //recount the comments
	
}

var new_comment_Failure = function(o){ //transaction failure case logic 
	alert("failure");
}

var new_comment_callback = 
{
	success:new_comment_Success,
	failure:new_comment_Failure
}