$(document).ready(function(){
	$(document).bind('keypress', function(e){
		if (e.which == 99){
			Chat.startChat();
		} else 	if (e.which == 113){
			Quote.showQuotes();
		}
	});
	
	$("#quotes .close").click(function(){
		$("#quotes").fadeOut(1000);
	});
	$("#quotes .random").click(function(){
		Quote.randomQuote();
	});
	$("#close_chat").click(function(){
		$("#chat").fadeOut(1000);
		clearTimeout($updatechats);
		$(document).bind('keypress', function(e){
			if (e.which == 99){
				Chat.startChat();
			} else 	if (e.which == 113){
				Quote.showQuotes();
			}
		});
	});
	$("#message").click(function(){
		if($(this).val() == "type your message here") {
			$(this).val("");
		}
	});
	$("#send_button").click(function(){
		Chat.sendChat();
	});
	$("#sender_name a").click(function(){
		Chat.renameUser();
	});
	$("#message").keypress(function(e){
		if (e.which == 13) {
			Chat.sendChat();
		}
	});
	runMe();
})
function runMe(){
	if(window.location.hash) {
		if(window.location.hash == "#chat") {
			setTimeout(Chat.startChat(), 2000);
		} else if (window.location.hash == "#quote") {
			setTimeout(Quote.showQuotes(), 2000);
		}
	}
}

function checkKeyPress(e) {
	if (e.which == 99){
		Chat.startChat();
	} else 	if (e.which == 113){
		Quote.showQuotes();
	}
}

function resizeImage(){
	var newWidth = window.innerWidth;
	var newHeight = window.innerWidth/1.6;
	$("#background_image").attr({width: newWidth, height: newHeight});	
}

var Quote = {
	
	showQuotes : function() {
		$("#quotes").fadeToggle(1000);
		document.location = "#quote";
		if ($("#quotes .quote_text").html() == "") {
			var number = Quote.generateNum();
			if (myQuotes[number] == $("#quotes .quote_text").html()) {
				var number = Quote.generateNum();
				$("#quotes .quote_text").html(myQuotes[number]);
			} else {
				$("#quotes .quote_text").html(myQuotes[number]);
			}
		}
	},
	
	randomQuote : function() {
		var number = Quote.generateNum();
		$("#quotes .quote_text").fadeOut(500);
		if (myQuotes[number] == $("#quotes .quote_text").html()) {
			var number = Quote.generateNum();
			setTimeout("$(\"#quotes .quote_text\").html('"+myQuotes[number]+"')", 750);
		} else {
			setTimeout("$(\"#quotes .quote_text\").html('"+myQuotes[number]+"')", 750);
		}
		setTimeout("$(\"#quotes .quote_text\").fadeIn(1000)", 1000);
	},
	
	generateNum : function() {
		var numQuotes = myQuotes.length;
		var number = Math.floor(Math.random()*numQuotes);
		return number;
	}
	
}

var myQuotes = [
	"have passion, it’ll help with creativity",
	"user experience is just that, an experience. make it one to remember",
	"do something good, every day",
	"design is not just what it looks like and feels like. design is how it works.",
	"everything is designed. few things are designed well."
]

jQuery.fn.fadeToggle = function(speed, easing, callback) { 
   return this.animate({opacity: 'toggle'}, speed, easing, callback); 
};

/* Start Chat System */
var Chat = {
	
	startChat : function(){
		$("#quotes").fadeOut(1000);
		$("#chat").fadeToggle(1000);
		Chat.loadChats();
		$(document).unbind('keypress');
		document.location = "#chat";
	},
	
	loadChats : function(){
		$("#show_chats").load("/backend/show_chats.php");
		$updatechats = setTimeout("Chat.loadChats()", 4000);
	},
	
	sendChat : function(){
		var username = $("#username").val();
		var text = $("#message").val();
		if (text == "") {
			alert("No message, nice.")
		} else {
			$.ajax({
				type: "POST",
				url: "/backend/send_chat.php",
				data: "m="+text+"&u="+username,
				success: function(msg){
					clearTimeout($updatechats);
					Chat.loadChats();
					$("#message").val("");
				}
			});
		}
	},
	
	renameUser : function() {
		var username = prompt("Please enter your name:","");
		if (username.toLowerCase() == 'jarques') {
			var pword = prompt("That name is reserved, please enter keycode:","");
			$.ajax({
				type: "POST",
				url: "/backend/reserved_names.php",
				data: "n="+username+"&p="+pword,
				success: function(msg){
					if(msg == "correct") {
						$("#sender_name").html("<a href='javascript:void(0)'>" + Chat.stripHtml(username) + "</a>");
						$("#username").val(Chat.stripHtml(username));
					} else {
						alert("Sorry, keycode was not correct.")
					}
				}
			});
		} else {
			$("#sender_name").html("<a href='javascript:void(0)'>" + Chat.stripHtml(username) + "</a>");
			$("#username").val(Chat.stripHtml(username));
		}
	},
	
	stripHtml : function(username) {
		return username.replace(/<\/?[^>]+>/gi, '');
	},
	
	isOnline : function() {
		var username = $("#username").val();
	}	
}