google.load("jquery", "1.4.3");

google.setOnLoadCallback(function() {
	
	var imgh, imgw, imgr;

    function scaleBackground(){
        w = $(window).width();
        h = $(window).height();
        if(w/h >= imgr){
            $("#fullscreen_img").css('height', 'auto');
            $("#fullscreen_img").css('width', w+'px');
        }
        else{
            $("#fullscreen_img").css('height', h+'px');
            $("#fullscreen_img").css('width', 'auto');
        }
    };

	function scaleVertBackground() {
		w = $(window).width();
        h = $(window).height();
        if(w/h <= imgr){
            $("#fullscreen_vert_img").css('height', 'auto');
            $("#fullscreen_vert_img").css('width', w+'px');
        }
        else{
            $("#fullscreen_vert_img").css('height', h+'px');
            $("#fullscreen_vert_img").css('width', 'auto');
        }
		var img_w = ($('#fullscreen_vert_img').attr('width')/2)*-1;
		$('#fullscreen_vert_img').css('left', '50%');
		$('#fullscreen_vert_img').css('margin-left', img_w );
	};
	
	function scaleSmallBackground() {
		w = ($(window).width())*.7;
        h = ($(window).height())*.7;

		if(w/h <= imgr) {
			$("#fullscreen_small_img").css('height', 'auto');
	        $("#fullscreen_small_img").css('width', w+'px');
		}else{
			$("#fullscreen_small_img").css('height', h+'px');
	        $("#fullscreen_small_img").css('width', 'auto');
		}
		
		var neg_w = ($('#fullscreen_small_img').width()/2)*-1;
		var neg_h = ($('#fullscreen_small_img').height()/2)*-1;
		
		$("#fullscreen_small_img").css('top', '50%');
		$("#fullscreen_small_img").css('left', '50%');
		$("#fullscreen_small_img").css('margin-left', neg_w+'px');
		$("#fullscreen_small_img").css('margin-top', neg_h+'px');
	}
	
	function backgroundSmallLoaded() {
		imgw = $("#fullscreen_small_img").width();
        imgh = $("#fullscreen_small_img").height();
        imgr = imgw/imgh;
        scaleSmallBackground();
		$('#fullscreen_small_img').css({ opacity: 1});
	}
	
	function backgroundVertLoaded() {
		imgw = $('#fullscreen_vert_img').width();
		imgh = $('#fullscreen_vert_img').height();
		imgr=imgw/imgh;
		scaleVertBackground();
		$('#fullscreen_vert_img').css({opacity:1});
	};

    function backgroundLoaded(){
        imgw = $("#fullscreen_img").width();
        imgh = $("#fullscreen_img").height();
        imgr = imgw/imgh;
        scaleBackground();
		$('#fullscreen_img').css({ opacity: 1});
    };

	backgroundSmallLoaded();
	backgroundVertLoaded();
	backgroundLoaded();

    $(window).resize(function(){
        backgroundSmallLoaded();
		backgroundVertLoaded();
		backgroundLoaded();
    });
	
	function get_info() {
		$('.info').stop().animate({opacity: 1}, 300);
	}
	function close_info() {
		$('.info').stop().animate({opacity: 0}, 300);
	}
	
	$(document).keydown(function(e) {
		var url=false;
		var flash = false;
		if (e.which==39) {
			url = $('.prev a').attr('href');
			flash = ".left_button";
		}
		else if (e.which==37) {
			url = $('.next a').attr('href');
			flash = ".right_button";
		}
		else if (e.which==38) {
			url = $('.site_title a').attr('href');
			if (!url) {
				url = $('.menu a').filter(':first').attr('href');
			}
		}
		else if (e.which==40) {
			url = $('.index_thumb a').filter(':first').attr('href');
		}
		else if (e.which==73) {
			get_info();
		}
		else if (e.which==88) {
			close_info();
		}
		// else if (e.which==88) {
		// 	url = $('.site_title a').attr('href');
		// 	if (!url) {
		// 		url = $('.menu a').filter(':first').attr('href');
		// 	}
		// 	window.location = url;
		// }
		else if (e.which==27) {
			close_info();
		}
		if (url) {
			$(flash).stop().animate({opacity: 1}, 300);
			window.location = url;
		}
	});
	$('.left_button').hover(function() {
		var lurl = false;
		lurl = $('.left_button a').attr('href');
		if (lurl) {
			$(this).stop().animate({opacity: 1}, 300);
		}
	});
	$('.right_button').hover(function() {
		var rurl = false;
		rurl = $('.right_button a').attr('href');
		if (rurl) {
			$(this).stop().animate({opacity: 1}, 300);
		}
	});
	$('.left_button, .right_button').mouseout(function() {
		$(this).stop().animate({opacity: 0}, 300);
	})
	$('.left_button').click(function() {
		var url = false;
		url = $('.left_button a').attr('href');
		if (url) {
			window.location = url;
		}
		url = false;
	});
	$('.right_button').click(function() {
		var url = false;
		url = $('.right_button a').attr('href');
		if (url) {
			window.location = url;
		}
		url = false;
	});
	$('.nav_panel').hover(function() {
		$(this).stop().animate({opacity: 1}, 300);
	});
	$('.home_button').hover(function() {
		$('.nav_panel').stop().animate({opacity: 1}, 300);
	});
	$('.info_button').hover(function() {
		$('.nav_panel').stop().animate({opacity: 1}, 300);
	});
	$('.some_meta').hover(function() {
		$('.nav_panel').stop().animate({opacity: 1}, 300);
	});
	$('.nav_panel').mouseout(function() {
		$(this).stop().animate({opacity: .3}, 300);
	});
	$('.info_button').click(function() {
		get_info();
	});
	$('.home_button').click(function() {
		url = $('.site_title a').attr('href');
		window.location = url;
	});
	$('.blur_float').click(function() {
		url = $('.site_title a').attr('href');
		window.location = url;
	});
	$('.close').click(function() {
		close_info();
	});
	
	$('.index_thumb img').each(function(i){
		$(this).fadeTo(0,0, function() {
		$(this).show();
		var fadestart = i;
		fadestart *= 50;
		$(this).delay(fadestart).fadeTo(200, 1);
	});
});
	
});
