// Create a pop-out window.

(function($) {
	
	$.popup = function(el, options)
	{
		var popup = el;
		popup.isInitialised = false;
		popup.isShowing = false;
		popup.window = { width: 0, height: 0 };
		popup.rect = { left: 0, top: 0, width: 100, height: 100 };
		popup.toRect = { left: 0, top: 0, width: 100, height: 100 };
		popup.background = null;
		popup.content = '';
		popup.closeFn = null;

    popup.init = function(options)
		{
			popup.options = $.extend({}, $.popup.defaults, options);
      popup.data('popup', true);
			popup.updateWindowBounds();
			popup.rect.left = popup.window.width/2;
			popup.rect.top = popup.window.height/2;
			popup.toRect.left = popup.window.width/2;
			popup.toRect.top = popup.window.height/2;
			popup.createBackground();
			popup.createPopupContainer();
			popup.isInitialised = true;
    }

    popup.destroy = function()
		{
      popup.removeData('popup');
    }
		
		popup.updateWindowBounds = function()
		{
			popup.window.width = document.documentElement.clientWidth;
			popup.window.height = document.documentElement.clientHeight;
		}

		// Create new background div.
		popup.createBackground = function()
		{
			popup.background = $('<div class="PopupBackground"></div>').hide().appendTo('body').css('background-color', 'black').css('z-index', 1000).width(popup.window.width).height(popup.window.height).css('left', 0).css('top', 0);
		}

		popup.createPopupContainer = function()
		{
			// Create new content div.
			popup.container = $('<div class="PopupContainer"><div class="PopupContent"></div></div>').hide().appendTo('body').css('z-index', 1010);
			$(popup.container).css('border', 'solid 5px #CCC').css('background-color', 'white').css('padding', 0);  // 10
			$(popup.container).css('-webkit-border-radius', 5).css('-moz-border-radius', 5).css('border-radius', 5);
			$(popup.container).css('-moz-box-shadow', '0px 0px 15px #EEE').css('-webkit-box-shadow', '0px 0px 15px #EEE').css('box-shadow', '0px 0px 15px #EEE');
			$(popup.container).css('position', 'absolute').css('top', popup.rect.top ).css('left', popup.rect.left ).css('width', popup.rect.width).css('height', popup.rect.height);

			popup.closeButton = $('<div style="font-size:14px;text-align:center;margin:0;padding:0;line-height:1.2em;font-weight:bold;">X</div>').hide().appendTo('body').css('position', 'absolute').css('z-index', 1011).css('background-color', '#EEE').width(18).height(18);
			$(popup.closeButton).css('border', 'solid 3px #CCC').css('cursor', 'pointer');
			$(popup.closeButton).css('-webkit-border-radius', 15).css('-moz-border-radius', 15).css('border-radius', 15);
			$(popup.closeButton).css('-moz-box-shadow', '0px 0px 15px #EEE').css('-webkit-box-shadow', '0px 0px 15px #EEE').css('box-shadow', '0px 0px 15px #EEE');
	
			// Add close event.
			$(popup.closeButton).click(function() {
				popup.hide();
				if (popup.closeFn != null)
					(popup.closeFn)();
			});
		}

		popup.show = function(callback)
		{
			// Show at current position.
			$(popup.background).fadeTo(400, 0.7);
			$(popup.container).width(popup.rect.width).height(popup.rect.height).css('left', popup.rect.left).css('top', popup.rect.top);
			$(popup.container).show();
			popup.isShowing = true;

			var position = { left:popup.rect.left + popup.rect.width - 5, top:popup.rect.top - 10 };
			$(popup.closeButton).show().offset(position);

			// Rescale/move to next position.
			popup.move(callback);
		}
		
		popup.move = function(callback)
		{
			// Replace content.
			$(popup.container).find('.PopupContent').hide().html('<div class="PopupContent" style="position:relative">'+popup.content+'</div>').fadeIn(400);
			
			// Animate to new position.
			popup.toRect.left = (popup.window.width - popup.toRect.width) / 2;
			popup.toRect.top = (popup.window.height - popup.toRect.height) / 2;
			if (popup.toRect.left < 0) popup.toRect.left = 0;
			if (popup.toRect.top < 20) popup.toRect.top = 20;

			// Reposition close button.
			var position = { left:popup.toRect.left + popup.toRect.width - 5, top:popup.toRect.top - 10 };
			$(popup.closeButton).animate( { left:position.left, top:position.top }, 500 );

			$(popup.container).animate( { width:popup.toRect.width, height:popup.toRect.height, left:popup.toRect.left, top:popup.toRect.top }, 500, function() {
				// Update new position.
				popup.rect = popup.toRect;
				
				// Fire callback, if defined.
				if (callback)
					(callback)();
			});
		}
		
		popup.hide = function()
		{
			$(popup.container).fadeOut(400, function() { $(this).remove(); });
			$(popup.background).fadeOut(400, function() { $(this).remove(); });
			$(popup.closeButton).fadeOut(400, function() { $(this).remove(); });
		}

		// Initialize
    popup.init(options);
	}

	var methods = 
	{
		init : function(options)
		{
			this.init(options);
			return this;
		},
		show : function(callback)
		{
			if (!(this.isShowing))
				this.show(callback);
			else
				this.move(callback);
			return this;
		},
		hide : function()
		{
			this.hide();
			return this;
		},
		html : function(html)
		{
			this.content = html;
			return this;
		},
		start : function(params)
		{
			this.rect.left = params.left;
			this.rect.top  = params.top;
			this.rect.width = 0;
			this.rect.height = 0;
			return this;
		},
		size : function(params)
		{
			this.toRect.width = params.width;
			this.toRect.height = params.height;
			return this;
		},
		fromObject : function(el)
		{
			var offset = $(el).offset();
			this.rect.left = offset.left;
			this.rect.top  = offset.top;
			this.rect.width = $(el).width();
			this.rect.height = $(el).height();
			return this;
		},
		
		// Events
		onClose : function(callback)
		{
			this.closeFn = callback;
			return this;
		}		
	}

  // Popup: Default Settings
  $.popup.defaults = {
    maxWidth: 600,      //Integer: Maximum width of the popup content
    maxHeight: 400      //Integer: Maximum height of the popup content
  }
  
  // Popup: Plugin Function
  $.fn.popup = function(method) {
		if (!this.isInitialised)
			new $.popup(this, arguments);
		
		// Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
			alert('non method');
			return methods.init.apply( this, arguments );
    } else {
	      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    
  }  
})(jQuery);
