//Javascript Document

function MakeExpandable( _t ) {
	var t = (typeof _t == "string") ? $(_t) : _t;
	t.find("h4").click(function() {
		var r = 125;
		var h = $(this).next("ul").innerHeight();
		
		if( $(this).next("ul").children().is(":hidden") ) {
			$(this).next("ul").children().show();
			h = $(this).next("ul").innerHeight()
			$(this).next("ul").children().hide();
		}
		
		Veil.toOpen.duration = ( (h < r*2) ? h : r*2) / 125 * 1000;
		Veil.toClose.duration = Veil.toOpen.duration * .6;
		if( !$(this).next("ul").is(":animated") ) {
			$(this).toggleClass("ctrl-expand");
			Veil.showHide($(this).next("ul"));
		}
	});
}

function MakeStep2AJAX(_href) {
	$("#step-2 h4").next("ul").find("a").click(function(e) {
		handleDocumentChange( $(this).attr("id") );
		return false;
	});
}

function hashLinks(_select) {
	$(_select + "[href]").each(function() {
		$(this).attr("href", "#/" + $(this).attr("href").replace(/^http:\/\/\w+\.wattsradiant\.com\/support\/literature\/?|^#\//,"") ).address();
	});
}

function handleSubjectChange(_id, _dur, _idDocument) {
	var dur = (_dur) ? _dur : 600;
	$("#step-3 .content").fadeOut(0, function(){ $(this).html(""); });
	$("#step-1").find(".active-true").removeClass("active-true").addClass("active-false");
	$("#step-1").find("a[id=" + _id + "]").closest("li").removeClass("active-false").addClass("active-true");
	$("#step-2 .screen-loading").fadeIn(dur/2);
	$("#step-2 .content ").fadeOut(dur/2, function() {
		$(this).load( ("/support/literature/subject/" + _id), {}, function() {
			hashLinks(".lit-documents a");
			Veil.hide($("#step-2 h4").next("ul"), {duration:2,easing:"jswing"});
			Veil.show($("#step-2").find("a[id=" + _idDocument + "]").parents("ul"), {duration:2,easing:"jswing"});
			MakeExpandable("#step-2");
			// Make Step 2 AJAX links
			MakeStep2AJAX();
			
			$("#step-2 .screen-loading").fadeOut(dur/2);
			$(this).fadeIn(dur);
			
			if(_idDocument) {
				handleDocumentChange(_idDocument, _dur);
			}
		});
	});
}

function handleDocumentChange(_id, _dur) {
	var dur = (_dur) ? _dur : 600;
	$("#step-2").find(".active-true").removeClass("active-true").addClass("active-false");
	$("#step-2").find("a[id=" + _id + "]").closest("li").removeClass("active-false").addClass("active-true");
	Veil.show($("#step-2").find("a[id=" + _id + "]").closest("ul"), {duration:2,easing:"jswing"});
	$("#step-3 .screen-loading").fadeIn(dur/2);
	$("#step-3 .content").fadeOut(dur/2, function() {
		$(this).load( ("/support/literature/document/" + _id), {}, function() {
			$(this).find(".download").tabs({fx: {height: 'toggle', duration: 600}});
			$("#step-3 .screen-loading").fadeOut(dur/2);
			$(this).fadeIn(dur);
		});
	});
}

function makeHashParams() {
	var locHash = window.location.hash.replace(/^#\/?/,"");
	return (locHash.length > 0) ? locHash.split("/") : null;
}

$(function() {

	// Determine hash value. We need to eliminate the deep pathing that is present in the url from the hash
	// incase the visitor book marked a page/link when visiting without JS enabled then foolwed that link with JS enabled.
	var locPath = window.location.pathname.replace(/\/$/, "").replace(/^\/support\/literature\/?/, "");
	var hashParams = makeHashParams();
	
	// Handle externel hash request
	$.address.externalChange(function(e){
		hashParams = makeHashParams();
		if(hashParams) {
			if(hashParams.length == 2) {
				handleSubjectChange(hashParams[0],600,hashParams[1])
			} else {
				handleSubjectChange(hashParams[0],600)
			}
		} else {
			$("#step-2 .content, #step-3 .content").fadeOut(300, function(){ $(this).html(""); });
			$("#step-1").find(".active-true").removeClass("active-true").addClass("active-false");
		}
	});
	/*
	$.address.internalChange(function(e){
		var hash = window.location.hash.replace(/^#\/?/,"").split("/");
		alert( hash.length );
		if(hash) {
			if(hash.length == 2) {
				handleDocumentChange(hash[1],2)
			} else {
				handleSubjectChange(hash[0],2)
			}
		}
	});
	*/
	hashLinks(".lit-subjects a, .lit-documents a");
	
	// Make categories collapsable //
	// Setup Veil easing
	Veil.toOpen.easing = "easeInOutQuad";
	Veil.toClose.easing = "easeInOutQuad";
	// Initially hide all ul's.
	Veil.hide($("#step-2 h4, #step-1 h4").next("ul"), {duration:2,easing:"jswing"});
	// Veil.hide($("#step-2 h4").next("ul"), {duration:10,easing:"jswing"});
	// Show any ul's that have an active li.
	if(hashParams) {
		Veil.show($("#" + hashParams[0]).parents("ul"), {duration:2,easing:"jswing"});
		if(hashParams[1] && typeof hashParams[1] != "undefined") {
			Veil.show($("#step-2").find("a[id=" + hashParams[1] + "]").parents("ul"), {duration:2,easing:"jswing"});
		}
	}
	// Initially make Step 1 and Step 2 categories expandable
	MakeExpandable("#step-1, #step-2");
	// Make Step 1 AJAX links
	$("#step-1 h4").next("ul").find("a").click(function(e) {
		$this = $(this);
		handleSubjectChange( $this.attr("id") );
		return false;
	});
	
	// Make Step 2 AJAX links
	MakeStep2AJAX()
	
	// Make download link tabs
	$("#step-3 .nav-tabs").tabs({fx: {height: 'toggle', duration: 600}});
});