/**
 * Oe24Console Object/Function
 * @author Roland Eigelsreiter
 * @example oe24Console.init(), oe24Console.init({initStory:2, autoOff:true})
 * @version 1.0
 * @since 01/2010
 * @link r.eigelsreiter@oe24.at
 */

oe24Console = {
	/**
	 * Initial function
	 * param.initStory = Initial Story -> oe24Console.init({initStory:2}) -> Starts with Story 3!!! (Begin from 0)
	 * param.autoOff = AutoJumpOff -> oe24Console.init({autoOff:true}) -> No automation
	 */

	init : function(param){
		/*Set automation speed*/
		this.speed = 6000;

		/*Init Vars*/
		this.moveBox = document.getElementById('console_tinyStorys');
		this.el_tinyStorys = this.moveBox.getElementsByTagName('div');
		this.bigStoryBox = document.getElementById('console_bigStorys');
		this.tmp_el_bigStorys = this.bigStoryBox.getElementsByTagName('div');
		this.storyCounter = document.getElementById('console_counter');
		this.tinyStorys = [];
		this.el_bigStorys = [];
		var auto = 1;

		/*Get all Big Storys*/
		for(i in this.tmp_el_bigStorys){
			if(isNaN(i)){continue;}
			if(this.tmp_el_bigStorys[i].className.match(/bigStory/)){
				this.el_bigStorys[this.el_bigStorys.length] = this.tmp_el_bigStorys[i];
			}
		}

		/*Get all Tiny and Big Storys and add onclick event*/
		for(i in this.el_tinyStorys){
			if(isNaN(i)){continue;}
			this.tinyStorys[i] = this.el_tinyStorys[i];
			this.tinyStorys[i].setAttribute("consoleParam",i);

		}

		/*Duplicate Content for seamless action*/
		var html = this.moveBox.innerHTML;
		this.moveBox.innerHTML = html+html;

		/*Add all onclick events*/
		for(i in this.moveBox.getElementsByTagName('div')){
			if(isNaN(i)){continue;}
			this.moveBox.getElementsByTagName('div')[i].onclick = function(){
				clearInterval(oe24Console.IV);
				oe24Console.story = parseInt(this.getAttribute("consoleParam"));
				oe24Console._jump();
		        if(typeof reloadOewaLog == "function"){
		            reloadOewaLog();
		        }
				return false;
			}
		}

		if(typeof param!="undefined"){
			/*If init storyjumper is Set, set runtime story var*/
			if(typeof param.initStory!="undefined"){
				this.story = param.initStory;
			}
			/*Set automation Flag*/
			if(typeof param.autoOff!="undefined"){
				var auto = (param.autoOff)?false:true;
			}
		}

		/*If nothing is set, begin with first story*/
		if(typeof initStory=="undefined"){
			this.story = 0;
		}

		if(auto){
			/*Start automation*/
			this._start();
		}else{
			/*Jump to Entry*/
			this._jump();
		}
	},

	/**
	 * Jump to a Story, only internal function
	 */
	_jump : function(){
		/*Display only needed Elements*/
		for(i in this.el_tinyStorys){
			if(isNaN(i)){continue;}
			if(i>= this.story && (i-4)<this.story){
				this.el_tinyStorys[i].style.display = 'block';
			}else{
				this.el_tinyStorys[i].style.display = 'none';
			}
		}

		/*Display the current bigStory*/
		var tmpBig = [];
		for(i in this.el_bigStorys){
			if(isNaN(i)){continue;}
			if(this.el_bigStorys[i].className.match(/bigStory/)){
				tmpBig[i] = this.el_bigStorys[i];
				tmpBig[i].style.display = 'none';
			}
		}
		tmpBig[this.story].style.display = 'block';

		/*Counter Update*/
		this.storyCounter.innerHTML = (this.story+1)+"/"+this.tinyStorys.length;
	},

	/**
	 * Start automation, only internal function
	 */
	_start : function(){
		this._jump();
		this.IV = setInterval("oe24Console.right(true)",this.speed);
	},

	/**
	 * Left Click Handler
	 */

	left : function(){
		/*On a left click*/
		clearInterval(this.IV);
		this.story--;
		if(this.story<0){
			this.story = this.tinyStorys.length-1;
		}
		this._jump();
	},

	/**
	 * Right Click Handler
	 */

	right : function(noClear){
		/*Clear Interval if userAction*/
		if(!noClear){
			clearInterval(this.IV);
		}

		/*On a right click*/
		this.story++;
		if(this.story>=this.tinyStorys.length){
			this.story = 0;
		}
		this._jump();
	}
}
