var devId = "va1_SuXA83k41qnN";
                var colorpicker;
                var colorBox;
                var selectedColor="#000000";
                var selectedAction="drawLine";
                var selectedActionDiv=null;
                var selectedStroke="1";
                var selectedStrokeDiv=null;
                
                Rico.loadModule('ColorPicker', 'Color');
                Rico.onLoad( function() {
                
                
                        colorpicker=new Rico.ColorPicker("colorpicker1");
                        colorpicker.atLoad();
                        colorpicker.returnValue=ProcessColorSelection;
                        colorBox=$('colorDiv');
                });
                
		var prevKeyCode = -1;
		var userName = ""; //AIM.params.user
		var session = ""; //AIM.params.sessionId
		var lastMessageId = 0;
		var lastCanvasImageId = 0;
		var mTimer = setTimeout('getChatText();', 3000);
		var jg = new jsGraphics("canvas");
		//jg.setPrintable(true);
		
		
		function onLogin() {
		      if(!isLoggedIn()) {
                        doStartSession();
                        jg = new jsGraphics("canvas");
                      }
                }
                
		function doStartSession() {
		
                  AIM.init();
                  AIM.params.wimKey=devId;
                  AIM.params.SHOW_OFFLINE=true;
                  AIM.core.subscriptions = "buddylist,presence,im";
                  AIM.transactions.getToken(AIM.core.subscriptions);
          
                  //AIM.widgets.buddyList.launch();
                  //AIM.core.watchAuthRequest();
                  //AIM.transactions.startSession(AIM.core.subscriptions);
                  //token = AIM.params.token;
                }
		
		function isLoggedIn(){
		//document.getElementById('chatText').value="test\ntest\ntest\ntest\ntest\ntest\ntest\ntest";
		//document.getElementById('chatText').scrollTop = document.getElementById('chatText').scrollHeight;
		//AIM.params.sessionId='test';
		//session='test';
//  return true;
	
                        if(AIM.params.token == null) return false;
                        
                        return true;
                }
		
		function getAjaxObj(){
			if (window.XMLHttpRequest) {
				return new XMLHttpRequest();
			} else if(window.ActiveXObject) {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} 
			return null;
		}

		//Add a message to the chat server.
		function sendMessage() {
			var send = getAjaxObj();
			var textInput = document.getElementById("chatInputText");
			if (send.readyState == 4 || send.readyState == 0) {
				var message = textInput.value;
				textInput.value="";
				if(message == "") return;

				send.open("POST", "postMessage.php?last="+lastMessageId+"&session="+session, true);
				send.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				//send.onreadystatechange = getHandler(send, handleSendChat);
				var param = "message="+message;
				send.send(param);
			}
			textInput.focus();
		}

		//Callback handler for sending messages.  Currently does nothing.
		function handleSendChat(send) {
		}

                var wasLoggedIn=false;
		//Gets the current messages from the server
		function getChatText() {
		      if(!isLoggedIn()) {
		              mTimer = setTimeout('getChatText();', 1250);
                                return;
                        }
                        //document.getElementById('mainChatDiv').style.display='block';
                        
                        if(isLoggedIn() && !wasLoggedIn && AIM.params.sessionId!=null) {
		        wasLoggedIn=true;
                        var send = getAjaxObj();
			if (send.readyState == 4 || send.readyState == 0) {
				send.open("POST", "login.php?session="+AIM.params.sessionId, true);
				send.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				//send.onreadystatechange = getHandler(send, handleSendChat);
				var param = "name="+AIM.params.user;
				send.send(param);
			}
                        document.getElementById('mainChatDiv').style.display='block';
                        var textInput = document.getElementById("chatInputText");
                        textInput.focus();
                        session=AIM.params.sessionId;
                        userName=AIM.params.user;
                        
                        upload_field=document.getElementById('file');
                        upload_field.form.action="postImage.php?session="+session;
                        AIM.widgets.buddyList.launch();
                      }
			receive = getAjaxObj();
			if (receive.readyState == 4 || receive.readyState == 0) {
				receive.open("GET", "getMessages.php?last="+lastMessageId+"&session="+session, true);
				receive.onreadystatechange = getHandler(receive, handleReceiveChat);
				receive.send(null);
			}
		}

		//Function for handling the return of chat text
		function handleReceiveChat(receive) {
			if (receive.readyState == 4) {
				//var chat_div = document.getElementById('div_chat');
				var xmldoc = receive.responseXML;
				//alert(receive.responseText);
				//handle chat messages
				var message_nodes = xmldoc.getElementsByTagName("MESSAGE");
				var n_messages = message_nodes.length;
				var isDrawn = false;
				for (i = 0; i < n_messages; i++) {
					var user_node = message_nodes[i].getElementsByTagName("USER");
					var text_node = message_nodes[i].getElementsByTagName("TEXT");
					var id = parseInt(message_nodes[i].getAttribute('id'));
					var type = message_nodes[i].getElementsByTagName("TYPE");
					
					if(text_node[0].firstChild!= null) {
					//alert(text_node[0].firstChild.nodeValue);
						if(type[0].firstChild.nodeValue == '1') {
							if(id <= lastMessageId) continue;
							lastMessageId = id;
							addMessage(user_node[0].firstChild.nodeValue, text_node[0].firstChild.nodeValue);
						} else if(type[0].firstChild.nodeValue == '2') {
							if(id <= lastMessageId) continue;
							lastMessageId = id;
							addDrawMessage(text_node[0].firstChild.nodeValue);
							isDrawn = true;
						} else if(type[0].firstChild.nodeValue == '3'){
							if(id <= lastCanvasImageId) continue;
							lastCanvasImageId = id;
							setCanvasImage(text_node[0].firstChild.nodeValue);
						}
					}
				}
				if(isDrawn) jg.paint();
				
				//handle rooms
				message_nodes = xmldoc.getElementsByTagName("ROOM");
				n_messages = message_nodes.length;
				var rooms = Array();
				for (i = 0; i < n_messages; i++) {
					//alert(parseInt(message_nodes[i].getAttribute('id')));
					rooms[i]=Array()
					rooms[i]['id'] = parseInt(message_nodes[i].getAttribute('id'));
					rooms[i]['isHere'] = parseInt(message_nodes[i].getAttribute('isHere'));
					rooms[i]['public'] = parseInt(message_nodes[i].getAttribute('public'));
					var user_node = message_nodes[i].getElementsByTagName("USER");
					var name_node = message_nodes[i].getElementsByTagName("NAME");
					if(user_node[0].firstChild)
						rooms[i]['user'] = user_node[0].firstChild.nodeValue;
					if(name_node[0].firstChild)
						rooms[i]['name'] = name_node[0].firstChild.nodeValue;
				}
				handleRooms(rooms);
				
				//handle users
				message_nodes = xmldoc.getElementsByTagName("USER");
				n_messages = message_nodes.length;
				var users = Array();
				var userInd=0;
				for (i = 0; i < n_messages; i++) {
					var name_node = message_nodes[i].getElementsByTagName("NAME");
					if(name_node[0] && name_node[0].firstChild) {
						users[userInd]=Array();
						users[userInd]['id'] = parseInt(message_nodes[i].getAttribute('id'));
						users[userInd]['name'] = name_node[0].firstChild.nodeValue;
						userInd++;
					}
				}
				handleUsers(users);
				
				handleImageOnLoad();
				mTimer = setTimeout('getChatText();', 1250);
			}
		}
		
		function handleRooms(rooms) {
			var roomsDiv = document.getElementById('roomsDiv');
			for(var x=0; x<roomsDiv.childNodes.length; x++){
				var elem = roomsDiv.childNodes[x];
				if (!elem.tagName || elem.tagName.toLowerCase() != 'div') continue;
				var existingRoomId = elem.roomId;
				var found=false;
				for(var y=0; y<rooms.length; y++) {
					if(rooms[y]['id'] == existingRoomId) {
						rooms[y]['exists'] = true;
						//alert(rooms[y]['public']);
						if(elem.isPublic==1 && rooms[y]['public']==0) {
							//room was changed to private
							elem.isPublic=0;
							elem.style.color="#AAAAAA";
							elem.onclick=new Function("return false;");
						} else if(elem.isPublic==0 && rooms[y]['public']==1) {
							//room was changed to public
							//alert('hee');
							elem.isPublic=1;
							elem.style.color="#000000";
							elem.onclick=new Function("moveToRoom(this.roomId, this.innerHTML);");
						}
						if(elem.isPublic==1) {
							elem.onclick=new Function("moveToRoom(this.roomId, this.innerHTML);");
						}
						if(rooms[y]['isHere']==1){
							elem.onclick=new Function("return false;");
						}
						elem.innerHTML = rooms[y]['name'];
						found=true;
						break;
					} 
				}
				if(!found) {
					elem.parentNode.removeChild(elem);
					x--;
				}
			}
			
			for(var x=0; x<rooms.length; x++){ 
				if(rooms[x]['exists']) continue;
				var div = document.createElement('div');
				div.style.cursor="default";
				div.innerHTML=rooms[x]['name'];
				div.roomId=rooms[x]['id'];
				div.isPublic=rooms[x]['public'];
				if(div.isPublic) {
					div.onclick=new Function("moveToRoom(this.roomId, this.innerHTML);");
				} else {
					div.style.color="#AAAAAA";
				}
				if(rooms[x]['isHere'] == 1) {
					div.onclick=new Function("return false;");
				}
				roomsDiv.appendChild(div);
			}
		}

		function handleUsers(users) {
			var contactsDiv = document.getElementById('contactsDiv');
			for(var x=0; x<contactsDiv.childNodes.length; x++){
				var elem = contactsDiv.childNodes[x];
				//alert('-'+elem.tagName.toLowerCase()+'-');
				if(!elem.tagName || elem.tagName.toLowerCase() != 'div') continue;
				if(!elem.contactId) continue;
				var existingContactId = elem.contactId;
				var found=false;
				for(var y=0; y<users.length; y++) {
					if(users[y]['id'] == existingContactId) {
						users[y]['exists'] = true;
						found=true;
						break;
					} 
				}
				if(!found){
					elem.parentNode.removeChild(elem);
					x--;
				}
			}
			
			for(var x=0; x<users.length; x++){ 
				if(users[x]['exists']) continue;
				var div = document.createElement('div');
				div.style.cursor="default";
				div.innerHTML=users[x]['name'];
				div.contactId=users[x]['id'];
				//div.onclick=new Function("AIM.ui.createIMWindow(this.innerHTML);");
				if(this.innerHTML != userName)
        				div.onclick=new Function("AIM.widgets.IMMe.launch(this.innerHTML);");
				contactsDiv.appendChild(div);
			}
		}
		
		function createNewRoom() {
			var roomName = document.getElementById('newRoomName').value;
			if(roomName.length == 0) {
				alert('Please enter a room name.');
				return;
			}
			
			var send = getAjaxObj();
			if (send.readyState == 4 || send.readyState == 0) {
				send.open("POST", "createNewRoom.php?session="+session, true);
				send.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				var param = "room="+roomName;
				send.send(param);
			}
			
			jg.clear();
			lastMessageId=0;
			lastCanvasImageId=0;
			var div = document.getElementById('imageDiv');
			for(var x=0; x<div.childNodes.length; x++){
				div.childNodes[x].parentNode.removeChild(div.childNodes[x]);
				x--;
			}
		}
		
		function moveToRoom(roomId, roomName) {
			var answer = confirm("Are you sure you want to move to the room \""+roomName+"\"?");
			if(!answer) return;
			
			var send = getAjaxObj();
			if (send.readyState == 4 || send.readyState == 0) {
				send.open("POST", "moveToRoom.php?session="+session, true);
				send.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				var param = "roomId="+roomId;
				send.send(param);
			}
			jg.clear();
			lastMessageId=0;
			lastCanvasImageId=0;
			var div = document.getElementById('imageDiv');
			for(var x=0; x<div.childNodes.length; x++){
				div.childNodes[x].parentNode.removeChild(div.childNodes[x]);
				x--;
			}
		}
		
		function addMessage(user, text){
			var chatArea = document.getElementById("chatText");
			chatArea.value=chatArea.value+user+": "+text+"\n";
			chatArea.scrollTop = chatArea.scrollHeight;
		}
		
		function addDrawMessage(command){
			var color = command.substr(command.indexOf('#'), command.length);
			var com = command.substr(0,command.indexOf('#'));
			jg.setColor(color);
			
			var ind=com.indexOf("(\\'");
			if(ind>0){
				com=com.substr(0,ind)+"('"+com.substr(ind+3, com.length);
				ind=com.lastIndexOf("\\',");
				com=com.substr(0,ind)+"',"+com.substr(ind+3, com.length);
			}
			eval(com);
			//jg.paint();
		}

		function getHandler(r, h){
			return function(){
				if(r.readyState==4){
					if(r.status==200) h(r);
					else alert("Error: "+r.status);
				}
			}
		}

		var startX = -1;
		var startY = -1;
		var endX = -1;
		var endY = -1;
		var prevX = -1;
		var prevY = -1;
		var lineX = Array();
		var lineY = Array();
		var textCreated = false;
		
		function handleCanvasMouseOver(event) {
			if(selectedAction != 'drawPen') return;
			var xx = event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
			var yy = event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
			xx-=2;
			yy-=2;
			
			var div = document.getElementById('canvas');
			var width=800;//parseInt(div.style.width);
			var height=450;//parseInt(div.style.height);
			if(xx+4>=width || yy+4>=height){
				handleCanvasMouseUp(event);
				return;
			}
			if(startX==-1 || startY==-1 || endX!=-1 || endY!=-1) return;
			if(prevX == -1 || prevY == -1) {
				prevX=startX;
				prevY=startY;
				lineX[lineX.length]=prevX;
				lineY[lineY.length]=prevY;
			}
			if(Math.abs(prevX-xx)<4 && Math.abs(prevY-yy)<4) return;
			lineX[lineX.length]=xx;
			lineY[lineY.length]=yy;
			jg.setColor(selectedColor);
			jg.setStroke(parseInt(selectedStroke));
			jg.drawLine(prevX, prevY, xx, yy);
			jg.paint();
			prevX=xx;
			prevY=yy;
		}
		function handleCanvasMouseDown(event) {
			var xx = event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
			var yy = event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop));

			startX = xx;
			startY = yy;
		}
		function handleCanvasMouseUp(event) {
			var xx = event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
			var yy = event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
			endX = xx;
			endY = yy;
			
			jg.setColor(selectedColor);
			jg.setStroke(parseInt(selectedStroke));
			if(selectedAction == 'drawLine') {
				jg.drawLine(startX, startY, endX, endY);
				jg.paint();
				sendDrawMessage("jg.setStroke("+parseInt(selectedStroke)+");jg.drawLine("+startX+","+startY+","+endX+","+endY+");"+selectedColor);
			}
			
			if(selectedAction == 'drawRect') {
				jg.drawRect(Math.min(startX, endX)-1, Math.min(startY, endY)-1, Math.abs(endX-startX)-1, Math.abs(endY-startY)-1);
				jg.paint();
				sendDrawMessage("jg.setStroke("+parseInt(selectedStroke)+");jg.drawRect("+(Math.min(startX, endX)-1)+","+(Math.min(startY, endY)-1)+","+(Math.abs(endX-startX)-1)+","+(Math.abs(endY-startY)-1)+");"+selectedColor);
			}
			
			if(selectedAction == 'fillRect') {
				jg.fillRect(Math.min(startX, endX)-1, Math.min(startY, endY)-1, Math.abs(endX-startX)-1, Math.abs(endY-startY)-1);
				jg.paint();
				sendDrawMessage("jg.setStroke("+parseInt(selectedStroke)+");jg.fillRect("+(Math.min(startX, endX)-1)+","+(Math.min(startY, endY)-1)+","+(Math.abs(endX-startX)-1)+","+(Math.abs(endY-startY)-1)+");"+selectedColor);
			}
			
			if(selectedAction == 'drawEllipse') {
				jg.drawEllipse(Math.min(startX, endX)-1, Math.min(startY, endY)-1, Math.abs(endX-startX)-1, Math.abs(endY-startY)-1);
				jg.paint();
				sendDrawMessage("jg.setStroke("+parseInt(selectedStroke)+");jg.drawEllipse("+(Math.min(startX, endX)-1)+","+(Math.min(startY, endY)-1)+","+(Math.abs(endX-startX)-1)+","+(Math.abs(endY-startY)-1)+");"+selectedColor);
			}
			
			if(selectedAction == 'fillEllipse') {
				jg.fillEllipse(Math.min(startX, endX)-1, Math.min(startY, endY)-1, Math.abs(endX-startX)-1, Math.abs(endY-startY)-1);
				jg.paint();
				sendDrawMessage("jg.setStroke("+parseInt(selectedStroke)+");jg.fillEllipse("+(Math.min(startX, endX)-1)+","+(Math.min(startY, endY)-1)+","+(Math.abs(endX-startX)-1)+","+(Math.abs(endY-startY)-1)+");"+selectedColor);
			}
			
			if(selectedAction == 'drawText') {
				if(!textCreated){
					var divE = document.createElement("DIV");
					divE.style.position="absolute";
					divE.style.top=""+endY+"px";
					divE.style.left=""+endX+"px";
					var inputE = document.createElement("INPUT");
					inputE.x=endX;
					inputE.y=endY;
					inputE.type="text";
					inputE.maxLength=32;
					//var typeHandler = function(event){handleTextDraw(event);}
					
                                        //inputE.onkeydown=new Function("handleTextDraw(this, window.event);");
                                        //if (typeof inputE.attachEvent!='undefined') {
                                        //   inputE.attachEvent('onkeydown',handleTextDraw);
                                        //} else {
                                        //   inputE.addEventListener('onkeydown',handleTextDraw,false);
                                        //}
                                        
					inputE.onkeydown=function(event){handleTextDraw(event);}
					//inputE.addEventListener("onkeydown", function(event){handleTextDraw(this, event);}, false);
					divE.appendChild(inputE);
					document.getElementById('canvas').appendChild(divE);
					divE.style.display='block';
					textCreated=true;
				}
			}
			
			if(selectedAction == 'drawPen' && lineX.length>0) {
				var msg="jg.setStroke("+parseInt(selectedStroke)+");jg.drawPolyline(new Array(";
				for(var x=0; x<lineX.length; x++){
					msg+=lineX[x];
					if(x<lineX.length-1) msg+=',';
				}
				msg+="),new Array(";
				for(var x=0; x<lineY.length; x++){
					msg+=lineY[x];
					if(x<lineY.length-1) msg+=',';
				}
				msg+="));"+selectedColor;
				sendDrawMessage(msg);
				lineX=Array();
				lineY=Array();
			}
			startX=-1;
			startY=-1;
			endX=-1;
			endY=-1;
			prevX=-1;
			prevY=-1;
		}
		
		//Add a draw message to the chat server.
		function sendDrawMessage(command) {
			var send = getAjaxObj();
			if (send.readyState == 4 || send.readyState == 0) {
				send.open("POST", "postDrawMessage.php?last="+lastMessageId+"&session="+session, true);
				send.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				var param = "command="+command;
				send.send(param);
			}
		}
		
		function handleTextDraw(event){
		//alert('here');
                        if(!event) event=window.event;
                        
                        var textE = null;
                        if(event.srcElement) textE=event.srcElement;
                        else if(event.target) textE=event.target;
                        //alert(textE);
			if(event.keyCode == 13) {
			
				if(textE.value == '') {
					textE.parentNode.parentNode.removeChild(textE.parentNode);
					textCreated=false;
					return;
				}
				jg.setColor(selectedColor);
				jg.drawString(textE.value, textE.x, textE.y);
				jg.paint();
				sendDrawMessage("jg.drawString('"+textE.value+"',"+textE.x+","+textE.y+");"+selectedColor);
				textE.parentNode.parentNode.removeChild(textE.parentNode);
				textCreated=false;
			}
		}
		
		function setActiveTool(tool, div){
			selectedAction = tool;
			if(selectedActionDiv != null) {
				selectedActionDiv.style.borderColor='#B0CAE6';
			}
			selectedActionDiv = div;
			selectedActionDiv.style.borderColor='#FF6600';
		}
		
		//sets the stroke for drawing on the canvas
		function setActiveStroke(stroke, div){
			selectedStroke = stroke;
			if(selectedStrokeDiv != null) {
				selectedStrokeDiv.style.borderColor='#FFFFFF';
			}
			selectedStrokeDiv = div;
			selectedStrokeDiv.style.borderColor='#FF6600';
		}
		
		function setVisibleMenu(menuName){
			if(menuName == 'tools') {
				document.getElementById('BuddyCallsDiv').style.visibility ='hidden';
				document.getElementById('roomsMainDiv').style.display="none";
				document.getElementById('buddiesDiv').style.display="none";
				document.getElementById('toolsDiv').style.display="block";
				
				document.getElementById('toolsHeaderDiv').className="headerDiv mdSelected";
				document.getElementById('buddiesHeaderDiv').className="headerDiv";
				document.getElementById('buddiesCallHeaderDiv').className="headerDiv";
				document.getElementById('roomsHeaderDiv').className="headerDiv";
				
				document.getElementById('toolsHeaderDiv').onmouseover = function(){};
				document.getElementById('toolsHeaderDiv').onmouseout = function(){};
				
				document.getElementById('roomsHeaderDiv').onmouseover = function(){this.className='headerDiv mdHover';};
				document.getElementById('roomsHeaderDiv').onmouseout = function(){this.className='headerDiv';};
				document.getElementById('buddiesHeaderDiv').onmouseover = function(){this.className='headerDiv mdHover';};
				document.getElementById('buddiesHeaderDiv').onmouseout = function(){this.className='headerDiv';};
				document.getElementById('buddiesCallHeaderDiv').onmouseover = function(){this.className='headerDiv mdHover';};
				document.getElementById('buddiesCallHeaderDiv').onmouseout = function(){this.className='headerDiv';};
			}
			if(menuName == 'buddies') {
				document.getElementById('BuddyCallsDiv').style.visibility ='hidden';
				document.getElementById('roomsMainDiv').style.display="none";
				document.getElementById('toolsDiv').style.display="none";
				document.getElementById('buddiesDiv').style.display="block";
				
				document.getElementById('toolsHeaderDiv').className="headerDiv";
				document.getElementById('buddiesHeaderDiv').className="headerDiv mdSelected";
				document.getElementById('buddiesCallHeaderDiv').className="headerDiv";
				document.getElementById('roomsHeaderDiv').className="headerDiv";
				
				document.getElementById('buddiesHeaderDiv').onmouseover = function(){};
				document.getElementById('buddiesHeaderDiv').onmouseout = function(){};
				
				document.getElementById('roomsHeaderDiv').onmouseover = function(){this.className='headerDiv mdHover';};
				document.getElementById('roomsHeaderDiv').onmouseout = function(){this.className='headerDiv';};
				document.getElementById('toolsHeaderDiv').onmouseover = function(){this.className='headerDiv mdHover';};
				document.getElementById('toolsHeaderDiv').onmouseout = function(){this.className='headerDiv';};
				document.getElementById('buddiesCallHeaderDiv').onmouseover = function(){this.className='headerDiv mdHover';};
				document.getElementById('buddiesCallHeaderDiv').onmouseout = function(){this.className='headerDiv';};
			}
			if(menuName == 'calls') {
				document.getElementById('roomsMainDiv').style.display="none";
				document.getElementById('toolsDiv').style.display="none";
				document.getElementById('buddiesDiv').style.display="none";
				document.getElementById('BuddyCallsDiv').style.visibility ='visible';
				
				document.getElementById('toolsHeaderDiv').className="headerDiv";
				document.getElementById('buddiesHeaderDiv').className="headerDiv";
				document.getElementById('buddiesCallHeaderDiv').className="headerDiv mdSelected";
				document.getElementById('roomsHeaderDiv').className="headerDiv";
				
				document.getElementById('buddiesCallHeaderDiv').onmouseover = function(){};
				document.getElementById('buddiesCallHeaderDiv').onmouseout = function(){};
				
				document.getElementById('roomsHeaderDiv').onmouseover = function(){this.className='headerDiv mdHover';};
				document.getElementById('roomsHeaderDiv').onmouseout = function(){this.className='headerDiv';};
				document.getElementById('toolsHeaderDiv').onmouseover = function(){this.className='headerDiv mdHover';};
				document.getElementById('toolsHeaderDiv').onmouseout = function(){this.className='headerDiv';};
				document.getElementById('buddiesHeaderDiv').onmouseover = function(){this.className='headerDiv mdHover';};
				document.getElementById('buddiesHeaderDiv').onmouseout = function(){this.className='headerDiv';};
			}
			if(menuName == 'rooms') {
				document.getElementById('BuddyCallsDiv').style.visibility ='hidden';
				document.getElementById('toolsDiv').style.display="none";
				document.getElementById('buddiesDiv').style.display="none";
				document.getElementById('roomsMainDiv').style.display="block";
				
				document.getElementById('toolsHeaderDiv').className="headerDiv";
				document.getElementById('buddiesHeaderDiv').className="headerDiv";
				document.getElementById('buddiesCallHeaderDiv').className="headerDiv";
				document.getElementById('roomsHeaderDiv').className="headerDiv mdSelected";
				
				document.getElementById('roomsHeaderDiv').onmouseover = function(){};
				document.getElementById('roomsHeaderDiv').onmouseout = function(){};
				
				document.getElementById('buddiesCallHeaderDiv').onmouseover = function(){this.className='headerDiv mdHover';};
				document.getElementById('buddiesCallHeaderDiv').onmouseout = function(){this.className='headerDiv';};
				document.getElementById('toolsHeaderDiv').onmouseover = function(){this.className='headerDiv mdHover';};
				document.getElementById('toolsHeaderDiv').onmouseout = function(){this.className='headerDiv';};
				document.getElementById('buddiesHeaderDiv').onmouseover = function(){this.className='headerDiv mdHover';};
				document.getElementById('buddiesHeaderDiv').onmouseout = function(){this.className='headerDiv';};
			}
		}
		
		function clearCanvas(){
			var answer = confirm("Are you sure you want to clear the canvas?");
			if(!answer) return;
			jg.clear();
		}
		
		function ColorClick(e) {
			if (Element.visible(colorpicker.container)) {
                                colorpicker.close();
                        } else {
                                RicoUtil.positionCtlOverIcon(colorpicker.container,$('colorDiv'));
                                colorpicker.open();
                        }
                        Event.stop(e);
        }

		function ProcessColorSelection(newVal) {
			selectedColor=newVal;                        
			colorBox.style.backgroundColor=newVal;
        }
        
        function jsUpload(upload_field) {
			var re_text = /\.gif|\.jpg|\.jpeg|\.png/i;
			var filename = upload_field.value;

			/* Checking file type */
			if (filename.search(re_text) == -1) {
				alert("File does not have image(gif, jpg, jpeg, png) extension");
				upload_field.form.reset();
				return false;
			}

			upload_field.form.submit();
			document.getElementById('upload_status').value = "uploading file...";
			upload_field.disabled = true;
			return true;
		}
        
        var downloadedImages = Array();
        
        function setCanvasImage(imageSrc){
                var img = document.createElement('img');
		var div = document.getElementById('imageDiv');
                for(var x=0; x<div.childNodes.length; x++) div.removeChild(div.childNodes[x]);
                img.src=imageSrc;
                var div2=document.createElement('div');
                div2.style.display='none';
                div2.appendChild(img);
                div.appendChild(img);
                return;
                
                for(var x=0; x<downloadedImages.length; x++) {
                        if(imageSrc == downloadedImages[x]) {
                                 handleImageOnLoad(imageSrc);
                                 return;
                        }
                }
                downloadedImages[downloadedImages.length]=imageSrc;
			var image = new Image();
			image.onLoad=handleImageOnLoad(imageSrc);
			image.src=imageSrc;
			document.images[document.images.length]=image;
			return;
			//alert(imageSrc);
			if(image.width==0 && image.height ==0){
				lastCanvasImageId--;
				return;
			} //else {
				//alert(image.width+" "+image.height);
			//}
			
			var img = document.createElement('img');
			var div = document.getElementById('imageDiv');
			var divW = 800;//parseInt(div.style.width);
			var divH = 450;parseInt(div.style.height);
			//alert(div.style.width+"x"+div.style.height);
			for(var x=0; x<div.childNodes.length; x++) div.removeChild(div.childNodes[x]);
			//alert(divW+"-"+divH);
			if(image.height > divH) {
				image.width = ""+((1.0*divH/image.height)*image.width);
				image.height = divH;
			}
			if(image.width > divW) {
				image.height = ""+((1.0*divW/image.width)*image.height);
				image.width = divW;
			}
			img.src=image.src;
			//alert(image.width+" "+image.height);
			img.style.height=""+image.height+"px";
			img.style.width=""+image.width+"px";
			div.appendChild(img);
			//div.style.backgroundImage=imageSrc;
        }
        
        function handleImageOnLoad(){
                var div = document.getElementById('imageDiv');
                var images = div.getElementsByTagName("img");
                if(images.length == 0) return;
                var image = images[0];
                if(image.height == 0 || image.width == 0) return;
                var divW = 800;//parseInt(div.style.width);
		var divH = 450;parseInt(div.style.height);
		if(image.height > divH) {
			image.width = ""+((1.0*divH/image.height)*image.width);
			image.height = divH;
		}
		if(image.width > divW) {
			image.height = ""+((1.0*divW/image.width)*image.height);
			image.width = divW;
		}
		image.parentNode.style.display='block';
                return;
        //alert(imageSrc);
                var image = new Image();
                image.src=imageSrc;
                if(image.width==0 && image.height ==0){
				lastCanvasImageId--;
				return;
			}
                var img = document.createElement('img');
			var div = document.getElementById('imageDiv');
			var divW = 800;//parseInt(div.style.width);
			var divH = 450;//parseInt(div.style.height);
			//alert(div.style.width+"x"+div.style.height);
			for(var x=0; x<div.childNodes.length; x++) div.removeChild(div.childNodes[x]);
			//alert(divW+"-"+divH);
			if(image.height > divH) {
				image.width = ""+((1.0*divH/image.height)*image.width);
				image.height = divH;
			}
			if(image.width > divW) {
				image.height = ""+((1.0*divW/image.width)*image.height);
				image.width = divW;
			}
			img.src=image.src;
			//alert(image.width+" "+image.height);
			img.style.height=""+image.height+"px";
			img.style.width=""+image.width+"px";
			div.appendChild(img);
        }
        
        function noop(){}
