
var _watchMenu = function()
{
	timeTilFire--;
	if ( timeTilFire > 0 )
		return;
	timeTilFire = timeTilFireStart;
	//hide all open menus that shouldn't be open...
	for ( i in openMenus )
	{
		if ( openMenus[i] && !overMenus[i] )
			closeMenu(i);
	}
};

var closeMenu = function(id) {
	if ( animation == 1 )
		$('li[menunum='+id+'] > ul').slideUp('fast');
	else
		$('li[menunum='+id+'] > ul').fadeOut('fast');
	openMenus[id] = false;
};

var showMenu = function(id) {
	//hide all open menus that shouldn't be open...
	for ( i in openMenus )
	{
		if ( openMenus[i] && !overMenus[i] )
			closeMenu(i);
	}
	if ( animation == 1 )
	{
		position = $('li[menunum='+id+'] > ul').parent('li:eq(0)').offset();
		pheight = $('li[menunum='+id+'] > ul').parent('li:eq(0)').height();
		$('li[menunum='+id+'] > ul').css({
			position: 'absolute',
			left: '0px',
			top: pheight+'px'
		});
		$('li[menunum='+id+'] > ul').slideDown('fast');
	}
	else
	{
		$('li[menunum='+id+'] > ul').fadeIn('fast');
	}
	openMenus[id] = true;
	timeTilFire = timeTilFireStart;
};

var menuTimeout = 10;
var timeTilFireStart = 100;
var timeTilFire = 100;
var animation = 0;
var openMenus = [ ];
var overMenus = [ ];

var lastMouseOverMenu = null;
var mouseInMenu = null;
var inmenubar = false;
function wtShowMenu()
{
  if(lastMouseOverMenu == mouseInMenu && mouseInMenu != null) {
    $('> ul', mouseInMenu).show();
    inmenubar = true;
  } else {
    inmenubar = false;
  }
}

function setupFlyouts()
{
	$('div#navigation ul').hover(function() {}, function() { inmenubar = false; } );
	$('div#navigation ul.menu li').hover(function(){
		mouseInMenu = this;
		lastMouseOverMenu = this;
		if(inmenubar) {
			wtShowMenu();
		} else {
			setTimeout("wtShowMenu()", 300);
		}
//		$('> ul',this).show();
	}, function(){
		mouseInMenu = null;
		$('> ul',this).hide();
	});
}
function _setupFlyouts()
{
	//add the classes to show that there are submenus
	//assign an identifier to each sub menu
	$('div#navigation ul.menu li').attr('menunum','-2');
	$('div#navigation ul.menu ul').hide();
	$('div#navigation ul.menu ul').each(function(i){
		var id = i+1;
		$(this).parent().addClass('parent');
		$(this).parent().attr('menunum',id);
	});
	if ( $('link[href=styles/menu.top.css]').length > 0 )
		animation = 1;
	setInterval(_watchMenu,menuTimeout);
	$('div#navigation ul.menu li[menunum]').hover(function(){
		id = $(this).attr('menunum');
		overMenus[id] = id;
		setTimeout(function(){
			showMenu(id);
		},100);
	},function(){
		timeTilFire = timeTilFireStart;
		id = $(this).attr('menunum');
		overMenus[id] = false;
	});
}

/* small parts of this gal code are loosely based on Thickbox */
var gallength = 0;
function setupGallery()
{
	gallength = $('div.gallery a').length;
	if ( gallength > 0 )
		gallength--;
	$('div.gallery a').each(function(i){
		$(this).attr('target',''); //get rid of target if it is set...
		$(this).attr('galindex',i);
		$(this).click(function(){
			galleryShowImage(i);
			return false; //prevent browser going to link
		});
	});
}

/* thanks, Thickbox =) */
function tb_getPageSize()
{
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function galleryShowImage(i,noappend)
{
	href = $('div.gallery a:eq('+i+')').attr('href');
	caption = $('div.gallery a:eq('+i+') img').attr('alt');
	if ( !caption )
		caption = $('div.gallery a:eq('+i+') strong').text();
	if ( !noappend )
		$('body').append('<div id="galOverlay" style="position: fixed;z-index:100;top: 0px;left: 0px;height:100%;width:100%;background: #000 url(behaviour/loader.gif) center center no-repeat;filter:alpha(opacity=75);-moz-opacity:0.75;opacity:0.75;"></div><div id="galImage" style="position: fixed;background:#FFFFFF url(images/galPopBack.gif) repeat-x bottom left;z-index: 102;color:#000000;display:none;border: 4px solid#666666;top:50%;left:50%;text-align: center;padding: 20px 0 5px 0;"></div>');
	$('div#galOverlay').css({backgroundImage: 'url(behaviour/loader.gif)'});
	img = new Image();
	img.onload = function()
	{		
		img.onload = null;
		var pagesize = tb_getPageSize();
		var x = pagesize[0] - 150;
		var y = pagesize[1] - 150;
		var imageWidth = img.width;
		var imageHeight = img.height;
		if (imageWidth > x)
		{
			imageHeight = imageHeight * (x / imageWidth); 
			imageWidth = x; 
			if (imageHeight > y)
			{ 
				imageWidth = imageWidth * (y / imageHeight); 
				imageHeight = y; 
			}
		}
		else if (imageHeight > y)
		{ 
			imageWidth = imageWidth * (y / imageHeight); 
			imageHeight = y; 
			if (imageWidth > x)
			{ 
				imageHeight = imageHeight * (x / imageWidth); 
				imageWidth = x;
			}
		}
		galWIDTH = imageWidth + 30;
		galHEIGHT = imageHeight + 60;
		document.onkeydown = function(e)
		{ 	
			if (e == null)
			{ // ie
				keycode = event.keyCode;
			}
			else
			{ // mozilla
				keycode = e.which;
			}
			if(keycode == 27)
			{ // close
				galleryHideImage();
			}
		};
		_scale();
		$(window).resize(_scale);
		$('div#galImage').append('<div style="text-align: right;padding-right: 15px;float: right; margin:-10px 0 10px 0;"><a href="javascript:;">CLOSE</a></div>');
		$('div#galImage').append('<img src="'+img.src+'" width="'+imageWidth+'" height="'+imageHeight+'" />');
		$('div#galImage').append('<div style="clear:both;"></div><div style="display: none;float: right; padding-right: 16px;"><a href="javascript: galleryHideImage();">CLOSE</a></div>');
		var content = '';
		if ( caption )
			content += '<div style="text-align: center;">'+caption+'</div>';
		if ( i != 0 )
			content += ' <a href="" class="previous" style="float:left;color:#fff;display:block; width:87px; height:30px;padding:7px 0 0 0; margin:0 0 -5px 8px; background:url(images/galBtnBack.gif) no-repeat;">&laquo; Previous</a>';
		//if ( i != 5 )
		if ( i != gallength )
			content += ' <a href="" class="next" style="float:right;color:#fff;margin-right:8px;margin-bottom:-5px;display:block; width:87px; height:30px;padding:7px 0 0 0;background:url(images/galBtnBack.gif) no-repeat;;">Next &raquo;</a>';
		url = $('div.gallery a:eq('+i+') span[url]').attr('url');
		link = '';
		if ( url )
			link = '<a href="'+url+'" style="color:#fff;>Launch Site</a>';
		$('div#galImage').append('<div style="width: 100%; text-align: center;margin-top:20px;">'+content+link+'</div>');
		
		$('a.next').click(function(){
			$('div#galImage').empty();
			galleryShowImage(i+1,true);
			return false;
		});
		$('a.previous').click(function(){
			$('div#galImage').empty();
			galleryShowImage(i-1,true);
			return false;
		});
		
		
		$('div#galImage').fadeIn('fast');
		$('div#galControls').fadeIn('fast');
		$('div#galControls a').hover(function(){
			$(this).css('font-weight','bold');
		}, function(){
			$(this).css('font-weight','normal');
		});
		$('div#galControls').hover(function(){
			$(this).css('-moz-opacity','1');
			$(this).css('opacity','1');
			$(this).css('filter','alpha(opacity=100)');
		}, function(){
			$(this).css('-moz-opacity','.1');
			$(this).css('opacity','.1');
			$(this).css('filter','alpha(opacity=10)');
		});
		$('div#galOverlay').css({backgroundImage: 'url()'});
	};
	img.src = href;
	$('div#galOverlay,div#galImage').click(galleryHideImage);
	$('div#galImage, div#galImage *').css({cursor: 'pointer'});
}

function galleryHideImage()
{
	$(window).resize(function(){});
	$('div#galOverlay').fadeOut('fast',function(){
		$('div#galImage, div#galControls').fadeOut('medium',function(){
			$('div#galOverlay, div#galImage, div#galControls').remove();
		});
	});
}

var galWIDTH = 100;
var galHEIGHT = 100;
function _scale()
{
	$('div#galImage').css({marginLeft: '-' + parseInt((galWIDTH / 2),10) + 'px', width: galWIDTH + 'px'});
	$("div#galImage").css({marginTop: '-' + parseInt((galHEIGHT / 2),10) + 'px'});
}


function setupSearchForm()
{
	$('input[name=keywords]').each(function(){
		$(this).parents('form:eq(0)').submit(function(){
			if ( $('input[name=keywords]',this).val() == '' )
			{
				$('input[name=keywords]',this).focus();
				alert('Please enter a value for your search.');
				return false;
			}
			return true;
		});
	});
}

function setupAnchors()
{
	$('a').each(function(){
		var href = String($(this).attr('href'));
		if ( href.match(/#.*/) )
		{
			href = href.replace(/^[^#]*#/,'');
			$(this).click(function(){
				var goto = $('*[id='+href+'],*[name='+href+']').offset();
				window.scroll(goto.top,goto.left);
				return false;
			});
		}
	});
}

$(document).ready(function(){
	setupFlyouts();
	//prevent older versions of IE from doing the gallery
	if ( !($.browser.msie && $.browser.version < 7) )
		setupGallery();
	//set up the site search form additions
	setupSearchForm();
});


