var tUserId = "";
var fUserId = "";

function createWindow(username,fromUserId,toUserId,isOpen){	//	if when user click on username isOpen = 0 and open dyanamic = 1
	var username = username;
	this.username = username;
	
	var fromUserId = fromUserId;
	this.fromUserId = fromUserId;
	
	var toUserId = toUserId;
	this.toUserId = toUserId;
	
	var isOpen = isOpen;
	this.isOpen = isOpen;
	
	
	var form = document.createElement("form");
	this.form = form;
	this.form.setAttribute("name","miMarathi");
	this.form.setAttribute("action","#");
	this.form.setAttribute("method","post");
	//--------------------------------------------------------------------------------	
	
	var mainDiv = document.createElement("div");
	this.mainDiv = mainDiv;
	this.mainDiv.setAttribute("id","chatwindow_" + (this.isOpen == 1?this.fromUserId:this.toUserId));		//	Dynamic
	this.mainDiv.setAttribute("class","window");
	this.mainDiv.className = "window";
	_setStyle(this.mainDiv,'background-color:#c0c0c0; border:2px solid #000000; margin:0px; padding:2px; position:absolute; text-align:left; visibility:hidden; left:666px; top:150px; z-index:12; width:280px;');
	//this.mainDiv.setAttribute("style","border:1px solid #000000;left: 666px; top: 150px; z-index: 12; width: 280px; visibility: hidden;");
	
	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","mirrorOneUp.php?reqFile=templates/default/images/buttons.gif");
			this.titleBarButtons.setAttribute("useMap","#sampleMap1" + (this.isOpen == 1?this.fromUserId:this.toUserId));	//	Dynamic
			this.titleBarButtons.setAttribute("width","50");
			this.titleBarButtons.setAttribute("height","14");

			this.titleBar.appendChild(this.titleBarButtons);
			//--------------------------------------------------------------------------------
			var sampleMap1 = document.createElement("map");
			this.sampleMap1 = sampleMap1;
			this.sampleMap1.setAttribute("name","sampleMap1" + (this.isOpen == 1?this.fromUserId:this.toUserId));	//	Dynamic
			
			this.titleBar.appendChild(this.sampleMap1);
			//--------------------------------------------------------------------------------
				var map_area_1 = document.createElement("area");
				this.map_area_1 = map_area_1;
				this.map_area_1.setAttribute("shape", "rect");
				this.map_area_1.setAttribute("coords", "0,0,15,13");
				this.map_area_1.setAttribute("href", "");
				this.map_area_1.setAttribute("title", "Minimize");
				//this.map_area_1.setAttribute("onclick", "this.parentWindow.minimize();return false;");
				
				this.map_area_1.onclick = call_minimizeChatWindow;	//	call minimize event
				
				
				this.sampleMap1.appendChild(this.map_area_1);
				//--------------------------------------------------------------------------------
				var map_area_2 = document.createElement("area");
				this.map_area_2 = map_area_2;
				this.map_area_2.setAttribute("shape", "rect");
				this.map_area_2.setAttribute("coords", "16,0,31,13");
				this.map_area_2.setAttribute("href", "");
				this.map_area_2.setAttribute("title", "Restore");
				this.map_area_2.setAttribute("onclick", "this.parentWindow.restore();return false;");
				
				this.map_area_2.onclick = call_restoreChatWindow;	//	call restore event
				
				this.sampleMap1.appendChild(this.map_area_2);
				//--------------------------------------------------------------------------------
				var map_area_3 = document.createElement("area");
				this.map_area_3 = map_area_3;
				this.map_area_3.setAttribute("shape", "rect");
				this.map_area_3.setAttribute("coords", "34,0,49,13");
				this.map_area_3.setAttribute("href", "");
				this.map_area_3.setAttribute("title", "Close");
				this.map_area_3.setAttribute("onclick", "this.parentWindow.close();return false;");
				
				this.map_area_3.onclick = call_closeChatWindow;	//	call close event
				
				this.sampleMap1.appendChild(this.map_area_3);
				//--------------------------------------------------------------------------------

		var clientArea = document.createElement("div");
		this.clientArea = clientArea;
		this.clientArea.setAttribute("class","clientArea");
		this.clientArea.setAttribute("style","height: 320px;");
		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:230px; overflow:auto; border:1px solid #000000;");
			this.userChatWindow.className = "userChatWindow";
		
			this.clientArea.appendChild(this.userChatWindow);
			//--------------------------------------------------------------------------------
			var div = document.createElement("div");
			this.div = div;
			this.div.setAttribute("style","height:7px;");
		
			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("textarea");
				this.chatText = chatText;
				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("rows","2");
				this.chatText.setAttribute("cols","30");
				
				this.div_1.appendChild(this.chatText);
				//--------------------------------------------------------------------------------
			var div_2 = document.createElement("div");
			this.div_2 = div_2;
			this.div_2.setAttribute("style","padding-top:2px;");
		
			this.clientArea.appendChild(this.div_2);
			//--------------------------------------------------------------------------------
				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.setAttribute("onclick","javascript:doSend(" + (this.isOpen == 1?toUserId:fromUserId) + "," + (this.isOpen == 1?fromUserId:toUserId) + ");");	//	Dynamic
				
				tUserId = (this.isOpen == 1?fromUserId:toUserId);	//	assign id to global varibale for isDoSend function
				fUserId = (this.isOpen == 1?toUserId:fromUserId);	//	assign id to global varibale for isDoSend function
				
				this.btnSend.onclick = call_isDoSend;	//	call doSend event
				
				this.div_2.appendChild(this.btnSend);
				//--------------------------------------------------------------------------------
	document.body.appendChild(this.form);
}

function call_isDoSend(evt){
	doSend(fUserId,tUserId);
}

function call_minimizeChatWindow(evt){
	this.parentWindow.minimize();return false;
}

function call_restoreChatWindow(evt){
	this.parentWindow.restore();return false;
}

function call_closeChatWindow(evt){
	this.parentWindow.close();return false;
}


 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;
 }
