(function($){
	$.fn.thickbox = function(options) {
		
		var
		  defaults = {
		  	background: '#fff',
			color: 'black',
			height: '400',
			width: '600',
			method: 'post',
			url: '',
			data: '',
			view: '',
			closeOnESC: true,
			closeOnClick:true,
			loaderGif: true,
			closeHeader: true
		  },
		  settings = $.extend({}, defaults, options);
		  
		  this.each(function() {
		  	var $this = $(this);

			if($this.is('a') || $this.is('input')) {
				$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()
							  .fadeIn(350);
	
						$('<div id="view-window" />')
							  .appendTo('body')
							  .css({
								height: settings.height+'px',
								width: settings.width+'px',
								top: (((Wheight-settings.height)/2)/Wheight)*100+'%',
								left: (((Wwidth-settings.width)/2)/Wwidth)*100+'%',
								right: (((Wwidth-settings.width)/2)/Wwidth)*100+'%'
							  })
							  .hide()
							  .delay(350)
							  .fadeIn();
						
						if(settings.closeHeader)
						{
							$('<div id="view-window-header"><a href="#" class="close">close</a></div>')
								.appendTo('div#view-window');
							
							$('<div id="load-view" />')
								.appendTo('div#view-window');

							$('div#view-window-header a.close').live('click', function(event){
								$("div#overlay-window, div#view-window").fadeOut(350).detach(); 
								return false;
							});
						}
											
						if(settings.loadingGif=='true'){
							$('#view-window')
								.html('<div id="loading-gif" style="display:block;">Loading<br /><img src="./images/ajax-loader.gif" /></div>')
						}
											
						if(settings.url!='')
						{
							$.ajax({
								type: settings.method,
								url: settings.url,
								data: settings.data,
								success: function(data) { $('#load-view').html(data) }
							});
						}
						else
						{
							$('#load-view').html( $(''+settings.view+'').html() );
						}
	
						if(settings.closeOnESC)
						{
							$(document).bind('keydown', function(event){
								if(event.keyCode && event.keyCode==27)
								{
									$("div#overlay-window, div#view-window").fadeOut(350).detach(); 
									return false;
								}
								
							});
						}
	
//						if(settings.closeOnClick=='true')
//						{
//							$('div#overlay-window').not('div#overlay-window div#view-window').bind('click', function(event){
//								console.log('yes');
//								//$("div#overlay-window").fadeOut(350); 
//								return false;
//							});
//						}
						
						
					return false;
				});	
			}
		  });
		  // returns the jQuery object to allow for chainability.
		  return this;
	}
})(jQuery);

