/*
 * 
	bm_main
	used to remove all globals from bm_stories
 */

;(function($){

var $$;


$$ = $.fn.bm_comments = function($options) {

	// set default options
	var $defaults = {
		poemid   : 0,
		history : true,
		onLoad     : function() {},
	};
	
	// extend the options
	var $opts = $.extend($defaults, $options);
	
	// bring the options to the jquery object
	for (var i in $opts) {
		$.bm_comments[i]  = $opts[i];
	}
};


$$.onPageLoad = function() {	
	alert("in onPageLoad");
}

$.extend({bm_comments : {
	COMMENTS_INSERT_URL : "php/commentsInsert.php",
	COMMENTS_GET_URL : "php/commentsGet.php",
	COMMENTS_ADD_BUTTON : "Add Comment",
	COMMENTS_SUBMIT_BUTTON : "Submit Comment",
	COMMENTS_MINIMUM_LENGTH : 3,
	COMMENTS_TAB_MENU : ".comments-tab-menu",
	COMMENTS_TAB_CONTAINER : "#comments-tab-container .comments-content",
	commentsEventHandler :function()
	{
		var e = document.getElementById('comments-add-button');
		e.onmouseup = function() 
		{
			$.bm_comments.commentsAdd();
		}
		e = document.getElementById('your-comments');
		e.onfocus = function() {$.bm_comments.commentsOnFocus();}
		//e.onblur = function() {commentsOnBlur();}
	},
	commentsOnFocus : function()
	{
		document.getElementById("your-comments").select();
		//$.bm_main.current_edit_mode = $.bm_main.EDITING_COMMENTS;
		$("#blind3").css({backgroundColor: $.bm_ui.TEMPLATE_COLOR});
		//NB Non-generic Hardwired reference
		$("#blind3").css({color:$.bm_ui.BOX_COLOR_DARK});

	},
	commmentsInsert : function(psComments)
	{
		try
		{
			var sParams = "?login_name="+escape(document.getElementById("username").value)+"&user_key="+$.bm_login.current_user_key+"&comments="+escape(psComments)+"&story_id="+$.colflow.bookid+"&chapter_id="+$.colflow.chapterid;
			alert("in comments insert "+sParams);
			httpRequest.open("GET", $.bm_comments.COMMENTS_INSERT_URL+sParams, true);
			httpRequest.onreadystatechange = $.bm_comments.commentsInsertHttpResponse;
			httpRequest.send(null);	
		} catch(err)
		{
			errorHandler("commentsInsert",err);
			$.bm_login.login_in_process = false;
		}
	},
	commentsInsertHttpResponse : function()
	{
		try
		{
			var sResults = httpRequest.responseText;
			var sResultsArray;
			var e;
			if (httpRequest.readyState == 4) 
			{
				sResultsArray = sResults.split(",");
				if (sResultsArray[0] == SUCCESS)
				{
					$.bm_comments.commentsGet($.colflow.bookid,$.colflow.chapterid);
					$.bm_comments.commentsShowExistingHideNew();
				} else
				{
					//alert("No comments were returned");
				}
			}		
		} catch(err)
		{
			errorHandler("commentsInsertHttpResponse",err);
		}
	},
	commentsGet : function(piCurrentStory,piCurrentChapter)
	{
		try
		{
			//alert("in comments get");
			var sParams = "?login_name="+escape(document.getElementById("username").value)+"&user_key="+$.bm_login.current_user_key+"&story_id="+piCurrentStory+"&chapter_id="+piCurrentChapter;

			httpRequest.open("GET", $.bm_comments.COMMENTS_GET_URL+sParams, true);
			httpRequest.onreadystatechange = $.bm_comments.commentsGetHttpResponse;
			httpRequest.send(null);	
		} catch(err)
		{
			errorHandler("commentsGet",err);
		}
	},
	commentsGetHttpResponse : function()
	{
		try
		{
			if (httpRequest.readyState == 4) 
			{
				var sResults = httpRequest.responseText;
				var sResultsArray;
				var e;
				sResultsArray = sResults.split(",");
				if (sResultsArray[0] == SUCCESS)
				{
					// Return message structure
					// It'd be nice to get either an associative array, or something already structured for html.
					// I'm not certain an associative array can be passed from php to javascript
					/*
					row-id (programatically generated for purposes of formating)
					evenOdd,
					C.comments,
					A.user_key, 
					A.login_name,
					A.city,
					C.reply_to,
					*/
					//alert("commentsGetHttpResponse: The number of elements returned is "+sResultsArray.length+" -"+sResults);
					$.bm_comments.commentsRemoveAndAddNodes(true,"comments-root",sResultsArray);
					//commentsShowExistingHideNew();
				} else
				{
					$.bm_comments.commentsRemoveAndAddNodes(false,"comments-root",sResultsArray);
				}
			}
		} catch(err)
		{
			errorHandler("commentsGetHttpResponse",err);
		}
	},
 	commentsAdd : function()
	{
		try
		{
			// NB Brian - this process is triggered off of the value of the add button
			if (document.getElementById("comments-add-button").value==$.bm_comments.COMMENTS_ADD_BUTTON)
			{
				$.bm_comments.commentsShowNewHideExisting();
			} else if (document.getElementById("comments-add-button").value==$.bm_comments.COMMENTS_SUBMIT_BUTTON)
			{
				var sCommentsText = document.getElementById("your-comments").value;
				if (sCommentsText.length>=$.bm_comments.COMMENTS_MINIMUM_LENGTH)
				{
					$.bm_comments.commmentsInsert(sCommentsText);
				} else
				{
					alert("please enter at least "+$.bm_comments.COMMENTS_MINIMUM_LENGTH+" characters.");
				}
			}		
		} catch(err)
		{
			errorHandler("comments Add",err);
		}
	},
	commentsShowNewHideExisting : function()
	{
		try
		{
			switchCommentsTab("#div-add-comment a");
			return;
			document.getElementById("your-comments").style.display="block";
			document.getElementById("comments-add-button").value=$.bm_comments.COMMENTS_SUBMIT_BUTTON;
			document.getElementById("your-comments").focus();		
		} catch(err)
		{
			errorHandler("commentsShowNewHideExisting",err);
		}
	},
	commentsShowExistingHideNew : function()
	{
		try
		{
			$.bm_comments.switchCommentsTab("#div-view-comments a");
		} catch(err)
		{ 
			errorHandler("CommentsShowExistingHideNew",err);
		}
	},
	commentsTabOnMouseUp : function(e)
	{
		$.bm_comments.switchCommentsTab(e);
	},
	switchCommentsTab : function(e)
	{
		$($.bm_comments.COMMENTS_TAB_MENU+" li.selected").removeClass("selected");
		$(e).parent().addClass("selected");

		var iIndex=$($.bm_comments.COMMENTS_TAB_MENU+" li").index($(e).parent());

		$($.bm_comments.COMMENTS_TAB_CONTAINER+".selected").removeClass("selected");
		$($.bm_comments.COMMENTS_TAB_CONTAINER).eq(iIndex).addClass("selected");	
	},
	commentsRemoveAndAddNodes : function(pbRemoveOnlyFlag,psElementId,psCommentsArray)
	{
		// commentsRemoveAndAddNodes deletes the paragraphs currently being displayed and replaces them
		// with the nodes from the next or previous page. 
		// An alternate architecture involves drawing all columns up front and simply hiding and displaying.
		// This version is lighter and quite fast.	// Note: when the last page is on the left hand column
		// piRemoveOnlyFlag is set to 1 so that the right hand column is erased.
		try
		{
			//alert(psElementId);
			//var eNewParagraph; 
			//var eOldParagraph;
			var bFirstTime=true;
			var eComment = document.getElementById(psElementId);
			var j; //counter


			if (eComment.hasChildNodes() )
			{
			    while (eComment.childNodes.length >= 1 )
			    {
			        eComment.removeChild(eComment.firstChild );       
			    } 
			}
			if (!pbRemoveOnlyFlag){return;}
			// When pbRemoveOnlyFlag is true, then the contents of the array aren't populated.
			//alert(psCommentsArray[2]+" "+psCommentsArray[3]+" "+psCommentsArray[4]+" "+psCommentsArray[5]+" "+psCommentsArray[6]+" "+psCommentsArray[7]);
			for (j=2;j<psCommentsArray.length-1;j++)
			{ 	
				//alert(psCommentsArray[j]+psCommentsArray[j+1])
				var eNewCommentContainer; 
				var sId;
				eNewCommentContainer = document.createElement("div");
				sId = "container-"+psCommentsArray[j];
				eNewCommentContainer.setAttribute('id',sId);//psCommentsArray[j]
				eNewCommentContainer.setAttribute('class','comments-container');
				eNewCommentContainer.setAttribute('className','comments-container');
				$("#"+psElementId).append(eNewCommentContainer);

				var eNewComment = document.createElement("div");
				eNewComment.setAttribute('id',psCommentsArray[j]);//psCommentsArray[j]
				j++;
				eNewComment.setAttribute('class','comments-text');
				eNewComment.setAttribute('className','comments-text');

				if (bFirstTime)
				{
					//eNewParagraph.style.marginTop = gasTopMargin[piPage];
					bFirstTime = false;
				}
				j++;
				eNewComment.innerHTML = psCommentsArray[j];
				$("#"+sId).append($(eNewComment));

				//document.getElementById(psElementId).appendChild(eNewComment);
				j++;
				eNewCommentContainer = document.createElement("div");
				eNewCommentContainer.setAttribute('id',"container-"+psCommentsArray[j]);//psCommentsArray[j]

				eNewComment = document.createElement("div");
				eNewComment.setAttribute('id','comments-user-key-'+j);//psCommentsArray[j]
				eNewComment.setAttribute('class','comments-invisible');
				eNewComment.setAttribute('class-name','comments-invisible');
				eNewComment.innerHTML = psCommentsArray[j];
				//document.getElementById(psElementId).appendChild(eNewComment);			
				$("#"+sId).append($(eNewComment));
				j++;
				eNewCommentContainer = document.createElement("div");
				eNewCommentContainer.setAttribute('id',"container-"+psCommentsArray[j]);//psCommentsArray[j]

				eNewComment = document.createElement("div");
				eNewComment.setAttribute('id','comments-username'+j);//psCommentsArray[j]
				eNewComment.setAttribute('class','comments-username');
				eNewComment.setAttribute('class-name','comments-username');
				eNewComment.innerHTML = psCommentsArray[j];
				$("#"+sId).append($(eNewComment));

				//document.getElementById(psElementId).appendChild(eNewComment);
				j++;
				eNewComment = document.createElement("div");
				eNewComment.setAttribute('id','comments-city-'+j);//psCommentsArray[j]
				eNewComment.setAttribute('class','comments-city');
				eNewComment.setAttribute('classname','comments-city');
				eNewComment.innerHTML = psCommentsArray[j];
				//document.getElementById(psElementId).appendChild(eNewComment);
				$("#"+sId).append($(eNewComment));
				j++;
				eNewComment = document.createElement("div");
				eNewComment.setAttribute('id','comments-reply-to'+j);//psCommentsArray[j]
				eNewComment.setAttribute('class','comments-invisible');
				eNewComment.setAttribute('classname','comments-invisible');
				eNewComment.innerHTML = psCommentsArray[j];
				$("#"+sId).append($(eNewComment));
				//document.getElementById(psElementId).appendChild(eNewCommentContainer);
			}
		}
		catch(err)
		{
			errorHandler("commentsRemoveAndAddNodes",err);

		}
		//document.getElementById("comments-title").style.display="block";
	}
}
})

})(jQuery);

