(function($)
{
	// Declare our class
	$.myLightboxClass = function ( )
	{	// This is the handler for our constructor
		//this.construct();
	};

	// Extend jQuery elements for Lightbox
	$.mylightbox = function ( options )
	{	
		opt = $.extend({
			speed:		400, 
			opacity:	0.7, 
			title: 		'', 
			showTitle:	true,
			text: 		'',
			width:		'400px',
			scroll: 	'follow'			
		}, options);
		$.myLightbox = $.myLightbox || new $.myLightboxClass();
			// Display lightbox
			//alert(this.selector);
			if ( !$.myLightbox.start(this.selector, opt) )
			{	return this;	}		
		
		return this; 
	}
	
	$.closeLightbox = function ( options )
	{
		return $.myLightbox.finish();
		
	}
	
	$.extend($.myLightboxClass.prototype,
	{	// Our LightboxClass definition
	
		speed:			400,		// Duration of effect, milliseconds
		title:			'',
		opacity:		0.7,
		title:			true,
		keys: {
			close:	'c',
			prev:	'p',
			next:	'n'
		},
		
		handlers: {
			// For custom actions
			show:	null
		},
		
		padding:		null,		// if null - autodetect
		
		scroll:         'follow', //follow
		ie6:			false,
		visible:		false,
		obgTarget:		null,
		
	construct: function ( options )
		{	// Construct our Lightbox
			if ( navigator.userAgent.indexOf('MSIE 6') >= 0 )
			{	// Is IE6
				this.ie6 = true;
			}
			else
			{	// We are not IE6
				this.ie6 = false;
			}
			$(function() {
					// DOM is ready, so fire our DOM handler
					$.myLightbox.domReady();
				});
			/*options = $.extend({
				speed: 400,
				title: ''
			}, options || {});
			
			for ( var i in this.options )
			{	// Cycle through the options
				var name = this.options[i];
				alert(name);
				if ( (typeof options[name] === 'object') && (typeof this[name] === 'object') )
				{	// We have a group like text or files
					this[name] = $.extend(true, this[name], options[name]);
				}
				else if ( typeof options[name] !== 'undefined' )
				{	// We have that option, so apply it
					this[name] = options[name];
				}
			}	delete i;
			*/
			return this;
		},
		
		domReady: function ( )
		{
			
			return true;
		},
		
		start: function (htmlText, options)
		{	
			$('#lightbox,#lightbox-overlay').remove();
			$('body').prepend('<div id="lightbox-overlay"></div><div id="lightbox"><div id="lightbox-border" '+(options.width!='400px'?'style="width:'+options.width+'"':'')+'>'+(options.showTitle?'<div id="lightbox-close">Chiudi<div id="lightbox-title">'+options.title+'</div><div class="clear"></div></div>':'')+'<div id="lightbox-content" '+(options.showTitle?'':'style="border-top: 1px solid #666666;"')+'><div id="lightbox-imageBox"><div id="lightbox-imageContainer">'+options.text+'</div></div></div></div></div>');
			
			// Hide
			$('#lightbox,#lightbox-overlay').hide();
				
			var $body = $((navigator.userAgent.indexOf('MSIE 6') >= 0 ) ? document.body : document);
			$('#lightbox-overlay').css({
				width:		$body.width(),
				height:		$body.height()
			});
			delete $body;
			this.visible = true;
			
			this.repositionBoxes({'speed':0});

			// Adjust scrolling
			if ( options.scroll === 'disable' ){
				//$(document.body).css('overflow', 'hidden');
				$(window).unbind('scroll');
			}else{
				$(window).scroll(function (){
					$.myLightbox.repositionBoxes();
				});				
			}
			
			$('embed, object, select').css('visibility', 'hidden');
			

			$('#lightbox-close').css('cursor','pointer').click(function() {
				$.myLightbox.finish();	
			});

			// Display the boxes
			$('#lightbox-overlay').css('opacity',options.opacity).fadeIn(options.speed, function(){
				// Show the lightbox
				$('#lightbox').fadeIn(200);
			});
			// All done
			return true;
		},
		
		finish: function ( )
		{			
			$('#lightbox').hide();
			$('#lightbox-overlay').fadeOut(function() { $('#lightbox-overlay').hide(); });
			
			$('embed, object, select').css({ 'visibility' : 'visible' });//.show();
			
			if ( this.scroll === 'disable' ){	
				$(document.body).css('overflow', 'visible');
			}
			$(window).unbind('scroll');
			this.visible = false;
		},
		
		/*resizeBoxes: function ( type )
		{	// Resize the boxes
			// Used on transition or window resize
			return true;
			// Resize Overlay
			if ( type !== 'transition' )
			{	// We don't care for transition
				var $body = $(this.ie6 ? document.body : document);
				$('#lightbox-overlay').css({
					width:		$body.width(),
					height:		$body.height()
				});
				delete $body;
			}
			
			// Handle cases
			switch ( type )
			{
				case 'general': // general resize (start of lightbox)
					return true;
					break;
				case 'resized': // window was resized
					if ( this.auto_resize === false )
					{	// Stop
						// Reposition
						this.repositionBoxes({'nHeight':nHeight, 'speed':options.speed});
						return true;
					}
				case 'transition': // transition between images
				default: // unknown
					break;
			}
			
			// Get image dimensions
			var iWidth  = this.lbWidth;
			var iHeight = this.lbHeight;
			
			// Get window dimensions
			var wWidth  = $(window).width();
			var wHeight = $(window).height();
			
			// Check if we are in size
			// Lightbox can take up 4/5 of size
			if ( this.auto_resize !== false )
			{	// We want to auto resize
				var maxWidth  = Math.floor(wWidth*(4/5));
				var maxHeight = Math.floor(wHeight*(4/5));
				var resizeRatio;
				while ( iWidth > maxWidth || iHeight > maxHeight )
				{	// We need to resize
					if ( iWidth > maxWidth )
					{	// Resize width, then height proportionally
						resizeRatio = maxWidth/iWidth;
						iWidth = maxWidth;
						iHeight = Math.floor(iHeight*resizeRatio);
					}
					if ( iHeight > maxHeight )
					{	// Resize height, then width proportionally
						resizeRatio = maxHeight/iHeight;
						iHeight = maxHeight;
						iWidth = Math.floor(iWidth*resizeRatio);
					}
				}
			}
			
			// Get current width and height
			var cWidth  = $('#lightbox-imageBox').width();
			var cHeight = $('#lightbox-imageBox').height();
	
			// Get the width and height of the selected image plus the padding
			// padding*2 for both sides (left+right || top+bottom)
			var nWidth	= (iWidth  + (this.padding * 2));
			var nHeight	= (iHeight + (this.padding * 2));
			
			// Diferences
			var dWidth  = cWidth  - nWidth;
			var dHeight = cHeight - nHeight;
			
			// Set the overlay buttons height and the infobox width
			// Other dimensions specified by CSS
			$('#lightbox-nav-btnPrev,#lightbox-nav-btnNext').css('height', nHeight); 
			$('#lightbox-infoBox').css('width', nWidth);

				// Animate Lightbox
				$('#lightbox-image').animate({width:iWidth, height:iHeight}, this.speed);
				$('#lightbox-imageBox').animate({width: nWidth, height: nHeight}, this.speed);

			
			// Reposition
			this.repositionBoxes({'nHeight':nHeight, 'speed':this.speed});
			
			// Done
			return true;
		},*/
		
		repositioning:			false,	// are we currently repositioning
		reposition_failsafe:	false,	// failsafe
		repositionBoxes: function ( options )
		{
			if ( this.repositioning )
			{	// Already here
				this.reposition_failsafe = true;
				return null;
			}
			this.repositioning = true;
			
			options = $.extend({}, options);
			options.callback = options.callback || null;
			options.speed = options.speed || 'slow';
			
			var pageScroll = this.getPageScroll();
			
			var nHeight = options.nHeight || parseInt($('#lightbox').height(),10);
			
			// Display lightbox in center
			// var nTop = pageScroll.yScroll + ($(document.body).height() /*frame height*/ - nHeight) / 2.5;
			var nTop = pageScroll.yScroll + ($(window).height() /*frame height*/ - nHeight) / 2.5;
			if (nTop<0) nTop=0;
			var nLeft = pageScroll.xScroll;
			
			// Animate
			var css = {
				left: nLeft,
				top: nTop
			};
			if (options.speed) {
				$('#lightbox').animate(css, 'slow', function(){
					if ( $.myLightbox.reposition_failsafe )
					{	// Fire again
						$.myLightbox.repositioning = $.myLightbox.reposition_failsafe = false;
						$.myLightbox.repositionBoxes(options);
					}
					else
					{	// Done
						$.myLightbox.repositioning = false;
						if ( options.callback )
						{	// Call the user callback
							options.callback();
						}
					}
				});
			}
			else
			{
				$('#lightbox').css(css);
				if ( this.reposition_failsafe )
				{	// Fire again
					this.repositioning = this.reposition_failsafe = false;
					this.repositionBoxes(options);
				}
				else
				{	// Done
					this.repositioning = false;
				}
			}
			
			// Done
			return true;
		},

		getPageScroll: function ( ) {
			var xScroll, yScroll;
			if (self.pageYOffset)
			{	// Some browser
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop)
			{	// Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body)
			{	// All other browsers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			var arrayPageScroll = {'xScroll':xScroll,'yScroll':yScroll};
			return arrayPageScroll;
		},
		
		pause: function ( ms ) {
			var date = new Date();
			var curDate = null;
			do { curDate = new Date(); }
			while ( curDate - date < ms);
		}
	
	}); // We have finished extending/defining our LightboxClass
	// Instantiate
	if ( typeof $.myLightbox === 'undefined' )
	{	// 
		$.myLightbox = new $.myLightboxClass();
	}
})(jQuery);
