//////////////////////////////////////////////////////
//
//	The purpose of this file is to
//	apply a hover class on mouseover
//	on the <li> elements in the nav.
//	This is because IE doesn't put
//	the pseudo class :hover on <li> elements
//
//	This is code that uses the
//	jquery javascript library (http://jquery.com/)
//
//////////////////////////////////////////////////////
$(window).load(function() {
	$(".navtop li.has_navchild > a").click(function() {
		$(this).parent().siblings("li").find("ul").slideUp("fast");
		$(this).siblings("ul").slideDown("fast");
		return false;
	});
	// slide up other nav items before redirecting to page that has no subnav.
	$(".navtop li:not(.has_navchild) a").click(function() {
		var linkto=this.href;
		$(this).parent().siblings("li").find("ul").slideUp("fast");
		setTimeout(function(){
			window.location=linkto;
		},500);
		return false;
	});
	
	if($(".scroll").length > 0)
		$(".scroll").jScrollPane({scrollbarWidth:9,showArrows:true,scrollbarMargin:35});

	
	Cufon.replace('h1');
	Cufon.replace('h2');
	
	if($('div.scrollable').length > 0) {
		$('div.scrollable ul').addClass('galleria');
		$('div.scrollable ul').addClass('items');	// adds new class name to maintain degradability
		
		var test = 0;
		$('ul.galleria').galleria({
			history   : true, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#main_image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Next image >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				);
			
				$('.scrollable li')[test].id = test;
				test++;
				if(test >= $('.scrollable li').length) {
					$('div.scrollable').scrollable();
					$('.scrollable li img').each(function () {
						this.onclick = (function(old) {
							return function() {
								if (old) old.call(this, arguments);
								var scroll = $('div.scrollable').scrollable();
								scroll.seekTo($(this).parent().attr('id')-2);
							}
						})(this.onclick);
					});
				}
				
				// add this if you want the first image to be displayed by default
				if(_li.attr('id') == '0')
					thumb.click();
			}
		});
		if($('div.scrollable ul li').length <= 6)
			$('.next').css('display','none');
	}
	
	// only apply this on the publications page
	if($('#pub').length > 0) {
		$('h3 a').mouseover(function () {
			$('#pub_img').html('<img src="'+this.href+'" alt="Publication Image"/>');
		});
		$('h3 a').click(function () {
			$('#pub_img').html('<img src="'+this.href+'" alt="Publication Image"/>');
			return false;
		});
		$($('h3 a')[0]).click();
	}
});
