var categoryHeight = 0;
var keywordsHeight = 0;
var disableInput = false;

jQuery(document).ready(function() {
	// firefox won't measure category height which we need (a bug)
	// we will use a trick: copy the categories tho the keywords
	// box and measure its height instead, which gives us the same value
	jQuery("#keywords").html(jQuery("#category").html());
	categoryHeight = jQuery("#keywords").height();
	jQuery("#keywords").empty();
	
	// bind click events
	jQuery("#category li a").bind("click", onCategoryClick);
	jQuery("#btn_search").bind("click", onBtnSearchClick);
});

function onCategoryClick() {
	if (disableInput) return false;
	disableInput = true;
	var category = jQuery(this).attr("id").substring(4);
	
	var year = jQuery("#year").val();
	
	// sitestats
	loadSitestatsImage(
		"-refine/" + year + "/" + category,
		"&action=refine&year=" + year + "&category=" + category,
		null // no redirect
	);
	
	// load keywords
	loadKeywords(category);
	return false;
}

function loadKeywords(category) {
	var year = jQuery("#year").val();
	var data = jQuery("#data").val();
	var url = "/content/list-refine-keywords.php?year="+year+"&data="+data+"&category="+category;
	jQuery("#keywords").empty();
	// show loading icon
	jQuery("#ajaxLoading2").fadeIn("fast");
	jQuery("#keywords").load(url, onKeywordsLoaded);
}

function onKeywordsLoaded() {
	jQuery("#ajaxLoading2").fadeOut("fast");

	keywordsHeight = jQuery("#keywords").height();
	if (keywordsHeight > 360) {
		// set scrollbar css styles
		jQuery("#keywords li").css("width", 100);
		jQuery("#keywords li").css("padding-right", 4);
		jQuery("#keywords").css("height", 360);		
		jQuery("#keywords").css("overflow-y", "auto");
		keywordsHeight = 360;
	}
	
	if (keywordsHeight > categoryHeight) {
		// grow box before sliding in keywords
		jQuery(".filter-box").animate({"height": keywordsHeight + "px"}, "slow", slideKeywordsIn);
	} else {
		jQuery(".filter-box").css("height", categoryHeight);
		slideKeywordsIn();
	}
}

function slideKeywordsIn() {
	jQuery("#category").css("position", "absolute");
	jQuery("#category").animate({"left": "-=131px"}, "slow");
	jQuery("#keywords").animate({"left": "-=131px"}, "slow", function() {
		jQuery("#btn_search").html("Back");
		disableInput = false;
	});
}

function slideKeywordsOut() {
	jQuery("#category").animate({"left": "+=131px"}, "slow");
	jQuery("#keywords").animate({"left": "+=131px"}, "slow", function() {
		if (keywordsHeight > categoryHeight) {
			// shrink box after sliding out keywords
			jQuery(".filter-box").animate({"height": categoryHeight + "px"}, "slow", setCssToDefault);
		} else {
			setCssToDefault();
		}																															 
	});
}

function setCssToDefault() {
	// reset css styles to default
	jQuery("#category").css("position", "relative");
	jQuery(".filter-box").css("height", "auto");
	jQuery("#keywords li").css("width", 122);
	jQuery("#keywords li").css("padding-right", 9);
	jQuery("#keywords").css("height", "auto");		
	jQuery("#keywords").css("overflow-y", "hidden");
	disableInput = false;
}

function onBtnSearchClick() {
	if (disableInput) return false;	
	var buttonHtml = jQuery("#btn_search").html();
	if (buttonHtml == "Back") {
		disableInput = true;		
		jQuery("#btn_search").html("New Search");
		slideKeywordsOut();		
		return false;	
	} else {
		return true;
	}	
}
