/* 

OD Popup

2 possibilities:

- insert content in the first attribute and no title will be added
- insert a title in the first attribute and content in the second, and the title will be added in a H2 tag

*/
function OD_Popup(titleOrContent, contentOrOptions, options)
{

	var self = this;
	
	this.options = {};
	if(!contentOrOptions || typeof(contentOrOptions) == "object") {
    		this.content = titleOrContent;
			this.title = false;
    } else {
    		this.content = contentOrOptions;
			this.title = titleOrContent;
    }
	    
    if(!options && typeof(contentOrOptions) == "object") {
    		this.options = contentOrOptions;
    } else if(options) {
    		this.options = options;
    }    
	
	// backwards compatibility: show showCloseBtn used to be called hideCloseBtn
	if(this.options.hideCloseBtn == true){
		this.options.showCloseBtn = false;	
	}
	
    // Default options
    this.defaultOptions = {
    	showCloseBtn: true,
		closeBtnTxt : "close",
     	callback: false,
		animate: true,
		removeOnOverlayClick: true	
    }

    // set defaults
	this.overlay = null;
	this.popup = null;
	this.closeBtn = null;
	this.contentWrapper = null;
	this.titleWrapper = null;
	
	this.init = function()
	{
    this.options = this.augment(this.defaultOptions, this.options);
		this.ShowOverlay();
		this.ShowPopup();
	}
	
	// do not animate for the time being... buggy
	this.options.animate = false;
	
	
	this.ShowOverlay = function()
	{  	
		var self = this;
		this.overlay = document.createElement("div");
		$(this.overlay).addClass("OD_popup-overlay");		
		$(this.overlay).height($(document).height());

		$("body").append(this.overlay);
		if(!$.browser.msie) {
			$(this.overlay).hide();	    
			$(this.overlay).fadeIn(500);
		}
		
		if(this.options.removeOnOverlayClick) {
			$(this.overlay).click(function(){
				self.Remove();
			});
		}
     
     // ifnite catchescape() on keyup
     $(window).bind("keyup", self.catchEscape);
	}
    
    this.catchEscape = function(e)
    {

    		if(e.keyCode == 27) {
     		self.Remove();
     	}
    }
	
	this.onRemove = function()
	{
		return true;
	}
	this.ShowPopup = function()
	{
		var self = this;
		// Popup DIV HTML
		this.popup = document.createElement("div");
		$(this.popup).addClass("OD_popup");
    	$(this.popup).addClass("contentBlock");
		
		// Popup Title
    	if(this.title) {
			this.titleWrapper = document.createElement("h2");
			$(this.titleWrapper).addClass("OD_popup-title");
			$(this.titleWrapper).append(this.title);
			$(this.popup).append(this.titleWrapper);
		}
		
		// Popup Content
		this.contentWrapper = document.createElement("div");
		$(this.contentWrapper).addClass("OD_popup-content");
		this.contentWrapper.innerHTML = this.content;
		this.popup.appendChild(this.contentWrapper);
		
		// Popup Close Button
     	if(this.options.showCloseBtn) {
			this.closeBtn = document.createElement("a");
			$(this.closeBtn).addClass("OD_popup_closebtn");
    		$(this.closeBtn).attr("href", "#");
			$(this.closeBtn).html("<span>" + this.options.closeBtnTxt + "</span>");
			$(this.popup).append(this.closeBtn);
			$(this.closeBtn).click(function(){
				self.Remove();
				return false;
			});
     	}
     
		// Append Popup to body
		$(this.popup).hide();		
		$("body").append(this.popup);
		
		// Set Positions
		var endTop = $(document).scrollTop() + 50;
		var endLeft =  (parseInt($(document.body).width(), 10) / 2) - ($(this.popup).width() / 2);	 
    	var endHeight = $(this.popup).height();
    	var endWidth = $(this.popup).width();
    	var startWidth = 1;
    	var startHeight = 1;
    	var self = this;
     
    	// animate popup. show move animation 
		if(this.options.animate) {
			var clickArea = document.createElement("div");
			$(clickArea).addClass("OD_popup-overlay");		
			$(clickArea).height($(document).height());
			$(clickArea).css("background", "transparent");
			$("body").append(clickArea);
     		$(clickArea).one("mouseover", function(e){
				$(this).remove();
     			var startLeft = e.pageX;
        	  	var startTop = e.pageY;
     			$(self.popup).css("top", startTop + "px");
				$(self.popup).css("left", startLeft + "px");
	 			$(self.popup).width(startWidth);
				$(self.popup).height(startHeight);
				$(self.popup).animate({
					width: endWidth + "px",
					height: endHeight + "px",
					left: endLeft + "px",
					top: endTop + "px"}, 200, "swing", function(){
						self.SetHeights();
		
						if(self.options.callback) {
							self.options.callback(self.contentWrapper);
						}
					}
				);
				
			});
		} 
		// do a simple fade in if animation is false
		else {			
			$(self.popup).css("top", endTop + "px");
			$(self.popup).css("left", endLeft + "px");
	 		$(self.popup).width(endWidth);
			$(self.popup).height(endHeight);
			$(self.popup).hide();
			$(self.popup).fadeIn(500, function(){
				self.SetHeights();				
				if(self.options.callback) {
					self.options.callback(self.contentWrapper);
				}
			});
		}		
	}
	
	this.SetHeights = function() 
	{
		var endHeight = $(document).height();
		if(($(this.popup).offset().top + $(this.popup).outerHeight()) > endHeight) {
			endHeight = $(this.popup).offset().top + $(this.popup).outerHeight();
		} 
		$(this.overlay).height(endHeight);
		$(this.popup).css("height", "auto");
	}
	
	this.Remove = function()
	{
		if(this.onRemove()!== false) {
			this.RemoveOverlay();
			this.RemovePopup();
		}
	}
	
	this.RemoveOverlay = function()
	{
    	var self = this;
		$(this.overlay).remove();
     	$(window).unbind("keyup", self.catchEscape);
		this.overlay = null;
	}
	
	this.RemovePopup = function()
	{
		$(this.popup).remove();
		this.popup = null;
	}
    
    
/*
  Augment an object by replacing its key:value pairs with those
  from other object(s), and adding pairs from other object(s) that don't
  exist in you.  Key:value pairs from later objects will
  overwrite those from earlier objects.
  
  If null is given as the initial object, a new one will be created.
  
  This mutates and returns the object passed as oSelf. The other objects are not changed.
*/
	this.augment = function(oSelf, oOther) {
    	if (oSelf == null) {
    	    oSelf = {};
    	}
    	for (var i = 1; i < arguments.length; i++) {
        	var o = arguments[i];
        	if (typeof(o) != 'undefined' && o != null) {
            	for (var j in o) {
          	      oSelf[j] = o[j];
     	       }
    	    }
    	}
    	return oSelf;
	}
	this.init();
}	

