$(document).ready(function () {

var TOOLTIP_DELAY_MS = 75;
var TOOLTIP_FADEIN_MS = 150;
var TOOLTIP_FADEOUT_MS = 800;

function initApp() {
	$("#tabStrip a").click(tabSwitchHandler);
	$("#unlockPrereq").css("max-width",250);
	$("#unlockPrereq").fadeOut(1);
	
	
	$(".lockIcon").data("waiting",false); // store whether we are waiting to show tooltip, since there is a delay on when we present
	
	// Suppress default tooltips by moving tip to separate data store and clearing the title attribute
	// if jquery is not supported, then the title attribute will be unmodified and the user will only
	// see the default browser tooltips with the correct text
	$(".lockIcon").each(function() {
		$(this).data("toolTipText",this.title);
		this.title = "";
	})
	$(".lockIcon").mouseover(lockIconOverHandler);
	$(".lockIcon").mouseout(function() {$("#unlockPrereq").fadeOut(TOOLTIP_FADEOUT_MS);$(this).data("waiting",false)});
	
	$("#advToggle").click(advToggleHandler);
}

function tabSwitchHandler(e) {
	$("#tabStrip a").removeClass("active").addClass("inactive");
	$(this).removeClass("inactive").addClass("active");

	$("#tabsWrap *[linkedto]").hide();
	$("#tabsWrap *[linkedto="+this.id+"]").show();
}

function lockIconOverHandler(e) {
	var elm = this;
	$(this).data("waiting", true)
	setTimeout(function() {
		showDesc(elm,{x:e.pageX,y:e.pageY});
	},TOOLTIP_DELAY_MS);
}

function showDesc(elm,pos) {
	if ($(elm).data("waiting")) {
		$("#unlockPrereq").html($(elm).data("toolTipText")).css("left",pos.x).css("top",pos.y-$("#unlockPrereq").height()-20).fadeIn(TOOLTIP_FADEIN_MS);
	}
}

function advToggleHandler() {
	$("#advToggle").toggleClass("active");
	
	if($("#advToggle").hasClass("active")) {
		$("#filtersWrap").show();
		$(".filters input").attr("disabled","");
	}
	else {
		$("#filtersWrap").hide();
		$(".filters input").attr("disabled","disabled");
	}
}

initApp();

});

