// hic sunt leones
Splash = Class.create();
Splash.prototype = {
	initialize: function(){
		this.didauto = true;
		this.playing = true;
		this.interval = 6000;
		this.container = $("splashes");
		this.menu = $("splashitems");
		this.menues = $A(this.menu.getElementsByTagName("li"));
		this.pointer = $("splashpointer");
		this.splashes  = $A(this.container.getElementsByTagName("div"));
		this.number_of_splashes = this.splashes.length;
		if (this.number_of_splashes >= 5){
			this.number_of_splashes = 5;
		}
		if (this.number_of_splashes == 0) return false;
		this.current_splash = 0;
		this.previous_splash = null;
		this.hideSplash();
		this.showSplash();
		this.timer = setInterval(this.showSplash.bind(this), this.interval);
		var targetOffset = Math.floor(Element.getWidth(this.menues[0]))-10;
		var pos = Position.positionedOffset(this.menues[0]);
		pos = pos[0] + 5; //+ targetOffset - 5;
		this.myMove = new Effect.Move(this.pointer, {duration: 0.0, x: pos, y: 27, mode: 'absolute'});
		this.myScale = new Effect.Morph(this.pointer, { style: 'width:'+targetOffset+'px', duration: 0.0});
		var link = this.menues[0].getElementsByTagName("a");
		link = link[0];
		Element.setStyle(link, {color: "black"});
  	},
	showSplash: function(){
		if (this.playing){
			this.mainFade = Effect.Appear(this.splashes[this.current_splash], {duration: 0.5});
			var link = this.menues[this.current_splash].getElementsByTagName("a");
			link = link[0];
			Element.setStyle(link, {color: "black"});
			setTimeout(this.fadeSplash.bind(this), this.interval-750);
			if (this.current_splash < this.number_of_splashes-1){
				this.previous_splash = this.current_splash;
				this.current_splash = this.current_splash + 1;
			} else {
				this.current_splash = 0;
				this.previous_splash = this.number_of_splashes - 1;
			}
		}
	},
	fadeSplash: function(){
		if(this.playing){
			this.mainFade = Effect.Fade(this.splashes[this.previous_splash], {duration: 0.5});
			var link = this.menues[this.previous_splash].getElementsByTagName("a");
			link = link[0];
			Element.setStyle(link, {color: "#777777"});
			var targetOffset = Math.floor(Element.getWidth(this.menues[this.current_splash]))-10;
			var pos = Position.positionedOffset(this.menues[this.current_splash]);
			pos = pos[0] + 5; //+ targetOffset - 5;
			var currentPos = Position.page(this.pointer);
			var currentWidth = Element.getWidth(this.pointer);
			currentPos = currentPos[0];
			this.myMove = new Effect.Move(this.pointer, { duration: 1.0, x: pos, y: 27, mode: 'absolute' });
			this.myScale = new Effect.Morph(this.pointer, { style: 'width:'+targetOffset+'px', duration: 1.0});
		}
	},
	hideSplash: function(){
		Element.hide($('splashholder'));
		Element.setStyle($("splashes"), {display: "block"});
		this.splashes.each(function(splsh){
			Element.hide(splsh);
		});
	},
	manualPlay: function(id){
		this.playing = false;
		id -= 1;
		this.myMove.cancel();
		this.mainFade.cancel();
			this.mainFade = Effect.Fade(this.splashes[this.previous_splash], {duration: 0.5, to:0.0});
			var link = this.menues[this.previous_splash].getElementsByTagName("a");
			link = link[0];
			Element.setStyle(link, {color: "#777777"});
			this.previous_splash = id;
			var targetOffset = Math.floor(Element.getWidth(this.menues[id]))-10;
			var pos = Position.positionedOffset(this.menues[id]);
			pos = pos[0] + 5; // + targetOffset - 5;
			var currentPos = Position.page(this.pointer);
			currentPos = currentPos[0];
			this.myMove = new Effect.Move(this.pointer, { duration: 1.0, x: pos, y: 27, mode: 'absolute' });
			this.myScale = new Effect.Morph(this.pointer, { style: 'width:'+targetOffset+'px', duration: 1.0});
			setTimeout(this.manualNext.bind(this), 500);
		
	},
    forceInstant: function(id){
		this.playing = false;
		id -= 1;
		this.myMove.cancel();
		this.mainFade.cancel();
			this.mainFade = Effect.Fade(this.splashes[this.previous_splash], {duration: 0.5, to:0.0});
			var link = this.menues[this.previous_splash].getElementsByTagName("a");
			link = link[0];
			Element.setStyle(link, {color: "#777777"});
			this.previous_splash = id;
			var targetOffset = Math.floor(Element.getWidth(this.menues[id]))-10;
			var pos = Position.positionedOffset(this.menues[id]);
			pos = pos[0] + 5; // + targetOffset - 5;
			var currentPos = Position.page(this.pointer);
			currentPos = currentPos[0];
			this.myMove = new Effect.Move(this.pointer, { duration: 0.0, x: pos, y: 27, mode: 'absolute' });
			this.myScale = new Effect.Morph(this.pointer, { style: 'width:'+targetOffset+'px', duration: 0.0});
			setTimeout(this.manualNext.bind(this), 500);
		
	},
	manualNext: function(){
		this.mainFade = Effect.Appear(this.splashes[this.previous_splash], {duration: 0.5, to:1.0});
		var link = this.menues[this.previous_splash].getElementsByTagName("a");
		link = link[0];
		Element.setStyle(link, {color: "black"});
	}

};


		
Event.observe(window, 'load', function() {
	window.SplashPlayer = new Splash();
    var hash = document.location.hash;
    for (var i = 0; i < window.SplashPlayer.menues.length; i++){
        var ls = window.SplashPlayer.menues[i].getElementsByTagName("a");
        var href = ls[0].href;
        href = href.split('#');
        href = "#"+href[1];
        if (document.location.hash == href){
            window.SplashPlayer.forceInstant(i+1);
        }
    }
}, false);


