	
	/**
	 * dTpopup - the jquery plugin
	 *
	 * this text should tell you how the plugin works. too bad it doesn't.
	 */
	(function( $ ){
		
		//
		// Default settings
		var defaultSettings = {
			'close' :			'.close',
			'width' :			'440px',
			'content' :			'',
			'overlayColor' :	'#3b3b3b',
			'overlayAlpha' :	0.6,
			'setup' :			null
		};
		
		//
		// Methods
		var methods = {
		
			//
			// Initialize
			init : function( options ) {
				
				var _this = (typeof(this)=='object')? this : $(window);
				
				return _this.each(function() {
				
					var $this = $(this);
					
					//
					// Apply settings
					var settings = defaultSettings;
					if ( options ) settings = $.extend( {}, defaultSettings, options );
					$this.data( 'dTpopup', settings );
					
					//
					// Apply overlay
					methods.applyOverlay.call($this);
					
					//
					// Move previous popups
					$('#dTpopup .content > div').each(function() {
							$(this).appendTo('.popups');
						});
					
					$('#dTpopup').remove();
					
					//
					// Create popup
					$('body').append(
						$('<div id="dTpopup" />').css({
							display :		'none',
							zIndex :		7000,
							position : 		'absolute',
							width : 		settings.width
						})
					);
					
					//
					// Setup popup
					$('#dTpopup')
						.append('<a href="#" class="close">x</a>')
						.append('<div class="content" />');
					
					//
					// Get content
					if( typeof(settings.content) == 'object' ) {
						$(settings.content).appendTo('#dTpopup .content');
					} else {
						$('#dTpopup .content').append(settings.content);
					}
					
					//
					// Position
					methods.position.call($this);
					$(window).resize(function() { methods.position.call($this); });
					$(window).scroll(function() { methods.position.call($this); });
					
					//
					// Apply handlers
					$('#dTpopup').find(settings.close + ', .closePopup').click(function(e) { e.preventDefault(); methods.close.call($this); });
					$('#dToverlay').click(function(e) { e.preventDefault(); methods.close.call($this); });
					
					//
					// Show popup
					$('#dTpopup').fadeIn(300);
					
					//
					// Trigger callbacks
					if( typeof(settings.setup) == 'function' ) {
						settings.setup.call(settings.content);
					}
					
					//
					// Save settings
					$this.data( 'dTpopup', settings );
								
				});
				
			},
			
			//
			// Position
			position : function() {
			
				var _this = (typeof(this)=='object')? this : $(window);
			
				return _this.each(function() {
				
					$('#dTpopup').css({
							top			: $(window).scrollTop() + Math.ceil($(window).height()/2),
							left 		: '50%',
							marginLeft 	: -1*($('#dTpopup').outerWidth()/2),
							marginTop	: -1*($('#dTpopup').height() / 2)
						});
					
				});
			
			},
			
			//
			// Apply Overlay
			applyOverlay : function() {
			
				return this.each(function() {
				
					var $this = $(this);
					var settings = $this.data('dTpopup');
				
					if( !$('#dToverlay').length ) {
						
						var ie6 = $.browser.msie && $.browser.version.substr(0,1)<7;
						var width = 	(ie6)? 	$('html').width() : 	'100%';
						var height = 	(ie6)? 	$('html').height() : 	'100%';
						var position = 	(ie6)? 	'absolute' : 			'fixed';
					
					
						$('body').append(
							$('<div id="dToverlay" />').css({
								display :	 	'none',
								zIndex :		6000,
								position :		position,
								width :			width,
								height :		height,
								top :			0,
								left :			0,
								background : 	settings.overlayColor,
								opacity :		settings.overlayAlpha
							})
						);
						
						if( ie6 ) {
						
							$('#dToverlay').append(
								$('<iframe id="dThideselect" />').css({
									display :		'block',
									zIndex :		7000,
									position :		position,
									width :			width,
									height :		height,
									top :			0,
									left :			0,
									background :	'#ffffff',
									opacity :		0,
									border :		'none'
								})
							);
						
						}
						
						$('#dToverlay').fadeIn(200);
						
					}
				
				});
			
			},
			
			//
			// Remove Overlay
			removeOverlay : function() {
			
				return this.each(function() {
					var $this = $(this);
					$('#dToverlay').fadeOut( 100, function() { $(this).remove(); } );
				});
			
			},
			
			//
			// Close Popup
			close : function() {
			
				var _this = (typeof(this)=='object')? this : $(window);
			
				return _this.each(function() {
					var $this = $(this);
					var settings = $this.data('dTpopup');
					methods.removeOverlay.call($this);
					$('#dTpopup').fadeOut( 200, function() {
							if( typeof(settings.content) == 'object' )
								$(settings.content).appendTo('.popups');
							$(this).remove();
						});
				});
			
			},
			
			//
			// Load Popup
			load : function( url, data, name, callback, width ) {
	
				if( width == undefined ) width = 440;
	
				if( !$('.popups div.' + name).length ) {
				
					$.ajax({
							type: "GET",
							data: data,
							url: url,
							success: function(popup) {
								
								$('.popups').append(
										$('<div class="'+ name + '" />')
											.append(popup)
									);
								
								var $popup = $('.popups div.' + name);
								
								$.dTpopup({
										width : width,
										content : $('.popups div.' + name)
									});
								
								if( typeof( callback ) == 'function' )
									callback.call($popup);
									
							}
						});
				
				} else {
				
					$.dTpopup({
							width : width,
							content : $('.popups div.' + name)
						});
				
				}
					
			}
					
		}
	  
	  	var plugin = function(method) {
	  	
			//
			// Method call
			if ( methods[method] ) {
				return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
			
			//
			// Default init call
			} else if ( typeof method === 'object' || ! method ) {
				return methods.init.apply( this, arguments );
			
			//
			// Method doesn't exist
			} else {
				$.error( 'Method ' +  method + ' does not exist on jQuery.dTpopup' );
			}
	  	
	  	}
	  
	  	$.dTpopup = plugin;
		$.fn.dTpopup = plugin;
	  
	})( jQuery );
	
