Back = function() {
	this.init_DOM();
}

Back.prototype = {
	init_DOM: function() {
		this.jBody = $('body').addClass('back');
		this.jContent = $('#layout');
		this.jTurn_link = $('#turn_hotel .pseudo_link');
		this.jShip_container = $('#ship_container');
		this.jShip = $('#ship');
		this.iShip_width = this.jShip[0].offsetWidth;
		this.jFlag_container = this.jShip.find('.flag');
		this.jFlag = this.jFlag_container.find('div');
		this.jHotel = $('#hotel');

		this.iFLAG_HEIGHT = this.jFlag_container[0].offsetHeight;
		this.bIs_animate = false;

		this.jShip[0].style.marginLeft = - this.jShip_container[0].offsetLeft + 'px';

		this.is_IE = $.browser.msie;

		this.bEn = this.jShip_container.hasClass('en');
		this.bDe = this.jShip_container.hasClass('de');

		this.init();
	},

	iSHOTS: 23,

	iCurrent_shot: 0,

	init: function() {
		this.attach_events();
	},

	attach_events: function() {
		var oThis = this;

		jTweener.removeTween();
		if(oThis.bEn) {
			oThis.iShip_width -= 355;
		}
		if(oThis.bDe) {
			oThis.iShip_width -= 555;
		}
		oThis.jShip[0].style.left = - oThis.iShip_width + 'px';

		oThis.bIs_animate = true;

		oThis.attach_animation();
		oThis.flag_animation();
	},

	attach_animation: function() {
		this.iShip_width = this.jShip[0].offsetWidth;

		this.iWidth = 5000;//$('body')[0].offsetWidth;

		var
			iTime = (this.iWidth + this.iShip_width * 2)/40,
			oThis = this,
			submarineTime = ($('body')[0].offsetWidth + this.iShip_width * 2)/40;

	   	jTweener.addTween(
			this.jShip,
			{
				left: this.iWidth,
				transition: 'linear',
				time: iTime,
				onComplete: function() {
					oThis.bIs_animate = false;
					oThis.jShip.removeAttr('style');
				},
				onUpdate: function() {
					if(
						!oThis.iBoot &&
						(oThis.bEn || oThis.bDe) &&
						oThis.jShip[0].offsetLeft > -400
					) {
						oThis.boot(0, 0, submarineTime);
					}
					
					if(oThis.jShip[0].offsetLeft > $('body')[0].clientWidth/2) { /* fix resize problem */
						jTweener.removeTween(oThis.jShip);
						oThis.bIs_animate = false;
						oThis.jShip.removeAttr('style');
					}
                }
			}
		);

		if(this.bEn || this.bDe) {
			this.boot(1, this.jShip[0].offsetHeight, submarineTime);
		}
	},

	boot: function(direction, iHeight, iTime) {
		this.iBoot = true;

		if(direction == 1) {
			this.jShip[0].style.height = 0;
		}

		var oThis = this;

		jTweener.addTween(
			this.jShip,
			{
				height: iHeight,
				transition: 'easeOutQuad',
				time: iTime/3,
				onComplete: function() {
					oThis.iBoot = false;
				}
			}
		);
    },

	animation_opacity: function(iStart){
		var
			oThis = this,
			iFinish = 1,
			iDelay = 0;

		if(iStart == 1) {
			iFinish = 0;
			iDelay = this.iWidth/100;
		}

		jTweener.addPercent({
			delay: iDelay,
			time: oThis.iShip_width / 50,
			transition: 'linear',
			everyFrame: function(iPercent){
				var iOpacity = iStart + (iFinish - iStart) * iPercent;
				oThis.jShip.css("opacity", Math.round(iOpacity * 100) / 100);
			}
		});
	},

	flag_animation: function() {
		if(this.bIs_animate) {
			var	oThis = this;
			this.timer = setTimeout(function() {
				oThis.jFlag[0].style.bottom = -oThis.iFLAG_HEIGHT * oThis.iCurrent_shot + 'px';

				oThis.iCurrent_shot = (oThis.iCurrent_shot >= oThis.iSHOTS) ? 0 : (oThis.iCurrent_shot+1);

				oThis.flag_animation();
			}, 100);
		}
    }
}


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

