var dropdownOn = false;
var dropdownOver = false;

$(document).ready(function()
{
	init_drop_downs();
	init_nav();
	
});

var do_search = function(f)
{
	if(f.search_text.value!='') location = '/search/query/'+f.search_text.value;
}

var init_nav = function()
{
	$('#nav li').each(function()
	{
		$(this).mouseover(function(e)
		{
			if( typeof( window.innerWidth ) == 'number' ) {
			    //Non-IE
			    var w = window.innerWidth;
			  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			    //IE 6+ in 'standards compliant mode'
			    var w = document.documentElement.clientWidth;
			  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			    //IE 4 compatible
			    var w = document.body.clientWidth;
			  }
			var wrapLeft = (w/2) - 500;
			var mousePos = e.pageX;
			var navPos = mousePos - wrapLeft;
			$(this).addClass('over');
			if(navPos > 900) {
				$(this).children('ul:first').each(function()
				{
					$(this).css('left',$(this).parent().width() - 228);
				});
			} else
			{
				$(this).children('ul:first').each(function()
				{
					$(this).css('left',-2);
				});
			}
			
		});
		$(this).mouseout(function()
		{
			$(this).removeClass('over');
		});
	});
}

var init_drop_downs = function()
{
	// initialise drop downs...
		$('.drop_down a.active').each(function()
		{
			$(this).mouseover(function(){ dropdownOver = true; }); // we're over a dropdown button
			$(this).mouseout(function(){ dropdownOver = false; }); // we've left a dropdown button
			$(this).click(function()
			{
				var p = $(this).parent().parent();
				// is this already open?
				var o = p.hasClass('expanded') ? 1:0;
				// close all open dropdowns
				$('.expanded').each(function(){
					$(this).removeClass('expanded');
				});
				dropdownOn = false; // reset on variable 

				// open this dropdown if not already open
				if(!o) {
					p.addClass('expanded');
					dropdownOn = true; // it's down
				}
			});
		});
		// close drop down functions
		$(document).click(function()
		{
			if(dropdownOn && !dropdownOver) { // check a dropdown is open and we're not hovering over it
				$('.expanded').each(function(){
					$(this).removeClass('expanded');
					dropdownOn = false;
				});
			}
		});
}