(function($) {
	
	$.widget("ui.tomshow", {
		options: {
			// Selectors
			openSelector: 'a.tomshow',
			
			// Animation
			opacity: 1,
			duration: 300
		},
		
		_create: function() {
			this.links = $(this.options.openSelector);
			this.isOpen = false;
			this.collection; // images
			this.collectionIndex;
			
			// creates Overlay
			this.overlay = $('<div id="tomshow-overlay" />');
			this.overlay.bind("click", { widget:this }, this._overlayClickHandler);
			$("body").append(this.overlay);
			
			// Background
			this.background = $('<div id="tomshow-background" />');
			this.background.bind("click", this._stopImmediatePropagation);
			$(this.overlay).append(this.background);
			
			// Content
			this.content = $('<div id="tomshow-content" />');
			this.content.bind("click", this._stopImmediatePropagation);
			$(this.background).append(this.content);
			
			// Buttons
			this.closeButton = $('<a id="tomshow-close" href="#">close</a>');
			this.leftButton = $('<a id="tomshow-left" href="#" />');
			this.rightButton = $('<a id="tomshow-right" href="#" />');
						
			this.closeButton.bind("click", { widget:this }, this._hide );
			this.leftButton.bind("click", { widget:this }, this.prev );
			this.rightButton.bind("click", { widget:this }, this.next );
			$(this.background).append(this.closeButton, this.leftButton, this.rightButton);
			
			// Adds EventListener to every Link
			this.links.bind("click", { widget:this }, this._linkClickHandler);

			$(window).bind("keyup", { widget:this }, this._keyUpHandler);
		},
		
		_linkClickHandler: function(event) {
			event.preventDefault();
			
			var widget = event.data.widget,
				href = $(this).attr("href"),
				allElements;
							
			if ( widget._isImage(href) ) {
				// load image/-s
				
				widget.content.html('<img src="' + href + '">');
				
				if ($(this).attr("rel")) {
					// properbly belongs to collection
					allElements = $('[rel="' + $(this).attr("rel") + '"]');
					widget._setCollection(allElements, this);
					
				} else {
					// doesn't belong to collection
				}
				
				widget._setArrows();
			} else {
				// load iframe
				widget.leftButton.hide();
				widget.rightButton.hide();
				
				widget.content.html('<iframe src="'+ href +'" width="100%" height="100%" name="content" scrolling="no" border="0" frameborder="0"></iframe>');
			}
			
			
			widget.show();
		},
		
		_keyUpHandler: function(event) {
			
			if (event.keyCode == 27) {
				// ESC Pressed
				event.data.widget.hide();
			}
		},
		
		_overlayClickHandler: function(event) {
			var widget = event.data.widget;
			widget.hide();
		},
		
		_setCollection: function(allElements, clickedElement) {
			this.collection = allElements;
			
			for (var i = this.collection.length - 1; i >= 0; i--) {
				if (this.collection.eq(i)[0] == clickedElement ) {
					this.collectionIndex = i;
				}
			}
		},
		
		_isImage: function(url) {
			return (/\.jpg|\.jpeg|\.gif|\.png/).test(url);
		},
		
		_scrollPos: function() {
			var pos, txt;
			
			return {
				'x': window.pageXOffset ? window.pageXOffset : document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft,
				'y': window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop
			};
		},
		
		_stopImmediatePropagation: function(event) {
			event.stopImmediatePropagation();
		},
		
		_updateImage: function() {
			var href = this.collection.eq(this.collectionIndex).attr("href");
			this.content.html('<img src="' + href + '">');
		},
		
		_iOS: function() {
			return 	(navigator.userAgent.indexOf('iPhone') != -1) ||
					(navigator.userAgent.indexOf('iPod') != -1) ||
					(navigator.userAgent.indexOf('iPad') != -1);
		},
		
		_setArrows: function() {
			this.leftButton.show();
			this.rightButton.show();
			
			if (this.collectionIndex == 0) {
				this.leftButton.hide();
			} else if (this.collectionIndex == (this.collection.length - 1)) {
				this.rightButton.hide();
			}
		},
		
		prev: function(event) {
			event.preventDefault();
			event.stopPropagation();
			event.stopImmediatePropagation();
			var widget = event.data.widget;
			
			widget.collectionIndex--;
			if ( widget.collectionIndex < 0 ) {
				widget.collectionIndex = 0;
			}
			
			widget._setArrows();
			widget._updateImage();
		},

		next: function(event) {
			event.preventDefault();
			event.stopPropagation();
			event.stopImmediatePropagation();
			var widget = event.data.widget;
			
			var lastIndex = widget.collection.length - 1;
			
			widget.collectionIndex++;
			if ( widget.collectionIndex > lastIndex ) {
				widget.collectionIndex = lastIndex;
			}
			
			widget._setArrows();
			widget._updateImage();
		},
		
		show: function() {
			this.isOpen = true;
			this.overlay.show();
			this.overlay.animate({"opacity": this.options.opacity}, this.options.duration);
			
			if (this._iOS()) {
				window.scroll( 0, 0 );
			}
		},
		
		_hide: function(event) {
			event.preventDefault();
			event.data.widget.hide();
			event.stopImmediatePropagation();
		},
		
		hide: function() {
			this.isOpen = false;
			//this.background.animate({"opacity": 0}, this.options.duration);
			this.overlay.animate({"opacity": 0}, this.options.duration, function() {
				$(this).hide();
			});
		},
		
		destroy: function() {}
	});
	
})(jQuery);

$("body").tomshow({
	openSelector: "a.open-tomshow"
});
