$(document).ready(function() {
	$("#logout").click( function() {
		return (confirm("Are you sure you want to logout?"));
	});

	$(".imageClick").live("click", function() {
		$.facebox("<center><img src='" + $(this).attr("src") + "' /></center>");
	});

	if($("#shoutBox:visible").length == 1) {
		$("#shoutBox").submit(function() {
			$.post("/shoutbox/add", $(this).serialize(), function(data) {
				if(typeof(data.message) != "undefined") {
					$("#layout-message").html(data.message).center().fadeIn("slow");
					$("#shout").val("");
					window.setTimeout('layoutMessage()', 3000);
					updateMajor();
				}
			}, "json");

			return false;
		});

		$("#layout-shouts").animate({scrollTop: $("#layout-shouts").attr("scrollHeight")}, 10, function() {
			window.setTimeout('updateMajor()', 20000);
		});
	}

	$("#notification").click(function() {
		var notification	= $("#notification-cell");
		var cell			= notification.html().indexOf('<li>');
		var loading			= notification.html().indexOf('loading.gif');
		if(cell == -1 && loading == -1) {
			$("#notification-cell").html("<img src='/images/misc/loading.gif' />");
			$(".notification-center").animate({width: '250px'}, 1000, function() {
				$.post("/shoutbox/notifications", {}, function(data) {
					notification.html("<ul></ul>");

					for(var i in data) {
						$("#notification-cell ul").append("<li>" + data[i] + "</li>");
					}
				}, "json");
			});
		} else if(cell > 0) {
			$("#notification").fadeOut();
		}
	});

	$.fn.center = function () {
		this.css("position","absolute");
		this.css("top", (($(window).height() - this.outerHeight(true)) / 3+$(window).scrollTop()) + "px");
		this.css("left", (($(window).width() - this.outerWidth(true)) / 2+$(window).scrollLeft()) + "px");
		return this;
	}

	$.fn.bounce = function () {
		this.animate({bottom:"+=20px"},200).animate({bottom:"-=20px"},200).animate({bottom:"+=15px"},200).animate({bottom:"-=15px"},200).animate({bottom:"+=10px"},200).animate({bottom:"-=10px"},200);
		return this;
	}
	
	$(window).resize(function() {
		if($("#layout-message:visible").length > 0) {
			$("#layout-message").center();
		}
	}).scroll(function() {
		if($("#layout-message:visible").length > 0) {
			$("#layout-message").center();
		}
	});

	updateMajor();

	if(typeof(FB) == "object") {
		FB.init({appId: '316102776614', status: true, cookie: true, xfbml: true});
	}
});

function updateMajor() {
	$.post("/shoutbox/update", {lastId:$("#shoutBox").attr("lastId")}, function(data) {
		if($("#shoutBox:visible").length == 1) {
			var lastId			= $("#shoutBox").attr("lastId");
			var newId			= lastId;
			var newClass		= "";
			var html			= "";
			var shouts			= data.shoutbox;
			var cell			= $("#notification-cell").html().indexOf('<li>');
			var notification	= $("#notification");
			var visible			= $("#notification:visible").length;

			for(var i in shouts) {
				newId		= shouts[i]['id'];

				if(newId != $("#shoutBox").attr("lastId")) {
					newClass	= ($("#layout-shouts li:last").hasClass("color-two")) ? "" : "color-two";
					html		= "<li id=\"shoutbox-" + shouts[i]['id'] + "\" class=\"" + newClass + "\"><span class=\"shoutbox-user\"><a href=\"/user/" + shouts[i]['username'] + "\">" + shouts[i]['username'] + "</a></span><br /><span class=\"shoutbox-text\">" + shouts[i]['text'] + "</span></li>";
					$(html).hide().appendTo("#layout-shouts").fadeIn();
					$("#shoutBox").attr("lastId", newId);
				}
			}

			if(newId != lastId) {
				$("#layout-shouts").animate({scrollTop: $("#layout-shouts").attr("scrollHeight")}, 3000);
			}
		}

		if(typeof data.notifications != "undefined" && data.notifications > 0) {
			if(visible == 0 || (visible == 1 && cell == -1)) {
				$("#notification-cell").html("<span>" + data.notifications + "</span>");

				if(visible == 0) {
					notification.fadeIn();
				}
			}
		}

		if(visible == 1 && cell == -1) {
			notification.bounce();
		}
	}, "json");

	window.setTimeout('updateMajor()', 10000);
}

function displayLayoutMessage(message, reload, timeout) {
	if(!reload || reload.length == 0) {
		reload	= "false";
	}

	if (!timeout || timeout.length == 0) {
		timeout =  2000;
	}

	$("#layout-message").html(message).fadeIn("slow").center();
	window.setTimeout("removeLayoutMessage('" + reload + "')", timeout);
}

function removeLayoutMessage(reload)
{
	if(reload == "true") {
		location.reload(true);
	} else if(reload.length > 0 && reload != "false") {
		window.location.href = reload;
	} else {
		$("#layout-message").fadeOut("slow");
	}
}

function layoutMessage(reload)
{
	if (typeof(reload) != "undefined" && reload.length > 0) {
		if(reload == "true") {
			window.location.reload(true);
		} else if(reload != "false") {
			window.location.href = reload;
		}
	} else {
		$("#layout-message").fadeOut("slow");
	}
}