function popup_show(id) {
  // Get Top Offset
  var browser_height = popup_calculate_windowheight();
  var current_yoffset = popup_calculate_yoffset();
	//var top_offset = (browser_height / 8) +  current_yoffset;
  var top_offset = 40 +  current_yoffset;
  var content_offset = top_offset + 11;

  // Set Height
  spjs_set_style('popup_'+id+'_border', 'top:'+top_offset+'px');
  spjs_set_style('popup_'+id+'_contentdiv', 'top:'+content_offset+'px');

  // Show Popup
  spjs_show_fadein('popup_'+id);
}

function popup_hide(id) {
  spjs_show_fadeout('popup_'+id);
}

function popup_busy(id) {
  spjs_show('popup_'+id+'_title_busytext');
  spjs_hide('popup_'+id+'_title_close');
  spjs_show('popup_'+id+'_title_busy');
}

function popup_free(id) {
  spjs_hide('popup_'+id+'_title_busytext');
  spjs_show('popup_'+id+'_title_close');
  spjs_hide('popup_'+id+'_title_busy');
}

function popup_calculate_yoffset() {
  var y_offset = 0;

	if (window.pageYOffset != null)	{
    //alert("1: "+window.pageYOffset);
		y_offset += window.pageYOffset;
	}
	else if (document.documentElement.scrollTop != null)	{
    //alert("3: "+document.documentElement.scrollTop);
		y_offset += document.documentElement.scrollTop;
	}
  else if (document.body.scrollTop != null)	{
    //alert("2: "+document.body.scrollTop);
		y_offset += document.body.scrollTop;
	}
  //else if (document.body.parentElement) {
//    alert("4: "+document.body.parentElement);
    //y_offset += document.body.parentElement.scrollTop
  //}

  return y_offset;
}

function popup_calculate_windowheight() {
  var viewportwidth = 0;
  var viewportheight = 0;
  if(typeof window.innerWidth != 'undefined') {
    viewportwidth = window.innerWidth,
    viewportheight = window.innerHeight
  }
  else if (typeof document.documentElement != 'undefined' &&
           typeof document.documentElement.clientWidth !='undefined' && 
           document.documentElement.clientWidth != 0) {
    viewportwidth = document.documentElement.clientWidth,
    viewportheight = document.documentElement.clientHeight
  }
  else {
    viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
    viewportheight = document.getElementsByTagName('body')[0].clientHeight
  }
  return viewportheight;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function popup_wrapper(id) {
  // unique name given to a popup (used to hide and show)  
  this.id = id;
  
  // onShow / onHide custom functions
  this.on_show = function() { };
  this.on_hide = function() { };
}

popup_wrapper.prototype.show = function() {
  popup_show(this.id);
  this.on_show();
}

popup_wrapper.prototype.hide = function() {
  popup_hide(this.id);
  this.on_hide();
}
