/*
	Author		: 	Mukesh Rane
	Email  		: 	r.mukesh@cssaglobal.com
	created		:	Friday - 13-12-2008
*/
var oldTitle = document.title;
var timeoutId = "";
var timeoutId_list = new Array();
var blink_status = "gbt"; 
function newExcitingAlerts_1(msg)
{
	//alert("1"); alert(msg);
	if(blink_status=='gbt')
	{
		timeoutId = setInterval(function() {document.title = document.title == msg ? 'Sedaa Consulting':msg;}, 1000);
		//timeoutId_list.push(tmp);
		blink_status="";
	}
}
function stopBlinking()
{
	window.onmousemove = function(){
		clearInterval(timeoutId);
		timeoutId = null;
		document.title = oldTitle;
		window.onmousemove = null;
		blink_status = "gbt"; 
	};
/*	if(timeoutId_list.length>0){
		window.onmousemove = function(){
			for(i=0; i<timeoutId_list.length; i++)
			{
				clearInterval(timeoutId_list[i]);
			}
			timeoutId_list = null;
			document.title = oldTitle;
			window.onmousemove = null;
		};	
	}
*/
}
	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:fixed; text-align:left; visibility:hidden; right:' + rightPositionOfWindow + 'px; bottom:250px; z-index:12; width:280px;');			
			//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/min.gif");
				this.titleBarButtons.onclick =function(){
						winList["chatwindow_" + (isOpen == 1?fromUserId:toUserId)].minimize();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/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 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:230px; 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("style","width:260px;");
				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 div_2 = document.createElement("div");
				this.div_2 = div_2;
				this.div_2.setAttribute("style","padding-top:5px;");
			
				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.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);
			*/
				//--------------------------------------------------------------------------------
			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];

		if(k || v){
		  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;
	 }