
// Copyright (c) NimbleCat, 2008

// make sure that the required includes are there
 if (typeof NC == 'undefined') {
       alert("conversationdialog.js requires the NC JavaScript framework");
 }
 
 if (typeof NC.widget == 'undefined') {
       alert("conversationdialog.js requires the NC JavaScript framework");
 }

 NC.widget.ConversationDialog = function(sourceUrl, parameters, targetDiv, targetFormName, onShowFn, onHideFn) {
	var fnSuperClass = NC.widget.ConversationDialog.superclass.constructor;
	fnSuperClass.call( this, sourceUrl, parameters, targetDiv, targetFormName, true);
	this.setShowFn( onShowFn);
	this.setHideFn( onHideFn);
 }
 
 NC.widget.ConversationDialog.scrollToEnd = function() {
		var objDiv = document.getElementById("ConversationContainer");
		if (objDiv != null) {
			objDiv.scrollTop = objDiv.scrollHeight;
		}
 	}
 
 
 NC.lang.extend(NC.widget.ConversationDialog, NC.widget.ModalDialog, {
 	post: function() {
 		var element = document.getElementById(this.target);
 		var descendants = NC.util.Dom.descendants(element);
 		for (var i = 0; i < descendants.length; ++i) {   
 			var item = descendants[i];   
			if (item.name == this.targetFormName) {
				var parms = NC.util.Dom.serializeForm(item);
				/*
				var callback =
				{
	  				success:function( o) {
	  					alert( o.responseText);
	  				},
	  				failure:function( o) {
	  				},
					scope: this    				
				};
				var connection = new NC.util.AjaxConnection( 'GET', updateUrl, null);
				connection.executeMethod( callback);
				* */
				var updateUrl = this.sourceUrl +'?' + parms;
				var updater = new NC.util.AjaxDialogDisplayExtended( 'GET', updateUrl, this.target,  this.target, null,
									NC.widget.ConversationDialog.scrollToEnd);
				updater.doUpdate( true);
				/*
				var request = new Ajax.Updater(
					this.target,
					this.sourceUrl, 
					{
						method: 'get', 
						parameters: parms,
						evalScripts: true
					}
				);
				*/
				break;
			}
 		}
 	}
 	
 }
 );
 
 (function () {

 NC.widget.ConversationManager = function() {
	/* private variables and methods */
	var conversationDialog = null;
	var conversationContent = null;
 	
 	return {
 		/* public methods */
 		
		showConversationPopup: function ( event, contentType, detailId, title)
		{
			var pars = 'ContentType=' + contentType;
			pars += '&DetailId=' + detailId;
			pars += '&Title=' + title;
			pars += '&rand=' + new Date().getTime();
			pars = encodeURI( pars);
			this.dialog = new NC.widget.ConversationDialog(
			// sourceUrl, parameters, targetDiv, targetForm
				'showConversations.php', 
				pars, 
				'NCConversation', 
				'ShowConversationsForm'	, this.onShow, this.onHide);
			conversationDialog = this.dialog;	
	 		// position thie dialog
	 		var widthX = 300;
	 		var heightY = 325;
	 		var leftX = (NC.util.Dom.getViewportWidth() - widthX)/2 ;
	 		var topY = (NC.util.Dom.getViewportHeight() - heightY)/2;
//			this.dialog.show( event.clientX, Math.max(150, event.clientY - 150), 300);	
			this.dialog.show(leftX, topY, widthX);
			NC.widget.ConversationDialog.scrollToEnd();	
		},
		
 		/* public methods */
		showConversationPopupById: function ( event, conversationId, title)
		{
			var pars = 'ConversationId=' + conversationId;
			pars += '&Title=' + title;
			pars += '&rand=' + new Date().getTime();
			pars = encodeURI( pars);
			this.dialog = new NC.widget.ConversationDialog(
			// sourceUrl, parameters, targetDiv, targetForm
				'showConversations.php', 
				pars, 
				'NCConversation', 
				'ShowConversationsForm'	, this.onShow, this.onHide);
			conversationDialog = this.dialog;	
	 		// position thie dialog
	 		var widthX = 300;
	 		var heightY = 325;
	 		var leftX = (NC.util.Dom.getViewportWidth() - widthX)/2 ;
	 		var topY = (NC.util.Dom.getViewportHeight() - heightY)/2;
//			this.dialog.show( event.clientX, Math.max(150, event.clientY - 150), 300, 0, NC.widget.ConversationDialog.scrollToEnd);		
			this.dialog.show(leftX, topY, widthX);
			NC.widget.ConversationDialog.scrollToEnd();	
		},
		
 		/* public methods */
		showConversationPopupByIdXY: function ( posX, posY, conversationId, title)
		{
			var pars = 'ConversationId=' + conversationId;
			pars += '&Title=' + title;
			pars += '&rand=' + new Date().getTime();
			pars = encodeURI( pars);
			this.dialog = new NC.widget.ConversationDialog(
			// sourceUrl, parameters, targetDiv, targetForm
				'showConversations.php', 
				pars, 
				'NCConversation', 
				'ShowConversationsForm'	, this.onShow, this.onHide);
			conversationDialog = this.dialog;	
			this.dialog.show( posX, posY, 300);		
			NC.widget.ConversationDialog.scrollToEnd();	
		},
		
		onShow: function( targetDiv) {
			conversationContent = null;;
		},
		
		onHide: function( targetDiv) {
			conversationContent = null;;
		},
		
		getConversationDialog: function() {
			return conversationDialog;
		},
		
		addToConversation: function() {
			conversationDialog.post();
		},
		 
		addNewConversation: function ( event)
		{
			if( event.keyCode == 13){
				dialog.post();
			}
		},
		
		getVisibleConversationId: function () {
			var convField = document.getElementById( 'ConversationId');
			if (convField != null) {
				return NC.util.Dom.getValue( convField);
			}
			else {
				return -1;
			}
		},
		
		isConversationContentPopulated: function() {
			if (conversationContent == null) {
				return false;
			}
			else {
				return true;
			}
		},
		
		updateConversation: function( conversation) {
			var updateConversation = false;
			if (conversationContent == null) {
				conversationContent = {}; // new object
				// empty conversation content
			}
			else {
				updateConversation = true; // new entries will be written to the conversation
			}
			var conversationItems = conversation.childNodes; 
			var ic;
			var contentDiv = document.getElementById("ConversationContainer");
			for ( ic = 0; ic < conversationItems.length; ic++) {
				var conversationItem = conversationItems.item(ic).childNodes;
				var iic;
				for ( iic = 0; iic < conversationItem.length; iic++) {
					var detail = conversationItem.item(iic);
					if (detail.nodeName == 'conversationelement') {
						detailelements = detail.childNodes;
						var il;
						var id = ''; 
						var authorValue = '';
						var timestampValue = '';
						var contentValue = '';
						for ( il = 0; il < detailelements.length; il++) {
							var name = detailelements.item(il).nodeName;
							var value = detailelements.item(il).firstChild.data;
							if (name == 'id') {
								id =  value;
							}
							else if (name == 'name') {
								authorValue = value;
							}
							else if (name == 'timestamp') {
								timestampValue = value;
							}
							else if (name == 'content') {
								contentValue = value;
							}
						}
//						NC.util.Debug.writeToConsole( "item: " + id + " " + author + " " + timestamp + " " + content);
						var element = { author: authorValue,
									    timestamp: timestampValue,
									    content: contentValue
									  };
						if (typeof conversationContent[id] == 'undefined') {
							conversationContent[id] = element;
							if (updateConversation == true) {
/// start formatting new element
								var newcontent = '';
		
								newcontent += '<div class="separator">';
								newcontent += '<p class="name">';
								newcontent += authorValue + ':';
								newcontent += '</p>';
								newcontent += '<p class="date">';
								newcontent += timestampValue;
								newcontent += '</p>';
								newcontent += '<div class="clearer"></div>';
								newcontent += '</div>';
								newcontent += '<p>';
								newcontent += contentValue;
								newcontent += '</p>';
// end								
								contentDiv.innerHTML = contentDiv.innerHTML + newcontent;
								NC.widget.ConversationDialog.scrollToEnd();	
//								alert( "new entries.");
							}
						}
					}
				}
			}
//			NC.util.Debug.writeToConsole( conversationContent.length + " items to start");
		}
 	}
  }	
();

})(); 	
 
 
