// JavaScript Document
var fakebox = {
    w : null,
    h : null,
    url : "about:blank",
    btn_close_url : "/images/close.gif",
    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 = (50+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 = 50+this.getscrolltop();
        retrieve_light.style.left = 0.5*((this.getwinsize()[0]) - (fakebox.w+10))+"px";
      } 
    },
    
	show: function(url, w, h, id, wintitle, callback, boxtype) {
		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 = "#fff";
		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 = "#00a8d5";
		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;
		
		this.light.style.width = (this.w+4)+"px";
		this.light.style.height = (this.h+10+34)+"px";
     
		var lightinner = "";
		lightinner += "<div style='width:"+(this.w)+"px;height:40px;background:#fff;margin:0 auto'>"
		lightinner += "<a href='javascript:void(0);' style='display:block;float:right;margin-right:3px;margin-top:5px;' onClick='fakebox.hide(\""+this.id+"\")' ><img src='"+this.btn_close_url+"' style='border:none' /></a>";
		lightinner += "</div>";
      	
      	switch(boxtype) {
			case "video":
				//Ã¨ un swf di youtube, non serve l'iframe, serve un host che accolga l'swf (post iniettato con swfobject2)
				lightinner += "<div id='youtubehost-"+this.id+"'>yt</div>";
				this.light.innerHTML = lightinner;
				this.ifr = this.light.getElementsByTagName("div")[1];
				var ytflashvars = {};
				var ytparams = {};
				var ytattributes = {};
				swfobject.embedSWF( this.url, "youtubehost-"+this.id, this.w, this.h, "8", null, ytflashvars, ytparams, ytattributes );
			break;
			
			case "message":
				//Ã¨ un messaggio, accoglie semplicemente un html che ciene postato come url
				lightinner += "<div>"+this.url+"</div>";
				this.light.innerHTML = lightinner;
				this.ifr = this.light.getElementsByTagName("div")[1];
				this.ifr.style.backgroundColor = "#f6f6f6";
				this.ifr.style.margin = "0 auto";
				this.ifr.style.width = this.w+"px";
				this.ifr.style.height = this.h+"px";
				this.ifr.style.overflow = "hidden";
			break;
			
			default :
				//Ã¨ una pagina esterna, serve un iframe che la accolga
			    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];
			    this.ifr.src = this.url;
			    this.ifr.style.width = this.w+"px";
				this.ifr.style.height = this.h+"px";
		}
		
		window.onscroll = function() {
			fakebox.setposition(this.id);
		}
      
		this.setposition();
		this.light.style.display='block';
		this.fade.style.display='block';
	},   
    
    getContent : function(id) {
		if(!id || id == "") {
	    	var id = "fakebox";
	    }
	    
	    var light_el = document.getElementById(id+"light");
	    return light_el;
	},
    
    youtube : function(url, w, h, id) {
		this.show(url, w, h, id, null, null, "video");
    },
    
    message : function(url, w, h, id, callback) {
		this.show(url, w, h, id, null, callback, "message");
	},
    
    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();
    }
    
}

