//
//	js by Yuri Koval'ov
//

// image preload function from http://www.mattfarina.com/2007/02/01/preloading_images_with_jquery
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

// set image size/workarea size
function setImageSize( imgWidth, imgHeight )
{
	var max = 1024;
	var min = 300;
	var ratio = imgHeight/imgWidth;
	var fh = window.innerHeight - 160;
	var fw = window.innerWidth - (60 + 35)*2;
	
	if (fh > max) { fh = max;} else if (fh < min) {fh = min};
	if (fw > max) { fw = max;} else if (fw < min) {fw = min};
	
	var height, width;
	
	if (fw > fh) {
		// adjust to width
		height = fh;
		width = Math.round(height*(1/ratio));

	} else {
		// adjust to height
		width = fw;
		height = width*ratio;
	}
	
	// check for overflows + minimums
	if ( (height > fh) ) {
		height = fh;
		width = Math.round(height*(1/ratio));
	}
	
	if ( (width > fw) ) {
		width = fw;
		height = width*ratio;
	}
	// center image
	var halfheight = Math.round(height/2 - 40);
	var halfwidth = Math.round(width/2);
	$("#work").width(width);
	$("#work").height(height);
	$("#work").css("margin-top", "-" + halfheight + "px");
	$("#work").css("margin-left", "-" + halfwidth + "px");
}

// define starting point
var album = 0;
var current = 0;
var animateSpeed = 1000;
$(function() {
	// define buttons
	$("#name").click(function () {window.location='./';}).attr("title", "Yuri Koval'ov");
	$("#about").click(function () {window.location='./about.php';}).attr("title", "About");
	$("#works").click(function () {window.location='./works.php';}).attr("title", "Works");
	
	// check if it's a regular page or an album
	if ($("#navigation").length > 0) {
			// initial onload
		$(document).ready(function(){
			// preload images
			// get initial image
			$.loadImage(current);

		});
		// Go to next by clicking
		$("#right").click( function() {$.loadImage(current+1); current++;}).attr("title", "Next");
		
		// Go previous
		$("#left").click( function() {$.loadImage(current-1); current--;}).attr("title", "Previous");
		
		// trigger navigation by keyboard
		$(window).keydown(function(event){
				if (event.keyCode == 39) {$.loadImage(current+1); current++;};
				if (event.keyCode == 37) {$.loadImage(current-1); current--;};
			});
		}
}); // close $(

// Load Image function.
jQuery.loadImage = function( imageToDisplay ) {
	$.ajax({	type:"GET",
				url:"ajax_getimage.php",
				datatype:"xml",
				data:{display : imageToDisplay, 'album' : album},
				success:function(xml) {
					var imgSrc = $(xml).find('src').text();
					var imgWidth = parseInt($(xml).find('width').text());
					var imgHeight = parseInt($(xml).find('height').text());
					var imgTitle = $(xml).find('title').text();
					var imgDesc = $(xml).find('desc').text();
					$.preloadImages(imgSrc);
					$("#content").animate({opacity: 0.0},  animateSpeed, "linear",function(){
						$("#content").html("");
						$("<img>").attr("src", imgSrc).attr("id", "work").appendTo("#content");
						$("#work").width( imgWidth );
						$("#work").height( imgHeight );
						setImageSize( imgWidth, imgHeight );
						}).animate({opacity: 1.0}, animateSpeed );
				}});
	}