(function($){

	$.fn.extend({ 
 		
 		twOverlay: function(options, callback) {
			
			var loadType = "";
			var _modal = false;
			
			// define defaults
			var defaults = {
				
				overlayWidth: 600,
				overlayHeight: 400,
				startWidth: 0,
				startHeight: 0,
				openAnimation: true,
				overlayCloseLabel: 'Close window',
				modal: false,
				background_color:'#ffffff'
				
			};
			
			var options =  $.extend(defaults, options);
			
			// Iterate over the current set of matched elements
	    	return this.click(function() {
				
				var o = options;
	
				// --------------------------------------------
				
				
					var obj = $(this);
					
					var target = obj.attr("href");
					
					// define the load type
					// --------------------------------------------
					if ( obj.hasClass("ajaxload") ) loadType = "loadAjax";
					if ( obj.hasClass("inline") ) loadType = "loadInline";
					// --------------------------------------------
					// is modal?
					// --------------------------------------------
					if ( obj.hasClass("modal") ) _modal = true;
					// --------------------------------------------
					
						// preapare new required html markup
						// --------------------------------------------
						$("body").append('<div id="overlay-holder"><div id="overlay"></div><div id="overlay-wrap"><div id="overlay-heading"><a href="#close" id="close-overlay" style="display:none">'+o.overlayCloseLabel+'</a></div><div id="overlay-window" style="background-color:'+o.background_color+';"><div id="overlay-content" style="background-color:'+o.background_color+';"><div class="overlayCont"></div></div></div></div></div>');
						
						// hide close button
						// ----------------------------
						if ( o.overlayCloseLabel == "" ) $("#close-overlay").hide(); 
						// ----------------------------
						
						// calculate the margins for centered positioning
						var ovWidth = parseInt($(window).width());
						var ovHeight = parseInt($(window).height());
						
						//alert( ovWidth + " / " + ovHeight);
						
						var headingHeight = (o.overlayCloseLabel!="") ? $("#overlay-heading").css("height").split("px") : 0;
						
						var topMargin = Math.round( ( ovHeight - o.startHeight) / 2 );
						var leftMargin = Math.round( ( ovWidth - o.startWidth) / 2  );
						
						$("#overlay-holder").css({ 'display':'block', 'width': ovWidth+'px','height': ovHeight+'px' });
						$("#overlay").css({'width': ovWidth+'px','height': ovHeight+'px','background':'#000000','opacity':'0.8','-moz-opacity':'0.8','filter':'alpha(opacity=80)'}).fadeIn("slow", function() {
							
							if ( o.openAnimation )
							{
								$("#overlay-window").animate({
									width : (o.overlayWidth)+'px',
									height : (o.overlayHeight)+'px'
								});
							} 
							else
							{
								$("#overlay-window").css({'width': o.overlayWidth +'px', 'height': o.overlayHeight+'px'});
							}
							
							// new dimensions and centering
							var newTopMargin = Math.round( (ovHeight - o.overlayHeight) / 2 );
							var newLeftMargin = Math.round( ( ovWidth - o.overlayWidth) / 2 );
							
							if ( o.openAnimation )
							{
								$("#overlay-wrap").css({ 'display': 'block', 'opacity' : '0' , 'position' : 'fixed', 'top' : topMargin+'px', 'left' : leftMargin+'px', 'width' : ((o.startWidth)) + 'px', 'height' : ((o.startHeight)) + 'px' })
								.animate({ 
									opacity : 1,
									width : o.overlayWidth+'px',
									height : (o.overlayHeight + parseInt(headingHeight[0]))+'px',
									marginTop: (newTopMargin - topMargin)  + 'px',
									marginLeft: (newLeftMargin - leftMargin )  + 'px'
									}, 1000,

									function() {

										var overlayContentPadding = $(".overlayCont").css("padding-top").split("px");
										$(".overlayCont").css({ 'height' : (o.overlayHeight - overlayContentPadding[0]) +'px' });


										loadContent(loadType);
										$("#close-overlay").fadeIn();
									}
									);
							}
							else 
							{
								
								
								$("#overlay-wrap").css(
									{
										'display': 'block',
										'opacity' : '0' ,
										'position' : 'absolute',
										'top' : newTopMargin +'px',
										'left' : newLeftMargin +'px',
										'width' : ((o.overlayWidth)) + 'px',
										'height' : (o.overlayHeight + parseInt(headingHeight[0])) + 'px'
									}
								).animate({opacity:1}, function() {
									
									var overlayContentPadding = $(".overlayCont").css("padding-top").split("px");
									$(".overlayCont").css({ 'height' : (o.overlayHeight - overlayContentPadding[0]) +'px' });
									
									loadContent(loadType);
									$("#close-overlay").fadeIn();
									
								});
								
							}
							
							
						});
						// --------------------------------------------
					
					
					// insert content
					// reset window content
					function loadContent(loadType) {
						
						// load inline content
						//----------------------------------
						if ( loadType == "loadInline" )
						{
							var inputContent = $(target).clone().css({'display':'none'});
							if (navigator.appName.indexOf("Microsoft")!=-1) {

								$(".overlayCont").prepend( inputContent.show() );

							} else {

								$(".overlayCont").prepend( inputContent.fadeIn("slow", 
									function() {
										if(typeof callback == 'function') callback.call(this);
									} 
									));
								

							}
						}
						//----------------------------------
						
						// load inline content
						//----------------------------------
						if ( loadType == "loadAjax" )
						{
							
							$('.overlayCont').load(target,
								function() {
									if(typeof callback == 'function') callback.call(this);
								}
							);
						}
						//----------------------------------
						
						
						
						
						// close overlay
						//----------------------------------
						$(".close-overlay").live("click", function() { closeTwOverlay(); return false; });
						
						if( _modal==false && o.modal==false )
						{
							$("#overlay, #close-overlay").click(function() { closeTwOverlay(); return false; });
						}
						else
						{
							$("#close-overlay").click(function() { closeTwOverlay(); return false; });
						}
						//----------------------------------
						
						// open overlay links inside the open overlay
						$("#overlay-window a[rel=overlay]").live("click", function(){
							
							var target = this.href.split('#');
							replaceInnerContent(target[1]);
							
							return false;
							
						});
						
					};
					
					function closeTwOverlay() {
						
						$("#overlay-wrap").fadeOut("slow", function() {

							$("#overlay").fadeOut("slow",function() {

								$("#overlay-holder").remove();

							});

						});
					}
					
					function replaceInnerContent(target) {
						
						if (navigator.appName.indexOf("Microsoft")!=-1) {
							
							$("#overlay-content").css("display","none");
							
							replaceInnerContentAction(target);

						} else {

							$("#overlay-content").fadeOut("slow", function() {

								replaceInnerContentAction(target);

							});

						}
						
						
					}
					
					
					
					function replaceInnerContentAction(target) {
						
						$("#overlay-content").remove();
						
						$("#overlay-window").append('<div id="overlay-content"><div class="overlayCont"></div></div>');
						
						var overlayContentPadding = $(".overlayCont").css("padding-top").split("px");
						$(".overlayCont").css({ 'height' : (o.overlayHeight - overlayContentPadding[0]) +'px' });
						
						var inputContent = $('#'+target).clone();
						if (navigator.appName.indexOf("Microsoft")!=-1) {
							
							$("#overlay-content").show();
							$(".overlayCont").prepend( inputContent ).show();

						} else {

							$(".overlayCont").prepend( inputContent ).show();
							$("#overlay-content").fadeIn("slow");

						}
						
					}
					
					
					// on resize
					// ajdust overlay backround size
					// to fit the browser window
					// --------------------------------------------
					$(window).resize(function() {

						if (navigator.appName.indexOf("Microsoft")!=-1) {

							winW = document.body.offsetWidth;
							winH = document.body.offsetHeight;
						}
						else {

							var winW = window.innerWidth;
					  		var winH = window.innerHeight;
					  	}
					
						
						
						var resTopMargin = Math.round(( $(window).height() - o.overlayHeight ) / 2) ;
						if( resTopMargin < 0 ) resTopMargin = 0;
						
						var resLeftMargin = Math.round(( $(window).width() - o.overlayWidth ) / 2) ;
						if( resLeftMargin < 0 ) resLeftMargin = 0;

					  	$("#overlay").css({'height':winH,'width':winW});
						$("#overlay-wrap").css({ 'top' : resTopMargin+'px', 'left' : resLeftMargin+'px', 'margin-top' : '0', 'margin-left' : '0' });

					});
					// --------------------------------------------
					// close overlay
					// --------------------------------------------
					
					/*var closeOverlay = function(){
						$("#overlay-window").fadeOut("slow", function() {
							
							$("#overlay").fadeOut("slow", function() {

								$("#overlay").remove();
								$("#overlay-window").remove();

							});
							
						});
					};*/
					// --------------------------------------------
					
					
					
				
				return false;
			
    		});
    	}
	});
			
})(jQuery);
