/**
 * @author alexander.farkas
 */
(function($){
		$.widget('ui.imageGallery', {
	        init: function(){
	            var that = this,
				o = this.options;
				this.links = $(o.selLink, this.element[0]).attr('role', 'button');
				this.activeLink = this.links.filter('.'+o.activeClass);
				
				this.next = $(o.selNext, this.element[0]).attr('role', 'button');
				this.prev = $(o.selPrev, this.element[0]).attr('role', 'button');
				
				this.displayActiveIndex = $(o.selActiveIndex, this.element[0]);
				
				this.displayAmountIndex = $(o.selAmountIndex, this.element[0]).text(this.links.length);
				
				this.imageStage = $(o.selImageStage, this.element[0]).attr('tabIndex', '-1');
				
				if($.imgPreLoad){
					this.links.each(function(){
		                $.imgPreLoad.add($(this).attr('href'));
		            });
				}
				
				/* Bindings */
				function switchPic(e){
					that.switchImgStage.call(that, this, e);
					/*
					setTimeout(function(){
						that.imageStage[0].focus();
					}, 200);
					*/
		            return false;
		        }
				
				this.links.bind('click.imageGallery', switchPic);
				
				function nextPic(e){
					that.nextPrev.call(that, 1, e);
		            return false;
		        }
				this.next.bind('click.imageGallery', nextPic);
				
				function prevPic(e){
					that.nextPrev.call(that, -1, e);
		            return false;
		        }
				this.prev.bind('click.imageGallery', prevPic);
				this.updateDisplay();
	        },
			
	        ui: function(){
	            return {
	                instance: this,
	                options: this.options
	            };
	        },
	        propagate: function(n, e, extra){
	            var args = (extra) ?
					$.extend(this.ui(), extra) :
					this.ui();
				this.element.triggerHandler("gallery" + n, [e, args]);
	        },
			switchTrans: {
				fadeInOut: function(old, neu){
					$('<img src="'+neu.imgsrc+'" alt="" />')
						.css({opacity: 0})
						.insertBefore(old.img[0])
						.animate({opacity: 1});
					
					old.img.animate({opacity: 0}, 
						{complete: function(){
							old.img.remove();
						}
					});
				},
				defaultSwitch: function(old, neu){
					
				}
			},
			nextPrev: function(dir, e){
				var nextIndex = this.links.index(this.activeLink[0]) + dir,
					nextElm = this.links.get(nextIndex);
					if(nextElm){
						this.switchImgStage(nextElm, e);
					}
				
			},
			updateDisplay: function(){
				var activeIndex = this.links.index(this.activeLink[0]),
					o = this.options,
					activated, inactivated;
				
				if(activeIndex === 0 && !this.prev.is('.'+o.inactivePrevNextClass)){
					inactivated = this.prev.addClass(o.inactivePrevNextClass);
				} else if(activeIndex !== 0 && this.prev.is('.'+o.inactivePrevNextClass)){
					activated = this.prev.removeClass(o.inactivePrevNextClass);
				}
				if(activeIndex+1 === this.links.length && !this.next.is('.'+o.inactivePrevNextClass)){
					inactivated = this.next.addClass(o.inactivePrevNextClass);
				} else if(activeIndex+1 !== this.links.length && this.next.is('.'+o.inactivePrevNextClass)){
					activated = this.next.removeClass(o.inactivePrevNextClass);
				}
				
				this.displayActiveIndex.text(activeIndex+1);
				
				if(activated){
					this.propagate('prevnextLinkchange', null, {status: 'activated', elm: activated});
				}
				if(inactivated){
					this.propagate('prevnextLinkchange', null, {status: 'inactivated', elm: inactivated});
				}
			},
			switchImgStage: function(elm, e){
				jElm = $(elm);
				var o = this.options,
					old = {
						img: $('img', this.imageStage),
						link: this.links.filter('.'+o.activeClass).removeClass(o.activeClass)
					},
					neu = {
						imgsrc: jElm.attr('href'),
						link: jElm
					};
				this.activeLink = jElm.addClass(o.activeClass);
		        if(o.switchImg){
					if(this.switchTrans[o.switchImg]) {
						this.switchTrans[o.switchImg](old, neu);
					} else {
						$('<img src="'+neu.imgsrc+'" alt="" />').insertBefore(old.img[0]);
						old.img.remove();
					}
				} 
				this.updateDisplay();
				this.propagate('imgswitch', e, {oldImg: old, newImg: neu});
			}
	    });
	    $.ui.imageGallery.defaults = {
	        selLink: '#photoindex a',
	        selImageStage: '#gallery-stage',
			selNext: '#gallery-controls a.next',
			selPrev: '#gallery-controls a.prev',
			selActiveIndex: '#gallery-controls  span.active-img-index',
			selAmountIndex: '#gallery-controls span.img-index', 
			//selGalleryControl: '#gallery-controls',
			activeClass: 'active',
			inactivePrevNextClass: 'inactive',
			getPictureDescription: false,
			switchImg: 'fadeInOut',
			sildeshow: false
	    };
		$.ui.imageGallery.prototype._init = $.ui.imageGallery.prototype.init;
	})(jQuery);
