function initDropDowns ( ) {
	$("div##MainNavigation ul li.mainNav").mouseover(subNavOverHandler);
	$("div##MainNavigation ul li.mainNav").mouseout(subNavOutHandler);
	$("div##MainNavigation ul li.mainNav ul.sub").each(calcHeights);	
	
	
}


var hideTimer;
var hideScope;
var showScope;

function calcHeights ( $e ) {
	$(this).data("maxHeight", $(this).height());
	$(this).data("initTop", $(this).position().top - $(this).height());
	//$(this).css("top", $(this).data("initTop"));
	$(this).css("opacity", .0);
	$(this).css("display", "none");
	$(this).css("height", 0);
	
	
	//alert($(this).parent().css("background-position"));
	
	//var positions;// = $(this).parent().css("background-position");
	
	//var leftPosition;
	
	if ($(this).css("background-position-x")) {
		var pos = $(this).parent().css("background-position-x").split(" ")[0];
		//alert("IE: " + pos);
	} else {
		var pos = $(this).parent().css("background-position").split(" ")[0];
		//alert("FF: " + pos);
	};
	
	//alert(pos);
	//var leftPosition = positions.split(" ")[0];
	
	$(this).data("leftOffset", pos);
	
	var minWidth = $(this).parent().width();
	var thisWidth = $(this).width();
	$(this).css("left", -1);
	if(minWidth > thisWidth){
		$(this).width(minWidth);
		
	} else {
		//alert($("li a", this).width());	
	}
	
	/*$("li", this).mouseover(
		function () {
			$("a", this).css("color", "#f7d789");
			$(this).css("background-color", "#262012");	
		}
	);
	
	$("li", this).mouseout(
		function () {
			$("a", this).css("color", "#000000");
			$(this).css("background-color", "");	
		}
	);*/
	
//	console.log(this + " " + $(this).data("maxHeight"));
}

function subNavOverHandler ( $e ) {
	//$("a:first", this).css("background-position", $("ul:first", this).data("leftOffset") + " -33px");
		
	if(hideScope && hideScope != this){
		hideSubNavHandler();	
	} else {
		clearTimeout(hideTimer);
	}
	showScope = $("ul.sub", this);
	$("ul.sub", this).each(showSubNav);
}

function subNavOutHandler ( $e ) {
	
		
	hideScope = this;
	hideTimer = setTimeout("hideSubNavHandler()", 200);
}


function hideSubNavHandler ( ) {
	//$("a:first", hideScope).css("background-position", $("ul:first", hideScope).data("leftOffset") + " 0px");
	$("ul.sub", hideScope).each(hideSubNav);
}

function showSubNav ( $e ) {
	//$(showScope).css("display", "block");
	//alert($(showScope).position().top + " " + $(showScope).position().left);
	//alert($(showScope).css("display"));
	
	/*if($(showScope).data("animating") != "showing"){
		$(showScope).css("display", "block");
		$(showScope).data("animating", "showing");
		$(showScope).stop();
		
		$(showScope).animate({opacity: 1}, {queue: false, duration: 200, complete: animatingComplete});;
	}*/
	if($(this).data("animating") != "showing"){
		$(this).css("display", "block");
		$(this).data("animating", "showing");
		//alert($(this).width());
		$(this).css("width", $(this).width());
		$(this).animate({opacity: .9, height: $(this).data("maxHeight")}, {queue: false, duration: 200, complete: animatingComplete});
		//$(this).stop();
		
		//$(this).slideDown("fast", animatingComplete);
		//$(this).show("fast", animatingComplete);
	}
}

function animatingComplete ( ) {
	if($(this).data("animating") == "hiding"){
		$(this).css("display", "none");	
	}
	
	$(this).data("animating", "");
}

function hideSubNav ( $e ) {
	if($(this).data("animating") != "hiding"){
		$(this).data("animating", "hiding");
		$(this).animate({opacity: 0, height: 0}, {queue: false, duration: 200, complete: animatingComplete});
		//$(this).hide("fast", animatingComplete);
		
		//$(this).slideUp("fast", animatingComplete);
	}
	//$(this).slideUp("normal");
}