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

Flag.prototype = {
	init_DOM: function() {
		this.jFlag_list = $('#langs li');
		this.jFlag = this.jFlag_list.find('a, strong');

		this.init();
	},

	init: function() {
		this.jFlag.each(
			function() {
				new Animation_flag($(this));
			}
		);
	}
}

Animation_flag = function(jFlag_link) {
	this.bIs_animate = false;
	this.jFlag_link = jFlag_link;
	this.jFlag = this.jFlag_link.find('.flag');

	this.init();
}

Animation_flag.prototype = {
	FLAG_HEIGHT: 311,

	SHOTS: 9,

	iCurrent_shot: 0,

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

	attache_animation: function() {
		var oThis = this;

		this.jFlag_link.hover(
			function() {
				oThis.stop_animate();
				oThis.bIs_animate = true;
				oThis.start();
			},
			function() {
				oThis.stop();
			}
		);
	},

	start: function() {
		if(this.bIs_animate) {
			var	oThis = this;

			this.timer = setTimeout(function() {
				oThis.jFlag[0].style.backgroundPosition = ('0 ' + (-oThis.FLAG_HEIGHT * oThis.iCurrent_shot) + 'px');

				if(oThis.iCurrent_shot >= oThis.SHOTS) {
					oThis.iCurrent_shot = 0;
				}
				else {
					oThis.iCurrent_shot++;
				}
				oThis.start();
			}, 100);
		}
	},

	stop: function() {
		var oThis = this;

		this.timer_stop = setTimeout(function() {
			oThis.stop_animate();
		}, 1000);
	},

	stop_animate: function() {
		this.bIs_animate = false;
		clearTimeout(this.timer);
		clearTimeout(this.timer_stop);
    }
}

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