// JavaScript Document
var fakebox = {
    w : null,
    h : null,
    url : "about:blank",
    fade : null,
    light : null,
    ifr : null,
    
    getwinsize: function() {
      var window_width, window_height;
	    if (self.innerHeight) {	// all except Explorer
		    window_width = self.innerWidth;
		    window_height = self.innerHeight;
	    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		    window_width = document.documentElement.clientWidth;
		    window_height = document.documentElement.clientHeight;
	    } else if (document.body) { // other Explorers
		    window_width = document.body.clientWidth;
		    window_height = document.body.clientHeight;
	    }	
	    return [window_width,window_height];
    },
    
    getscrolltop: function() {
      var theTop;
      
      if (document.documentElement && document.documentElement.scrollTop)
        theTop = document.documentElement.scrollTop;
      else if (document.body)
        theTop = document.body.scrollTop;

      return theTop;
    },
    
    setposition: function(id) {
      if (!id) {
        //upon creation
        this.fade.style.top = this.getscrolltop()+"px";
        this.light.style.top = this.getscrolltop()+"px";
        this.light.style.left = 0.5*((this.getwinsize()[0]) - (fakebox.w+10))+"px";
      } else {
        //upon scrolling
        var retrieve_fade = document.getElementById(id+"fade");
        var retrieve_light = document.getElementById(id+"light");
        retrieve_fade.style.top = this.getscrolltop();
        retrieve_light.style.top = this.getscrolltop();
        retrieve_light.style.left = 0.5*((this.getwinsize()[0]) - (fakebox.w+10))+"px";
      } 
    },
    
    show: function(url, w, h, id, wintitle, callback) {
      
      this.url = url || "about:blank";
      this.w = w || 400;
      this.h = h || 400;
      this.id = (id == null || id == "" ? "fakebox" : id);
      this.wintitle = wintitle || "Nuova Finestra";
      
      var fade_el = document.createElement("div");
      fade_el.setAttribute("id",this.id+"fade");
      fade_el.style.display = "none";
      fade_el.style.position = "absolute";
      fade_el.style.zIndex = "1001";
      fade_el.style.top = "0px";
      fade_el.style.left = "0px";
      fade_el.style.width = "100%";
      fade_el.style.height = "100%";
      
      if(/msie|MSIE 6/.test(navigator.userAgent)) fade_el.style.height = this.getwinsize()[1]; //ie6 hack 
      
      fade_el.style.backgroundColor = "#000";
      fade_el.style.opacity = 0.5;
      fade_el.style.filter = 'alpha(opacity=50)';
      
      var light_el = document.createElement("div");
      light_el.setAttribute("id",this.id+"light");
      light_el.style.display = "none";
      light_el.style.position = "absolute";
      light_el.style.top = "0px";
      light_el.style.left = "0px";
      light_el.style.width = "100%";
      light_el.style.height = "100%";
      light_el.style.padding = "5px";
      light_el.style.textAlign = "center";
      light_el.style.backgroundColor = "#eee";
      light_el.style.zIndex = "1002";
      light_el.style.overflow = "auto";
      
      var bdy = document.getElementsByTagName("body")[0] 
      bdy.appendChild(fade_el);
      bdy.appendChild(light_el);
      
      this.fade = document.getElementById(this.id+"fade");
      this.light = document.getElementById(this.id+'light');
      this.light.callback = callback;
      
      var lightinner = "";
      lightinner += "<div style='height:25px;background:#FDCB64 url(images/bck_fakebox_title.gif) repeat left top;'>"
      lightinner += " <div style='float:left;width:78%;height:20px;margin-left:1%;padding-top:4px;overflow:hidden;text-align:left;color:#fff;font:bold 12px Arial,sans-serif;'>"+this.wintitle+"</div>";
      lightinner += " <a style='display:block;float:right;width:18%;margin-right:1%;overflow:hidden;text-align:right;color:#fff;font:10px Arial,sans-serif;' href='javascript:void(0);' onClick='fakebox.hide(\""+this.id+"\")' style=''>chiudi</a>";
      lightinner += "</div>";
      lightinner += "<iframe src='about:blank' frameborder='0' marginheight='0' marginwidth='0' "+((/msie|MSIE 6/.test(navigator.userAgent)) ? "scrolling='yes'" : "")+" />";
      
      this.light.innerHTML = lightinner;
      
      this.ifr = this.light.getElementsByTagName("iframe")[0];
      window.onscroll = function() {
        fakebox.setposition(this.id);
      }
      this.light.style.width = (this.w+10)+"px";
      this.light.style.height = (this.h+10+25)+"px";
      this.ifr.style.width = this.w+"px";
      this.ifr.style.height = this.h+"px";
      this.ifr.src = this.url;
      this.setposition();
      this.light.style.display='block';
      this.fade.style.display='block';
    },
    
    hide: function(id) {
      if(!id || id == "") {
        var id = "fakebox";
      }
      var bdy = document.getElementsByTagName("body")[0];
      var fade_el = document.getElementById(id+"fade");
      var light_el = document.getElementById(id+"light");
      
      if(light_el.callback) {
        light_el.callback();
      }
      
      bdy.removeChild(fade_el);
      bdy.removeChild(light_el);
      //this.onHide();
    }
    
}
