$(document).ready(function() {
	
	$("#messagebox").animate({top:"40px",opacity:1},800).animate({opacity:1},1200).animate({top:"-44px",opacity:0},800);
	$(".postarea input[name=name],.postarea input[name=email]").cookieFill();
	
	$(".postarea #postform").submit(function() {
		var message = $(".postarea [name=message]").val();
		var trimmed = message.replace(/^\s+|\s+$/g, '') ;
		if(trimmed == "") {
			showError("You forgot to enter a message!");
			return false;
		}
		
		if($(".postarea [name=parent]").val() == 0 && $(".postarea [name=file]").val() == "") {
			showError("You forgot to select an image! Please try again.");
			return false;
		}
		
		$('.postarea input[name=name],.postarea input[name=email]').cookify();
	});
	
	$(".postarea input[name=password]").focus(function() {
		if($(this).attr("alt") == "") {
			$(this).attr("alt",$(this).val());
			$(this).val("");
		}
	});
	
	$(".postarea input[name=password]").blur(function() {
		if($(this).val() == "") {
			$(this).val($(this).attr("alt"));
			$(this).attr("alt","");
		}
	});
	
	$(".postblock").click(function() {
		$(this).next().find("input, textarea").focus();
	});
	
	$("#boards a").hover(function() {
		$("#boarddesc").text($(this).attr("title"));
	}, function() {
		$("#boarddesc").text("");
	});
	
	$(".replyto").click(function() {
		var id = $(this).parent().parent().attr("id").substring(1);
		var textbox = $(".postarea [name=message]");
		
		if($(".postarea").css("opacity") == 0) {
			$("#postForm").css({height:"350px"})
			$(".postarea").css({opacity:1});
			$("#hider").text("Hide");
		}
		
		if(textbox.val() == "") {
			var setTo = ">>"+id+"\n";
		} else {
			var setTo = textbox.val()+">>"+id+"\n";	
		}
		
		window.scroll(0,0);
		textbox.val(setTo);
		textbox.focus();
		
		return false;
	});
	
	$("h1").click(function() {
		var dir = location.pathname.substring(0,location.pathname.lastIndexOf('/')+1);
		document.location = dir;
	}); 
	
	$("#updatepage").click(function() {
		window.location.reload(false);
	});
	
	$("#hider").click(function() {
		if($(".postarea").css("opacity") != 0) {
			$("#postForm").stop().animate({height:"14px"}, 500);
			$(".postarea").stop().animate({opacity:0}, 500);
			$("#hider").text("Show");
			$.cookies.set("postform","hidden",{hoursToLive: 720});
		} else {
			$("#postForm").stop().animate({height:"350px"},500)
			$(".postarea").stop().animate({opacity:1}, 500);
			$("#hider").text("Hide");
			$.cookies.del("postform");
		}
	});
	
	$("#messagebox").click(function() {
		$(this).css("display","none");
	});
	
	$(".linewrap").click(function() {
		$(this).next().show();
		$(this).remove();
	});
	
	$(".icon").click(function() {
		
		if($(this).hasClass("delete")) {
			$("#deleteoverlay").stop().css("display","block").animate({opacity:1}, 500);
			$("#deleteoverlay input[name=id]").val($(this).parent().parent().attr("id").substring(1));
		}  else if($(this).hasClass("report")) {
			$("#reportoverlay").stop().css("display","block").animate({opacity:1}, 500);
			$("#reportoverlay span").text($(this).parent().parent().attr("id").substring(1));
		}
		
		$(".blackoverlay").stop().css("display","block").animate({opacity:0.8}, 500);
		
	});
	
	$(".blackoverlay, .closeoverlay").click(function() {
		$(".whiteoverlay, .blackoverlay").stop().css({opacity:0, display:"none"});
	});
	
	$("[rel=internal]").click(function() {
		var id = $(this).attr("title");
		$("#threads .reply").css("background","");
		$(".reply[id="+id+"]").css("background","#dddddd");
	});
	
	if(!(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)) { 
		$("img.external").click(function() {
			var href = $(this).parent().attr("href");
			window.open("../"+href);
			return false;
		});
		
		$("a.external").click(function() {
			var href = $(this).attr("href");
			window.open("../"+href);
			return false;
		});
	} 
	
	$("#reportoverlay .yes").click(function() {
		var id = $("#reportoverlay span").text();
		$(this).attr("href","/report/"+id);
	});
	
	
	function showError(message) {
		$("#messagebox").remove();
		$("#container").prepend('<div id="messagebox" class="error">'+message+'</div>');
		$("#messagebox").click(function() {
			$(this).css("display","none");
		});
		$("#messagebox").animate({top:"40px",opacity:1},500).animate({opacity:1},1000).animate({top:"-44px",opacity:0},500);
	}
	
});
	
function Embed(video,type) {
	switch (type) {
		case "YouTube":
			document.write('<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/'+video+'&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+video+'&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>');
		break;
		case "RedTube":
			document.write('<object height="344" width="434"><param name="movie" value="http://embed.redtube.com/player/"><param name="FlashVars" value="id='+video+'&style=redtube"><embed src="http://embed.redtube.com/player/?id='+video+'&style=redtube" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" height="344" width="434"></object>');
		break;
		default:
			return false;
		break;
	}		
}

