/*

	jPile: a jQuery plugin for creating navigable piles or stacks
	Author: Darrell Stephenson
	Author url: http://www.darrellstephenson.com

*/

(function( $ ){

  var methods = {
	
    init : function( options ) {
		
		var el = $(this);
				
		var settings = {
			'totalWidth'	: el.width(),
			'imageSpacing'	: 5,
			'imageClass'	: '.fan-image',
			'maxVisible'	: -1,
			'prevClass'		: '.prev-image',
			'nextClass'		: '.next-image'
		};
		
		if (options) {
			$.extend(settings, options);
		}
		
		var imageCollection = el.children().filter(settings.imageClass);
		var prevButton = el.find(settings.prevClass);
		var nextButton = el.find(settings.nextClass);
		
		if(prevButton){
			prevButton.bind('click', function(){
				methods.showPrevious(el);
			});
		}
		
		if (nextButton){
			nextButton.bind('click', function(){
				methods.showNext(el);
			});
		}
		
		var tempArray = [];
		
		for (var i = 0, length = imageCollection.length; i < length; i++){
			var image = $(imageCollection[i]);
			image.css({'position':'absolute', 'display':'hidden'});
			tempArray.push(image);
		}
		
		el.data('pile', {
			images : tempArray,
			pileSettings : settings
		});
		
		methods.drawPile(el);
		
		return this;
	},
    drawPile : function(el) {
		var pile = el.data('pile');
		var settings = pile.pileSettings;
		var length = 0;
		(settings.maxVisible > 0 ? length = settings.maxVisible : length = pile.images.length);
		
		for (var i = 0; i <= pile.images.length; i++){
			var image = $(pile.images[i]);
			var leftPosition = settings.totalWidth - image.width() - (i * settings.imageSpacing) + 'px';
			var topPosition = (i * settings.imageSpacing) + 'px';
			if (i <= length) {
				image.css({'z-index':length-i, 'display' : 'block'});
				//non webkit browsers don't handle the opacity animation very smoothly
				if ($.browser.webkit){
					image.animate({'top':topPosition, 'left':leftPosition, 'opacity':1 - (i / length)}, 200);
				} else {
					image.animate({'top':topPosition, 'left':leftPosition}, 200);
				}
			} else {
				image.hide();	
			}
		}
		
		return this;
	},
    showNext : function(el) {
		var pile = el.data('pile');
		var settings = pile.pileSettings;
		var tempArray = pile.images; 
		var currentImage = tempArray.shift();
		tempArray.push(currentImage);
		el.data('pile', {
			images : tempArray,
			pileSettings: settings
		});
		methods.drawPile(el);
		return this;
	},
    showPrevious : function(el) { 
		var pile = el.data('pile');
		var settings = pile.pileSettings;
		var tempArray = pile.images;
		var currentImage = tempArray.pop();
		tempArray.splice(0,0,currentImage);
		el.data("pile", {
			images : tempArray,
			pileSettings: settings
		});
		methods.drawPile(el);
		return this;
	}
  };

  $.fn.pile = function( method ) {
    if ( methods[method] ) {
      return methods[ method ].apply(this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist in the jPile plugin' );
    }    
  };

})( jQuery );

