$(function(){
	/**
	 * Setup Variables for gallery
	 */
	var imagelayers = [ $('<img>').appendTo('#gallery_image') , $('<img>').appendTo('#gallery_image')],
			curImage = 0,
			zIndex = 2,
			galleryFade = 500,
			scrollTimer = null;
	
	/**
	 * Smooth scroll thumbnails on mousedown of navigation links
	 */
	$('#gallery_nav a').mouseenter(function(){
		var dir = this.className;
		var h = $('#gallery_thumblist li').height() * $('#gallery_thumblist li').length;
		scrollTimer = setInterval(function(){
			var t = $('#gallery_thumblist').scrollTop();
			$('#gallery_thumblist').scrollTop( dir == 'up'? t-10 : t+10 );
			if($('#gallery_thumblist').scrollTop() <=0 || $('#gallery_thumblist').scrollTop()>= h){
				clearInterval(scrollTimer);
			}
		},50);
		return false;
	}).mouseout(function(){
		clearInterval(scrollTimer);
	}).click(function(){ return false;});
	
	/**
	 * Gallery View thumbnail
	 */
	$('#gallery_thumbs a').click(function(){
		$('#gallery_thumbs a').removeClass('active');
		$(this).addClass('active');
		$(imagelayers[curImage]).bind('load', function(){
			$(imagelayers[curImage==1?0:1]).css({zIndex: zIndex});
			$(this).unbind().animate({opacity:1}, galleryFade);
			curImage = curImage==1?0:1;
		}).css({opacity:0, zIndex:zIndex+1}).attr('src','').attr('src', this.href);
		return false;
	}).append('<span>+</span>');
	/**
	 * Inititalise Gallery
	 */
	$('#gallery_thumblist').scrollTop(0);
	$($('#gallery_thumblist a')[0]).click();
	
	/**
	 * IF less than IE7 add mouseover events for menu, all modern browsers use css :hover
	 */
	if($.browser.msie && parseInt($.browser.version) <7){
		$('#menu li').mouseenter(function(){
			$(this).addClass('hover');
		}).mouseleave(function(){
			$(this).removeClass('hover');
		});
	}
	
});