(function($){
	$.fn.thickbox = function(options) {
		
		var
		  defaults = {
		  	background: '#000',
			color: 'black',
			height: '400',
			width: '600',
			url: '',
			data: '',
			closeOnESC: 'false',
			closeOnClick: 'false'
		  },
		  settings = $.extend({}, defaults, options);
		  
		  this.each(function() {
		  	var $this = $(this);

			if($this.is('a')) {
				$this.click(function(e) {
				
				var Wheight = $(window).height();
				var Wwidth = $(window).width();
	
				var Dheight = $(document).height();
				var Dwidth = $(document).width();

					$('<div id="overlay-window" />')
					  .appendTo('body')
					  .hide()
					  .css({
					  	backgroundColor: settings.background,
						color: settings.color,
						top: '0',
						left: '0',
						right: '0',
						height: document.body.scrollHeight+"px",
						width: Dwidth+"px",
						opacity: '0.6',
						filter: 'alpha(opacity=60)',
						position: 'absolute',
						zIndex: '999999'
					  })
					  .fadeIn(350);

					$('<div id="view-window" />')
					  .appendTo('body')
					  .hide()
					  .css({
					  	backgroundColor: '#FFF',
						color: '#000',
						height: settings.height+'px',
						width: settings.width+'px',
						//position: 'relative',
						position: 'absolute',
						top: (Wheight-settings.height)/2+'px',
						left: (Wwidth-settings.width)/2+'px',
						right: (Wwidth-settings.width)/2+'px',
						bottom: (Wheight-settings.height)/2+'px',
						margin: '0 auto',
						overflow: 'auto',
						border: '6px solid #525252',
						zIndex: '999999'
					  })
					  .fadeIn(350);
					
					$.ajax({
						type: 'GET',
						url: settings.url,
						data: settings.data,
						success: function(data) { $('#view-window').html(data) }
					});

					$(document).bind('keydown', function(event){
						if(event.keyCode && (event.keyCode == 27) && settings.closeOnESC=='true'){
							$("#overlay-window").detach(); 
							$("#view-window").detach();
						}
						
					});

					if(settings.closeOnClick=='true'){
						$('#overlay-window').bind('click', function(event){
								$("#overlay-window").detach(); 
								$("#view-window").detach();
						});
					}

					return false
				});	
			}
		  });
		  // returns the jQuery object to allow for chainability.
		  return this;
	}
})(jQuery);

