/*
 * WHQ Image Gallery 1.0 by Sam Holton
 * 
 * USE: The image gallery requires an array of image paths. 
 */

//TO DO:

/*
 *load images one at a time
 * 
 */

(function($){
	
	var containerWidth = 0;
	
	$.fn.gallery = function(images, options){
		
		var defaults = {
				nextButton   : null,
				backButton   : null,
				description  : null,
				descriptionArray:null,
				autoCycle    : true,
				cycleDuration: 10000,
				startIndex   : 0,
				transitionTime: 800
		}
		
		var options = $.extend(defaults, options);  
		
		var imageIndex = 0;
		var lastWindowHeight = 0;
		var nextButton = options.nextButton;
		var backButton = options.backButton;
		var description = options.description;
		var descArr = options.descriptionArray;
		var autoCycle = options.autoCycle;
		var cycleDuration = options.cycleDuration;
		var timer;
		
		return this.each(function(){
			
			obj = $(this);
			
			$(window).resize(resizeImage);
			
			//event listeners
			
			nextButton.click(nextImage);
			backButton.click(previousImage);
			
			
			//initialize plugin
			init();
			
			
			function init(){
				$('.index_title h1.bottom').html(h2Array[imageIndex]);
				obj.append('<img alt="" src="'+images[0]+'" style="position:absolute; z-index:1;" />');
				resizeImage();
				setTimeout(resizeImage, 300);
				
				if(autoCycle == true){
					timer = setInterval(nextImage, cycleDuration);
				}
			}
			
			
			
			function nextImage(){
				clearInterval(timer);
				
				if(imageIndex < images.length -1){
					imageIndex++;
				}else{
					imageIndex = 0;
				}
				
				var lastFrameIndex = imageIndex == 0 ? images.length - 1 : imageIndex - 1;
				
				obj.append('<img alt="" src="'+images[imageIndex]+'" style="position:absolute; z-index:0;" />');
				obj.children('img').eq(0).css('z-index',1);
				
				obj.children('img').load(function(){
					resizeImage();
					
					obj.children('img').eq(0).fadeTo(options.transitionTime, 0, function(){
						$(this).remove();
					});
                                        $('.index_title').children('h1').fadeTo(options.transitionTime/2, 0, function(){
                                            $('.index_title').children('h1').html(descArr[imageIndex]);
                                            $('.index_title').children('h1').fadeTo(options.transitionTime/2, 1);

                                        });
				});

                                

				timer = setInterval(nextImage, cycleDuration);
			}
			
			
			
			function previousImage(){
				if(imageIndex > 0){
					imageIndex--;
				}else{
					imageIndex = images.length - 1;
				}
				
				var lastFrameIndex = imageIndex == images.length - 1 ? 0 : imageIndex + 1;
				
				obj.append('<img alt="" src="'+images[imageIndex]+'" style="position:absolute; z-index:0;" />');
				obj.children('img').eq(0).css('z-index',1);
				
				obj.children('img').load(function(){
					resizeImage();
					
					obj.children('img').eq(0).fadeTo(options.transitionTime, 0, function(){
						$(this).remove();
					});
                                        $('.index_title').children('h1').fadeTo(options.transitionTime/2, 0, function(){
                                            $('.index_title').children('h1').html(descArr[imageIndex]);
                                            $('.index_title').children('h1').fadeTo(options.transitionTime/2, 1);

                                        });
				});
			}
			
			
			
			function gotoImage(){
				
			}
			
			function resizeImage(){
				var height = obj.children('img').height();
				var width = obj.children('img').width();
				var windowWidth = $(window).width();
				var windowHeight = $(window).height();
				var ratio = width/height;

				if (windowWidth/windowHeight < ratio){
					//match window height
					obj.children('img').height(windowHeight);
					obj.children('img').width(ratio * windowHeight);
				}else{
					//match window width
					obj.children('img').width(windowWidth);
					obj.children('img').height((windowWidth * height)/width);
				}

				lastWindowHeight = windowHeight;
			}
			
		});
	}
})(jQuery);
