﻿
var popup = function()
{
  this.popupInstance    = null;
  this.popupStatus      = 0;  //0 means disabled; 1 means enabled;
  this.sourceOpen       = null; 
  this.sourceClose      = null; 
  this.backgroundPopup  = null;
  this.main             = null;

  this.init = function(popupObjectInstance, sourceOpenElement, sourceCloseElement, backgroundPopupElement, mainElement)
  {
    this.popupInstance = popupObjectInstance;
    this.sourceOpen = sourceOpenElement;
    this.sourceClose = sourceCloseElement;
    this.backgroundPopup = backgroundPopupElement;
    this.main = mainElement;
  
    //Click the button event!
    $(this.sourceOpen).bind("click", {popup: this}, this.handleSourceOpenClick);
	  //Click the x event!
    $(this.sourceClose).bind("click", {popup: this}, this.handleClose);
	  //Click out event!
    $(this.backgroundPopup).bind("click", {popup: this}, this.handleClose);
	  //Press Escape event!
    $(document).bind("keypress", {popup: this}, this.handleKeypress);

    if (this.popupInstance != null && this.popupInstance.postInit)
    {
      this.popupInstance.postInit(this);
    }
  };
  
  this.handleSourceOpenClick = function(event)
  {
    var me = event.data.popup;

    if (me.popupInstance != null && me.popupInstance.handleOpen)
    {
      me.popupInstance.handleOpen(me);
    }
    else
    {
      //centering with css
      me.centerPopup();
  		
      //load popup
      me.loadPopup();      
    }
  };
  
  this.handleClose = function(event)
  {
    var me = event.data.popup;
	  me.disablePopup();
  };

  this.handleKeypress = function(event)
  {
    var me = event.data.popup;

	  if(event.keyCode == 27 && me.popupStatus == 1)
	  {
		  me.disablePopup();
	  }
  };

  this.loadPopup = function()
  {
	  //loads popup only if it is disabled
	  if(this.popupStatus==0)
	  {
		  $(this.backgroundPopup).css({
			  "opacity": "0.7"
		  });
		  $(this.backgroundPopup).fadeIn(500);
		  $(this.main).fadeIn(500);
		  
		  if (this.popupInstance != null && this.popupInstance.postLoad)
		  {
		    this.popupInstance.postLoad(this);
		  }
		  
		  this.popupStatus = 1;
	  }
  };

  this.disablePopup = function ()
  {
	  //disables popup only if it is enabled
	  if(this.popupStatus==1)
	  {
		  $(this.backgroundPopup).fadeOut(500);
		  $(this.main).fadeOut(500);
		  this.popupStatus = 0;
	  }
  };

  this.centerPopup = function ()
  {
	  //request data for centering
	  var windowWidth = document.documentElement.clientWidth;
	  var windowHeight = document.documentElement.clientHeight;
	  var popupHeight = $(this.main).height();
	  var popupWidth = $(this.main).width();
	  
	  //centering
	  $(this.main).css({
		  "position": "absolute",
		  "top": windowHeight/2-popupHeight/2,
		  "left": windowWidth/2-popupWidth/2
	  });
	  
	  //only need force for IE6
	  $(this.backgroundPopup).css({
		  "height": windowHeight
	  });
  };
  
};
