$(document).ready(function(){


	/* adding that big arrow */
	$("#page-container").prepend("<div id=\"big-arrow\"><div class=\"t\"></div><div class=\"b\"></div></div>");

	/* adding the .first class to the first menu item */
	//$("#nav li:first").addClass("first"); // not needed anymore


	/* ajax stuff */
	//init_ajax(true);

	/* faq stuff */
	$(".faq li:not(.section, .on) > *").not("h2").hide();
	$(".faq li h2 a").click(function(){
		$(this).parents("li").children().not("h2").slideToggle("slow");
		return false;
	});

	$("#subscribe .submit").click(function(){
		window.open("subscribe.php?email="+$("#subscribe .text").val());
		return false;
	});

	/* screenshots */
	$(".screenshots li > a").hide();
	$(".screenshots li h2 a").click(function(){
		var temp_li = $(this).parents("li");
		$(temp_li).children("a").slideToggle("slow");
		return false;
	});

	/*expand collapse stuff */
	$(".faq, .screenshots").before("<p><a href='#' class='expand-all'>Expand All</a> | <a href='#' class='collapse-all'>Collapse All</a></p>");

	$(".collapse-all").click(function(){
		$(".faq li:not(.section, .on) > *").not("h2").hide();
		$(".screenshots li > a").hide();
		return false;
	});
	$(".expand-all").click(function(){
		$(".faq li:not(.section, .on) > *").not("h2").show();
		$(".screenshots li > a").show();
		return false;
	});
});
function init_ajax(first_time)
{
	if ( first_time )
	{
		$("#nav a").click(function(){
			$("#nav a.on").removeClass("on");
			$(this).addClass("on");
			ajax_inject($(this).attr("href"));
			return false;
		});
	}
	$("#subnav .tabs a").click(function(){
		$("#subnav li.on").removeClass("on");
		$(this).parent().addClass("on");
		ajax_inject($(this).attr("href"), this);
		return false;
	});
}
function ajax_inject(url, caller)
{
	$.ajax({
		dataType : "html",
		url: url+"?ajax",

		beforeSend: function(){
			$("#text > *").not("#subnav").fadeOut("slow");
			$("#text").addClass("loading");
		},
		success: function(data){
			$("#text").empty();
			var x= jQuery(data);
			//$("#text").html($(x).html());
			$("#text").replaceWith($(x));
			$("#text > *").not("#subnav").hide().fadeIn("slow");
			$("#text").removeClass("loading").removeClass("error");
			init_ajax(false);
		},
		error: function(){
			$("#text > *").not("#subnav").remove();
			$(caller).addClass("error");
			$("#text").append("<h1>Error</h1><p>The page you wanted could not be loaded</p>").addClass("error").removeClass("loading");
		}

	});
}
