/* *************************************************************************************************
 * Created By:    Erick Hauser
 * Created On:    04-21-2011
 * Last Modified: 06-22-2011
 ***************************************************************************************************
 * CHANGE LOG
 ***************************************************************************************************
 * 06-24-2011 / EDH : ALL - Fixed problem with NicEdit icons not loading in the new VJF design.
 * 06-22-2011 / EDH : ALL - Adjustments for integration into new VJF design.
 * 05-09-2011 / EDH : ALL - Merged methods from 'wsd.js'. File removed from project.
 *                        - Resize shortcut method replaced with bind event to remove an extra
 *                          internal jQuery function call. Speed up and allows event namespacing.
 *                        - Removed ':first' psuedo class selectors from resize event function.
 *                          This has resulted in ~7% speed increase when resizing.
 * 05-04-2011 / EDH : IE8 - Fixed extra line-break issue on NicEdit instance creation.
 * 05-02-2011 / EDH : IE8 - Fixed issue causing window to jump below cursor in IE on drag event.
 ***************************************************************************************************
 * BUGS & ISSUES
 ***************************************************************************************************
 * FF4 - '.chatBody' element can be resized beyond set min/max values for width and height.
 ************************************************************************************************ */

$(document).ready(function(){
	chatCount=new Array(); /* This is a required global for the page. */
	chatNicEditor = new nicEditor();
	
	var minWidth = 250; /* Minimum width and height for the chat window. */
	var minHeight = 185;
	
	chatAdd(); /* Creates the chat window. */
	
	$(".chatWindow").draggable({handle:".chatBar"}); /* Enable chat window drag and resize. */
	$(".chatWindow").resizable();
	
	$('.chatWindow').bind('resize',function(e){
		var chatWidth = $(this).width();
		var chatHeight = $(this).height();

		var chatTitleBar = $(this).find('.chatTitle');
		var chatEntryWrapper = $(this).find('.chatEntryWrapper');
		var chatBoxCenter = $(this).find('.chatBoxCenter');
		var chatEntryPanel = $(this).find('.chatEntryPanel');
		var chatBody = $(this).find('.chatBody');
		var chatFormatting = $(this).find('.chatFormatting');
		var chatFooter = $(this).find('.chatFooter');
		var chatFooterCenter = $(this).find('.chatFooterCenter');
		var chatBodyWrapper =  $(this).find('.chatBodyWrapper');
		
		if($(this).width()<minWidth){ /* Prevent resizing beyond the minimum width and height settings. */
			$(this).width(minWidth);
			chatWidth = $(this).width();
			}
		
		if($(this).height()<minHeight){
			$(this).height(minHeight);
			}

		$(chatEntryWrapper).width(chatWidth-100); /* Resize the rest of the scalable child elements. */
		$(chatFormatting).width(chatWidth-90);
		$(chatFooter).width(chatWidth)
		$(chatFooterCenter).width(chatWidth-12)
		$(chatTitleBar).width(chatWidth-12);
		$(chatBoxCenter).width(chatWidth-12);
		$(chatEntryPanel).width(chatWidth);
		
		$(chatBodyWrapper).width(chatWidth-14);
		$(chatBodyWrapper).height(chatHeight-78);
		
	});
});

/* ********************************************************************************************** */

function chatAdd(chatWidth,chatHeight){ /* Width and height aren't being used yet on initialization. Adding later. */
	var activeChats = chatCount.length;

	if(activeChats==0) /* Incremented container DIV ID to support multiple dynamically generated chat windows. */
		chatCount[activeChats] = 'window'+activeChats;
	else if(activeChats>0)
		chatCount[activeChats+1] = 'window'+activeChats;

	/* HTML to inject for chat window. Needs a few more adjustments for multi-window chat support. */
	var injectionHTML = '<div class="chatWindow" id="chatWindow'+chatCount.length+'">\
		<div class="chatBar">\
			<div class="chatLeftTop"></div>\
			<div class="chatTitle"></div>\
			<div class="chatRightTop"></div>\
		</div>\
		<div class="chatBox">\
			<div class="chatBoxLeft"></div>\
			<div class="chatBoxCenter">\
				<div class="chatBodyWrapper" id="chatBodyWrapper'+chatCount.length+'">\
					<div class="chatBody" id="chatBody">\
					</div>\
				</div>\
			</div>\
			<div class="chatBoxRight"></div>\
		</div>\
		<div class="chatEntryPanel">\
			<div class="chatEntryLeft"></div>\
			<div class="chatEntryCenter">\
				<div class="chatFormatting" id="chatFormatting'+chatCount.length+'"></div>\
				<div class="chatEntryWrapper">\
					<div class="chatEntry chatEntryTextStyle" id="chatEntry'+chatCount.length+'"></div>\
				</div>\
			</div>\
			<div class="chatEntryRight"></div>\
		</div>\
		<div class="chatFooter">\
			<div class="chatFooterLeft"></div>\
			<div class="chatFooterCenter"></div>\
			<div class="chatFooterRight"></div>\
		</div>\
		<div class="chatIcon">\
		</div>\
		<div class="sendControls">\
			<div class="sendButton"></div>\
		</div>\
		<div class="closeButton"></div>\
		<div class="dataBlock"></div>\
	</div>';
	
	chatFormattingID = 'chatFormatting'+chatCount.length;
	chatEntryID = 'chatEntry'+chatCount.length;
	
	$('#chatInjector').append(injectionHTML); /* Append the new DIV and child elements to the DOM before applying NicEdit to it. */
	
	chatNicEditor.setPanel(chatFormattingID); /* NicEdit formatting panel (Bold, Italic, Etc.). */
	chatNicEditor.addInstance(chatEntryID);   /* NicEdit rich text area. */
	nicEditors.findEditor(chatEntryID).setContent(''); /* Clear the content of NicEdit on creation. Fixes issue with extra line-break in IE8. */
}

/* ********************************************************************************************** */


