var ScreenPlayer = new Class({
	initialize: function() {
		this.index = 0;
		this.interval = 0;
		this.scenes = $$("#screen-scenes ul li");
		this.buttons = $$("#screen-buttons ul li a");

		if (this.scenes === null) {
			return;
		}

		if (this.buttons === null) {
			return;
		}

		this.scenes.each(function(scene, index, objects) {
			scene.setStyle("z-index", objects.length - index - 1);
			scene.setStyle("display", "block");
		});

		this.show(this.getIndex(this.scenes, this.buttons));

		this.buttons.each(function(button) {
			button.addEvent("click", function(event) {
				this.stop();
				this.show(event.target.href.split("#")[1].toInt() - 1);
				this.play();
			}.bind(this));
		}.bind(this));
	},

	play: function() {
		this.interval = this.animate.periodical(3000, this);
	},

	stop: function() {
		$clear(this.interval);
		this.scenes[this.index].get("tween").cancel();
	},

	show: function(at) {
		//클릭한 위치의 번호가 같을때 깥지 step를 호출하여 화면이 나오도록 구성.
		var index = 0;
		for (index = 0; index < this.scenes.length; index ++) {
			this.step();
			if (this.index === at) {
				break;
			}
		}
	},

	step: function() {
		//z-inex가 순차적으로 감소.
		this.scenes[this.index].setStyle("z-index", this.scenes[this.index].getStyle("z-index").toInt() - this.scenes.length);

		//FF BUG - firefox 에서 z-index가 음수로 가면 링크가 클릭되지 않는다.
		if (this.scenes[this.index].getStyle("z-index").toInt() < -(this.scenes.length - 1)) {
			this.scenes.each(function(scene, index, objects) {
				scene.setStyle("z-index", scene.getStyle("z-index").toInt() + objects.length);
			});
		}
//DEBUG CODE
//		$("log").innerHTML = "";
//		this.scenes.each(function(scene, index, objects) {
//			if (this.index === index) {
//				$("log").innerHTML += "<em>index:" + (index) + " " + scene.getStyle("z-index") + "</em><br>";
//			} else {
//				$("log").innerHTML += "index:" + (index) + " " + scene.getStyle("z-index") + "<br>";
//			}
//		}.bind(this));

		//IE BUG - onComplete에서 호출될때 자신의 opacity = 1 하게되면 이후에 제대로 작동하지 않는다.
		if (this.index > 0) {
			this.scenes[this.index - 1].setStyle("opacity", 1);
		} else {
			this.scenes[this.scenes.length - 1].setStyle("opacity", 1);
		}

		this.index = this.index + 1;
		this.index = this.index % this.scenes.length;

		this.buttons.each(function(button) {
			button.setStyle("color", "");
		});

		this.buttons[this.index].setStyle("color", "#077ab1");
	},

	animate: function() {
		this.scenes[this.index].set("tween", {
			duration: 500,
			transition: Fx.Transitions.Sine.easeInOutExpo,
			onComplete: function() {
				this.step();
			}.bind(this)
		});

		this.scenes[this.index].tween("opacity", 1, 0);
	},

	getIndex: function(scenes, buttons) {
		var index = parseInt(Cookie.read("screen-index"));
		if (!index) {
			index = 0;
		}
		index = index + 1;
		index = index % Math.min(scenes.length, buttons.length)
		Cookie.write("screen-index", index, {duration: 7});
		return index;
	}
})

var HorizontalRoll = function(element) {
	var empty = new Fx.Transition(function(p) {
		return p;
	});

	var play = function() {
		element.setStyle("left", element.getParent().getStyle("width"));
		element.tween("left", -(parseInt(element.getStyle("width"))));
	}

	element.set("tween", {
		duration: 20000,
		transition: empty,
		onComplete: play
	});

	play();
}

var go = function() {
	document.addEvent("mousedown", function(event) {
		if (event.rightClick != false) {
			alert("네오룩닷컴의 소스입니다. 필요하신분은 neo에게 연락주십시오!")
		}
	})

	HorizontalRoll($$("#description-roll p"));

	var screenPlayer = new ScreenPlayer();
	screenPlayer.play();
}
