Floor = function() {
	var oThis = this;
	
	setTimeout(function() {
		oThis.init_DOM();
	}, 1000);
}

Floor.prototype = {
	init_DOM: function() {
		this.jHotel_container = $('#hotel');
		this.jHotel = $('#hotel_hover_listener');
		this.jHotel_hover = $('#hotel_hover');
		this.jFloor = this.jHotel.find('a');
		
		this.init();
	},

	init: function() {
		var
			oThis = this,
			hotel_top = this.jHotel_container.find('.hotel_top'),
			hotel_bg = this.jHotel_container.find('.hotel'),
			img_top = document.createElement('img'),
			img_bg = document.createElement('img'),
			i = 0,
			amount = 2,
			my_reg = /url\("?([^"]+)"?\)/;
			
		function doOnload() {
			i++;
			if(i >= amount) {
				oThis.start_fading();
			}
		}
		
		img_top.onload = img_bg.onload = doOnload;

		img_top.src = hotel_top.css("background-image").match(my_reg)[1];
		img_bg.src = hotel_bg.css("background-image").match(my_reg)[1];
	},
	
	start_fading: function() {
		var oThis = this;
		this.jFloor.each(function(i) {
			new Fade($(this), oThis, i);
		});
	}
}

Fade = function(jFloor, oFloor, i) {
	this.jFloor = jFloor;
	this.oFloor = oFloor;
	this.sClass = this.jFloor.attr('class');
//	this.jFloor_inner = this.jFloor.find('span');
	this.i = i;
	this.isBack = $('body').hasClass('back') ? true : false;
	
	this.init();
}

Fade.prototype = {
	init: function() {
		var oThis = this;
			
		this.jParent = this.oFloor.jHotel_hover.find('div.' + this.sClass);
		this.jLink = this.jParent.find('a');

		if(!this.isBack) {
			setTimeout(function() {
				oThis.starting_animation();
			}, 300*oThis.i);
		}
		else {
			this.starting_animation();
		}
		
		this.attache_events();
	},
	
	starting_animation: function() {
		if(!this.jParent.hasClass('selected')) {
			this.jParent.css({opacity: 0}).addClass('selected');
			this.jLink.addClass('hover');
			this.animation(1, 0.3, true);
		}
	},
	
	attache_events: function() {
		var oThis = this;
		
		this.jFloor.hover(
			function() {
				jTweener.removeTween(oThis.jParent);
				oThis.jLink.addClass('hover');
				oThis.jParent.css({opacity: 0}).addClass('selected');
				oThis.animation(1, 0.1);
			},
			function() {
				jTweener.removeTween(oThis.jParent);
				oThis.animation(0, 0.1);
			}
		);
	},

	animation: function(iOpacity, iTime, fCallback) {
		var oThis = this;
		
		jTweener.addTween(
			oThis.jParent,
			{
				onStart: function() {
					if(oThis.jParent.hasClass('selected')) {
						jTweener.removeTween(oThis.jParent);
						return false;
					}
				},
				time: iTime,
				opacity: iOpacity,
				transition: 'linear',
				onComplete: function() {
					oThis.jParent.removeAttr('style');
					if(iOpacity === 0) {
						oThis.jParent.removeClass('selected');
						oThis.jLink.removeClass('hover');
					}
					if(fCallback) {
						setTimeout(function() {
							oThis.animation(0, 0.3);
						}, 100);
					}
				}
			}
		);
	}
}


$(function() {
	new Floor();
});