$(document).ready(function() {	
	
	$("#addfavorite").click(function() {
		var contestid = $("#contestid").val();
		$.post("/ajax_addfavorite.php", { contest_id: contestid },
		function(data) {
			if (data == '1') {
				$("#addfavorite").attr("src","/images/button_add_to_favorites.png");
				$("#setreminder").hide();
			} else if (data == '2') {
				$("#addfavorite").attr("src","/images/button_my_favorite.png");
				$("#setreminder").show();
			}
		});
	});
	
	$("#setreminder").click(function() {
		var contestid = $("#contestid").val();
		$.post("/ajax_addreminder.php", { contest_id: contestid },
		function(data) {
			if (data == '1') {
				$("#setreminder").attr("src","/images/button_set_a_reminder.png");
			} else if (data == '2') {
				$("#setreminder").attr("src","/images/button_reminder_set.png");
			}
		});
	});
	
	$("#loadmorebutton").click(function() {
		var curtotal = parseInt($("#curcomments").text());
		var totalcomments = parseInt($("#totalcomments").text());
		var contestid = $("#contestid").val();
		
		$.get("/ajax_loadcomments.php", { contestid: contestid, start: curtotal },
		function(data) {
			if (data == '0') {
			// error
			} else {
				$("#commentsbox").append(data);
				$("div.hidden").slideDown("fast").removeClass("hidden");
			}
		});
		
		// increment counter
		curtotal = curtotal + 3;
		if (curtotal > totalcomments) {
			curtotal = totalcomments;
			$("#loadmorebutton").hide();
		}
		$("#curcomments").text(curtotal);		
	});
	
	$("#comment_entry").click(function() {
		var value = $(this).val();
		if (value == "Write a comment...") {
			$(this).val("");
		}
	});
	
	$("#sendcomment").click(function() {
		var comment = $("#comment_entry").val();
		var contestid = $("#contestid").val();
		if (comment == "" || comment == "Write a comment...") {
			// do something to bitch that they suck at comments
		} else {
			// submit to AJAX to insert into DB
			
			$.post("/ajax_postcomment.php", { contest_id: contestid, comment: comment },
			function(data) {
				if (data == '0') {
					alert("There was an error posting your comment. Please wait until later or contact us to let us know there is a problem!");
				} else if (data == '1') {
					alert("Hey now! You've got to wait a little bit before you post another comment, cowpoke. So just chillax. Aiight?");
				} else {
					$("#commentsbox").prepend(data);
					$("div.hidden").slideDown("fast").removeClass("hidden");
					// increment counter and total
					var curtotal = parseInt($("#curcomments").text()); var tottotal = parseInt($("#totalcomments").text());
					curtotal = curtotal + 1; tottotal = tottotal + 1;
					$("#curcomments").text(curtotal); $("#totalcomments").text(tottotal);
					$("#comment_entry").val("");
				}
			});
		}
	});
	
	$("a.delcom").click(function() {
		var comid = $(this).attr("rel");
		var conid = $("#contestid").val();
		
		$.post("/ajax_delcomment.php", { contestid: conid, commentid: comid },
		function(data) {
			if (data == '0') {
				alert("There was an error deleting this comment.");
			}
		});
		$(this).parent().parent().parent().slideUp("fast");
		return false;
	});
	
	$("#winners img.clickme").click(function() {
		var winnerid = $(this).attr("rel");
		$("#winnervideo").load("/ajax_loadwinner.php?id="+winnerid+"&part=1");
		$("#winnercomment").load("/ajax_loadwinner.php?id="+winnerid+"&part=2");
	});
	
	$("#morebutton").hover(function() {
		$("#bnot").slideDown("fast");
	});
	
	$("#bnot").hover(function() {
		return true;
	}, function() {
		$("#bnot").slideUp("fast");
	});
	
	var coms = $('#vcont');
	pos = coms.offset();
});


$(window).scroll(function() {
	if ($(this).scrollTop() > pos.top && $('#bbar').hasClass('hidden')) {
		$('#bbar').animate({ top: '+=44px' }, 500);
		$('#bbar').removeClass("hidden").addClass("showing");
	} else if ($(this).scrollTop() <= pos.top && $('#bbar').hasClass('showing')) {
		$('#bbar').animate({ top: '-=44px' }, 500);
		$('#bbar').removeClass("showing").addClass("hidden");
		$("#bnot").slideUp("fast");
	}
});

