jQuery.extend({
        postJSON: function( url, data, callback ) {
                return jQuery.post(url, data, callback, "json");
        }
});

$(document).ready(function()
{

	//$("#wysiwyg").wysiwyg({css : "/lib/styles/jts-forum-input.css"});
	$.youtubin({
	        swfWidth : '480',
	        swfHeight : '400'
	    });
	
	/*$("a.reply").bind("click",function()
	{
		$.scrollTo("#reply", 500);
		
		return false;
	});*/
	
	
	$("div.ad").load("/adserve/get/250/250");
	
	$("form:last").bind("submit", function()
	{
		$("input#save").attr("disabled", "disabled");
	});
	
	
	// Vote Code
	
	$("li.rate a").bind("click",function()
	{
		var rating = $(this);
	
		rating.parent().addClass("loading");
		rating.parent().parent().children("li.rating").fadeOut(function()
		{
		
			$.getJSON(rating.attr("href"),
			function(data)
			{
				rating.parent().parent().children("li.rate").removeClass("loading").addClass("disabled");
				
		    	if(data.code == "success")
		    	{
					rating.parent().parent().children("li.rating").html("<abbr title='" + parseInt(data.rating.plus+data.rating.minus) + " Votes'>" + parseInt(data.rating.plus-data.rating.minus) + "</abbr>").fadeIn();
		    	}
		    	else
		    	{
					rating.parent().parent().children("li.rating").html(data.message).fadeIn();
		    	}
		    });
	    });
	
		return false;
	});
	
});

function changetitle()
{
	$("ul.trail li:last a").bind("click", function()
	{
		var el = $(this);
		
		el.hide().parent().append("<input type='text' value='" + el.html() + "' id='topic_title' />");
		
		$("input#topic_title").focus().select().bind("keypress", function(e)
		{
			if(e.which == 13)
			{
				var address = el.attr("href").split("/");
				var topic_id = address[address.length-1];
				
				$.postJSON("/forum/topic_title/" + topic_id, { title: $("input#topic_title").attr("value"), reason : prompt("Tell Rollback 3000 why you want to change this title?", "Spelled incorrectly.") },
				function(data)
				{
					if(data.code == "success" || data.code == "failed")
					{
					
						el.html($("input#topic_title").attr("value"));
						el.show();
						$("input#topic_title").remove();
					}
					else
					{
						alert(data);
					}
			    });
			}
		});
		 
		return false;
	});
}

function whovoted()
{
	$("li.rating").toggle(function()
	{
		var post_id = $(this).parent().parent().parent().attr("id");
	
		$.getJSON("/forum/who_voted/" + post_id,
		function(data)
		{
			
			$(this).remove();
			if(data.code == "success" || data.code == "failed")
			{
				$("div#" + post_id + " div.x35 div.post").prepend("<ul class='votes' style='display:none;'>" + data.message + "</ul>");
				$("div#" + post_id + " ul.votes").slideDown("fast");
			}
	    });
	},
	function()
	{
		var post_id = $(this).parent().parent().parent().attr("id");
		
		$("div#" + post_id + " ul.votes").slideUp("fast", function()
		{
			$(this).remove();
		});
	});
}

function setupban()
{
	$("li.ban a").toggle(function()
	{
		$(this).parent().addClass("loading");
		var user_id = $(this).parent().attr("rel");
		var post_id = $(this).parent().parent().parent().parent().attr("id");
		
		$("div#" + post_id + " div.x35 div.post").prepend("<div class='module' style='display:none;'></div>");
		
		banformsetup(user_id, post_id);
		
		return false;
	},
	function()
	{
		var post_id = $(this).parent().parent().parent().parent().attr("id");
		
		$("div#" + post_id + " div.module").slideUp("fast", function()
		{
			$(this).remove();
		});
	});
}

function banformsetup(user_id, post_id)
{
	$("div#" + post_id + " div.module").addClass("game").load("/ban/form/" + user_id, function()
	{
		$("div#" + post_id + " li.ban").removeClass("loading");
		$(this).slideDown("fast");
		
		$("div#" + post_id + " div.module li.active ul.tools a").bind("click", function()
		{
			var elem = $(this);
			
			$.getJSON(elem.attr("href"), function(data)
			{
				if(data.code == "success")
				{
					elem.parent().parent().parent().removeClass("active");
					elem.parent().parent().remove();
				}			
			});
			
			return false;
		});
		
		$("div#" + post_id + " div.module form").bind("submit", function()
		{
			var elem = $(this);
			
			$.ajax({
				type : "POST",
				url : $(elem).attr("action"),
				dataType : "json",
				data : "length=" + $(elem + " select.length").val() + "&reason=" + $(elem + " input.reason").val() + "&email=" + $(elem + " input.email").val(),
				success : function(e)
				{
					alert(e.message);
					banformsetup(user_id, post_id);
				}
			});
			
			alert("wine");
			
			return false;
		});
	});
}