$(document).ready(function(){
	$(".linkit li, .linkit tr, li.linkit, td.preview, div.linkit").not(".nolink").hover(
		function(){$(this).addClass("active");},
		function(){$(this).removeClass("active");}
	).click(function(){
		window.location = $(this).find("a").get(0);
	});

	var form_options = { target: '#lastcomment', success: post_callback, beforeSubmit: pre_callback };
	$("#leavecomment").ajaxForm(form_options);

	if ($.browser.msie) {
		$(".featured li, .classics a").not(".nofloat, .h2").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		}).click(function(){
			window.location = $(this).find("a").get(0);
		});
	}

	$("#side .tracker a").click(function(){
		$("#side .tracker li").removeClass("active");
		$(this).parent().addClass("active");
		$("#latest, #monthly, #alltime, #year, #favorited").hide();
		$($(this).attr("href")).show();
		return false;
	});

	$("#side .topusers a").click(function(){
		$("#side .topusers li").removeClass("active");
		$(this).parent().addClass("active");
		$("#users_alltime, #users_recently, #users_posts").hide();
		$($(this).attr("href")).show();
		return false;
	});

	$("#close").click(function(){
		$(this).parent().slideUp();
		createCookie('hide_announce', '1', 365);
		return false;
	});

	$("#login_link").toggle(function(){
			$(this).html("Search");
			$("#search").hide();
			$("#loginbox").fadeIn("fast");
			$("#login").focus();
			return false;
	},
	function(){
			$(this).html("Login");
			$("#loginbox").hide();
			$("#search").fadeIn("fast");
			$("#search_input").focus();
			return false;
	});

	$(".back").click(function(){
		$("#loginbox, #search").hide();
		$(".userli").not(".back").fadeIn("fast");
		$(this).hide();
		return false;
	});

	$("#preview").click(function(){this.select();});

	rateGame();
	favGame();
	changeFriend();
	removeFriend();
	ajaxComments();

	$("#myfav select, #mysub select").change(function(){
		document.location=this.options[this.selectedIndex].value;
	});

	$("#username").blur(function(){
		$("#name_check").load("/register/?do=userverify&q=" + this.value);
	});

	$("#toggledelete").click(function(){
		$(".checkbox").each(function(){
			this.checked = (this.checked) ? false : true;
		});
	});

	$(".popup a").click(function(){
		var dimensions = this.getAttribute("rel").split("|");
		pop(this.href, dimensions[0], dimensions[1]);
		return false;
	});

	$("fieldset.second_half label.radio input").click(function(){
		if ($("#nudity").is(":checked")) { $("h4.rated").hide(); $("h4.adult").show(); }
		else if ($("#violence, #bloodgore, #vulgarity").is(":checked")) { $("h4.rated").hide(); $("h4.mature").show(); }
		else { $("h4.rated").hide(); $("h4.everyone").show(); }
	});

	$("#verify_email").hide();
	$("input[@type=submit]").dblclick(function(){
		$(this).attr("disabled", "disabled");
		return false;
	});
});

function rateGame() {
	$(".rate a").click(function() {
		$(".rate ul").fadeTo("fast", .0).parent().load($(this).attr("href"), {is_ajax: true});
		return false;
	});
}

function favGame() {
	$("a.changefavorite").click(function() {
		$(this).fadeTo("fast", .0).parent().load($(this).attr("href"), {is_ajax: true}, function(oRes) {
			favGame(oRes);
		});
		return false;
	});
}

function changeFriend() {
	$("#friend a").click(function() {
		$(this).fadeTo("fast", .0).parent().load($(this).attr("href"), {is_ajax: true}, function(oRes) {
			changeFriend(oRes);
		});
		return false;
	});
}

function removeFriend() {
	$("a.remove").click(function() {
		$(this).parent().parent().remove().addClass("nostripe").load($(this).get(0), {is_ajax: true});
		stripe();
		return false;
	});
}

function ajaxComments() {
	$("#comment_area .alphabet a").click(function(){
		$("#comment_area").load($(this).attr("href"), {loadpage: 1}, function(oRes) {
			ajaxComments(oRes);
			});
		return false;
	});
}

function pre_callback() {
	$(document).find(".comment_fail").remove();
	$('input[@name=addcomment]').attr("disabled", "disabled");
	if ($("#message").val() == '') {
		$('#leavecomment').before('<h3 class="fail comment_fail">Must enter comment before posting.</h3>');
		$('input[@name=addcomment]').attr("disabled", "");
		return false;
	}
	$('#leavecomment').before('<h3 class="prepost obox">Posting comment...</h3>');
}

function post_callback() {
	$(document).find('.prepost.obox').html('Your comment has been added.');
	$("#message").val(" ");
	$('#leavecomment input.submitbutton').attr("disabled", "disabled").after(" <span>Refresh to comment again</span>");
	$('#lastcomment').slideDown("fast");
}

function stripe() {
	$("tr").removeClass("odd");
	$("tr").not(".nostripe").filter(":odd").addClass("odd");
}

function catChange(category) {
	document.location="/list/category/" + category.options[category.selectedIndex].value + "/";
}

function pop(url, height, width) {
	var newwindow;
	var left = (screen.width-width)/2;
	var top = (screen.height-height)/2;
	newwindow=window.open(url,'name','top='+top+',left='+left+',height='+height+',width='+width+',resizable=no,scrollbars=no');
	if (window.focus) {newwindow.focus()}
}

function createCookie(name, value, days)
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++){
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}