/*----------------------------------------------------------------------------------------------------------------------------------------------	Simple Overlay - jQuery plug-in */


(function($){



$.fn.SimpleOverlay = function(options)
{
	
	function SimpleOverlay()
	{
		if(typeof SimpleOverlay._initialized == 'undefined')//If prototype has not been defined yet
		{
	
	
	
	
	
	
	/* FALLBACK ----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
	

			SimpleOverlay.prototype.attachFallback = function()//Attach fallback to document
			{
				$('<div id="sg-fallback"></div>').css({//Create element
	                position: 'absolute',
	                top: 0,
	                left: 0,
					'z-index': 999,
	                'background-color': settings.fallbackBkgColor,
	                height: $(document.body).height(),
	                width: $(document.body).width(),
	                opacity: 0
	            }).click($.proxy(function($e) {//Attach click function
	                $e.preventDefault();
	                this.remove();
	            },this)).appendTo(document.body).animate({//Attach fallback to document
	                opacity: settings.fallbackOpacity
	            }, settings.fallbackEffectDuration, settings.fallbackEffect, $.proxy(function() {
					//Attach event to listen the window resize event in order to refresh fallback
					$(window).resize($.proxy(this.refreshFallback,this));
					this.placeOverlay();
	            },this));
			};

			SimpleOverlay.prototype.refreshFallback = function()//Refresh fallback
			{
				$('#sg-fallback').css({
	                height: $(document.body).height(),
	                width: $(document.body).width()
	            });	
			};

			SimpleOverlay.prototype.removeFallback = function()//remove fallback
			{
				$(window).unbind('resize',this.refreshFallback);//Remove listener from window resize event
				$('#sg-fallback').animate({//Fade out fallback
					opacity: 0						  
				},settings.fallbackEffectDuration,function(){
					$(this).remove();//Remove fallback after it has been faded out
				});
			};
		

	/* OVERLAY ----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
	
			SimpleOverlay.prototype.placeOverlay = function()//Attach fallback to document
			{
				var pos = this.calculateOverlayPosition();
				
				//If there is a close button add a click event to it if it has not been done so
				if($(this.overlayQuery + ' ' + settings.closeBtnQuery).length > 0 && $(this.overlayQuery + ' ' + settings.closeBtnQuery).hasClass('click') == false)
				{
					$(this.overlayQuery + ' ' + settings.closeBtnQuery).click($.proxy(function($e){
						$e.preventDefault();
						this.remove();		
					},this)).addClass('click');		
				}
				
				$(this.overlayQuery).css({
					position: 'absolute',
					top: pos.top,
					left: pos.left,
					'z-index': 1000,
					opacity: 0,
					display: 'block'		
				}).animate({
					opacity: 1		
				},settings.overlayEffectDuration,settings.overlayEffect,function(){
						
				});
				$(window).resize($.proxy(this.refreshOverlay,this));
			};
			
			SimpleOverlay.prototype.refreshOverlay = function()//Attach fallback to document
			{
				var pos = this.calculateOverlayPosition();
				$(this.overlayQuery).css({
					top: pos.top,
					left: pos.left		
				});
			};
			
			SimpleOverlay.prototype.displaceOverlay = function()//Attach fallback to document
			{
				$(window).unbind('resize',this.refreshOverlay);//Remove listener from window resize event
				$(this.overlayQuery).animate({
					opacity: 0		
				},settings.overlayEffectDuration,settings.overlayEffect,function(){
					$(this).css('display','none');	
				});		
			};
	

	/* UTILITY -----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

			
			SimpleOverlay.prototype.remove = function()
			{
				this.removeFallback();
				this.displaceOverlay();
			};
			
			SimpleOverlay.prototype.calculateOverlayPosition = function()//Calculate the position of the overlay according to the dimensions of the window
			{
				var top = (settings.overlayTop) ? $(window).scrollTop() + settings.overlayTop : Math.round($(window).scrollTop() + (($(window).height() / 2) - ($(this.overlayQuery).outerHeight() / 2)));
				var left = Math.round(($(window).width() / 2) - ($(this.overlayQuery).outerWidth() / 2));
				return{top: top, left: left};//Return calcuated position
			};
			
			SimpleOverlay.prototype.init = function()
			{
				this.overlayQuery = arguments[0];
				this.attachFallback();
			};
			
		}
		SimpleOverlay._initialized = true;

		this.init(arguments[0]);//Initiate
	}







/* STATIC VARIABLES --------------------------------------------------------------------------------------------------------------------------------------------------------------*/
	
	var settings = jQuery.extend({
		fallbackBkgColor: '#1a1a1a', 
		fallbackOpacity: 0.8,
		fallbackEffectDuration: 150,
		fallbackEffect: 'linear',
		overlayTop: null,
		overlayEffectDuration: 200,
		overlayEffect: 'linear',
		closeBtnQuery: '.close'
	},options);
	
	
	
	
	
	
	return this.each(function(){
		$(this).click(function($e){
			$e.preventDefault();
			var sg = new SimpleOverlay($(this).attr('rel'));
			
		});						  
	});
	
	
};
		  
})(jQuery);
