// google analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-83969-21']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();



// http://www.impressivewebs.com/css3-glow-tabs/
var subMenus = [];

// http://javascript-array.com/scripts/jquery_simple_drop_down_menu/
var timeout = 50;
var closetimer = 0;
var currentlyOpenMenu = null;

function jsddm_open() {
	jsddm_canceltimer();
	jsddm_close();

	for (var i = 0; i<subMenus.length; i++) {
		if ($(this).parent().hasClass(subMenus[i].parentClass)) {
			// save the currently open menu so we can close it
			currentlyOpenMenu = $('#' + subMenus[i].id).css('visibility', 'visible');
		}

	}
}

// separate function for submenus, the only differense is no jsddm_close() on line 2
function jsddm_open_submenu() {
	jsddm_canceltimer();

	for (var i = 0; i<subMenus.length; i++) {
		if ($(this).parent().hasClass(subMenus[i].parentClass)) {
			// save the currently open menu so we can close it*/
			currentlyOpenMenu = $('#' + subMenus[i].id).css('visibility', 'visible');
		}

	}
}

function jsddm_close() {
	if (currentlyOpenMenu) {
		currentlyOpenMenu.css('visibility', 'hidden');
	}
}

function jsddm_timer() {
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer() {
	if (closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

$(function() {
	window.setTimeout(function(){	// give a half second delay for the registration page's loading white screen to go away so the offsets are correct because the scrollbar rendered
	var subMenuCount = 0;	// used for random id for submenus
	$('ul#navigation > li > ul').each(function(){
		if ($(this).children('ul')) {
			// give the parent a special class
			var parentClass = 'jsddm_menu_' + subMenuCount;
			$(this).parent().addClass(parentClass);
			// save the parent class
			var tempObj = new Object;
			tempObj.parentClass = parentClass;
			// give the submenu a unique id
			var id = 'jsddm_submenu_' + subMenuCount;
			// save the submenu id
			tempObj.id = id;
			subMenus.push(tempObj);
			// move submenu
			$('body').append('<div id="' + id + '" style="visibility: hidden; position: absolute; top: ' + ($(this).parent().offset().top + $(this).parent().parent().height()) + 'px; left: ' + $(this).parent().offset().left + 'px;"></div>');
			$(this).appendTo('#' + id);
			$('#' + id + ' ul li a').each(function(){
				$(this).addClass('subnav');
			});
			// bind mouseovers/mouseouts
			$('#' + id).bind('mouseover', jsddm_open_submenu);
			$('#' + id).bind('mouseout', jsddm_timer);
			subMenuCount++;
		}
	});

	$('ul#navigation > li > a').bind('mouseover', jsddm_open);
	$('ul#navigation > li > a').bind('mouseout', jsddm_timer);
	}, 500);
});
