/****************************
* jQuery TimeMachine
* Author: Zynaga
* Release: 29-09-2010
* jQ Required Version: 1.4.0
*****************************
* @params
* @deep 	=  
* @scale 		= 
* @pos			= 
* @perspective 	= 
* @speed		= 
****************************/

(function($){ 
	$.fn.timeMachine = function(options) {
		
		if (typeof options == "string") {
			return;
		}
		
		var defaults = {
			deep: 5,
			scale: 	0.88,
			size: [790, 590],
			pos: [365, 0],
			perspective: [-0.7, 0.35],
			speed: 500,
			change: function (el) { }
		}; 
		var options = $.extend(defaults, options);
		
		var blocked = false;
		
		var defaultState = function (obj, i, total, speed) {
		
			blocked = true;
		
			var w = calcSize(options.size[0], i);
			var h = calcSize(options.size[1], i);
			
			obj.css({
				"z-index": 	total * 2 - i * 2,
				"left": 	calcPos(options.size[0], w, 0) + "px",
				"top": 		calcPos(options.size[1], h, 1) + "px",
				"overflow": "visible"
			});
			
			obj.width(w);
			obj.height(h);
			obj.fadeTo(speed, calcOpacity(i), function () {
				blocked = false;
			});
			
			return obj;
			
		}
		
		var calcPos = function (v1, v2, t) {
			return (v1 * options.perspective[t]) - (v2 * options.perspective[t]) + options.pos[t];
		}
		
		var calcSize = function (v1, i) {
			return v1 - (v1 * (1 - options.scale) * (i + 1));
		}
		
		var calcOpacity = function (i) {
			return i < options.deep ? 1 : 0;//((1 / options.deep) * -i) + 1;
		}
		
		var animate = function (box, boxChildren, go) {
						
			var total = boxChildren.length;
			
			boxChildren.each( function(i) {
				
				var w = calcSize(options.size[0], i - go);
				var h = calcSize(options.size[1], i - go);
		
				$(this).animate(
					{
						"width":  	w + "px",
						"height": 	h + "px",
						"left": 	calcPos(options.size[0], w, 0) + "px",
						"top": 		calcPos(options.size[1], h, 1) + "px",
						"opacity": 	i ? calcOpacity(i - 1) : 0
					}, 
					options.speed, 
					function () {
						if (i == 0) { 
							box.append(defaultState($(this), total - 1, total, options.speed));
						} else if (i == 1) {
							options.change($(this));
						}
						$(this).css({"overflow": "visible"});
					}
				).css({
					"overflow": "visible",
					"z-index": total * 2 - (i - 1) * 2
				});
				
			});
			
		};
		
		return this.each( function() {
		
			var box = $(this);
			var boxChildren = box.children(":not(.tmAnimate)");
			
			if (!options.size) {
				options.size = [box.width(), box.height()];
			}
			
			if (boxChildren.length <= options.deep) {
				box.append(boxChildren.clone());
				boxChildren = box.children(":not(.tmAnimate)");
			}
		
			$(window).load( function () {
			
				boxChildren.show();
			
				var mover = $("<a></a>")
								.addClass("tmAnimate")
								.css({ 
									"z-index": 	boxChildren.length * 2 - 1
								})
								.click(function () {
									if (blocked == false) {
										blocked = true;
										animate(box, box.children(":not(.tmAnimate)"), 1);
									}
								});
				
				box.prepend(mover);
				
				boxChildren.each( function (i) {
					
					defaultState($(this), i, boxChildren.length, 0);
					
					if (!i) options.change($(this));
					
				});
				
			});
			
		})
	}
})(jQuery); 

