
//initialize some variables
var currcount =0;
var totalitems = 0;
var dispcount = 0;
var classtofilter;
var secondclasstofilter;
var itemtofilter;

$(document).ready(function(){
	//work out what we are working with
	if ($("#w_news_list").length > 0) {itemtofilter=".news_item"};
	if ($("#w_briefing_list").length > 0) {itemtofilter=".news_item"};
	if ($("#w_vacancies_list").length > 0) {itemtofilter=".vacancy_item"};
	if ($("#w_seminar_list").length > 0) {itemtofilter=".seminar_item"};
	if ($("#w_bulletin_list").length > 0) {itemtofilter=".news_item"};
	if ($("#w_project_list").length > 0) {$("#drop_quarter, #drop_quarter_label").show();itemtofilter=".news_item"};
	if ($("#w_people_list").length > 0) {itemtofilter=".person_item"};
	
	//if either of the drop downs are changed
	$("#drop_practice, #drop_industry").change(function() {
		//get the selected value
		classtofilter=$(this).val();
		secondclasstofilter="";
		//reset value of whatever dropdown hasn't just been changed
		if ($(this).attr('id')=="drop_practice") {$("#drop_industry").val('all')}
		if ($(this).attr('id')=="drop_industry") {$("#drop_practice").val('all')};
		//clear any quarter filters
		$("#drop_quarter").val("all");
		//find the item type to be filtered
		if (($("#drop_industry").val()=="all") && ($("#drop_practice").val()=="all")) 
		{clearFilter()}
		else
		{
		secondclasstofilter = $(".active_selector").attr('id');
		if ((secondclasstofilter=="all") || (secondclasstofilter=="") || (secondclasstofilter==undefined)){
			secondclasstofilter="";
		}
		filterList(itemtofilter,classtofilter, secondclasstofilter)
		};
	});
	
	$("#drop_quarter").change(function(){
		$("#drop_industry, #drop_practice").val('all');
		if ($(this).val()=="all") {clearFilter();}
		else
		{
		filterList(itemtofilter,$(this).val(),"")};
	});
	
	$("#filter_clear").click(function() {
		//reset both drop downs
		clearFilter();
	});
	
	//back to top of list
	$("#top_nav").click(function() {
		$(itemtofilter).hide();
		$(".wanted:lt(6)").show();
		currcount = 6;
		setCountText();
	});
	
	//now initialize the list
	if ((itemtofilter==".vacancy_item") || (itemtofilter==".seminar_item")) {
	//do nothing
	}
	else {
	$("#back").hide();
	$(itemtofilter).hide();
	//count how many we have
	totalitems = $(itemtofilter).length;
	//if we have been passed a filter, use it
	if (!($("#url_param").text()=="")) {
		tmpval=$("#url_param").text();
		var filtertype = $("#url_param_type").text();
		if (filtertype=="1") {$("#drop_industry").val(tmpval).trigger('change')};
		if (filtertype=="2") {$("#drop_practice").val(tmpval).trigger('change')};
		//filterList(itemtofilter,tmpval,"");
	}
	else
	{
		//now show the first 6
		$(itemtofilter).addClass('wanted');
		if (isIE) {$(itemtofilter + ":lt(6)").show(); }else{$(itemtofilter + ":lt(6)").fadeIn(1000);}
		//should be show
		//increment the counter
		currcount = 6;
	}
	}
	
	//set the initial text to say how many there are and where we are currently
	setCountText();
	//bind click events
	$("#back").click(function(){
		$(this).hide();
		go("b");
	});
	$("#forward").click(function(){
		$(this).hide();
		go("f");
	});
});

function filterList(whatitem, whatclass, secondclass) {
		//fade in the filter clear link
		if (isIE) {$("#filter_clear").show()}else{$("#filter_clear").fadeIn(1000)};
		//hide everything
		$(whatitem).hide();
		//remove wanted class from all items which have it
		$(".wanted").removeClass('wanted');
		//add the wanted class to all relevant items
		if (secondclass=="") {
			$("." + whatclass).addClass('wanted');
		}
		else
		{
		//loop through the 1st class checking to see if it contains the 2nd class
		$("." + whatclass).each(function(){
			if ($(this).hasClass(secondclass)) {
				$(this).addClass('wanted');
			}
		});
		}
		//show the first 6 relevant items
		if (isIE) {$(".wanted:lt(6)").show()}else{$(".wanted:lt(6)").fadeIn(1000)};
		//set current count
		currcount = 6;
		//get total number of items, hidden and visible
		totalitems = $(".wanted").length;
		//hide the back link
		$("#back").hide();
		//if there's more than 6 in total, show the forward link
		if (totalitems > 6) {
		if (isIE){$("#forward").show()}else{$("#forward").fadeIn(1000)};
		}
		else
		{
		$("#forward").hide();
		}
		setCountText();
}

function go(direction){
	//hide all list elements
	$(itemtofilter).hide();
	if (direction=="f"){
		//go forward
		currcount +=6;
		//get all tagged items index greater than the current count minus 7 and show them
		if (isIE){$(".wanted:gt("+ (currcount-7) + ")").show()}else{$(".wanted:gt("+ (currcount-7) + ")").fadeIn(1000)};
		//get the tagged items index less than current count minus 6, and all greater than the current count minus 1 and hide them
		$(".wanted:lt("+ (currcount-6) + "), .wanted:gt("+ (currcount-1) + ")").hide(); 
	}
	else
	{
		//go back
		currcount -=6;
		if (currcount < 0) {currcount=0};
		if (isIE){$(".wanted:lt("+ currcount + ")").show()}else{$(".wanted:lt("+ currcount + ")").fadeIn(1000)}; //add a show
		$(".wanted:gt("+ (currcount-1) + "),.wanted:lt("+ (currcount - 6) + ")").hide(); //add a hide
	};
	//now set some visual values
	setCountText();
}

function setCountText() {
	if (currcount >=totalitems) {dispcount = totalitems}else{dispcount = currcount};
	$("#disp_count").text("Showing items " + (currcount -5) + " to " + dispcount + " of " + totalitems);
	if (currcount >=totalitems) {$("#forward").hide();$("#forward_grey").show()}else{$("#forward").show();$("#forward_grey").hide()};
	if (currcount <=6) {$("#back").hide();$("#back_grey").show()}else{$("#back").show();$("#back_grey").hide()};
	if (totalitems==0) {$("#empty_message").show()}else{$("#empty_message").hide()};
	if ($("#w_vacancies_list > h2").text()=="No documents found") {$("#w_vacancies_list > h2").hide()};
}

function clearFilter() {
		secondclasstofilter="";
		$("#drop_industry, #drop_practice").val('all');
		$(itemtofilter).hide(); //show all news items
		$(".wanted").removeClass('wanted');
		if (isIE){$(itemtofilter + ":lt(6)").show()}else{$(itemtofilter + ":lt(6)").fadeIn(1000)}; 
		$(itemtofilter).addClass('wanted');
		//clear any quarter filters
		$("#drop_quarter").val("all");
		//if there are any person filters, clear them and set the default
		if (itemtofilter==".person_item") {
		$(".active_selector").each(function(){
			$(this).removeClass("active_selector");
		});
		//set the AtoZ link as the default
		$("#all").addClass("active_selector");
		}
		//if there are any seminar filters, clear them and set the default
		if (itemtofilter==".seminar_item") {
		$(".active_selector").each(function(){
			$(this).removeClass("active_selector");
		});
		//set the AtoZ link as the default
		$("#active").addClass("active_selector").trigger('click');
		}
		//hide the clear link
		$("#filter_clear").hide();
		//default the current counter
		currcount = 6;
		//get the total number of items
		totalitems = $(itemtofilter).length;
		//hide the back link
		$("#back").hide();
		if (totalitems > 6) {
		if (isIE){$("#forward").show()}else{$("#forward").fadeIn(1000)};
		}
		else
		{
		$("#forward").hide();
		}
		setCountText();
}
