(function($)
{
	var s1 = {
		play: function(e, obj) {
			$(obj).trigger('click', [obj]);
			
		},
		stop: function(e)
		{
			//var l = $(e.data.obj).queue("fx");
			$(e.data.obj).stop(1);
			$(e.data.obj).css('spacegallery', 0);
			EYE.spacegallery.positionImages(e.data.obj);
		},
		resume: function(e)
		{
			alert(e.data.obj);
			$(e.data.obj).trigger('click', [obj]);
		},
		next: function(e, obj) {
			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('a.fancy:last')
								.prependTo(el);
							EYE.spacegallery.positionImages(el);
							el.spacegalleryCfg.after.apply(el);
							setTimeout(function(){$(obj).trigger('click', [obj]);}, 2000);
						},
						step: function(now) {
							
							$('img', this)
								.each(function(nr){
									var newWidth, top, next;
									if (nr + 1 == el.spacegalleryCfg.images) {
										top = this.spacegallery.origTop + this.spacegallery.nextTop * 4 * now /100;
										newWidth = this.spacegallery.width * top / this.spacegallery.origTop;
										$(this)
											.css({
												top: top + 'px',
												opacity: 0.7 - now/100,
												marginLeft: - parseInt((newWidth + el.spacegalleryCfg.border)/2, 10) + 'px'
											})
											.attr('width', newWidth);
									} else {
										next = this.spacegallery.current - this.spacegallery.increment * now /100;
										newWidth = this.spacegallery.origWidth - (this.spacegallery.origWidth - this.spacegallery.origWidth * el.spacegalleryCfg.minScale) * next;
										$(this).css({
											top: this.spacegallery.origTop + this.spacegallery.nextTop * now /100 + 'px',
											opacity: 1 - next,
											marginLeft: - parseInt((newWidth + el.spacegalleryCfg.border)/2, 10) + 'px'
										})
										.attr('width', newWidth);
									}
								});
						}
					});
			}
			this.blur();
			return false;
		}
	};
	
	var EYE2 = window.EYE2 = {
			init: function(opt) {
				opt = $.extend({}, EYE.spacegallery.defaults, opt||{});
				return this.each(function(){
					var el = this;
					if ($(el).is('.spacegallery')) {
						var tempel = $('<a href="#" ></a>')
							.css("position", "static")
							.appendTo(this)
							.addClass(opt.loadingClass)
							.bind('click', EYE.spacegallery.next);
							//alert(el);
							$(tempel).bind('play', function(event, param1){EYE.spacegallery.play(event, param1);});
							
							/*$(tempel).bind('mouseover',{obj: el}, EYE.spacegallery.stop);
							$(tempel).bind('mouseout', {obj: tempel}, EYE.spacegallery.resume);*/
							
						el.spacegalleryCfg = opt;
						el.spacegalleryCfg.images = el.getElementsByTagName('img').length;
						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.images, 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.images)/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;
								if (imgEl.complete) {
									el.spacegalleryCfg.loaded ++;
									elImg.spacegallery.origWidth = imgEl.width;
									elImg.spacegallery.origHeight = imgEl.height
								} else {
									imgEl.onload = function() {
										el.spacegalleryCfg.loaded ++;
										elImg.spacegallery.origWidth = imgEl.width;
										elImg.spacegallery.origHeight = imgEl.height
										if (el.spacegalleryCfg.loaded == el.spacegalleryCfg.images) {
										
											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);
						}
						
						setTimeout(function(){$(tempel).trigger("play", [tempel]);}, 4000);
					}
				});
			}
	};
	$.extend(true, EYE.spacegallery, s1);
	$.fn.extend(true, $.fn, {spacegallery:EYE2.init});
})(jQuery);
/*var app = Array();
for(var i in this)
{
	app.push(i);
}
alert(app.join("\n"));*/

