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 = [ ];
function setupFlyouts()
{
	$('div#navigation ul.menu li').hover(function(){
		$('> ul',this).show();
	}, function(){
		$('> 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(){
		var theClass = $(this).attr('class');
			galleryShowImage(i,theClass);
			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,gal,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"></div><div id="galImage"></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 class="galClose"><a href="javascript:;"><font color="#FFFFFF">Close</font></a></div>');
		$('div#galImage').append('<img src="'+img.src+'" width="'+imageWidth+'" height="'+imageHeight+'" />');
		var content = '';
		if(gal != 'noNext') {
			if ( i != gallength )
				content += ' <a href="" class="next">Next &gt;</a>';
			if ( i != 0 )
				content += ' <a href="" class="previous">&lt; Previous</a>';
		}
		link = '';
		$('div#galImage').append('<div id="galBtnHolder" style="width:'+imageWidth+'px">'+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();
});

