$(document).ready(function() {

	$(".min").click(function() {
		if($(this).hasClass("min")) {
			$(this).parents("div.section").children(".section_body").hide("blind",{},"fast");
			$(this).parents("div.section").children(".section_footer").hide("blind",{},"fast");
			$(this).removeClass("min");
			$(this).addClass("max");
			return false;
		} else if($(this).hasClass("max")) {
			$(this).removeClass("max");
			$(this).addClass("min");
			$(this).parents("div.section").children(".section_body").show("blind",{},"fast");
			$(this).parents("div.section").children(".section_footer").show("blind",{},"fast");
			return false;
		}
	});

	$("#collapse_all").click(function() {
		scroll(0,0);
		$(".section_body").each(function() {
			$(this).hide("blind",{},"slow");
		});
		$(".section_footer").each(function() {
			$(this).hide("blind",{},"slow");
		});
		$(".min").each(function() {
			$(this).removeClass("min");
			$(this).addClass("max");
		});
		return false;
	});

	$("#expand_all").click(function() {
		scroll(0,0);
		$(".section_body").each(function() {
			$(this).show("blind",{},"slow");
		});
		$(".section_footer").each(function() {
			$(this).show("blind",{},"slow");
		});
		$(".max").each(function() {
			$(this).removeClass("max");
			$(this).addClass("min");
		});
		return false;
	});

	var query_label = 'Enter keywords to search this page'; // default label for the search box

	$("#query").val(query_label);

	$("#query").focus(function() {
		if($(this).val() == query_label) {
			$(this).val('');
		}
	});

	$("#query").blur(function() {
		if($(this).val() == '') {
			$(this).val(query_label);
		}
	});

	$(".next").hide();

	$("#hide_message").click(function() {
		$("#message_box").hide("blind",{},"slow");
		return false;
	});

	$(".prev").click(function() {
		$(this).parents("div.section").children(".section_body").html('<p class="loading">Loading...</p>');
		var items = $(this).attr("rel"); // 10,5
		var items_array = items.split(',');
		var i_start = items_array[0] * 1;
		var i_items = items_array[1] * 1;
		var added = i_items + i_start;
		var newbut = added + i_start;
		var newitems = added + ',' + items_array[1];
		var next_start = items_array[0] - items_array[1];
		var next_items = next_start + ',' + items_array[1];
		$(this).removeAttr("rel");
		$(this).attr("rel",newitems);
		$(this).parents("ol").children("li").children("a.next").attr("rel",next_items);
		$(this).parents("div.section").children(".section_body").load($(this).attr("href") + items);
		$(this).parents("ol").children("li").children("a.next").show();
		return false;
	});

	$(".next").click(function() {
		$(this).parents("div.section").children(".section_body").html('<p class="loading">Loading...</p>');
		var items = $(this).attr("rel"); // 10,5
		var items_array = items.split(',');
		var i_start = items_array[0] * 1; // 
		var i_items = items_array[1] * 1; // 
		var previtems = i_start + i_items;
		var prevbut = previtems + ',' + i_items;
		var added = i_start - i_items; // 
		var newitems = added + ',' + items_array[1]; // 
		if(i_start == 0) {
			$(this).hide();
		}
		$(this).removeAttr("rel");
		$(this).attr("rel",newitems);
		$(this).parents("ol").children("li").children("a.prev").attr("rel",prevbut);
		$(this).parents("div.section").children(".section_body").load($(this).attr("href") + items);
		return false;
	});

	function replaceButton(message,remove,add,obj) {
		obj.text(message);
		obj.removeClass(remove);
		obj.addClass(add);
		obj.attr('title',message);
	}

	function updateItem(obj) {
		obj.css('background-image','url(/image/frontend/thumb-down_on.png)');
		setTimeout("obj.parents('li').hide('blind',{},'fast');",2500);
	}

	$(".flag").livequery('click',function() {

		obj = $(this);

		if(confirm('Are you sure you want to flag this item? This will remove the item from the site, pending moderator review.')) {

			$.get("/include/frontend/flag.php", {
				id: $(this).attr("rel")
			},
			function() {
				updateItem(obj);
			});

		}
		return false;
	});

	$(".fav").livequery('click',function() {
   
		obj = $(this);
   
		if($(this).hasClass("off")) {
			$.get("/include/frontend/favourite.php", {
				id: $(this).attr("rel"),
				action:"add"
			},
			function() {
				replaceButton("Remove from favourites","off","on",obj);
			});
			return false;
		}
		else if($(this).hasClass("on"))
			{
			$.get("/include/frontend/favourite.php", {
				id: $(this).attr("rel"),
				action:"del"
			},
			function(){
				replaceButton("Add to favourites","on","off",obj);
			});
			return false;
		}
	});

});