/*
========================================
===> [ Copyright © Swarm Media Ltd ] <==
========> All rights reserved. <========
========================================
#::- This code was written by Swarm -::#
========================================
====> RETAIN THIS COPYRIGHT NOTICE! <===
=======> http://www.swarmuk.com <=======
========================================
*/

$(document).ready(function(){
	/* Main image variables. */
	var interfaceHeight = 144 + 144 + 20 + 20;
	var horizontalMargin = 40;
	var resizeRatio = 1;
	var imagePad = 0;
	var origImageWidth = null;
	var origImageHeight = null;
	var curImageWidth = null;
	var curImageHeight = null;
	var photoSrc = null;
	var photoID = null;
	var maxImageWidth = $(window).width() - horizontalMargin;
	var maxImageHeight = $(window).height() - interfaceHeight;
	var fsImageWidth = $(window).width();
	var fsImageHeight = $(window).height();

	function getNextPhoto() {
		$('#gallery_main').css('background-image', 'url(../images/loading.gif)'); // Hide the loading icon.

		$.getJSON("next_photo.php", {id:photoID}, function(data){
			curphotoID = photoID;
			photoID = data.id;
			origImageWidth = data.width;
			origImageHeight = data.height;
			photoSrc = 'uploads/photos/' + photoID + '.jpg';
			$('#image img').attr("src", photoSrc); // Set main image src to first in gallery.
			update_image_size();
			//alert(curphotoID + " " + photoID);
		});

	}
	
	getNextPhoto();

	/* On main image being fully loaded. */
	$("#image img").load(function() {
		update_image_size();
		$('#gallery_main').css('background-image', 'none'); // Hide the loading icon.
		$('#image img').fadeIn('normal'); // Once sized, fade in the new image.
		
		$(this).delay(5000,function(){
			$('#image img').fadeOut('fast');
			getNextPhoto();
		});
		
	});
	
	/* On resizing the window. */
	$(window).resize(function() {
		update_image_size();
	});
	
	function update_image_size() {
		imagePad = 0;
		fsImageWidth = $(window).width();
		fsImageHeight = $(window).height();
		maxImageWidth = $(window).width() - horizontalMargin;
		maxImageHeight = $(window).height() - interfaceHeight;
		
		// Set the image dimensions based on the window size.
		if (origImageWidth > maxImageWidth || origImageHeight > maxImageHeight) {
			curImageWidth = origImageWidth;
			curImageHeight = origImageHeight;
			
			if (curImageWidth > maxImageWidth) {
				resizeRatio = maxImageWidth / origImageWidth;
				curImageWidth = maxImageWidth;
				curImageHeight = origImageHeight * resizeRatio;
			}
			
			if (curImageHeight > maxImageHeight) {
				resizeRatio = maxImageHeight / curImageHeight;
				curImageHeight = maxImageHeight;
				curImageWidth = curImageWidth * resizeRatio;
			}
			
			if (maxImageHeight > curImageHeight) {
				imagePad = (maxImageHeight - curImageHeight) / 2;	
			}

		} else {
			curImageWidth = origImageWidth;
			curImageHeight = origImageHeight;
			
			if (maxImageHeight > curImageHeight) {
				imagePad = (maxImageHeight - curImageHeight) / 2;	
			}

		}
		
		curImageWidth = Math.round(curImageWidth);
		curImageHeight = Math.round(curImageHeight);

		$('#image img').css('width', curImageWidth + 'px');
		$('#image img').css('height', curImageHeight + 'px');
		$('#image').css('padding-top', imagePad + 'px');
		$('#image').css('background-color', 'transparent');
	}

});
