$(document).ready(function(){
var DOMAIN = "lawschoolnumbers.com";
jQuery.fn.message = function(message,where) { return $(where).html(message).fadeIn(1000);}
// Prepare page
	$("#pageContentWrapperOut").corner("rounded top 12px");
	$("#pageContentWrapperIn").corner("rounded top 12px");
	if(!$.browser.msie){
		$("#tabs ul li").corner("rounded top 7px");
		$(".subMenu ul li").corner("rounded bottom 5px");
	}
// End Page Preparation	
/* Blog */
	$(".month").corner("rounded top 5px");
	$(".heading").corner("rounded top 10px");	
	$(".heading span a").bind("click",function(){
		if($(this).parent().attr("class") !== "active"){
			$(this).parent().parent().find("span").toggleClass("active");
			var a_class = $(this).attr("class");
			$("#" + a_class).parent().children().toggleClass("hidden");
		}
	});
	$("img.dashboard").one("click",function(){
		$.ajax({
			type: "GET",
			url: "ajax.php",
			data: "&do=dashboard",
			beforeSend: function(){
					$("#pageContentWrapperOut").prepend("<div id='dashboardOverlay'>&nbsp;</div><div id='dashboardContent'><img class='loading' src='http://<? echo DOMAIN; ?>/__images/loading.gif' alt='Loading...'/></div>").find("#dashboardOverlay,#dashboardContent").toggle();
				},
			dataType: "text",
			success: function(msg){
				if(msg){
					$("#pageContentWrapperOut").find("#dashboardContent").html(msg);
				}
				else{
					alert("There was an error. Please try again.");
				}
			},
			complete: function(){
				$("img.dashboard").bind("click",function(){ $("#pageContentWrapperOut").find("#dashboardOverlay,#dashboardContent").toggle(); });
			}
		});
	});
	$(".addComment").one("click",function(){
		var tid = $(this).attr("rel");
		var title = "Re: "+$(this).parent().parent().find(".post_title a").text();
		$(this).parent().append("<form method='post' action='http://<? echo DOMAIN;?>/post.php?topic="+tid+"'>Title: <input name='reply_title' type='input' value='"+title+"'/> Comment: <textarea name='post_body'></textarea><input type='submit' value='Post'/></form>");
		$(this).parent().find("textarea").autogrow();
	});
/*  End Blog */
	if($("#systemMessage").html() != ""){ $("#systemMessage").show("slow"); }
	if($("#errorMessage").html() != ""){ $("#errorMessage").show("slow"); }
/* All Full Width Tables */
	if(!$.browser.msie){
	//console.time("table")
	}
	$("table.fullWidth tr:odd").filter(":not(.last)").addClass("alt");
	if($.browser.msie){
		$("table.fullWidth tr").filter(":not(.last,.first)").bind("mouseenter mouseleave", function(){
			   $(this).toggleClass("over");
		});
	}

	$("table.fullWidth").listen('click','td,span,a,img,acronym',function(){
		$(this).parent("tr").toggleClass("clicked");
	});
	if(!$.browser.msie){
	//console.timeEnd("table")
	}
/* End All Full Width Tables */
// Bind certain selects
	$(".goOnChange select").bind("change",function(){
		var newSchool = $(this).val();
		var thisSearch = location.href.replace(/http:\/\/\w+\./,'http://'+newSchool+'.');
		window.location=thisSearch;
	});
	$("select[name=whichCycle]").bind("change",function(){
		var cycle = $(this).val();
		//var thisSearch = location.href.replace(/\&?workingCycle=[0-9]{4}/,'');
		var hasQS = location.href.replace(/\?(.*)/,'?workingCycle='+cycle);
		var newURL = location.href.replace(/\?workingCycle=[0-9]{4}/,'?workingCycle='+cycle);
		if(hasQS.match("workingCycle")){
			window.location=newURL;
		}
		else{
			window.location=hasQS+'?workingCycle='+cycle;
		}
	});
	$(".pagination select").bind("change",function(){
		var page = $(this).val();
		window.location=page;	
	});
/* End Select Binding */
/* Watch List*/
function removeWUL(user_id){
		$.ajax({
			type: 'POST',
			url: 'ajax.php',
			data: '&do=WUL&action=remove&user=' +  user_id,
			dataType: 'text',
			success: function(msg){
				if(msg != 1){
					window.location = location + "?message=notremoved";
				}
			}
		});
}

function addWUL(user_id){
		$.ajax({
			type: 'POST',
			url: 'ajax.php',
			data: '&do=WUL&action=add&user=' +  user_id,
			dataType: 'text',
			success: function(msg){
				if(msg != 1){
					var goTo = location.href.replace(/\?(.*)/,'');
					window.location = goTo + "?message=notadded"
				}
			}
		});
}
function addWULbind(){
	var addWULelement = $(this);
	var user_id = addWULelement.attr("id");
	addWULelement.fadeTo(500,.7);
	user_id = user_id.replace("w","");
	addWUL(user_id);
	if(addWULelement.parent("#user_menu")){ 
		addWULelement.fadeTo(1000,0,function(){ $(this).text("You are now watching this user.").fadeTo(1000,1); });
		addWULelement.one("click",removeWULbind);
	}
}
function removeWULbind(){
	var removeWULelement = $(this);
	var user_id = removeWULelement.attr("id");
	removeWULelement.hide();
	user_id = user_id.replace("w","");
	removeWUL(user_id);
	var table = removeWULelement.parent().parent().parent().parent().hasClass("fullWidth");
	var user_menu = removeWULelement.parent().attr("id");
	if(table == true){
		removeWULelement.parent().parent().remove();
	}
	else if(user_menu == "user_menu"){
		removeWULelement.text("Readd to Watch List").show().one("click",addWULbind);
	}
}

	$(".removeWUL").one("click",removeWULbind);
	$(".addWUL").one("click",addWULbind);
/* End Watch List */
/* Comment Management */

	$(".commentBody .delete").one("click",function(){
		var span = $(this);
		span.html("<img src='http://"+DOMAIN+"/__images/loading-small.gif' alt='loading...'/>");
		var comment_id = span.attr("id");
		comment_id = comment_id.replace("c","");
			$.ajax({
				type: 'POST',
				url: 'ajax.php',
				data: '&do=comment&action=delete&comment=' +  comment_id,
				dataType: 'text',
				success: function(msg){
					if(msg != 1){
						window.location = location + "?message=notdeleted";
					}
				}
			});	
		span.parent().parent().prev(".commentHead").andSelf().fadeTo(1000,0,function(){ $(this).remove() });
	});

/* End Comment Management */
 ////
});