window.addEvent('load', function() {
	// variabili globali
	var tabs = { 
			labels : $$('#labels ul li a'),
			buttons : $$('.arrows')
	} 
	var overflow = { 
			div : $$('.overflow'), 
			over : false
	}
	var tags = {
			ul : $$('#lastminute ul'), 
			li : new Array()
	}
	
	var id = { ul : 0, li : 0, FirsTime : true }
	

	// inizializzazione stili
	
		// creo array ul -> li
		for(k=0;k<tags.ul.length;k++) {
			tags.li[k] = tags.ul[k].getElements('li');
		}
	
		tags.li.each( function(li){ 
			li.each(function(li) {
				li.setStyles({'opacity' : '0', 'display' : 'block', 'top' : '0'})
			})
		});
	
		// eventi sul mouse over del lastminute
		overflow.div.addEvents({
			'mouseover':function(){
				overflow.over=true;
			}, 
			'mouseleave':function(){
				overflow.over=false;
			}, 
			'click' : function() {
				nav.gotolast();	
			}
		});
		
		// inizializzo i tabs
		tabs.labels.set('morph', { duration : 600 });
		tabs.labels.setStyles({'left':'-123px'});
		tabs.labels[id.ul].setStyles({'left':'-1px'});


	// classe del <li> del lastminute
	var Li = new Class ({
		// proprietà
		ul : 0,
		li : 0,
		heigth : 0,
		
//		active : false,
		
		slide : false,
		counter : 0,
		top : 0,
		
//		completed : false,
//		stopped : false,
		
		element : new Object(),
		
		// metodi
		initialize : function(ul, li) { // imposto le caratteristiche del li
			this.ul = ul;
			this.li = li;
			// creo l'oggetto mootools
			this.element = tags.li[ul][li];
			
			// check altezza
			this.getHeigth_Check();
			
			// setto il morphing
			this.element.set('morph', { duration : 200 });
			
//			this.checkActive();
			
		},
		
//		checkActive : function () {
//			if (this.ul == id.ul && this.li == id.li) { this.active = true; } else { this.active = false; }
//		},
		
		getHeigth_Check : function() {
			var LineHeigth = 17;
			lilength = this.element.get('html');
			lilength = tags.li[this.ul][this.li].get('html');
			lilength = lilength.toLowerCase();
			lilength = lilength.split('<br>');
			lilength = lilength.length;
			this.heigth = lilength * LineHeigth;
			if (this.heigth <= 120) { this.slide = false; } else { this.slide = true; }
			this.top = ((-1) * (this.heigth - 100));
		},


		alerts : function() {
			alert('altezza: ' + this.heigth + ', slide: ' + this.slide+'\n\nelement\'s HTML:\n ' + this.element.get('html')+ '\n\nSTOPPED : ' + this.stopped);
		},
		
		
		show : function() {
			this.element.morph({'opacity':'1'});
			if (this.slide) { 
					this.ATimer = this.scrolling.delay(4000, this); 
			} 
			else { 
					this.ATimer = nav.nextLi.delay(4500, nav); 
			}
		},
		

		scrolling : function() {
			
			this.ATimer = $clear(this.ATimer);
			
			var work = function() {
				if (this.counter > this.top && !overflow.over && !this.completed) {
					this.element.setStyles({'top' : this.counter-- });		
				} else if (this.counter > this.top && overflow.over && !this.completed) { 
					this.element.setStyles({'top' : this.counter}); 
				} else if (this.counter <= this.top && !overflow.over) { 
					this.completed = true; 
					this.counter = 0; 
					$clear(this.periodical);
					this.NextTimer = nav.nextLi.periodical(2500, nav);
				} 
			}

			this.periodical = work.periodical(30, this);
		},
		
		hide : function() {
			this.element.setStyles({'opacity':'0'});

		},

		riposiziona : function() {
			this.element.setStyles({'top' : '0'});
			this.counter = 0;
			this.completed = false;
			this.periodical = $clear(this.periodical);
		}

/*		resetta : function() {
			this.ul = 0;
			this.li = 0;
			this.heigth = 0;
			this.slide = false;
			this.counter = 0;
			this.top = 0;
			this.element = new Object();
		}, */
	});
	
	
	// classe di comando sulla classe LastMinute
	var nav = new Class ({
		
		// proprietà
		ul : 0,
		li : 0,
		
		x : new Object(),
		
		// metodi
		nextLi : function() {
			this.distruggiLi();
			
			// calcola il LI successivo da visualizzare
			if (tags.li[id.ul][id.li + 1] != undefined) { 
				id.li++; 
			} 
			else { 
				id.li = 0; 
				if (tags.ul[id.ul + 1] != undefined) { 
					id.ul++; id.li=0; 
					tabs.labels.each(function(uls) {uls.morph({'left':'-123px'});});
					tabs.labels[id.ul].morph({'left':'-1px'});
				} 
				else {
					id.ul = 0; id.li = 0;
					tabs.labels.each(function(uls) {uls.morph({'left':'-123px'});});
					tabs.labels[id.ul].morph({'left':'-1px'});
				} 
			} 
			
			this.ul = id.ul;
			this.li = id.li;
			
			this.creaLi();
		},

		precLi : function() {
			
			this.distruggiLi();
			
			// calcola il LI precedente da visualizzare 
			if (tags.li[id.ul][id.li - 1] != undefined) { 
				id.li--; 
			} 
			else { 
				if (tags.ul[id.ul - 1] != undefined) {
					id.ul-- ;
					id.li = tags.li[id.ul].length - 1; 
					tabs.labels.each(function(uls) {uls.morph({'left':'-123px'});});
					tabs.labels[id.ul].morph({'left':'-1px'});
				} 
				else { 
					id.ul = tags.ul.length - 1; 
					id.li = tags.li[id.ul].length - 1; 
					tabs.labels.each(function(uls) {uls.morph({'left':'-123px'});});
					tabs.labels[id.ul].morph({'left':'-1px'});
				} 
			}

			this.ul = id.ul;
			this.li = id.li;

			this.creaLi();
			
		},
		
		creaLi : function() {
			this.x = new Li(this.ul, this.li);
			if( this.ul == this.x.ul && this.li == this.x.li) { this.x.show(); } else { alert('ATTENZIONE: incongruenza!'); }
		},
		
		distruggiLi : function () {
			if (this.x != undefined) {
				this.x.hide();
				this.x.riposiziona();
				this.x.NextTimer = $clear(this.x.NextTimer);
				this.x.periodical = $clear(this.x.periodical);
				this.x.ATimer = $clear(this.x.ATimer);
				//this.x.resetta();
				//alert(this.x.counter + ' ' + this.x.completed + ' ' + this.x.PreScrollerTimer );
				this.x = null;
			}
		},
		
		cambiaUl : function (i) {
			nav.distruggiLi();

			id.ul = i;
			id.li = 0;
		
			this.ul = id.ul;
			this.li = id.li;		
			
			nav.creaLi();
		},
		
		gotolast : function() {
			var url = "";

			switch(id.ul) {
				case 0 :
					url = "http://www.lorenzihotels.com/atlantic/lastminute.php";
				break;
				
				case 1 :
					url = "http://www.lorenzihotels.com/nettuno/lastminute.php";				
				break;
				
				case 2 :
					url = "http://www.lorenzihotels.com/smeraldo/lastminute.php";				
				break;
				
				case 3 :
					url = "http://www.lorenzihotels.com/nevada/lastminute.php";				
				break;
			}
			
			document.location.href = url;
		}
		
	});


// variabile di controllo navigazione lastminute
var nav = new nav();

nav.creaLi();

	// gestione TABS
	tabs.labels.each(function(tab, i){
		tab.addEvents({
			'mouseenter':function(){
				tab.morph({'left':'-1px'});
			},
			'mouseleave':function(){
				if (i != id.ul) {
					tab.morph({'left':'-123px'});
				}
			},
			'click':function(){
				if (i != id.ul) {
					nav.cambiaUl(i);
					for(f=0; f<tabs.labels.length;f++) {
						if (f != id.ul) {tabs.labels[f].morph({'left':'-123px'});}
					}
				}
			}
		});
	});
	
	// gestione AVANTI e INDIETRO
	tabs.buttons.each(function(arrow) {
		arrow.set('morph', { duration : 500 });
		arrow.setStyles({'opacity' : '.4'});
		arrow.addEvents({
			'click' : function () {
				switch(arrow.getProperty('alt')) {
					case 'prec'	:
						nav.precLi();
					break;
					
					case 'next' :
						nav.nextLi();
					break;
				}
			},
			'mouseover' : function () {
				arrow.morph({'opacity' : '.7'});
			},
			'mouseleave' : function () {
				arrow.morph({'opacity' : '.4'});
			}
		});
	});
	

	//animazione iniziale 
	var wrap = { div : $$('#wrap-home'), lastminute : $$('#lastminute'), labels : $$('#labels') }
	
	wrap.div.set('morph', { duration : 1000 });
	wrap.lastminute.set('morph', { duration : 	1000 });
	wrap.labels.set('morph', { duration : 3000 });
	
	wrap.labels.setStyles({'opacity' : '0'});
	wrap.lastminute.setStyles({'opacity' : '0'}); 
	
	var scorrimento = function () {
		wrap.div.morph({ 'top' : '0', 'z-index' : '0' });
		
		var mostra = function() {
			wrap.lastminute.morph({'display' : 'block', 'opacity' : '1'});
			wrap.labels.morph({'display' : 'block', 'opacity' : '1'});
		}
		
		mostra.delay(500);
	}
	
	scorrimento.delay(1500);

});
