		function liveWindows() {
			$('#dialog_hanger .dialog').dialog({
				resizable: false, 
				modal: false,
				open: function() {
					var $self = $(this);
					$(this).dialog("option", "title", $(this).children("#title").html());
					var x = parseFloat($(this).css('left'));
					var y = parseFloat($(this).css('top'));
					$(this).dialog("option", "position", [x, y]);
//					$(this).children('#chat_response').load("ajax/chatroomlist.php?room_id=" + $(this).attr('id'), { }, $(this).children('#chat_response').animate( {scrollTop: $(this).children('#chat_response').attr("scrollHeight")}, 300) );
					$.get("ajax/chatroomlist.php",  {
						room_id: $(this).attr('id')
					}, function(data) {
					//	alert(data);
						$self.children('#chat_response').append(data);
						$self.children('#chat_response').animate( {scrollTop: $self.children('#chat_response').attr("scrollHeight")}, 0) ;
					});
					
				},
				close: function() {
//					alert("This closes the dialog box");
					$.get("ajax/chatroomlist.php",
					{
						type: "hide",
						room_id: $(this).attr('id'),
						last: $(this).children('#chat_response').children('div:last').attr('id')
					}, 
					function(data) {
//						alert(data);
					});
					$(this).html("");
				},
				dragStop: function(event, ui) {
					//alert(print_r(event));
					//alert($(this).attr('id'));

//					var g = this;
//					alert(ui.position.left+ ", " + ui.position.top);
					$.get("ajax/chatroomlist.php", 
						{
							type: "dialogposition",
							room_id: $(this).attr('id'),
							left: ui.position.left,
							top: ui.position.top
						}
					)
					//$('#Notification').jnotifyAddMessage({ text: position, permanent: false, type: 'error' });
					//alert("My new position will be taken from here");
				}
			});
			$('.dialog > #controller > #invite').click(function() {
				$self = $(this);
				$("#inviter").dialog({
					title: "Invite Friends",
					modal: true,
					resizable: false,
					buttons: {
						"Cancel" : function() {
							$(this).dialog("close");
						},
						"Ok" : function() {
//							alert($(this).children("#membs").attr("id"));
							$.get("ajax/chatroomlist.php", 
								{
									type: "invitemulti",
									room_id: $self.parent().parent().attr('id'),
									entrents: $(this).children("#membs").val().join(",")
								},
								function(data) {
//									alert(data);
								}
							);
							$(this).dialog("close");
						}
					}
				});
				
				//alert($self.parent().parent().attr('id'));
				$("#inviter").html("Loading list...");
				$("#inviter").load("ajax/chatroomlist.php?type=buildfriends&room_id=" + $self.parent().parent().attr('id'));				
				$("#inviter").dialog("open")
			});
			$('.dialog > #controller > #leave').click(function () {
				var g = this;
//				alert("This will get you to leave the chat... eventually! HAHAHA!");
				$.get("ajax/chatroomlist.php",
					{
						room_id: $(g).parent().parent().attr('id'),
						type: "leave"
					}
				);
				$(this).parent().parent().parent().html("");
			});			
			$('.dialog > #chat_send > input#text').keydown(function(event) {
				if (event.which == 13) {
					$(this).siblings('div').children('button#send_button').click();
				}
			});
			$('.dialog > #chat_send > div > button#send_button').click(function() {
		//		if (trim($(this).parent().siblings('#text').attr('value')) == "") return false;
//				alert("This should still work");
				var g = this;
				$.post("ajax/chatroomlist.php?type=add",
					{ 
						room_id: $(this).parent().parent().parent().attr('id'),
						data: $(this).parent().siblings('#text').attr('value')
					}
				);
				$(this).parent().siblings('#text').attr('value', '');
				if ($(g).parent().parent().siblings('#chat_response').html() == "") {
					$(g).parent().parent().siblings('#chat_response').html("<div id='0'></div>");
				}

				$.get("ajax/chatroomlist.php",
					{
						room_id: $(this).parent().parent().parent().attr('id'),
						last: $(this).parent().parent().siblings('#chat_response').children('div:last').attr('id')
					}, 
					function(data) { 
						$(g).parent().parent().siblings('#chat_response').append(data);
						if (data != '') {
							$(g).parent().parent().siblings('#chat_response').animate( {scrollTop: $(g).parent().parent().siblings('#chat_response').attr("scrollHeight")}, 3000);
						}						
					}
				);
//				$(this).parent().parent().siblings('#chat_response').remove();
				
			}); 
			$('.dialog').dialog("open");
		}

		$(document).ready(function(){
			$('table tr a#recover').click(function() {
				$self = $(this);
				
//				alert("This is where we reopen chats");
				$.get("ajax/chatroomlist.php",
					{
						type: "show",
						room_id: $(this).parent().parent().attr('id')
					}, 
					function(data) {
//						alert(data);
						$('#dialog_hanger').append(data);
						liveWindows();
						$self.remove();
					});
				
			});
			$('table tr a#kill').click(function() {
				$self = $(this);
				
//				alert("This is where we reopen chats");
				$.get("ajax/chatroomlist.php",
					{
						type: "destroy",
						room_id: $(this).parent().parent().attr('id')
					}, 
					function(data) {
						$self.parent().parent().remove();
						alert("Chatroom destroyed");
					});
				
			});
		
/*			$("div#link_friends").hover(
				function() {
					$("div.tip").load("ajax/friends.php").show("fast");
				}, 
				function() {
					$("div.tip").hide("slow");
				}
			)
			$("div#link_battles").hover(
				function() {
					$("div.tip").load("ajax/battles.php").show("fast");
				}, 
				function() {
					$("div.tip").hide("slow");
				}
			)
			$("div#link_messages").hover(
				function() {
					$("div.tip").load("ajax/messags.php").show("fast");				
				}, 
				function() {
					$("div.tip").hide("slow");
				}
			) */
				
			$("form#signin").submit(function() {
				var $self = $(this);
				$("#dialog").dialog({
					title: "Login",
					resizable: false, 
					modal: true
				});
				$('#dialog').html("Please wait...");
				$("#dialog").dialog("open");
				if ($self.data('isValidated') !== true) {
					$.post('signin.php',
						{	
							ajax: 1,
							username: $("input#username").val(),
							password: $("input#password").val()
						},
						function(data) {
							if (data == "1") {
								$self.data('isValidated', true).trigger('submit');
							} else {
								$self.data('isValidated', false);
								$('#dialog').html(data);
							}
						}
					); 
					return false;
				} 
				return true;
			});

/*			$('#signin').click(function() {
				//alert($("#login-menuitem").attr("left"));
				alert($("#login-menuitem").offset().left);
				$('#tip')
					.attr({ style: "position: absolute; left: " + $("#login-menuitem").offset().left + "; top : 50; width: 150; height: 200;" });
//					.html("{login box will go here... yknow<input value='' name='username'><input type=password name='password' value=''><input type=submit value='->'>}")
					.show("slow");
				alert("Hello" );
				return false;
			}) */
			liveWindows();
	//		$('.dialog').dialog("option", "title", function() { alert($(this)); );
			
			$('#signout').click(function() {
				$('#dialog').html("Please wait while we log you off...");
				$('#dialog').dialog({
					title: 'Log off',
					closeOnEscape: false,
					resizable: false, 
					modal: true
				});
				$('#dialog').dialog("open");
			});
		
			$('#Notification')
				.jnotifyInizialize({
					oneAtTime: false,
					appendType: 'append'
				})
				.css({ 'position': 'absolute',
					'marginTop': '20px',
					'right': '20px',
					'width': '250px',
					'z-index': '9999'
				});

/*			$("#battles").hover(function(){
				$("#battlelist").show("slow");
				flag = 1;
			//	alert ("Flag is 1");
			}, function() {
				flag = 0;
				setInterval(function(){	if (flag == 0) { $("#battlelist").hide("slow"); } }, 3000);
			});
			$("#battlelist").hover(function(){
				flag = 1;
			//	alert ("Flag is 1");
			}, function() {
				flag = 0;
				setInterval(function(){	if (flag == 0) { $("#battlelist").hide("slow"); } }, 3000);
			}); */
		}); 
		function refreshScreen() {
			$.getJSON("ajax/friends.php", { id: $("#loglist > div:first").attr("id") },
				function (data) {
					$('#friends').html(data.friends);
					$('#battlelist').html(data.battles);
					if (data.log != null) {
						for (items in data.log) {
							$('#loglist').prepend(data.log[items]);
							$('#loglist > div:last').remove();
						}
					}
				})
//			$('#Notification').jnotifyAddMessage({ text: 'Friends keepalive complete.', permanent: false, showIcon: false	});
//			$("#battlelist").load("ajax/battles.php");
//			$('#Notification').jnotifyAddMessage({ text: 'Battles keepalive complete.', permanent: false, showIcon: false	});
//			$("#loglist").load("ajax/loglist.php");
//			$('#Notification').jnotifyAddMessage({ text: 'Log refresh complete.', permanent: false, showIcon: false	});
			//$("#loglist a[title]").tooltip('#tip');
			$('.dialog > #chat_response').each(function() {
				var g = this;
				//alert($(this).attr('id'));
				//$(this).parent().siblings('#text').attr('value', '');
				if ($(this).html() == "") {
					$(this).html("<div id='0'></div>");
				}

				$.get("ajax/chatroomlist.php",
					{
						room_id: $(this).parent().attr('id'),
						last: $(this).children('div:last').attr('id')
					}, 
					function(data) { 
						$(g).append(data);
						if (data != '') {
							$(g).animate( {scrollTop: $(g).attr("scrollHeight")}, 3000);
						}
					}
				);
			});
		}
		setInterval("refreshScreen();", 10000 );
