// Scrolling picture gallery
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

jQuery(function( $ ){

	$('#scroller').serialScroll({
		items:'li',
		prev:'#nav-left a.left-button',
		next:'#nav-right a.right-button',
		offset:-130, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0,
		duration:1000,
		force:true,
		step:1,
		stop:false,
		lock:true,
		cycle:false,
		jump:false
	});
});


// Overlay picture gallery
var largeGallery = {
	currentImage:0,
	nextImage:0,
	pictures:[],
	picturePath: 'images/pictures/',
	prevWidth: 684,
	prevHeight: 513,
	
	ini: function(totalPics, path) {
		
		// create the pictures
		pics = [];
		var picNum = 1;
		
		for (var i=0; i < totalPics; i++) {
			
			if (picNum < 10) pics[i] = 'Picture_00' + picNum + '.jpg';
			else pics[i] = 'Picture_0' + picNum + '.jpg';
			picNum++;
		}
		
		largeGallery.pictures["large"] = pics;
		largeGallery.picturePath = path + largeGallery.picturePath;
		
		largeGallery.preloadImages();
	},
	
	preloadImages: function() {
		for(var i = 0; i < largeGallery.pictures["large"].length; i++) {
			largeImg = new Image();
			largeImg.src = largeGallery.picturePath + largeGallery.pictures["large"][i];
		}
	},

	showNextImage: function() {
		largeGallery.prevWidth = largeGallery.getImageWidth();
		largeGallery.prevHeight = largeGallery.getImageHeight();
		
		largeGallery.currentImage = largeGallery.currentImage + 1;
		if (largeGallery.currentImage == largeGallery.pictures["large"].length) largeGallery.currentImage = 0;
		largeGallery.displayImage();
	},
	
	showPrevImage: function() {
		largeGallery.prevWidth = largeGallery.getImageWidth();
		largeGallery.prevHeight = largeGallery.getImageHeight();
		
		if (largeGallery.currentImage == 0) {
			largeGallery.currentImage = largeGallery.pictures["large"].length - 1;
		} else {
			largeGallery.currentImage = largeGallery.currentImage - 1;
		}
		largeGallery.displayImage();
	},
	
	hide: function(hideItem) {
		if($.browser.msie){
			$(hideItem).hide();
		} else {
			$(hideItem).fadeOut();
		}
	},
	
	show: function(showItem) {
		if($.browser.msie){
			$(showItem).show();
		} else {
			$(showItem).fadeIn();
		}
	},
	
	displayImage: function() {
		
		// hide the image temporarily
		largeGallery.hide("#gallery-img");
		largeGallery.showPreloader();
		setTimeout("largeGallery.changeImage()", 500);
		largeGallery.centerGallery();
		largeGallery.show("#gallery-img");
	},
	
	showPreloader: function() {
		$("#gallery-img").replaceWith("<div id='gallery-img'><img id='loader' src='kanah-dha/images/ajax-loader.gif'></div>");
		largeGallery.updateDimensions();
		$("#loader").css({
			"top":(largeGallery.prevHeight-12)/2,
			"position":"relative"
		});
	},
	
	changeImage: function() {
		$("#gallery-img").replaceWith("<div id='gallery-img'><img id='img_"+largeGallery.currentImage+"' border='0' src='"+largeGallery.picturePath+largeGallery.pictures["large"][largeGallery.currentImage]+"'></div>");
		largeGallery.updateDimensions();
		largeGallery.centerGallery();
	},
	
	getImageWidth: function() {
		var currentImg = new Image();
		currentImg.src = largeGallery.picturePath + largeGallery.pictures["large"][largeGallery.currentImage];
		return currentImg.width;
	},
	
	getImageHeight: function() {
		var currentImg = new Image();
		currentImg.src = largeGallery.picturePath + largeGallery.pictures["large"][largeGallery.currentImage];
		return currentImg.height;
	},
	
	updateDimensions: function() {
		
		// dimensions of current image
		var newWidth = largeGallery.getImageWidth();
		var newHeight = largeGallery.getImageHeight();
		
		// in case the preloader didn't work - use previous size
		if (newWidth == 0) newWidth = largeGallery.prevWidth;
		if (newHeight == 0) newHeight = largeGallery.prevHeight;
		
		$("#gallery-container").css({
			"width":newWidth,
			"height":newHeight+38
		});
		$("#gallery-img").css({
			"width":newWidth,
			"height":newHeight
		});
		$("#gallery-nav").css({
			"width":newWidth
		});
		$("#gallery-close").css({
			"top":-52-newHeight
		});
	},
	
	centerGallery: function() {
	
		// window dimensions
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		
		// set position
		$(".gallery-container").css({  
			"top": windowHeight/2 - $(".gallery-container").height()/2 + document.documentElement.scrollTop,
			"left": windowWidth/2  - $(".gallery-container").width()/2,
			"z-index": 1000
		});
	},
	
	enlargeImage: function(imageNumber) {
		largeGallery.currentImage = imageNumber;
		largeGallery.displayGallery();
	},
	
	displayGallery: function() {
	
		// display blanket
		$("#gallery-blanket").height($("body").height());
		largeGallery.show("#gallery-blanket");
		
		// display the gallery container
		largeGallery.displayImage();
		largeGallery.show("#gallery-container");
	},
	
	closeGallery: function() {
	
		// close container
		largeGallery.hide("#gallery-container");
	
		// hide the blanket
		largeGallery.hide("#gallery-blanket");
	}
}