$(document).ready(function() {

	$("#right .photo").live({
		mouseenter:
			function()
			{
				$(this).children('.fade').stop().fadeTo('fast', 0.0, function(){});
			},
		mouseleave:
		function()
			{
				$(this).children('.fade').stop().fadeTo('fast', 1.0, function(){});
				$(this).addClass('photo-out');
			}
		}
	);
	
	$('#right .photo').live('click',function() {
		$('body').css("overflow", "hidden");
		//pResize();
		$('#overlay').fadeTo('fast', 1.0, function(){});
	});
	
	$('#overlay').live('click',function() {
		$(this).fadeTo('fast', 0.0, function(){
			$(this).css({'display': 'none'});
			$('body').css("overflow", "auto");
			$('#overlay').children('.photo').attr('src','');
		});
	});
	
	$(window).resize(function() {
		//pResize();
	});
	
	$('#left a').click(function() {
		var page = $(this).attr('href');
		if(page == "paintings"){aPage(1)}
		if(page == "sculpture"){aPage(2)}
		if(page == "graphics"){aPage(3)}
		if(page == "photography"){aPage(4)}
		
		return false;
	});
	
	function pResize(){
        var wW = $(window).width();
        var wH = $(window).height();
        var ratio = 0;
		var pW = $("#overlay .photo").css("width");
        var pH = $("#overlay .photo").css("height");

        if(pW > wW){
            ratio = wW / pW;
            $("#overlay .photo").css("width", wW);
            $("#overlay .photo").css("height", pH * ratio);
            pH = pH * ratio;
            pW = pW * ratio;
        }
        
        if(pH > wH){
            ratio = wH / pH;
            $("#overlay .photo").css("height", wH);
            $("#overlay .photo").css("width", pW * ratio);
            pW = pW * ratio;
        }
	}
	
	function aPage(data){
		$.getJSON('album.php?c=' + data, function(data) {
			$('#right').html("");
			$.each(data,function(i,p)
			{
				$('#right').append('<div class="photo" alt="' + p.Title + '" title="' + p.Title + '" onclick="$(\'#overlay .photo\').attr(\'src\',\'album/' + p.Image + '\')"><img src="album/thumbs/' + p.Thumbnail + '"><img class="fade" src="album/thumbs/' + p.Thumbnail2 + '"></div>');
				jQuery.preLoadImages("album/" + p.Thumbnail2);
			});
			$.each(data,function(i,p)
			{
				jQuery.preLoadImages("album/" + p.Thumbnail);
			});
			$.each(data,function(i,p)
			{
				jQuery.preLoadImages("album/" + p.Image);
			});
		});
	}
	
	(function($) {
		var cache = [];
		// Arguments are image paths relative to the current page.
		$.preLoadImages = function() {
			var args_len = arguments.length;
			for (var i = args_len; i--;) {
				var cacheImage = document.createElement('img');
				cacheImage.src = arguments[i];
				cache.push(cacheImage);
			}
		}
	})(jQuery)
	
	aPage(0);
	
});
