/**
 *
 * Spacegallery
 * Author: Stefan Petre www.eyecon.ro
 * 
 */
jQuery.fn.reverse = function()
{
	return this.pushStack(this.get().reverse(), arguments);
};

(function($){
	EYE.extend({
		
		spacegallery: {
			
			//default options (many options are controled via CSS)
			defaults: {
				border: 6, // border arround the image
				perspective: 140, // perpective height
				minScale: 0.5, // minimum scale for the image in the back
				maxXPosition: 0.5,
				duration: 800, // aimation duration
				loadingClass: null, // CSS class applied to the element while looading images
				before: function(){return false},
				after: function(){return false},
				divClass: 'spaceDiv',
				semiTransparent: true
			},
			
			animated: false,
			
			//position images
			positionImages: function(el) {
				//var a = $('#frameeees.img').get().reverse();
				//alert(a);
				/*.each(function(i) {
					alert(i);
				});*/
				
				var top = 0;
				EYE.spacegallery.animated = false;
				$(el)
					.find('a')
						.removeClass(el.spacegalleryCfg.loadingClass)
						.end()
					.find('div.' + el.spacegalleryCfg.divClass).reverse()
						.each(function(nr){
							var amount = el.spacegalleryCfg.images;
						    var at = amount - 1 - nr;
							
							var linPos = nr / (amount - 1);
							var invPos = 1 - linPos;
							
							var newWidth = (invPos * this.spacegallery.origWidth * (1 - el.spacegalleryCfg.minScale)) + (el.spacegalleryCfg.minScale * this.spacegallery.origWidth);
							var newHeight = (invPos * this.spacegallery.origHeight * (1 - el.spacegalleryCfg.minScale)) + (el.spacegalleryCfg.minScale * this.spacegallery.origHeight);
							
							// Positionen von 0 bis zur Hälfte...
							var newLeft = linPos * this.parentNode.offsetWidth * el.spacegalleryCfg.maxXPosition;
							
							// Font-Size / Line-height
							var font_size = Math.ceil(newWidth / this.spacegallery.origWidth * 100) + '%';
							
							$(this)
								.css({
									left: newLeft + 'px',
									top: (this.parentNode.offsetHeight - newHeight) / 2,
									opacity: (el.spacegalleryCfg.semiTransparent) ? 1 - (el.spacegalleryCfg.asins[at] / 2) : 1,
									width: Math.ceil(newWidth) + 'px',
									height: Math.ceil(newHeight) + 'px',
									fontSize: font_size,
									lineHeight: font_size
								});
							
							// Texte einblenden
							//$(this).children("div.texts").css("display", '');
							if (nr == 0) $(this).children("div.texts").fadeIn(200);
								
							this.spacegallery.next = el.spacegalleryCfg.asins[at+1];
							this.spacegallery.nextTop = el.spacegalleryCfg.tops[at+1] - el.spacegalleryCfg.tops[at];
							this.spacegallery.origTop = el.spacegalleryCfg.tops[at];
							this.spacegallery.opacity = 1 - el.spacegalleryCfg.asins[at];
							this.spacegallery.increment = el.spacegalleryCfg.asins[at] - this.spacegallery.next;
							this.spacegallery.current = el.spacegalleryCfg.asins[at];
							this.spacegallery.width = newWidth;
						})
			},
			
			//animate to nex image
			next: function(e) {
				if (EYE.spacegallery.animated === false) {
					EYE.spacegallery.animated = true;
					var el = this.parentNode;
					el.spacegalleryCfg.before.apply(el);
					$(el)
						.css('spacegallery', 0)
						.animate({
							spacegallery: 100
						},{
							easing: 'easeOut',
							duration: el.spacegalleryCfg.duration,
							complete: function() {
								$(el)
									.find('div.' + el.spacegalleryCfg.divClass + ':last')
									.prependTo(el);
								EYE.spacegallery.positionImages(el);
								el.spacegalleryCfg.after.apply(el);
							},
							step: function(now) {
								$('div.' + el.spacegalleryCfg.divClass, this)
									.each(function(nr){
										var amount = el.spacegalleryCfg.images;
										var at = amount - 1 - nr;
										
										var linPos = at / (amount - 1);
										var invPos = 1 - linPos;
										
										var tgtPos = (at - 1) / (amount - 1);
										var xPos = 1-tgtPos;
												   
										var newWidth, nowWidth, newHeight, nowHeight, nowPos, newPos, left, top, next;
										if (nr + 1 == el.spacegalleryCfg.images) {
											nowPos  = (linPos * this.parentNode.offsetWidth * el.spacegalleryCfg.maxXPosition);
											
											newWidth = (invPos * this.spacegallery.origWidth * (1 - el.spacegalleryCfg.minScale)) + (el.spacegalleryCfg.minScale * this.spacegallery.origWidth);
											newHeight = this.spacegallery.origHeight * (newWidth / this.spacegallery.origWidth);
											
											// Font-Size / Line-height
											var font_size = Math.ceil(newWidth / this.spacegallery.origWidth * 100) + '%';
											
											$(this).css({
												left: nowPos + 'px',
												opacity: 0.7 - now/100,
												top: (this.parentNode.offsetHeight - newHeight) / 2,
												width: Math.ceil(newWidth) + 'px',
												height: Math.ceil(newHeight) + 'px',
												fontSize: font_size,
												lineHeight: font_size
											})
										} else {
											// Positionen gewichten aus alter und wahrscheinlicher, neuer
											nowPos  = (linPos * this.parentNode.offsetWidth * el.spacegalleryCfg.maxXPosition);
											newLeft = (tgtPos * this.parentNode.offsetWidth * el.spacegalleryCfg.maxXPosition);
											newLeft = (1 - now * 0.01) * nowPos + (now * 0.01) * newLeft;
											
											// Breite gewichten
											nowWidth = (invPos * this.spacegallery.origWidth * (1 - el.spacegalleryCfg.minScale)) + (el.spacegalleryCfg.minScale * this.spacegallery.origWidth);
											newWidth = (xPos * this.spacegallery.origWidth * (1 - el.spacegalleryCfg.minScale)) + (el.spacegalleryCfg.minScale * this.spacegallery.origWidth);
											newWidth = (1 - now * 0.01) * nowWidth + (now * 0.01) * newWidth;
											
											newHeight = this.spacegallery.origHeight * (newWidth / this.spacegallery.origWidth);
											
											// Font-Size / Line-height
											var font_size = Math.ceil(newWidth / this.spacegallery.origWidth * 100) + '%';
											
											next = this.spacegallery.current - this.spacegallery.increment * now /100;
											$(this).css({
												left: newLeft + 'px',
												opacity: (el.spacegalleryCfg.semiTransparent) ? 1 - (next / 2) : 1,
												top: (this.parentNode.offsetHeight - newHeight) / 2,
												width: Math.ceil(newWidth) + 'px',
												height: Math.ceil(newHeight) + 'px',
												fontSize: font_size,
												lineHeight: font_size
											})
										}
										// Texte ausblenden
										$(this).children("div.texts").fadeOut(50);
									});
							}
						});
				}
					
				this.blur();
				return false;
			},
			
			//constructor
			init: function(opt) {
				opt = $.extend({}, EYE.spacegallery.defaults, opt||{});
				return this.each(function(){
					var el = this;
					if ($(el).is('.spacegallery')) {
						$('<a href="#"></a>')
							.appendTo(this)
							.addClass(opt.loadingClass)
							.bind('click', EYE.spacegallery.next);
						el.spacegalleryCfg = opt;
						el.spacegalleryCfg.images = $(el).find('div.' + el.spacegalleryCfg.divClass).length;
						el.spacegalleryCfg.loaded = 0;
						el.spacegalleryCfg.asin = Math.asin(1);
						el.spacegalleryCfg.asins = new Array();
						el.spacegalleryCfg.tops = new Array();
						el.spacegalleryCfg.increment = parseInt(el.spacegalleryCfg.perspective/el.spacegalleryCfg.images, 10);
						var top = 0;
						$('div.' + el.spacegalleryCfg.divClass, el)
							.each(function(nr){
								var elImg = this;
								el.spacegalleryCfg.asins[nr] = 1 - Math.asin((nr+1)/el.spacegalleryCfg.images)/el.spacegalleryCfg.asin;
								top += el.spacegalleryCfg.increment - el.spacegalleryCfg.increment * el.spacegalleryCfg.asins[nr];
								el.spacegalleryCfg.tops[nr] = top;
								elImg.spacegallery = {};
								
								elImg.spacegallery.origWidth = this.offsetWidth;
								elImg.spacegallery.origHeight = this.offsetHeight;
								
								// Texte ausblenden
								$(this).children("div.texts").fadeOut(1);
								
								if (nr == el.spacegalleryCfg.images - 1) {
									EYE.spacegallery.positionImages(el);
								}
							});
						el.spacegalleryCfg.asins[el.spacegalleryCfg.images] = el.spacegalleryCfg.asins[el.spacegalleryCfg.images - 1] * 1.3;
						el.spacegalleryCfg.tops[el.spacegalleryCfg.images] = el.spacegalleryCfg.tops[el.spacegalleryCfg.images - 1] * 1.3;
						if (el.spacegalleryCfg.loaded == el.spacegalleryCfg.images) {
							EYE.spacegallery.positionImages(el);
						}
					}
				});
			}
		}
	});
	
	$.fn.extend({
	
		/**
		 * Create a space gallery
		 * @name spacegallery
		 * @description create a space gallery
		 * @option	int			border			Images' border. Default: 6
		 * @option	int			perspective		Perpective height. Default: 140
		 * @option	float		minScale		Minimum scale for the image in the back. Default: 0.2
		 * @option	int			duration		Animation duration. Default: 800
		 * @option	string		loadingClass	CSS class applied to the element while looading images. Default: null
		 * @option	function	before			Callback function triggered before going to the next image
		 * @option	function	after			Callback function triggered after going to the next image
		 */
		spacegallery: EYE.spacegallery.init
	});
	$.extend($.easing,{
		easeOut:function (x, t, b, c, d) {
			return -c *(t/=d)*(t-2) + b;
		}
	});
})(jQuery);
