/*
	Code by: Luca Di Bella
	Company: Hands On Web di Luca Di Bella
	License: GPL
*/

/*
window.onLoad = function(){
		var fx = new Fx.Style($('preloader'), 'opacity', {
			duration: 600, 
			onComplete: function(el){
					$(el).remove();
				}
			}).start(1,0);
}.delay(500);
*/

var swapDepth = new Class({
	options: {},
	initialize: function(className){
		this.className = className;
		this.drags = $$('.'+this.className);
		
		this.drags.each(function(el, i){
			var elId = el.id;
			//console.log(elId);
			el.addEvent('mousedown', this.setToTop.pass([elId], this));
		}, this);
	},
	_getIndexes: function(){
		this.depthArray = [];
		this.drags.each(function(el){
			this.depthArray.push(Number(el.getStyle('z-index')));
		}, this);
		var sort = function(a, b){ return a - b; };
		this.depthArray.sort(sort);
		
		return this.depthArray;
	},
	_getLastIndex: function(){
		return Number(this._getIndexes().getLast());
	},
	setToTop: function(el){
		//console.log('el =' + el);
		var newDepth = Number(this._getLastIndex() + 1);
		//console.log(newDepth);
		$(el).setStyle('z-index', newDepth);
	}
})

var simplePrld = new Class({
	options: {},
	initialize: function(el){
		this.screen = $(el);
		window.addEvent('load', this.fadeScreen.delay(500, this));
	},
	fadeScreen: function(){
		new Fx.Style(this.screen, 'opacity', {
			duration: 600, 
			onComplete: function(el){
					//$$('body').setStyle('oveflow-x', 'hidden');
					$(el).remove();
				}
			}).start(1,0);
	}
});

var myMenu = new Class({
	options: {
		loader: 'loader'
	},
	initialize: function(menuClass){
		if(!menuClass){ throw('manca il parametro fondamentale')}
		this.menuClass = menuClass;
		this.menuLinkArray = $$('.' + this.menuClass + ' a');
		
		this.menuLinkArray.each(function(el){
			var closure = this;
			this.addAjaxEvent(el, closure)
		}.bind(this)); 
		
		// FX PER AJAX
		this.updaterFx = new Fx.Style('updater', 'opacity');
		this.loaderFx = new Fx.Style(this.options.loader, 'opacity').set(0);
		
		this.overFx = [];
		
		this.menuArray = $$('.'+ this.menuClass);
		this.menuArray.each(function(el, i){
			var slideFx = new Fx.Style(el, 'margin-left', {'duration': 200, 'wait': false, 'transition': Fx.Transitions.backOut});
			el.addEvent('mouseenter', function(e){
				slideFx.start(10);
			});
			
			el.addEvent('mouseleave', function(e){
				slideFx.start(0);
			});
		}, this)
	},
	
	addAjaxEvent: function(el, closure){
		el.addEvent('click', function(ev){
			// CAMBIO LO Z-INDEX DEL CONTAINER
			if($('pageWrapper').getStyle('z-index') < 8){
				$('pageWrapper').setStyle('z-index', 9);
			}
			ev = new Event(ev);
			ev.stop();
			var url = el.href;
			this.callAjax(url);
		}.bind(closure))
	},
	
	callAjax: function(url){
		this.loaderFx.set(1);
		this.updaterFx.set(0);
		var myAjax = new Ajax(url, {
			method: 'get',
			headers: {'Content-type': 'text/html charset=utf-8'},
			update: $('updater'),
			onComplete: function(){ 
				this.loaderFx.start(0);
				this.updaterFx.start(1);
			}.bind(this)
		}).request();
	}
})

var myPreload = new Class({
	options: {},
	initialize: function(screen){
		this.screen = $(screen);
		$('mainBody').addEvent('load', this.fadeScreen.delay(500, this));
	},
	fadeScreen: function(){
		var fx = new Fx.Style(this.screen, 'opacity', {
			duration: 600, 
			onComplete: function(el){
					try{
						$(el).remove();
					} catch(e){
						alert(e);
					}
				}
			}).start(1,0);
	}
})

var How = {};
How.fadeScreen = function(screen){
	var fx = new Fx.Style(screen, 'opacity', {
		duration: 600, 
		onComplete: function(el){
				try{
					$(el).remove();
				} catch(e){
					alert(e);
				}
			}
		}).start(1,0);
	return false;
}
How.buildMap = function(mapId, address, mapControl, zoomLevel){
	if($(mapId)){
		var address = address;
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById(mapId));
			if(mapControl === true){
				map.addControl(new GSmallMapControl());
			}

			var geocoder = new GClientGeocoder();
			geocoder.getLatLng(address,function(point){
					if (!point) {
			        	alert(address + " not found");
			      	} else {
						if(!zoomLevel){ zoomLevel = 14; };
			        	map.setCenter(point, zoomLevel);
						map.panTo(point);
			        	var marker = new GMarker(point);
			        	map.addOverlay(marker);
			      	}
			});
		}
	}
}
