/* INITIALIZATION */

//global variables
var postId = 0;
var commentCount = 0;
var commentSet = 0;

$(function() {
	//initalize navlink behavior
	$(".navLink").hover(function() {
		$(this).addClass("navLinkHi");
	}, function() {
		$(this).removeClass("navLinkHi");
	});

	//initialize all links
	$("#info a").hover(function() {
		$(this).addClass("linkHi");
	}, function() {
		$(this).removeClass("linkHi");
	});
	$("#navBar a[href]").each(function() {
		$(this).attr("href", "javascript:go('"+$(this).attr("href")+"')");
	});

	//initialize animations
	$("#navBar").css({
		width: "0px"
	}).animate({
		width: "470px"
	}, 1000).animate({
		width: "460px"
	});

	$("#wrapper").css({
		top: "100%"
	}).animate({
		top: "190px"
	}, 1000).animate({
		top: "200px"
	});

	//initialize archives
	$("#archives > ul ul").hide();
	$("#archives a").click(function() {
			$(this).next("ul").toggle();
	});

	//initialize comments
	if(commentCount>10)
		$("#comments > a").eq(1).html("next");

	$("#comments form").submit(function() {
			postComment($(this));
			return false;
	}).find("*:not(textarea)").hide().end().find("textarea").focus(function() {
		$(this).removeClass("gray").html("").height("150px").siblings().show();
	});

	//initialize collapse buttons
	$("#info button").click(function() {
			$(this).parent().parent().next("div").slideToggle();
	});

	//initialize draggables
	$(".draggable").mousedown(function(e) {
		var thing = this;
		
		setMousePos(e);

		$(document).mousemove(function(e) {
			drag(e,thing);
		});

	}).mouseup(function() {
		$(document).unbind("mousemove");
		$(document.body).click();
	});

});


function getCommentCount() {
	return commentCount;
}
function getPostId() {
	return postId;
}
function setCommentCount(count) {
	commentCount = count;
	commentSets = (count/10 | 0) + 1;
}


//animates things when the user clicks a link
//then loads the new page
function go(href) {
	$("#navBar").animate({
		width: "0px"
	}, 1000);

	$("#wrapper").animate({
		marginTop: "100%"
	}, 1000, function() {window.location = href;});
}


//comment functions
function postComment(form) {
	commentCount++;
	$.get("/php/postComment.php", { name: form.find("input[name=name]").val(), site: form.find("input[name=site]").val(), text: form.find("textarea").val(), postId: postId }, function(data) {
				form.siblings("ul").html(data);
	});

	form.find("input[type=reset]").click().end().find("*:not(textarea)").hide().filter("[type=text]").addClass("gray").end().end().find("textarea").addClass("gray").height("50px").html("Leave a comment.");
}

function hideCommenter() {
	$("#comments form").find("input[type=reset]").click().end().find("*:not(textarea)").hide().filter("[type=text]").addClass("gray").end().end().find("textarea").height("50px").addClass("gray").html("Leave a comment.");
}

function nextSet(link) {
	commentSet++;
	$.get("/php/getComments.php", { postId: postId, set: commentSet }, function(data) {
		$(link).siblings("ul").html(data);
	});

	if(commentSet==1)
		$(link).prev("a").html("previous");
	if(commentSet*10+10>commentCount)
		$(link).html("");

	$(link).nextAll("em").html((commentSet+1) + "/" + (commentSets));
}

function prevSet(link) {
	commentSet--;
	$.get("/php/getComments.php", { postId: postId, set: commentSet }, function(data) {
		$(link).siblings("ul").html(data);
	});

	if(commentSet*10<commentCount)
		$(link).next("a").html("next");
	if(commentSet==0)
		$(link).html("");

	$(link).nextAll("em").html((commentSet+1) + "/" + (commentSets));
}


