(function($){
	var undefined;
		
	$.Overlay = function(options){
		var
			self = this,
			settings = $.extend(true, {}, this.defaults, options);
			
		/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		 * start:private
		 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
		function _init(){
			self.$element = $('<div class="overlay" />');
			self.$header = $('<div class="overlayHeader" />');
			self.$content = $('<div class="overlayContent" />');
			self.$element.append(self.$header).append(self.$content);
			self.$header.append('<a href="javascript:void();" class="close icon"><span>close</span></a>').append('<h6 />');
			
			$('body').append(self.$element);
			
			if (settings.extraClass != undefined) self.$element.addClass(settings.extraClass);
			
			self.setDimensions({width: settings.width, height: settings.height});
			self.setHeader(settings.headerCopy);
			self.setContent(settings.content);
			
			if (settings.autoCenter){
				self.center();
			}
			else {
				self.setPosition({left: settings.left, top: settings.top});
			}
			
			self.$element.find('a.close').click(function(e){
				e.preventDefault();
				self.close();
			});
			
			$(window).resize(function(){
				self.resizeBlocker();
				if (settings.autoCenter) self.center();
			});
			
			// if custom init method is provided, execute it
			if (this.init) this.init();
		};
		
		function _blockUI(){
			if (!$('#blockUI').exists()){
				var $blocker = $('<div id="blockUI" />');
				$blocker.appendTo('body');
				self.resizeBlocker();
			}
		}
		
		function _unblockUI(){
			if ($('#blockUI').exists()){
				$('#blockUI').remove();
			}
		}
		
		
		/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		 * start:public
		 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
		this.resizeBlocker = function(){
			var maxHeight = 0;
			
			var heights = {
				html: $('html').outerHeight(),
				window: $(window).height(),
				body: $('body').outerHeight(),
				container: $('#container').outerHeight()
			};

			$.each(heights, function(k, v){
				if (v > maxHeight) maxHeight = v;
			});
			
			$('#blockUI').css('height', maxHeight);
		};
		
		this.setHeader = function(html){
			this.$header.find('h6').html(html);
		};
		
		this.setContent = function(html){
			this.$content.html(html);
		};
		
		this.setPosition = function(coords){
			if (coords.left != undefined) this.$element.css('left', coords.left);
			if (coords.top != undefined) this.$element.css('top', coords.top);
		};
		
		this.center = function(){
			var win = {
				width: $(window).width(), height: $(window).height(), scrollTop: $(window).scrollTop()
			};
			
			var overlay = {
				width: self.$element.outerWidth(), height: self.$element.outerHeight()
			};
			
			var position = {
				left: (win.width/2) - (overlay.width/2),
				top: ((win.height/2) - (overlay.height/2)) + win.scrollTop
			};
			
			position.top = (position.top < 0) ? 100 : position.top;
			
			this.setPosition(position);
		};
		
		this.setDimensions = function(dimensions){
			if (dimensions.width != undefined) this.$element.css('width', dimensions.width);
			if (dimensions.height != undefined) this.$element.css('height', dimensions.height);
		};
		
		this.open = function(){
			if (settings.blockUI) _blockUI();
			self.$element.show();
		};
		
		this.close = function(){
			if (settings.blockUI) _unblockUI();
			self.$element.hide().remove();
		};
		
		
		_init();
	};
	
	$.Overlay.prototype.defaults = {
		headerCopy: 'Header Copy', content: 'Content', width: 500, height: 'auto', autoCenter: true, top: 0, left: 0, blockUI: true
	};
})(jQuery);


