/*
	Author		: 	Mukesh Rane
	Email  		: 	r.mukesh@cssaglobal.com
	created		:	Friday - 13-12-2008
*/

	function classCreateWindow_MR(){
		var username = username;
		var fromUserId = fromUserId;
		var toUserId = toUserId;
		var isOpen = isOpen;
		var mainDiv = "";
		var form = "";
	
		this.createWindow_MR=function (username,fromUserId,toUserId,isOpen){	//	if when user click on username isOpen = 0 and open dyanamic = 1
			this.username = username;
			
			this.fromUserId = fromUserId;
			
			this.toUserId = toUserId;
			
			this.isOpen = isOpen;
			
			//--------------------------------------------------------------------------------	
			var countWindowOpen = countHowManyWindowOpen();
			
			this.mainDiv = document.createElement("div");
			this.mainDiv.setAttribute("id","chatwindow_" + (this.isOpen == 1?this.fromUserId:this.toUserId));		//	Dynamic
			this.mainDiv.setAttribute("class","window");
			this.mainDiv.className = "window";
			var rightPositionOfWindow = (countWindowOpen * 290);
			
			//_setStyle(this.mainDiv,'margin:0px; padding:2px; position:absolute; text-align:left; visibility:hidden; left:666px; top:150px; z-index:12; width:280px;');
			
			if (window.XMLHttpRequest) {	// IE 7, mozilla, safari, opera 9
				_setStyle(this.mainDiv,'margin:0px; padding:2px; position:fixed; text-align:left; visibility:hidden; right:' + rightPositionOfWindow + 'px; bottom:0; z-index:12; width:240px; height:353px');		
			}else{	// IE6, older browsers
				_setStyle(this.mainDiv,'margin:0px; padding:2px; position:absolute; text-align:left; visibility:hidden; right:' + rightPositionOfWindow + 'px; bottom:0; z-index:12; width:280px; height:353px');	
			}
			
			//this.form.appendChild(this.mainDiv);
			//--------------------------------------------------------------------------------
				var titleBar = document.createElement("div");
				this.titleBar = titleBar;
				this.titleBar.setAttribute("class","titleBar");
				this.titleBar.className = "titleBar";
				
			this.mainDiv.appendChild(this.titleBar);
			//--------------------------------------------------------------------------------
					var titleBarText = document.createElement("span");
					this.titleBarText = titleBarText;
					this.titleBarText.setAttribute("class","titleBarText");
					this.titleBarText.className = "titleBarText";
					
					this.titleBar.appendChild(this.titleBarText);
					//--------------------------------------------------------------------------------
						var titleBarTextNode = document.createTextNode(this.username);	// Dynamic
						this.titleBarTextNode = titleBarTextNode;
			
						this.titleBarText.appendChild(this.titleBarTextNode);
						//--------------------------------------------------------------------------------
											
						var titleBarButtons = document.createElement("img");
						this.titleBarButtons = titleBarButtons;
						this.titleBarButtons.setAttribute("class","titleBarButtons");
						this.titleBarButtons.className = "titleBarButtons";
						this.titleBarButtons.setAttribute("src",SITEROOT + "/js/myajaxchat/images/close.gif");
						this.titleBarButtons.onclick =function(){
								winList["chatwindow_" + (isOpen == 1?fromUserId:toUserId)].close();
								
								var d_nested = document.getElementById("chatwindow_" + (isOpen == 1?fromUserId:toUserId)); 
								var destroyed = document.body.removeChild(d_nested);
								return false;
						}
						this.titleBar.appendChild(this.titleBarButtons);
						
						var titleBarButtons = document.createElement("img");
						this.titleBarButtons = titleBarButtons;
						this.titleBarButtons.setAttribute("class","titleBarButtons");
						this.titleBarButtons.className = "titleBarButtons";
						this.titleBarButtons.setAttribute("src",SITEROOT + "/js/myajaxchat/images/max.gif");
						this.titleBarButtons.onclick =function(){
								winList["chatwindow_" + (isOpen == 1?fromUserId:toUserId)].restore();return false;
						}
						this.titleBar.appendChild(this.titleBarButtons);

						var titleBarButtons = document.createElement("img");
						this.titleBarButtons = titleBarButtons;
						this.titleBarButtons.setAttribute("class","titleBarButtons");
						this.titleBarButtons.className = "titleBarButtons";
						this.titleBarButtons.setAttribute("src",SITEROOT + "/js/myajaxchat/images/min.gif");
						this.titleBarButtons.onclick =function(){
								winList["chatwindow_" + (isOpen == 1?fromUserId:toUserId)].minimize();return false;
						}
						this.titleBar.appendChild(this.titleBarButtons);
					//--------------------------------------------------------------------------------
		
				var clientArea = document.createElement("div");
				this.clientArea = clientArea;
				this.clientArea.setAttribute("class","clientArea");
				this.clientArea.setAttribute("style","height: 300px;");
				this.clientArea.className = "clientArea";
			
			this.mainDiv.appendChild(this.clientArea);
			//--------------------------------------------------------------------------------
					var userChatWindow = document.createElement("div");
					this.userChatWindow = userChatWindow;
					this.userChatWindow.setAttribute("id","userChatWindow_" + (this.isOpen == 1?this.fromUserId:this.toUserId));		//	Dynamic
					this.userChatWindow.setAttribute("style","height:244px; overflow:auto; border:0px solid #000000;");
					this.userChatWindow.className = "userChatWindow";
				
					this.clientArea.appendChild(this.userChatWindow);
					//--------------------------------------------------------------------------------
					var div = document.createElement("div");
					this.div = div;
					this.div.setAttribute("style","height:15px;");
					this.div.setAttribute("id","userChatWindowStatus_" + (this.isOpen == 1?this.fromUserId:this.toUserId));		//	Dynamic
				
					this.clientArea.appendChild(this.div);
					//--------------------------------------------------------------------------------
					var div_1 = document.createElement("div");
					this.div_1 = div_1;
					
					this.clientArea.appendChild(this.div_1);
					//--------------------------------------------------------------------------------
						var chatText = document.createElement("input");
						this.chatText = chatText;
						this.chatText.setAttribute("type","text");
						this.chatText.setAttribute("name","chatText" + (this.isOpen == 1?this.fromUserId:this.toUserId));
						this.chatText.setAttribute("id","chatText_" + (this.isOpen == 1?this.fromUserId:this.toUserId));	//	Dynamic
						this.chatText.setAttribute("tabindex","1");
						this.chatText.setAttribute("style","width:240px;");
						this.chatText.className = "chatTextBox";

						this.chatText.onkeypress = function(e){
							
							if( !e ) {	if( window.event ) {//Internet Explorer
							  e = window.event;	} else {	return;	}}
							if( typeof( e.keyCode ) == 'number'  ) {//DOM
							  e = e.keyCode;} else if( typeof( e.which ) == 'number' ) {//NS 4 compatible
							  e = e.which;} else if( typeof( e.charCode ) == 'number'  ) {//also NS 6+, Mozilla 0.9+
							  e = e.charCode;} else {	//total failure, we have no way of obtaining the key code
							return;}
							
							//doDisplayTypingText_MR(toUserId,fromUserId,username + " typing message...");
							
							var charCode;	charCode = e ;	if (charCode == 13) doSend_MR((isOpen == 1?toUserId:fromUserId),(isOpen == 1?fromUserId:toUserId),chatText.value);	
						}
							
						this.div_1.appendChild(this.chatText);
						//--------------------------------------------------------------------------------
					//--------------------------------------------------------------------------------
					/*
						var btnSend = document.createElement("input");
						this.btnSend = btnSend;
						this.btnSend.setAttribute("type","button");
						this.btnSend.setAttribute("name","btnSend");
						this.btnSend.setAttribute("id","btnSend");
						this.btnSend.setAttribute("value"," Send ");
						
						this.btnSend.onclick = function(){
							doSend_MR((isOpen == 1?toUserId:fromUserId),(isOpen == 1?fromUserId:toUserId),chatText.value);
						}
						
						$("button").click(function () {
						  var text = $(this).text();
						  $("textarea").val(text);
						});
						
						
						this.div_2.appendChild(this.btnSend);
					*/
						//--------------------------------------------------------------------------------

					var lastdiv = document.createElement("div");
					this.lastdiv = lastdiv;
					this.lastdiv.setAttribute("class","chatEndmodule");
					var innerlastdiv = document.createElement("div");
					this.innerlastdiv = innerlastdiv;
					this.lastdiv.appendChild(this.innerlastdiv);			
					this.mainDiv.appendChild(this.lastdiv);
					
					



			document.body.appendChild(this.mainDiv);
		}
		
	}
	
	var classCreateWindow = new classCreateWindow_MR();
	
	 function _setStyle(element, declaration) {
	   if (declaration.charAt(declaration.length-1)==';')
		 declaration = declaration.slice(0, -1);
	   var k, v;
	   var splitted = declaration.split(';');
	   for (var i=0, len=splitted.length; i<len; i++) {
		  k = rzCC(splitted[i].split(':')[0]);
		  v = splitted[i].split(':')[1];
		  eval("element.style."+k+"='"+v+"'");
	
	   }
	 }
	
	 function rzCC(s){
	   // retrieving-css-styles-via-javascript/
	   for(var exp=/-([a-z])/; 
		   exp.test(s); 
		   s=s.replace(exp,RegExp.$1.toUpperCase()));
	   return s;
	 }
	 
	 function countHowManyWindowOpen(){
		var arr = jQuery.makeArray(document.getElementsByTagName("div"));
		var count=0;
		for(i=0;i<arr.length;i++){
			if(arr[i].className == "window"){
				count++;	
			}
		}
		return count;
	 }