var currentSN = null;
var hideSN_interval = null;
var SN_in_motion = false;
var nextSN = null;

function showSubNav(id) {
	
	if (currentSN != null && currentSN != id) {
		hideSubNav(currentSN,"fast");
	}
	
	if (currentSN != id) {
		
		if (SN_in_motion == false) {
		
			currentSN = id;
			
			document.getElementById(id).src = String(document.getElementById(id).src).replace("-out","-over");
			
			var pos = $("#"+id).offset(); 
			var offsetL = (100 - document.getElementById(id).width) / 2;
			
			// show BG
			$("#"+id+"_subNav").css( { "left": (pos.left - offsetL) + "px", "top": (pos.top + 47) + "px" } );
			$("#"+id+"_subNav").fadeTo(0,0);
			$("#"+id+"_subNav").slideDown("fast");
			$("#"+id+"_subNav").fadeTo(300,1);
			
			// show links
			$("#"+id+"_subNavLinks").css( { "left": (pos.left - offsetL) + "px", "top": (pos.top + 47) + "px" } );
			$("#"+id+"_subNavLinks").show(0);
			var snTotal = 0;
			$("#"+id+"_subNavLinks > a").each( function(i,v) {
				snTotal++;										
			});
			$("#"+id+"_subNavLinks > a").each( function(i,v) {
				var snLink = $(v);
				snLink.delay((25 * snTotal) + 50).slideDown("fast");
				snTotal--;
			});
			
			SN_in_motion = true;
			
			setTimeout("SN_finished()",300);
		
		} else {
			nextSN = id;
			setTimeout("showNextSN()",350);
		}
	}
}
function showNextSN() {
	if (nextSN != null) {
		showSubNav(nextSN);
		nextSN = null;
	}
}
function SN_finished() {
	SN_in_motion = false;
	hideSN_interval = setInterval("navTimeout()",500);
}
function hideSubNav(id,speed) {
	if (speed != null && speed != undefined && speed == "fast") {
		document.getElementById(id).src = String(document.getElementById(id).src).replace("-over","-out");	
		// hide BG
		$("#"+id+"_subNav").slideUp("fast");
		$("#"+id+"_subNav").fadeTo(100,0);
		// show links
		$("#"+id+"_subNavLinks > a").each( function(i,v) {
			var snLink = $(v);
			snLink.delay((5 * i)).slideUp("fast");
		});
		$("#"+id+"_subNavLinks").delay(50).hide(0);
	} else {
		document.getElementById(id).src = String(document.getElementById(id).src).replace("-over","-out");
		// hide BG
		$("#"+id+"_subNav").delay(100).slideUp("fast");
		$("#"+id+"_subNav").fadeTo(300,0);
		// show links
		$("#"+id+"_subNavLinks > a").each( function(i,v) {
			var snLink = $(v);
			snLink.delay((20 * i)).slideUp("fast");
		});
		$("#"+id+"_subNavLinks").delay(150).hide(0);	
	}	
	if (currentSN == id) {
		currentSN = null;
	}
}
function navTimeout() {
	if (currentSN != null) {
		var main_pos = $("#"+currentSN).offset(); 
		var sub_pos = $("#"+currentSN+"_subNavLinks").offset();
		if (overNav(currentSN)) {
			hideSubNav(currentSN);
			clearInterval(hideSN_interval);
		}
	} else {
		clearInterval(hideSN_interval);
	}
}
function overNav(id) {
	var main_pos = $("#"+id).offset(); 
	var sub_pos = $("#"+id+"_subNavLinks").offset();
	if (mouseX < (main_pos.left - 15) || mouseX > (main_pos.left + Number($("#"+id).css("width").replace("px",""))+15) || mouseY < (main_pos.top - 15) || mouseY > (sub_pos.top + Number($("#"+id+"_subNavLinks").css("height").replace("px",""))+15)) {
		return true;
	} else {
		return false;
	}
}

var mouseX = 0;
var mouseY = 0;
$(document).bind('mousemove',function(e){ 
	mouseX = e.pageX;
	mouseY = e.pageY;
});
