var current_display = null;

function grayPopup(vis, display){
   grayOut(vis);
   displayHtml(vis, display);
}

function grayOut(vis) {
  var dark=document.getElementById('darkScreen');
  if (!dark) {
     dark = document.createElement('div');
     dark.style.position = "absolute";
     dark.style.top = "0px";
     dark.style.left = "0px";
     dark.style.overflow = "hidden";
     dark.style.display = "none";
     dark.id = "darkScreen";

     document.getElementsByTagName("body")[0].appendChild(dark);
  }

  //hideDropDowns(vis);

  if (vis) {
    setOpacity(dark, 0.7);

    dark.style.zIndex =10100;
    dark.style.backgroundColor = "#000000";
    dark.style.width = getFullWidth() + "px";
    dark.style.height = getFullHeight() + "px";
    //dark.style.display = "block";
    $('#darkScreen').fadeIn('slow');
    $('#darkScreen').click(close);
  }
  else{
     //dark.style.display = "none";
    $('#darkScreen').fadeOut('slow');
  }
}

function hideDropDowns(vis){
   var dd = document.getElementsByTagName("select");

   if(!vis){
      for(x = 0; x < dd.length; x++){
         dd[x].style.display = dd[x].oldDisplay;
      }
   }
   else{
      for(x = 0; x < dd.length; x++){
         dd[x].oldDisplay = dd[x].style.display;
         dd[x].style.display = "none";
      }
   }
}

function setOpacity(obj, opacity){
   obj.style.opacity = opacity;
   obj.style.MozOpacity = opacity;
   obj.style.filter = "alpha(opacity=" + (opacity*100) + ")";
}

function getScreenHeight(){
	if (self.innerHeight)
		return self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	else if (document.body)
		return document.body.clientHeight;
}

function getScreenWidth(){
	if (self.innerWidth)
		return self.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	else if (document.body)
		return document.body.clientWidth;
}

function getFullHeight(){
   var d = document.getElementsByTagName("body")[0];
   var h = 0;
   
   if(d.scrollHeight)
      h = d.scrollHeight;
   else
      h = d.offsetHeight;
      
   if(h < getScreenWidth())
       h = getScreenWidth();
       
   return h;
}

function getFullWidth(){
   var d = document.getElementsByTagName("body")[0];
   var w = 0;
   
   if(d.scrollWidth)
      w = d.scrollWidth;
   else
      w = d.offsetWidth;
      
   if(w < getScreenWidth())
   	  w = getScreenWidth();
   	  
   return w;
}
var keepAdjusting = false;
var adjustWait = 10;
var adjustTotalWait = 100; //10000;
var adjustCount = 0;

function adjustLocation(){
   // var display = document.getElementById("displayPopup");
   var l = ((getScreenWidth() - current_display.offsetWidth)/2) + document.body.scrollLeft;

   if(l < 0) l = 0;

   if(keepAdjusting && (adjustCount++*adjustWait)<adjustTotalWait){
	   current_display.style.left = l + "px";
	   $(current_display).vCenter();
       setTimeout("adjustLocation()",adjustWait);
   }
   else{
      keepAdjusting = false;
      adjustCount = 0;
   }
}

function displayHtml(vis, display){
   keepAdjusting = vis;

   if(vis){
   	   current_display = display;
	   display.style.zIndex = 11000;
	   //display.style.backgroundColor = "#FFFFFF";
	   //display.style.display = "block";
	   $(display).fadeIn('slow');

	   //display.innerHTML = html;
	   $('.close_modal').click(close);
	   $('.close_modal').css('cursor', 'pointer');
	   adjustLocation();
	}
	else{
	   //display.style.display = "none";
	   $(current_display).fadeOut('slow');
	   //display.innerHTML = "";
	}
}


function close(){
   grayPopup(false);
}

window.close = close;