var SmallPopup = function(options) {		
    var options = typeof(options) == 'object' ?  options : {};
	this.url = window.location.href;
	this.url = this.url.replace(/^https?:\/\/[^\/]*/i, '');
	if (!this.url)
		this.url = '/';
		
    if (!jQuery('#smallPopup').length) {
      this.container = jQuery('<div id="smallPopup"></div>');
      this.close = jQuery('<div id="smallPopup-close"></div>');
      this.content = jQuery('<div class="smallPopup-content"></div>');  
      this.veil = jQuery('<div class="smallPopup-veil"></div>');
      this.veil.css('opacity', .7);
      this.container.css('opacity', 0);
	  this.content.css('opacity', 1);
     
      this.container.append(this.veil);
      this.container.append(this.content);
	  this.close.bind('click', this.hide);
	  this.close.css('opacity', 0);
      jQuery('body').append(this.close);
      jQuery('body').append(this.container);
    }
    else {
      this.container = jQuery('#smallPopup');
	  this.close = jQuery('#smallPopup-close');
	  this.veil = this.container.children('.smallPopup-veil');
	  this.content = this.container.children('.smallPopup-content');
    }
	
	this.onClose = [];
	this.keepOpen = false;
}

SmallPopup.prototype.pop = function(options) {
    var options = typeof(options) == 'object' ?  options : {};
	
	this.close.unbind('click', this.hide);
	if (options.keepOpen) 
		this.keepOpen = true;
	else {
	  this.close.bind('click', this.hide);
	  this.keepOpen = false;
	}
	
    this.onload = (options.onLoad) ? options.onLoad : function() { };
	if (options.onClose)
		this.onClose.push(options.onClose);
   
	if (options.object) { 
      this.content.empty();
      this.content.append(options.object);
	  
  	  this.url = window.location.href;
	  this.url = this.url.replace(/^https?:\/\/[^\/]*/i, '');
	  if (!this.url)
		this.url = '/';
		
      this.show();
    } else if (options.url)  {
	  this.url = options.url;
	  var opt = { url: this.url, context: this, complete: function(req) { 
	    if (req.responseText.match(/^\s*$/))
			smallPopup.hide();
		else 
			smallPopup.content.empty();
			smallPopup.container.css({'margin-left':'inherit', 'width':'auto'});
			smallPopup.content.html(req.responseText); 
			smallPopup.show();
	  }};
	  if (options.type) opt.type = options.type;
	  if (options.data) opt.data = options.data;
	  $.ajax(opt);
    } else if (options.message){  
      this.content.empty();
	  this.container.css({'margin-left':'inherit', 'width':'auto'});
      this.content.html(options.message);
	  
  	  this.url = window.location.href;
	  this.url = this.url.replace(/^https?:\/\/[^\/]*/i, '');
	  if (!this.url)
		this.url = '/';
	  
      this.show();
    }
};
 
SmallPopup.prototype.show = function() { 
	this.content.find('a, form').each(function() {
		SmallPopup.toSmallPopup(this);
	});
    this.container.css({'display':'block', 'visibility': 'hidden', 'opacity': 0});
	this.close.css('display', 'block');
	this.close.css('opacity', (this.keepOpen) ? .5 : 0 );
	
    var width = this.container.width();
    this.container.css({'margin-left':'-'+(width/2)+'px', 'visibility': 'visible', 'width': width+'px'}).animate({'opacity': 1},	this.onload);
};
 
SmallPopup.prototype.hide = function() {      
	this.keepOpen = false;
	if (jQuery('#smallPopup').length) {
		//jQuery('').animate({'opacity': 0, complete: function(e) { jQuery(e.target).css('display', 'none');}});
		jQuery('#smallPopup, #smallPopup-close').animate({'opacity': 0},  function() { jQuery(this).css('display', 'none');});
	}
	while (this.onClose.length)
		this.onClose.shift().call();
};

jQuery(document).ready(function() {
	smallPopup = new SmallPopup();
});

SmallPopup.toSmallPopup = function(item) {
	var obj = jQuery(item);
	switch(obj.attr('tagName').toLowerCase()) {
		case 'a':
			if (!obj.attr('href').match(/^#/) && obj.attr('target') != '_top')
				obj.bind('click', function(event) { 
					smallPopup.pop({url: jQuery(event.target).attr('href')}); event.preventDefault(); return false; 
				});	
			break;
		case 'form': 
			obj.find('[type^=submit]').click(function(event) {
				var me = jQuery(event.target);
				obj.append(jQuery('<input type="hidden" name="' + me.attr('name') + '" value="' + me.val() + '" />'));
			});
			
			var props = {};
			obj.bind('submit', function(event) { 
				var me = jQuery(event.target);
				props.url = me.attr('action') || smallPopup.url;
				props.type = me.attr('method') || 'post';
				props.data = me.serialize();
				if (obj.hasClass('keep-open'))
					props.keepOpen = true;
				smallPopup.pop(props); event.preventDefault(); return false; 
			});
			break;
	}
}
