

function setupFlyouts()
{
	//add the classes to show that there are submenus
	$('div#navigation ul.menu ul').each(function(){
		$(this).parent().addClass('parent');
	});
	$('div#navigation ul.menu li').hover(function(){
		$('> ul',this).show('fast');
	},function(){
		$('> ul',this).fadeOut('fast');
	});
}

/* small parts of this gal code are loosely based on Thickbox */
function setupGallery()
{
	$('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 ( !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;z-index: 102;color:#000000;display:none;border: 4px solid#666666;top:50%;left:50%;text-align: center;padding: 20px 0 10px 0;"></div>');
	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('<img src="'+img.src+'" width="'+imageWidth+'" height="'+imageHeight+'" />');
		$('div#galImage').append('<div style="clear:both;"></div><div style="float: right; padding-right: 16px;"><a href="javascript: galleryHideImage();">CLOSE</a></div>');
		var content = '';
		if ( caption )
			content += caption+'<br />';
		if ( i != 0 )
			content += ' <a href="" class="previous">&laquo; Previous</a>';
		if ( i != 5 )
			content += ' <a href="" class="next">Next &raquo;</a>';
		$('div#galImage').append('<div style="text-align: left;padding-left: 16px;float: left;">'+content+'</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)');
		});
	};
	img.src = href;
	$('div#galOverlay,div#galImage,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'});
}

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

