$.fn.pause = function (n) {
	return this.queue(function () {
	var el = this;
	setTimeout(function () {
	  return $(el).dequeue();
	}, n);
  });
};

$(document).ready(function(){
	//globals
	var pioneerVars = {
		curProjImages : "",
		position : 0,
		$overlayImage : $('#o_image')
	};
	
	$('#navigation li ul').hover(function(){
		$(this).prev('a').addClass('menu_selected');
	}, function(){
		$(this).prev('a').removeClass('menu_selected');
	});
	
	$('.proj_img').click( function(){
		var img = new Image();
		//get large image address
		var target = $(this).attr('href');
		
		$(img).load(function(){
			var image = this;
			pioneerVars.$overlayImage.fadeOut(function(){
				$(this).append(image);
			}).fadeIn();
		})
		.attr('src', target );
		
		//find the associated project's images
		pioneerVars.curProjImages = $(this).parents('.project').find('.img_links a');
		pioneerVars.curProjImages.each(function(index){
			if( $(this).hasClass('selected') )
			{
				pioneerVars.position = index;
			}
		});
	}).overlay({
				absolute : false, 
				left : 'center', 
				top : 210, 
				close : '.close_btn', 
				closeOnClick : false, 
				expose : {
							color:'#111', 
							opacity : 0.9, 
							loadSpeed : 'fast',
							maskId : 'exposeMask', onBeforeLoad : function(){ $('#exposeMask').height( $(document).height() - 149 ).css({minWidth:'1000px', top:'149px'}) }
						},
				onClose : function(){ $('#o_image').empty(); }
				});
	
	//Poject navigation in expanded mode
	$('#prev').click(function(){
		var img = new Image();
		
		if(pioneerVars.position === 0)
		{
			pioneerVars.position = pioneerVars.curProjImages.length - 1;
		}
		else
		{
			pioneerVars.position -= 1;
		}
		
		$('#o_image > img').fadeOut(function(){
			
			$(img).load(function(){
				$(this).hide();
				pioneerVars.$overlayImage.empty().append(this);
				$(this).fadeIn('900');
			})
			.attr('src', pioneerVars.curProjImages[pioneerVars.position] );
		});
		return false;
	});
	
	$('#next').click(function(){
		var img = new Image();	
	
		if(pioneerVars.position === pioneerVars.curProjImages.length - 1)
		{
			pioneerVars.position = 0;
		}
		else
		{
			pioneerVars.position += 1;
		}
		
		$('#o_image > img').fadeOut(function(){
			
			$(img).load(function(){
				$(this).hide();
				pioneerVars.$overlayImage.empty().append(this);
				$(this).fadeIn('900');
			})
			.attr('src', pioneerVars.curProjImages[pioneerVars.position] );
		});
		return false;
	});
	
	$('.zoom').click( function(){
		$(this).next().click();
	});
	
	$('.img_links').click( function(e){
		
		var $clickedItem = $(e.target);
		if( $clickedItem.hasClass('selected') ) return false;
		
		//get the right image and project it into .proj_img
		if( $clickedItem.is('a') )
		{			
			var link = $clickedItem.attr('href');
			
			var $projectImage = $(this).parents('.project').find('.proj_img');
			
			//deselect prev. selected image
			$(this).find('a.selected').removeClass('selected');
			
			$clickedItem.addClass('selected');
			$projectImage.attr('href', link);
			link = link.replace(".", "s.");
			
			$projectImage.find('img').fadeOut(700, function(){
				$(this).attr('src', link);
			}).pause(1000).fadeIn(700);
		}
		e.preventDefault();
	});
	
	//initial selection
	$('.img_links a:first-child').addClass('selected');
});