//image resize
function resize(targetted) {
	$(targetted).each(function() {
		var ratio = Math.max($(window).width()/$(this).width(),($(window).height()-60)/$(this).height());	
		
		if ($(this).hasClass('portrait')) {
			// portrait image
			$(this).css({width:'auto',height:$(window).height()-60});
		//	return; 		
		} else if ($(window).width() > $(window).height()) {
			$(this).css({width:$(this).width()*ratio,height:'auto'});
		} else {
			$(this).css({width:'auto',height:$(this).height()*ratio});
		}	
   	});
};
			

// history
	// PageLoad function
	// This function is called when:
	// 1. after calling $.historyInit();
	// 2. after calling $.historyLoad();
	// 3. after pushing "Go Back" button of a browser
	function pageload(hash) {
		// hash doesn't contain the first # character.
		if(hash) {
			
			// restore ajax loaded state
			if($.browser.msie) {
				// jquery's $.load() function does't work when hash include special characters like aao.
			//	hash = encodeURIComponent(hash);
			}
			
			$('ul#menu li a').removeClass('active');			
			$('body').removeClass('home').removeClass('white').removeClass('black'); // get rid of homepage styles
					
			var navDiv = $('#nav');
			var maskDiv = $('#mask');
			var infoDiv = $('#info');
			var imageContent = $("#images img");

       		maskDiv.css({opacity:1}).addClass('loading');  
						
			// active menu item
			$("#menu li a[href='"+hash+"']").addClass('active');
			if ($("#menu li a[href='"+hash+"']").hasClass('black')) {
				$('body').addClass('black');
			} else {
				$('body').addClass('white');			
			}

			$("#images").load(hash,{ type: 1 }, function() {	
			
				// gallery				
				$('#nav a').remove();
				$("#images img").each(function(index) {
    				navDiv.append('<a>'+(index+1)+'</a>');
  				});
  				$('#nav a').eq(0).addClass('active');
  				$('#images img').eq(0).addClass('active');  				
			
				// see if first image loaded
				$("#images img:last").one("load",function(){
					resize("#images img"); // resize images				
					infoDiv.load(hash, {type:2}, function() {
						maskDiv.stop().animate({opacity:0},500, function(){
							maskDiv.removeClass('loading');	
						});
					});	
				})
				.each(function(){
					if(this.complete) $(this).trigger("load");
				});				
			});
		} else {
			// start page
			$("#images").empty();
			$("#info").empty();	
			$("#nav").empty();				
			$("body").addClass('home')
			$("body").removeClass('black');	
			$("body").removeClass('white');
			$('ul#menu li a').removeClass('active');					
		}
	}
	

$(document).ready(function(){
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload, "http://www.steve-watson.com/");

	// set onlick event for buttons
	$("a[rel='history']").click(function(){
		$('ul#menu li a').removeClass('active');
		var hash = $(this).attr("href");
		hash = hash.replace(/^.*#/, '');
		$('body').removeClass('black white');
		$('body').removeClass('home');
			
		// moves to a new page. 
		// pageload is called at once. 
		// hash don't contain "#", "?"
		$.historyLoad(hash);
		return false;
	});

	// fade in each 
	//http://www.mail-archive.com/jquery-en@googlegroups.com/msg55935.html
	$(function() {
	   var $sequence = $('ul#menu li, ul#previews li').hide(), div = 0;
		(function(){
  		 if ( div < $sequence.length )
   		  $($sequence[div++]).fadeIn(200, arguments.callee);
		})();
	});
		
	// resize images
	resize("#images img");

	// resize images on window resize
	$(window).resize(function(){
	    resize("#images img");
	});

	// gallery
   $("#next").hover(
      function () {
        $(this).children().fadeIn(500);
      }, 
      function () {
        $(this).children().fadeOut(250);
      }
    );
    
    // next image
   $("#next").click(function () { 
		var current = $("#images img.active").index("#images img");
		var total = ($("#images img").size()-1); // minus one as current starts at 0
		$('#nav a, #images img').removeClass('active');
			// console.log('current:'+current+' total:'+total);			
		if (current == total) { // last image
			$("#images img:first").addClass('active');
			$('#nav a:first').addClass('active');
		} else {		
			$("#images img").eq(current+1).addClass('active');			
			$('#nav a').eq(current+1).addClass('active');					
		}			
   });

	 $('#nav a').live("click", function(e) {
		var current = $("#nav a").index(this);
		$('#nav a').removeClass('active');
		$("#images img").removeClass('active').eq(current).addClass('active');		
		$(this).addClass('active');  
	 });    
    
	// $('#images img.portrait').live("mousemove", function(e) {
   	// 	var offset = $('#images img.portrait').css('top');
    //	var imageratio = ($('#images img').height()/($(window).height()-60));
   	// 	var current = e.pageY;
    //	var top = (current - offset);
	//	var amount = (Math.abs(current * imageratio))/2;
   	//	$('#images img.portrait').css({'top' : -amount}); 		
	// });
	
});
