var latest_request_id = "";

function onToggleClick()
{
	var li = $(this);
	li.toggleClass("toggle-on");
	
	//var newval = li.hasClass("toggle-on") ? "" : "disabled";
	if (li.hasClass("toggle-on"))
	{
		li.children("input:hidden").removeAttr("disabled").attr('checked', true);
	}
	else
	{
		li.children("input:hidden").attr("disabled", "disabled").attr('checked', false);
	}
	
	
	//alert($("#searchform").serialize());
	$("#unit-view-more-movies ul li").fadeOut();
	$("#unit-view-more-movies ul").addClass("loading");
	
	var id = Math.random()*99999;
	latest_request_id = id;
	
	$.post("/mmlib/includes/firstlight/ajax.movie_search.php?search=1", $("#searchform").serialize(),
		function(data)
		{
			
			//console.log(id + " == " + latest_request_id);
			
			if (id == latest_request_id)
			{
				$("#unit-view-more-movies ul").html(data);
				$("#unit-view-more-movies ul").removeClass("loading");
				registerMovies( true );
			}
		}, "html");
	
	//return false so the browser doesn't jump around
	return false;
}

function registerMovies( updateHash )
{
	$("#unit-view-more-movies li").each(function(){
		//get the movie slug
		var slug = $(this).attr("title");
		
		if( updateHash ) {
			$('#searchform li.toggle-on input').hashvalues({prefix: $.hashvalues.prefix()});
		}
		
		$(this).find("a").click(function(){
			
			//change the hash
			//window.location.hash = slug;
			
			$('#searchform li.toggle-on input').hashvalues({prefix: slug});
			$("#main-content #unit-showcase").children().fadeOut();
			$("#main-content").addClass("loading");

			try{
				if(pageTracker){
					pageTracker._trackPageview('/movies/' + slug);
				}
			}catch(e){}
			
			//do the work to load in the new movie
			$.post("/mmlib/includes/firstlight/ajax.movie_page.php", {movie_id: slug},
				function(data)
				{
					$("#main-content").removeClass("loading");
					$("#main-content").html(data);
					
					registerComments();
					
				}, "html");
			return false;
		});
	});
}

function registerComments()
{
	$("#comments-form").submit(function(){
		//use ajax to post the comments
		$.post("/mmlib/includes/firstlight/ajax.movie_comments.php?post=1", $("#comments-form").serialize(),
			function(data)
			{
				if (data.success)
				{
					$("#unit-post-comments form").fadeOut("fast", function(){
						$("#unit-post-comments form").html("<p>" + data.message + "</p>");
						$("#unit-post-comments form").fadeIn();
					});
				}
				else
				{
					var list = "";
					
					for (var i = 0;i<data.errors.length;i++)
					{
						list += "<li>" + data.errors[i] + "</li>";
					}
					
					var html = '<div id="errorwrapper"><p>Your comment could not be posted because of the following problems:</p><ul>' + list + '</ul></div>';
					var e = $(html);
					e.hide();
					
					$("#errorwrapper").remove();
					$("#unit-post-comments form").prepend(e);
					e.fadeIn("fast");
				}
			}, "json");
			
		return false;
	});
}

$(document).ready(function(){

	$("#unit-awards li, #unit-themes li, #unit-genres li, .award-list li").click(onToggleClick);
	
	
	$("#unit-groups-and-orgs h4, #unit-awards-schemes h4").click(function(){
		$(this).next().slideToggle("fast");
		$(this).parent().toggleClass("expanded-list");
		
		return false;
	});
	registerMovies( false );
	registerComments();
//	$(".unit input:hidden").attr("disabled", "disabled");
});
