// what happens on page load
$(document).ready(function() {
	initLoginStatus();
	
	$("#tabs > ul").tabs();
	
	$("#bio,#url").editable("profile.php", {
		id: "attribute",
		name: "value",
		submitdata: { name: $.cookie("name"), hash: $.cookie("hash") },
		placeholder: "[ Click to edit ]",
		onblur: "submit",
		style: "inherit"
	}).hover(
		function() { $(this).addClass("editablehover"); },
		function() { $(this).removeClass("editablehover"); }
	);
	
	if (xmlFilename != "") {
		var so = new SWFObject('http://thisismyjam.com/flash/jam.swf?'+xmlFilename,'mpl','400','250','8');
		so.addParam('allowscriptaccess','always');
		so.addParam('allowfullscreen','true');
		so.addParam('bgcolor','#000000');
		so.addVariable('height','400');
		so.addVariable('width','250');
		so.write('player');
		
		var pityb = so.getSWFHTML();
		$("#pityb").val(pityb.substring(0, pityb.length - 2) + "></embed>");
	}
	
	$("a.copyjam").hover(
		function() { $("#jamdetail img.track").addClass("copy"); },
		function() { $("#jamdetail img.track").removeClass("copy"); }
	);
	
	$("a.deljam").click(function() {
		var timestamp = this.id;
		
		tb_show(null, "confirm.html", false, function() {
			$("#TB_ajaxContent input[type=button]").click(function() {
				if ($(this).val() == "Yes") {
					$.post("rmjam.php", {name: $.cookie("name"), hash: $.cookie("hash"), timestamp: timestamp}, function() {
						window.location.href = "http://" + $.cookie("name") + ".thisismyjam.com/";						
					});
				} else {
					tb_remove();
				}
			});
		});
		
		return false;
	}).hover(
		function() { $("#jamdetail img.track").addClass("del"); },
		function() { $("#jamdetail img.track").removeClass("del"); }
	);
	
	$("a.editjam").hover(
		function() { $("#jamdetail img.track").addClass("edit"); },
		function() { $("#jamdetail img.track").removeClass("edit"); }
	);
	
	$("a.favoritejam").click(function() {
		if ($.cookie("name") != null) {
			toggleFavorite();
		} else {
			tb_login(postFavorite);
		}
		
		return false;
	});
	
	$("#addfriend,#delfriend").click(function() {
		if ($.cookie("name") != null) {
			toggleFriend();
		} else {
			tb_login(postFriend);
		}
		
		return false;
	});
	
	$("img.track").tooltip();
	
	$("div.link input").focus(function() { this.select(); }); //.keypress(function() { return false; });
	
	$("#add-comment").click(function () {
		$("#new-comment textarea,#new-comment input").attr("disabled", "true");
		
		$.post(
			"comment.php",
			{
				name: $.cookie("name"),
				hash: $.cookie("hash"),
				mixid: mixid(),
				comment: $("#new-comment textarea").val()
			},
			function(data) {
				$("#new-comment").slideUp(500);
				$("#comments > p").slideUp(500);
				
				$('<div class="comment" style="display:none;"><span class="timestamp">0 minutes ago</span><span class="user"><a href="http://' + $.cookie("name") + '.thisismyjam.com/">' + $.cookie("name") + '</a></span><p>' + $("#new-comment textarea").val() + '</p></div>').prependTo("#comments").slideDown(500);
				$("#tabs ul li:eq(1) a").text("Comments (" + $(".comment").size() + ")");
			}
		);
	});
	
	$("a#action-login,a#comments-login").unbind("click").click(function() {
		tb_login(function() { activateMyControls(); updateStatus(); activateComments(); }, "What's your username and password?");
		return false;
	});
	
	$("a#action-logout").click(function() {
		$(".mycontrols").slideUp(250, function() { $(".mycontrols").remove(); });
		$("#delfriend").fadeOut(250, function() { $("#addfriend").fadeIn(250); });
		$("#new-comment").slideUp(500);
		return false;
	});
});

function mixid() {
	var mixid = /([0-9a-z\-]+)\/([0-9]{14})/(xmlFilename);
	return mixid[2] + "-" + mixid[1];
}

function toggleFavorite() {
	$.post(
		"togglefavorite.php",
		{name: $.cookie("name"), hash: $.cookie("hash"), mixid: mixid()},
		function(data) {
			if (data.status == true) {
				if ($("a.favoritejam").text().substr(0, 1) == "+") {
					$("a.favoritejam").fadeOut(250, function() {
						$(this).text("- favorite").fadeIn(250);
					});
				} else {
					$("a.favoritejam").fadeOut(250, function() {
						$(this).text("+ favorite").fadeIn(250);
					});
				}
			}
		},
		"json"
	);
}

function postFavorite() {
	activateMyControls();
	activateComments();
	
	$.getJSON("isfavorite.php", {name: $.cookie("name"), hash: $.cookie("hash"), mixid: mixid()}, function(data) {
		if (data.status == true) {
			$("a.favoritejam").fadeOut(250, function() {
				$("#isfavorite").fadeIn(250, function() {
					function holdMsg() {
						$("#isfavorite").fadeOut(250, function() {
							$("a.favoritejam").text("- favorite").fadeIn(250);
						});
					}
					
					setTimeout(holdMsg, 500);
				});
			});
		} else {
			toggleFavorite();
		}
	});
}

function toggleFriend() {
	if ($("#addfriend:visible").size()) {
		var clicked = "#addfriend", other = "#delfriend";
	} else {
		var clicked = "#delfriend", other = "#addfriend";
	}
	
	if ($.cookie("name") != $("#username").val()) {
		$.post(
			"togglefriend.php",
			{name: $.cookie("name"), hash: $.cookie("hash"), friend: $("#username").val()},
			function(data) {
				if (data.status == true) {
					$(clicked).fadeOut(250, function() { $(other).fadeIn(250); });
				}
			},
			"json"
		);
	} else {
		$(clicked).fadeOut(250, function() { $("#myprofile").fadeIn(250); });
	}
}

function postFriend() {
	activateMyControls();
	activateComments();
	
	$.getJSON("isfriend.php", {name: $.cookie("name"), hash: $.cookie("hash"), friend: $("#username").val()}, function(data) {
		if (data.status == true) {
			if ($("#addfriend:visible").size()) {
				var clicked = "#addfriend", other = "#delfriend";
			} else {
				var clicked = "#delfriend", other = "#addfriend";
			}
			
			$(clicked).fadeOut(250, function() {
				$("#arefriends").fadeIn(250, function() {
					function holdMsg() {
						$("#arefriends").fadeOut(250, function() {
							$(other).fadeIn(250);
						});
					}
					
					setTimeout(holdMsg, 500);
				});
			});
		} else {
			toggleFriend();
		}
	});
}

function activateMyControls() {
	if ($.cookie("name") == $("#username").val()) {
		$(".mycontrols").slideDown(250);
	}
}

function updateStatus() {
	if ($.cookie("name") != $("#username").val()) {
		$.getJSON("isfriend.php", {name: $.cookie("name"), hash: $.cookie("hash"), friend: $("#username").val()}, function(data) {
			if (data.status == true) {
				$("#addfriend").fadeOut(250, function() { $("#delfriend").fadeIn(250); });
			}
		});
	} else {
		$("#addfriend").fadeOut(250, function() { $("#myprofile").fadeIn(250); });
	}
}

function activateComments() {
	if ($(".comment").size() == 0) { $("#comments p").html("No comments &ndash; be the first!"); }
	$("#new-comment").slideDown(500);
}
