/**
 *
 * Spacegallery
 * Author: Stefan Petre www.eyecon.ro
 * 
 */

var galleries = Array();

(function($){
	EYE.extend({

		spacegallery: {

			//default options (many options are controled via CSS)
			defaults: {
				border: 2, // border arround the image
				perspective: 100, // perpective height
				minScale: 0.2, // minimum scale for the image in the back
				duration: 500, // aimation duration
				loadingClass: null, // CSS class applied to the element while looading images
				before: function(){return false},
				after: function(){return false},
				first: true,
				shown: false,
				visible: 5
			},

			animated: false,

			//position images
			positionImages: function(el,d) {
				EYE.spacegallery.animated = false;
				$(el)
					.find('a')
						.removeClass(el.spacegalleryCfg.loadingClass)
						.end()
					.find('img')
						.removeAttr('height')
						.each(function(nr){
							var show = false;
							if ( el.spacegalleryCfg.visible < el.spacegalleryCfg.images ) {
//								if ( EYE.spacegallery.first && el.spacegalleryCfg.visible < el.spacegalleryCfg.images - 2 ) {
//									EYE.spacegallery.first = false;
								//} else 
								if ( nr >= el.spacegalleryCfg.images - el.spacegalleryCfg.visible )
									show = true;
							} else { show = true }

							if ( show && el.spacegalleryCfg.first && nr == el.spacegalleryCfg.images - 1 ) {
								el.spacegalleryCfg.first = false;
								show = false;
								$(this).hide();
								//_next();
								//alert ("FIRST");
							}

							var backshow = false;

							if ( nr == el.spacegalleryCfg.images - 1 && d == -1 ) {
								show = true;
								backshow = true;
							} else if ( d == -1 )
								show = false

							if ( show ) {
								nr-= (el.spacegalleryCfg.images - el.spacegalleryCfg.visible) ;
								var newWidth = this.spacegallery.origWidth - (this.spacegallery.origWidth - this.spacegallery.origWidth * el.spacegalleryCfg.minScale) * el.spacegalleryCfg.asins[nr];

								var t1 = el.spacegalleryCfg.tops[nr];
								var t2 = parseInt((newWidth + el.spacegalleryCfg.border)/2, 10);
								var t3 = el.spacegalleryCfg.asins[nr]
								var t4 = parseInt(newWidth)
								if ( !isNaN(t1) && !isNaN(t2) && !isNaN(t3) && !isNaN(t4) )
								$(this)
									.css({
										top: t1 + 'px',
										marginLeft: - t2 + 'px',
										opacity: 1 - t3,
										width: t4 + 'px'
									})
								if ( ! backshow )
									$(this).show();
						//			.attr('width', parseInt(newWidth));
								this.spacegallery.next = el.spacegalleryCfg.asins[nr+1];
								this.spacegallery.prev = el.spacegalleryCfg.asins[nr-1];
								this.spacegallery.nextTop = el.spacegalleryCfg.tops[nr+1] - el.spacegalleryCfg.tops[nr];
								this.spacegallery.prevTop = el.spacegalleryCfg.tops[nr-1] - el.spacegalleryCfg.tops[nr];
								this.spacegallery.origTop = el.spacegalleryCfg.tops[nr];
								this.spacegallery.opacity = 1 - el.spacegalleryCfg.asins[nr];
								this.spacegallery.increment = el.spacegalleryCfg.asins[nr] - this.spacegallery.next;
								this.spacegallery.decrement = el.spacegalleryCfg.asins[nr] - this.spacegallery.prev;
								this.spacegallery.current = el.spacegalleryCfg.asins[nr];
								this.spacegallery.width = newWidth;
							} else if ( d != -1 ) {
							//	this.spacegallery.opacity = 0
								$(this).hide();
							}

						})
			},

			//animate to prev image
			prev: function(e) {
				if (EYE.spacegallery.animated === false) {
					EYE.spacegallery.animated = true;
					if ( e == -1 )
						var el = this;
					else
						var el = this.parentNode;
					el.spacegalleryCfg.before.apply(el, new Array("-1"));
					$(el)
						.find('img:first')
						.hide()
						.appendTo(el);
					EYE.spacegallery.positionImages(el,-1);

					$(el)
						.css('spacegallery', 0)
						.animate({
							spacegallery: 100
						},{
							easing: 'easeOut',
							duration: el.spacegalleryCfg.duration,
							complete: function() {
								EYE.spacegallery.positionImages(el);
								el.spacegalleryCfg.after.apply(el, new Array(el.num, el.spacegalleryCfg.active, -1));
							},
							step: function(now) {
								$('img', this)
									.each(function(nr){
										var show = false;
										if ( el.spacegalleryCfg.visible < el.spacegalleryCfg.images ) {
											if ( nr >= el.spacegalleryCfg.images - el.spacegalleryCfg.visible )
												show = true;
										} else { show = true }

										if ( show == true ) {
											var newWidth, newTop, prev;
											if (nr + 1 == el.spacegalleryCfg.images ) {
												el.spacegalleryCfg.active = this.spacegallery.id;
												//newTop = this.spacegallery.origTop + this.spacegallery.prevTop * 4 * now /100;
												//newWidth = this.spacegallery.width * 1.1 //newTop / this.spacegallery.origTop;
													$(this)
														.fadeIn("slow");
											} else {
												prev = this.spacegallery.current - this.spacegallery.decrement * now /100;
												newWidth = this.spacegallery.origWidth - (this.spacegallery.origWidth - this.spacegallery.origWidth * el.spacegalleryCfg.minScale) * prev;
												newTop = this.spacegallery.origTop + this.spacegallery.prevTop * now /100;
												if ( newTop > 0 && newWidth >= 0 ) {
													$(this).css({
														'top': newTop + 'px',
														opacity: 1 - prev,
														'width': newWidth + 'px',
														marginLeft: - parseInt((newWidth + el.spacegalleryCfg.border)/2, 10) + 'px'
													});
													//.attr('width', newWidth);
												}
											}
										} else { // invisible
											// $(this).hide();
										}
									});
							}
						});
				}

				this.blur();
				return false;
			},


			//animate to nex image
			next: function(e) {
				if (EYE.spacegallery.animated === false) {
					EYE.spacegallery.animated = true;
					if ( e == -1 )
						var el = this;
					else
						var el = this.parentNode;

					var dur = 0;

					if ( el.spacegalleryCfg.shown == false ) {
						el.spacegalleryCfg.shown = true;
						$("#myGallery")
							.show(200)
							.animate( { opacity:1 }, 300 );
						dur = 500;
					}

					el.spacegalleryCfg.before.apply(el,new Array("1"));
					$(el)
						.css('spacegallery', 0)
						.animate({ spacegallery: 0 }, dur)
						.animate({
							spacegallery: 100
						},{
							easing: 'easeOut',
							duration: el.spacegalleryCfg.duration,
							complete: function() {
								$(el)
									.find('img:last')
									.hide()
									.prependTo(el);
//									.fadeOut("fast");
								EYE.spacegallery.positionImages(el);
								el.spacegalleryCfg.after.apply(el, new Array(el.num, el.spacegalleryCfg.active,1));
							},
							step: function(now) {
								$('img', this)
									.each(function(nr){
										var show = false;
										if ( el.spacegalleryCfg.visible < el.spacegalleryCfg.images ) {
											if ( nr >= el.spacegalleryCfg.images - el.spacegalleryCfg.visible )
												show = true;
										} else { show = true }

										if ( show == true ) {
											var newWidth, newTop, next;
											if (nr + 1 == el.spacegalleryCfg.images ) {
												newTop = this.spacegallery.origTop + this.spacegallery.nextTop * 4 * now /100;
												newWidth = this.spacegallery.width * 1.1 //newTop / this.spacegallery.origTop;
												if ( !isNaN(newTop) && !isNaN(newWidth) ) {
													$(this)
														.css({
														//	'top': newTop + 'px',
															opacity: 0.7 - now/100
													//		'width': newWidth + 'px',
													//		'marginLeft': - parseInt((newWidth + el.spacegalleryCfg.border)/2, 10) + 'px'
														});
														//.attr('width', newWidth);
												}
											} else {
												if (nr + 1 == el.spacegalleryCfg.images - 1) 
													el.spacegalleryCfg.active = this.spacegallery.id;
												next = this.spacegallery.current - this.spacegallery.increment * now /100;
												newWidth = this.spacegallery.origWidth - (this.spacegallery.origWidth - this.spacegallery.origWidth * el.spacegalleryCfg.minScale) * next;
												newTop = this.spacegallery.origTop + this.spacegallery.nextTop * now /100;
												if ( !isNaN(newTop) && !isNaN(newWidth) && !isNaN(next) ) {
													$(this).css({
														'top': newTop + 'px',
														opacity: 1 - next,
														'width': newWidth + 'px',
														marginLeft: - parseInt((newWidth + el.spacegalleryCfg.border)/2, 10) + 'px'
													});
													//.attr('width', newWidth);
												}
											}
										} else { // invisible
											// $(this).hide();
										}
									});
							}
						});
				}

				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')) {
						el.num = galleries.length;
						galleries[galleries.length] = this;
						$('<a href="#"></a>')
							.appendTo(this)
							.addClass(opt.loadingClass)
							.bind('click', EYE.spacegallery.next);
						this.height = 300;
						el.next = EYE.spacegallery.next;
						el.prev = EYE.spacegallery.prev;
						el.spacegalleryCfg = opt;
						el.spacegalleryCfg.images = el.getElementsByTagName('img').length;
						if ( el.spacegalleryCfg.visible == -1 )
							el.spacegalleryCfg.visible = el.spacegalleryCfg.images;
						else if ( el.spacegalleryCfg.images < el.spacegalleryCfg.visible )
							el.spacegalleryCfg.visible = el.spacegalleryCfg.images;
						el.spacegalleryCfg.loaded = 0;
						el.spacegalleryCfg.asin = Math.asin(1);
						el.spacegalleryCfg.asins = {};
						el.spacegalleryCfg.tops = {};
						el.spacegalleryCfg.increment = parseInt(el.spacegalleryCfg.perspective/el.spacegalleryCfg.visible, 10);
						var top = 0;
						$('img', el)
							.each(function(nr){
								var imgEl = new Image();
								var elImg = this;
								el.spacegalleryCfg.asins[nr] = 1 - Math.asin((nr+1)/el.spacegalleryCfg.visible)/el.spacegalleryCfg.asin;
								top += el.spacegalleryCfg.increment - el.spacegalleryCfg.increment * el.spacegalleryCfg.asins[nr];
								el.spacegalleryCfg.tops[nr] = top;
								elImg.spacegallery = {};
								imgEl.src = this.src;
								imgEl.id = this.id;
								$(this).prependTo(el);
								$(this)
									.bind('click', EYE.spacegallery.next);
								if (imgEl.complete) {
									el.spacegalleryCfg.loaded ++;
									elImg.spacegallery.origWidth = imgEl.width;
									elImg.spacegallery.origHeight = imgEl.height
									elImg.spacegallery.id = imgEl.id;
									if (el.spacegalleryCfg.loaded == el.spacegalleryCfg.images) {
										EYE.spacegallery.positionImages(el);
										//if ( this.click )
										//this.click();
									}
								} else {
									imgEl.onload = function() {
										el.spacegalleryCfg.loaded ++;
										elImg.spacegallery.origWidth = imgEl.width;
										elImg.spacegallery.origHeight = imgEl.height;
										elImg.spacegallery.id = imgEl.id;
										if (el.spacegalleryCfg.shown == false) {
											if (el.spacegalleryCfg.loaded >= 5 || ( el.spacegalleryCfg.images < 5 && el.spacegalleryCfg.images == el.spacegalleryCfg.loaded )) {
												EYE.spacegallery.positionImages(el);
												if ( this.click )
													this.click();
											}
										}
									};
								}
							});
						el.spacegalleryCfg.asins[el.spacegalleryCfg.visible] = el.spacegalleryCfg.asins[el.spacegalleryCfg.visible - 1] * 1.3;
						el.spacegalleryCfg.tops[el.spacegalleryCfg.visible] = el.spacegalleryCfg.tops[el.spacegalleryCfg.visible - 1] * 1.3;
						if (el.spacegalleryCfg.loaded == el.spacegalleryCfg.visible) {
							EYE.spacegallery.positionImages(el);
						}
						//return 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);

