/***********************************************
 * Flowstack Popup
 *
 * CREDITS:
 *
 * Based on script by Adrian "yEnS" Mato Gondelle
 * (www.yensdesign.com)
 *
 * USAGE:
 * 
 * fs-popup-contact - the popup
 * fs-popup-bg      - the popup background
 *
 * These div ids must be present in order for
 * the script to work.
 *
 * REQUIREMENTS
 *
 * jquery.dimensions plugin
 * http://plugins.jquery.com/project/dimensions
 *
***********************************************/

function FSPopup(delay) {

  x = this;

  // Normal popup
  this.status = 0;
  
  // Exit popup
  this.showOnExit = 1;
  this.hasBeenSeen = 0;
  this.pageEntryTime = new Date();
  this.timeToShow = delay;
  
  this.timer = setTimeout(function() { x.load(); }, this.timeToShow);
  
  // DOM elements
  this.bgElm = $("#fs-popup-bg");
  this.popupElm = $("#fs-popup-contact");
  this.closeElm = $("#fs-popup-close");
    
  this.closeElm.click(function(){
    x.close();
  });
    
  this.bgElm.click(function(){
	  x.close();
  });
  
  $(document).keypress(function(e){
	  if(e.keyCode==27 && popup.status==1){
		 x.close();
	  }
   });
	
}

/**
 * Show the popup
 */
FSPopup.prototype.load = function() {
  if (this.status == 0) {
    this.center();
    this.bgElm.css("opacity", "0.7");
    this.bgElm.fadeIn("slow");
    this.popupElm.fadeIn("slow");
    this.status = 1;
    this.hasBeenSeen = 1;
  }
}

/**
 * Hide the popup
 */
FSPopup.prototype.close = function() {
  if (this.status == 1) {
    this.bgElm.fadeOut("slow");
    this.popupElm.fadeOut("slow");
    this.status = 0;
  }
}

/**
 * Center popup on screen
 */
FSPopup.prototype.center = function() {
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var popupHeight = this.popupElm.height();
	var popupWidth = this.popupElm.width();
	var topOffset = $(window).scrollTop();
	
	var top = (windowHeight/2 - popupHeight/2) + topOffset;
				
	this.popupElm.css({
		"position": "absolute",
		"top": top,
		"left": windowWidth/2 - popupWidth/2
	});
	
	this.bgElm.css("height", windowHeight);
}
