/*

 * Create an Image fading slider to the popular Flash fader



 * ImageFadingSlider 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; version 3 of the License.

 *

 * ImageFadingSlider 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.

 * @license http://www.gnu.org/licenses/gpl-3.0.txt GPL

 *

 * You should have received a copy of the GNU General Public License

 * along with imagefader; if not, write to the Free Software

 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 *

 * @name		ImageFadingSlider

 * @author		Youngjae Ji

 * @contact		zirho6@gmail.com

 * @version		1.0

 * @date		Nov 7 2009

 * @type    	jQuery plugin

 *

 * The latest version of ImageFadingSlider can be obtained from:

 * http://www.wingtech.co.kr/imagefader

 *

 */



(function($) {

	//

	// plugin definition

	//

	$.fn.imageFadingSlider = function(options) {

		// build main options before element iteration

		var main_opts = $.extend({}, $.fn.imageFadingSlider.defaults, options);



		// iterate and reformat each matched element

		return this.each(function() {

			var $this = $(this);

			var currItem = $("img:eq(0)", $this);

			var opts = $.metadata ? $.extend({}, main_opts, $this.metadata()) : main_opts;

			var nextEasing;

						

			$this

				.css("background","transparent")

				.css("width",opts.item_width)

				.css("height",opts.item_height)

				.css("border","3px #888 solid")

				.css("overflow","hidden")

				.css("display","block");



			$("img", $this)

				.css("width",opts.item_width+opts.dist)

				.css("height",opts.item_height+opts.dist)

				.css("margin-left",-opts.dist)

				.css("margin-top",-opts.dist)

				.css("position","absolute")

				.css("top","0px")

				.css("left","0px");



			if (opts.total_time - opts.fading_time - 500 < 0 ) opts.fading_time = opts.total_time - 500;



			if(opts.easing == "random") thisEasing = $.fn.imageFadingSlider.easing[ Math.floor( Math.random()*$.fn.imageFadingSlider.easing.length ) ];

			else thisEasing = $.fn.imageFadingSlider.easing[opts.easing];



			doFadingEffect(currItem, thisEasing);



			function doFadingEffect(iCurrItem, iEasing){

				

				//make the image visible

				iCurrItem

					.css("margin-top", -(iEasing[0]*opts.dist))

					.css("margin-left", -(iEasing[1]*opts.dist))

					.css("display", "block")

					.css("z-index", 100);

				

				//reserve first image fade

				setTimeout(function(){

					iCurrItem.animate({"opacity": "hide"}, { duration: opts.fading_time, queue: false});

				}, opts.total_time - opts.fading_time - 500);



				//make sibling invisible

				iCurrItem.siblings()

					.css("display", "none")

					.css("z-index", 98);

					

				if(opts.easing == "random") nextEasing = $.fn.imageFadingSlider.easing[ Math.floor( Math.random()*$.fn.imageFadingSlider.easing.length ) ];

				else nextEasing = $.fn.imageFadingSlider.easing[opts.easing];



				//make the next image visible

				var iNextImage = getNextImg(iCurrItem);

				iNextImage

					.css("margin-top", -(nextEasing[0]*opts.dist))

					.css("margin-left", -(nextEasing[1]*opts.dist))

					.css("display", "block")

					.css("z-index", 99);



				//make the image move

				iCurrItem.animate({"marginTop": -(iEasing[2]*opts.dist),"marginLeft": -(iEasing[3]*opts.dist)}, opts.total_time, function(){ doFadingEffect(iNextImage, nextEasing);} );				

			};

			

			// get next image circular

			function getNextImg(cur){

				var next = cur.next();

				if(!next.size()) next = $("img:eq(0)", cur.parent());

				return next;

			};

		});

	};



	//

	// plugin defaults

	//

	$.fn.imageFadingSlider.defaults = {

		item_width: 100,

		item_height: 100,

		dist: 40,

		total_time: 6000,

		fading_time: 2000,

		easing: "4"

	};

	//

	// plugin easings

	//

	$.fn.imageFadingSlider.easing = [

		["1", "0", "0", "0"],

		["0", "1", "0", "0"],

		["0", "0", "1", "0"],

		["0", "0", "0", "1"],



		["1", "1", "0", "0"],

		["1", "0", "0", "1"],

		["0", "1", "1", "0"],

		["0", "0", "1", "1"]

	];

//

// end of closure

//

})(jQuery);


