﻿// Show the white wash 
function _showWhiteWash()
{
    _showInline(document.getElementById("whitewash"));
}

// Hide the white wash
function _hideWhiteWash()
{
    _hide(document.getElementById("whitewash"));
}

// Show the popup specified by the id
function showPopup(id, forceInit)
{    
    if(!overlay.doneInit || forceInit){
      overlay.init(id);
    }    
    overlay.popup_id = id;
    toggleOverlay(true);
    _showInline(document.getElementById(id));
}

// Hide the popup with the specified id
function hidePopup(id)
{
    //_hideWhiteWash();
    toggleOverlay(false);
    _hide(document.getElementById(id));
}

// Show the popup specified by the id
function showProcessing(forceInit)
{    
    if(!overlay.doneInit || forceInit){
      setTimeout('overlay.init()', 0);
    }
    toggleOverlay(true);    
}

// Hide the popup with the specified id
function hideProcessing(id)
{    
    toggleOverlay(false);    
}
        
// Hide the object
function _hide(obj)
{
    obj.style.visibility = 'hidden';
    obj.style.display = 'none';
}

// Show the object with inline display
function _showInline(obj)
{
    obj.style.visibility = 'visible';
    obj.style.display = 'inline';
}


var overlay = {
      
      click_on_overlay_hide: false,

      show_loading_image: false,

      loading_image: "/images/art/ajax-loader-fb.gif",
      
      overlay_image: "/images/art/overlay.png",
      
      popup_id: null,
      
      doneInit: false,
      
      scroll: null,
      
      objAbsolutes: [],

      $: function(id){
            return document.getElementById(id);
      },

      init: function(popupID){
            var ol_div = document.createElement("div");

            ol_div.id = "overlay";
            ol_div.style.display = "none";
			//ol_div.style.height = ((window.innerHeight)? window.innerHeight : document.body.clientHeight) + "px";
			//ol_div.style.width = ((window.innerWidth)? window.innerWidth : document.body.clientWidth) + "px";
			ol_div.style.width = (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth) + "px";
			ol_div.style.height = (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight) + "px";
            ol_div.onclick = (this.click_on_overlay_hide)? overlay.hide : null;

            if(popupID && this.$(popupID)){              
              this.popup_id = popupID;              
            }
            else{            
                  var l_img = document.createElement("img");

                  l_img.src = this.loading_image;
                  l_img.style.position = "absolute";
                  l_img.style.top = ((((window.innerHeight)? window.innerHeight : document.body.clientHeight) - l_img.height) / 2) + "px";
                  l_img.style.left = ((((window.innerWidth)? window.innerWidth : document.body.clientWidth) - l_img.width) / 2) + "px";

                  ol_div.appendChild(l_img);
            }            

            document.body.appendChild(ol_div);
            this.doneInit = true;
      },

      show: function(){
            if(this.$("overlay")){
				  //this.$("overlay").style.top = (document.all) ? document.body.scrollTop : window.pageYOffset;'
				  var pyo = window.pageYOffset ? window.pageYOffset : 0;
				  var dst = document.documentElement ? document.documentElement.scrollTop : 0;
				  var bst = document.body ? document.body.scrollTop : 0;
				  
				  var scrollYPos = f_filterResults(pyo, dst, bst); // + 'px';
				  scrollYPos = parseInt(scrollYPos);
				  
				  var d = document.documentElement;
	              var b = document.body;
	              var who = d.offsetHeight? d: b;
				  var browserHeight = Math.max(document.documentElement.clientHeight, Math.max(who.scrollHeight, who.offsetHeight));
					  				                                      
                  if(typeof document.body.style.maxHeight != "undefined") {
                      // IE 7, mozilla, safari, opera 9                      
                      //this.$("overlay").style.top = scrollYPos + 'px';
                      this.$("overlay").style.top = '0px';
                      this.$("overlay").style.height = browserHeight + 'px';
                      this.$("overlay").style.display = "";
                        
                      this.scroll = document.getElementsByTagName("body")[0].style.overflow;                                    
                      document.getElementsByTagName("body")[0].style.overflow = "hidden";
                  }
                  else{
                        // IE6, older browsers
                        //this.scroll = document.body.style.overflow;
                        //document.documentElement.style.overflow = "hidden";  
                        
                        this.$("overlay").style.top = '0px';
				        var h = this.$("overlay").style.height;				  
				        h = parseInt(h);
				        h = isNaN(h)?0:h;				  
				        this.$("overlay").style.height = (h + scrollYPos) + 'px';
                        this.$("overlay").style.display = "";
                        
                        //
                        this.objAbsolutes = [];
                        var selects = document.documentElement.getElementsByTagName('select');
                        for(var i=0; i<selects.length; i++){
                          if(!selects[i].disabled){
                            selects[i].disabled = true;
                            this.objAbsolutes.push(selects[i]);
                          }
                        }
                  }
                  
				  if(this.$(this.popup_id)){
                    this.$(this.popup_id).style.display = "block";
                    this.$(this.popup_id).style.visibility = "visible";
                  }
            }
      },

      hide: function(){
            if(overlay.$("overlay")){
                  if(this.$(this.popup_id)){
                    this.$(this.popup_id).style.display = "none";
                    this.$(this.popup_id).style.visibility = "hidden";
                  }
                  overlay.$("overlay").style.display = "none";                  
                  
                  if(typeof document.body.style.maxHeight != "undefined") {
                        // IE 7, mozilla, safari, opera 9
                        //document.getElementsByTagName("body")[0].style.overflow = "scroll";
				        document.getElementsByTagName("body")[0].style.overflow = this.scroll;				                          
                  }
                  else{
                        // IE6, older browsers                        
                        //document.documentElement.style.overflow = this.scroll;
                        for(var i=0; i<this.objAbsolutes.length; i++){
                          this.objAbsolutes[i].disabled = false;                          
                        }
                  }                  
				  
            }
      }

}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	if (n_body && (!n_result || (n_result > n_body)))
	    n_result = n_body;	
	return n_result;
}


function toggleOverlay(flag){  
  if(flag){    
    setTimeout('overlay.show()', 0);    
  }
  else{
    setTimeout('overlay.hide()', 100);
  }
}

//preload image
var imgOvLoading = new Image();
imgOvLoading.src = overlay.loading_image;
var imgOverlay = new Image();
imgOverlay.src = overlay.overlay_image;

