/*
	VISUAL for caffemeletti.com v0.4a
	by Luca Di Bella (http://www.handsonweb.it) - MIT-style license.
	Relay on Mootools dev release 87 
	- Using Mootools V1.00 Class Asset
	- Safari Bug Corrected
	
*/

Meletti = {
	init: function(){
		this.prelaodSlimboxImages();
		this.buildAccordion();
		this.swapDepth($('header'));
		this.swapDepth($('contentpane'));
		
		this.createBgContainer();
		this.bigImage = null;
		
		// chiamo la funzione che restituisce il path dell'immagine in base all'ora del server
		new Ajax('wp-custom-functions/fotoora.php', {
			method: 'get',
			onComplete: function(req){ 
				this.loadBigImage(req);
				//console.log(req);
			}.bind(this)
		}).request();
		
		//Apro il pannello eventi con un delay di 2 sec
		(function(){ navbar.display(2)} ).delay(2000);
	},
	
	buildAccordion: function(){
		navbar = new Accordion($$('#summary .section'), $$('#summary .brief'), { display: null, alwaysHide: true });
	},
	
	swapDepth: function(el){
		el.onmouseover = function(){
			$(el).setStyles({'z-index': 10});
		};
		el.onmouseout = function(){
			$(el).setStyles({'z-index': 5});
		};
	},
	
	createBgContainer: function(){
		//se non esiste creo il container per l'immagine di sfondo
		if(!$('bgContainer')) {
			bgContainer = new Element('div');
		}
			$(bgContainer).id = 'bgContainer';
			$(bgContainer).setStyles({'position': 'fixed', 'width': '100%', 'height': '100%', 'z-index': 1});
			$(bgContainer).injectInside(document.body);

	},
	
	// carico l'immagine di sfondo se è diversa da quella presente	
	loadBigImage: function(newimage){
		if(Meletti.bigImage != newimage){
			bgEffect = new Fx.Style('bgContainer', 'opacity', {transition: Fx.Transitions.cubicOut, duration: 500}).hide();
			
			
			if(window.khtml == true){ // FIX FOR SAFARI onload EVENT
				var sImg = new Asset.image(newimage, {});
				$(sImg).onload = function(){
					var mySrc = this.src;
					$('bgContainer').setStyles({'background': 'transparent url("' + mySrc + '") no-repeat center center'});
					bgEffect.start(0,1);
					Meletti.bigImage = mySrc;
				}
			} 
			else {
				var pImg = new Asset.image(newimage, {
					onload: function(){
						//console.log('fatto');
						$('bgContainer').setStyles({'background': 'transparent url("' + this.src + '") no-repeat center center'});
						bgEffect.start(0,1);
						Meletti.bigImage = this.src;
					}
				});
			}
		}
	},
	
	prelaodSlimboxImages: function(){
			var ass = new Asset.images(['css/closelabel.gif', 'css/loading.gif', 'css/nextlabel.gif', 'css/prevlabel.gif'], {
		    onComplete: function(){
		        //console.log('all images loaded!');
				//console.dir(ass);
		    }
		});
	},
	// debugging and unused
	testVar: function(myVar){
		alert(myVar);
	},
	
	resizeBg: function(){
		$('bgContainer').setStyles({'width': '100%', 'height': '100%'});
		//console.log('resized');
	}
}
