// Realizzato da Domenico M. Maresca <domenico.maresca@gmail.com>
// per Fausto Cuomo - Consorzio Commercianti Sorrentofree in July of 2007 by Jenna ‘Blueberry’ Fox <blueberry@creativepony.com>
// Requires MooTools 1.0 

var virtualBox = new Class({
 
	initialize: function(url) {
		this.url = url;
	},
 
	customize: function() {
 
		var opts = {duration:1000, transition:Fx.Transitions.Cubic.easeInOut};
		
		
 
		if($('virtua_box')) return;
		
		var boxHeight;
		var leggiComunicatoHeight=555;
		
		if(window.getHeight()<1000){
			boxHeight=window.getHeight()-125;
			leggiComunicatoHeight=boxHeight-80;
		}
		else boxHeight=640;
 
		this.box = new Element('div', {
			'id': 'virtual',
			'class': 'virtual_box',
			'styles': {
				'height': boxHeight+'px'
			}
		});
		
		this.box_shadow = new Element('div', {
			'id': 'box_shadow',
			'styles': {
				'z-index': '1001',
				'position': 'absolute',			
				'top': '-50px',
				'left': '50%',
				'overflow': 'hidden',
				'background-color': '#000',
				'width': '483px',
				'height': (boxHeight+3)+'px'
			}
		});
 
		this.overlay = new Element('div', {
			'id': 'overlay',
			'styles': {
				'z-index': '999',
				'position': 'absolute',
				'top': '0px',
				'left': '0px',
				'width': '100%',
				'height': '100%',
				'background-color':'#333333',
				'opacity': '-0.10'
			}
		});
 
		this.intestazione = new Element('div', {'class': 'intestazione-comunicato'});
		this.leggi = new Element('div', {'id': 'comunicato-contenuto','class': 'leggi-comunicato', 'styles': {'height': leggiComunicatoHeight}});
		
		//this.intestazione.appendText("chiudimi");
		//this.intestazione.onclick = this.overlay.onclick = this.close.bind(this);

 		new Element('img', {'src': 'images/layout/print.gif', 'alt': 'stampa...', 'class': 'print'}).injectInside(this.intestazione).onclick = this.stampa.bind(this);
		new Element('img', {'src': 'images/layout/close.png', 'alt': 'chiudi', 'class': 'close'}).injectInside(this.intestazione).onclick = this.overlay.onclick = this.close.bind(this);
 
		this.box_shadow.injectInside(this.overlay);
		this.intestazione.injectInside(this.box);
 		this.leggi.injectInside(this.box);
		this.box.injectInside(document.body);
		this.overlay.injectInside(document.body);

		var marginLeft = (this.box.getStyle('width').toInt()/2);
		var marginTop = (125+window.getScrollTop());
		
		this.box.setStyles({'marginLeft': - marginLeft});
		this.box_shadow.setStyles({'marginLeft': - marginLeft});
		
		this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getScrollHeight()});
 
		new Fx.Style(this.box, 'margin-top', opts).start(marginTop);
		new Fx.Style(this.box_shadow, 'margin-top', opts).start(125);
		new Fx.Style(this.overlay, 'opacity', opts).start(0.8);
 
 
		new Ajax(this.url+'&check='+Math.random(), {
			method: 'get',
			update: this.leggi
		}).request();

	},
 
	start: function() {
		this.customize();
		this.switchCSS('/css/print-style.css', '/css/print-style-comunicati.css')
	},
 
 	close: function() {
		new Fx.Style(this.overlay, 'opacity', this.opts).start(-0.10);
		
	 	this.box.remove();
	 	this.overlay.remove();
		
		this.switchCSS('/css/print-style-comunicati.css', '/css/print-style.css')
		
		return true;
	},
	
 	stampa: function() {
		self.print();
		
		return true;
	},
	
	switchCSS: function(oldfilename, newfilename){
		var targetelement="link";
		var targetattr="href";
		var allsuspects=document.getElementsByTagName(targetelement)
		for (var i=allsuspects.length; i>=0; i--){
			if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
				var newelement=this.createCSS(newfilename);
				allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
			}
		}
	},
	
	createCSS: function(filename){
		var fileref=document.createElement("link")
		fileref.setAttribute("rel", "stylesheet")
		fileref.setAttribute("type", "text/css")
		fileref.setAttribute("media", "print")
		fileref.setAttribute("href", filename)

		return fileref
	}
 
});