/*
	Copyright (C) 2011  Navig8 Ltd.

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


	Plugin developed and maintained by Navig8 Ltd, London (www.navig8.co.uk)
	Developer: Lorenzo Campanis
*/
(function( $ ){
	
	var methods = {
		/*
			function sets all global variables for the masthead animation
			Then fires the animation
		*/
		init : function( options ) {
				
			return this.each(function() {
			
				var settings = {
					delay 				: 2000, // in milliseconds
					fadedelay 		: 1000, // in milliseconds
					images 				: 1,
					id 						: "imagin8",
					playbackwards : false
				};
				
				if(options.playbackwards == true) {
					settings.backwards = false;
				}
					
				if ( options )
					$.extend( settings, options );
				
				var $this = $(this);
				
				/*
					Private function processes all images and transitions whithin the masthead
				*/
				var transition = function() {
					// get all the images inside the masthead. Only 1 img when the anim 1st starts. Then at most 2 in total
					var last_img = $this.find("img:last"); // get the last image

					// take the image path
					var img = new Object();
							img.path 			= last_img.attr("src").split("/");// implode to get full path
							img.filename 	= img.path[ img.path.length-1 ];
							img.number 		= parseInt(img.filename.match(/\d[0-9]*/));// get 1 from 1.jpg
							img.extension = img.path[ img.path.length-1 ].split(".")[1]; // for this to work the path needs to have only 1 . as in 12.jpg
							
							// process the new image we want to put
							last_img.css("zIndex", 1);// make sure it's behind any other image
							last_img.before('<img src="" alt="'+last_img.attr("alt")+'" />');// put the empty image
					
					// get the first image, which NOW is the new one
					var new_img = $this.find("img:first"); 
					
					// check if the animation is done, so it can play backwards
					if(settings.playbackwards == true) {
						if(settings.backwards == true && img.number-- <= 1) {
							settings.backwards = false;
							img.number+=2;
						}
						else if(settings.backwards == false && img.number++ >= settings.images) {
							settings.backwards = true;
							img.number-=2;
						}
					}
					// no backwards playback
					else {
						// reset the src number
						if(++img.number > settings.images)
							img.number = 1;
					}
					
					// load event for when image has finished loading
					new_img.load(function() {
						new_img.unbind("load");	
						last_img.fadeOut(settings.fadedelay, function() {
							new_img.next("img").remove();

							setTimeout(function() { transition(); }, settings.delay);
						});
					});
					
					//format the path without the filename and assign it	
					new_img.attr("src", img.path.splice(0, img.path.length-1).join("/") +"/"+ img.number +"."+ img.extension);
				};
				
				setTimeout(function() { transition(); }, settings.delay);
				
			});
		}
	};
	
  $.fn.imagin8 = function( method ) {
		// Method calling logic
    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 {
      alert( 'Method ' +  method + ' does not exist on jQuery.imagin8' );
    } 
  };
	
})( jQuery );

