// JavaScript Document

MegaDrop = function() {
	var limits = {
		left: 90,
		right: 1000
	}

	$("#topnav li > div").each(function(i) {
		// get parent element properties
		var p = {};
		p.element = $(this).parent();
		p.offset = p.element.position().left + 8;
		p.width = p.element.width();

		// get menu properties
		var m = {};
		m.element = $(this);
		m.width = m.element.innerWidth();
		m.left = parseInt(p.offset - (m.width - p.width) / 2) + 12;
		//m.left += 24;

		// background properties
		var bg = {};
		bg.offset = parseInt(p.width / 2);

		// check if dropdown would go off-limits
		if (m.left < limits.left) {
			m.left = limits.left;
		} else if (m.left + m.width > limits.right) {
			m.left = limits.right - m.width;
		}

		// calculate where to position dropdown arrow and do it
		bg.left = (p.offset - m.left + bg.offset) + "px";
		$(this).css("background-position", bg.left + " 2px");

		// set position of dropdown
		$(this).css("left", m.left);
	});

	// $("#topnav > ul").addClass("script-enhanced");

	$("#topnav > ul > li").bind("mouseenter", function() {
		$("div", this).show();
	}).bind("mouseleave", function() {
		$("div", this).hide();
	});
}
AddDropShadow = function() {
	var elm = $("#topnav li > div > div");

	if (typeof(elm.css("box-shadow")) == "undefined" && typeof(elm.css("-moz-box-shadow")) == "undefined") {
		$(elm).after("<span class=\"drop-shadow left\"></span><span class=\"drop-shadow right\"></span>");
	}
}
	
function setActive() {
	aObj = document.getElementById('topnav').getElementsByTagName('a');
	for(i=0;i<aObj.length;i++) {
		if(document.location.href.indexOf(aObj[i].href)>=0) {
			aObj[i].className='active';
		}
	}
}
		
$(window).load(function() {
	MegaDrop();
	AddDropShadow();
});

$(document).ready(function() {
	if(location.pathname != "/") {
		$('#topnav li a.parent[href^="/' + location.pathname.split("/")[1] + '"]').addClass('active');
	} else $('#topnav a:eq(0)').addClass('active');
});
