var modul = {
	init: function(){
		// Sendungen Ticker
		this.initBroadcastTicker();
		// Schlagzeilen Ticker
//		this.initBannerHeadlinesTicker();
	},
	// ################################################
	// IMAGE TICKER
	ticker: {
		view: null,
		entriesView: null,
		entries: null,
		entryWidth: 0,
		left:0,
		ticker:true,
		tickerEvent:null,
		tickerSeconds:5,
		carrousel:true,
		carrouselEvent:null,
		naviTag:"button"
	},
	initBroadcastTicker: function(){
		this.ticker.view = $("ticker").down(".box_left").down(".view");
		this.ticker.entriesView = this.ticker.view.down("div.entries");
		this.ticker.entries = this.ticker.view.select("div.entry");
		
		// karussell
		if(this.ticker.carrousel){
			var tmp = null;
			this.ticker.entriesView.insert(
				tmp = new Element("div",{className:"entry"}).insert(
					this.ticker.entries[0].innerHTML
				)
			);
		}
		// float clearer
		this.ticker.entriesView.insert(
			new Element("div",{className:"clearer"})
		);
		this.setViewWidth();
		
		// direction buttons
		var btns = this.ticker.view.down("div.navi").select(this.ticker.naviTag);
		if(this.ticker.entries.size()>1){			
			// navi buttons
			this.ticker.view.down("div.navi").select(this.ticker.naviTag).each(function(e,i){				
				e.observe("click",function(){
					var left = (this.ticker.entryWidth*i)*-1;
					this.ticker.left = left;
					this.scroll(e);
				}.bind(this));
			}.bind(this));
			
			// mouseover view
			this.ticker.view.observe("mouseenter",function(){
				this.handleTicker("stop");
			}.bind(this)).observe("mouseleave",function(){
				this.handleTicker("start");
			}.bind(this));
			// ticker handler
			this.handleTicker("start");
		} else {
			btns.invoke("hide");
		}
	},
	handleTicker: function(event){
		if(!this.ticker.ticker)
			return false;
		
		if(event=="start"){
			this.ticker.tickerEvent = window.setInterval(this.scroll.bind(this,"next"),this.ticker.tickerSeconds*1000);
		} else if(event=="reset"){
			this.handleTicker("stop");
			this.handleTicker("start");
		} else if(event=="stop"){
			window.clearInterval(this.ticker.tickerEvent);
			this.ticker.tickerEvent = null;
		}
	},
	// view ticker width setter
	setViewWidth: function(){
		this.ticker.entryWidth = this.ticker.entries[0].getWidth();
		if(this.ticker.entryWidth==0){
			this.setViewWidth();
			return false;
		}
		if(this.ticker.carrousel){
			var size = this.ticker.view.select("div.entry").size();
		} else {
			var size = this.ticker.entries.size();
		}
		var width = size*this.ticker.entryWidth;		
		this.ticker.entriesView.setStyle({width:width+"px"});
	},
	// scroll handler
	scroll: function(direction){
		this.handleTicker("reset");
		if(!Object.isElement(direction)){
			var	left = direction=="next"?this.ticker.left-this.ticker.entryWidth:this.ticker.left+this.ticker.entryWidth;
			var type = "";
			
			if(this.ticker.carrousel){
				var width = this.ticker.entriesView.getWidth()-this.ticker.entryWidth;
			} else {
				var width = this.ticker.entriesView.getWidth();
			}
			
			if(left==(width*-1)){
				left = 0;
				type = "start";
			} else if(left>0){
				left = (width-this.ticker.entryWidth)*-1;
				type = "end";
			}
			this.ticker.left = left;
		} else {
			var	left = this.ticker.left;
		}
		// karussell
		if(this.ticker.carrousel){
			if(type=="start"){
				left = (this.ticker.entriesView.getWidth()-this.ticker.entryWidth)*-1;
				this.ticker.left;
				this.handleCarrousel("start",type);
			}
			
			this.ticker.entriesView.morph({left:left+"px"});
			this.handleController(this.ticker,direction,type);
		} else {
			this.ticker.entriesView.morph({left:left+"px"});
			this.handleController(this.ticker,direction,type);
		}		
	},
	handleCarrousel: function(event,type){
		if(event=="start"){
			this.ticker.carrouselEvent = window.setInterval(this.handleCarrousel.bind(this,"check",type),(this.ticker.tickerSeconds*1000)/2);
		} else if(event=="stop"){
			window.clearInterval(this.ticker.carrouselEvent);
			this.ticker.carrouselEvent = null;
		} else if(event=="check"){
				this.handleCarrousel("stop");
				this.ticker.entriesView.setStyle({left:"0px"});
		}
	},
	handleController: function(ticker,direction,type){
		var entries = ticker.view.down("div.navi").select(".btn");
		if(!Object.isElement(direction)){
			var active = ticker.view.down("div.navi").down(".active");
				
			if(!Object.isUndefined(type) && type != ""){
				entries.invoke("removeClassName","active");
				if(type=="start"){
					entries[0].addClassName("active");
				} else {
					entries[entries.size()-1].addClassName("active");
				}
			} else if(direction=="next"){
				active.removeClassName("active");
				active.next(ticker.naviTag).addClassName("active");
			} else {
				active.removeClassName("active");
				active.previous(ticker.naviTag).addClassName("active");
			}
		} else {
			entries.invoke("removeClassName","active");
			direction.addClassName("active");
		}
	},
	// ###############################################
	// banner headlines
	bHeadlines: {
		view: null,
		entries: null,
		ticker: true,
		tickerEvent:null,
		tickerSeconds:5,
		naviTag:"button"
	},
	initBannerHeadlinesTicker: function(){
		this.bHeadlines.body = $("ticker").down(".box_right").down(".body");
		this.bHeadlines.loader = this.bHeadlines.body.down(".loader");
		this.bHeadlines.view = this.bHeadlines.body.down(".view");
		this.bHeadlines.entries = this.bHeadlines.view.select("div.text");
		this.bHeadlines.rButtons = this.bHeadlines.body.select(".sender_selection a");
		// loader
		this.bHeadlines.loader.hide();
		// navi 
		this.initNavi();
		// mouseover view
		this.bHeadlines.view.observe("mouseenter",function(){
			this.handleTicker2("stop");
		}.bind(this)).observe("mouseleave",function(){
			this.handleTicker2("start");
		}.bind(this));
		// region buttons
		this.bHeadlines.rButtons.each(function(e){
			e.observe("click",function(){
				if(e.hasClassName("choosed"))
					return false;
				
				this.bHeadlines.rButtons.invoke("removeClassName","choosed");
				
				this.handleTicker2("stop");
				this.bHeadlines.view.update("");
				this.bHeadlines.loader.show();
				// new region selected
				var region = e.readAttribute("region");
				main.ajax({
					path:"home.ajax",
					parameters: {action:"banner_headlines",region:region},
					onSuccess: function(json){
						// replace new banner headlines
						this.bHeadlines.view.update(json.html);
						this.bHeadlines.entries = this.bHeadlines.view.select("div.text");
						this.bHeadlines.loader.hide();
						// reinit
						this.initNavi();
						this.handleTicker2("start");
						// button is active
						e.addClassName("choosed");
					}.bind(this)
				});
			}.bind(this));
		}.bind(this));
		// ticker handler
		this.handleTicker2("start");
	},
	initNavi: function(){
		this.bHeadlines.view.down("div.navi").select(this.bHeadlines.naviTag).each(function(e,i){				
			e.observe("click",function(){
				this.smoothTicker(e,i);
			}.bind(this));
		}.bind(this));
	},
	handleTicker2: function(event){
		if(!this.bHeadlines.ticker)
			return false;
		
		if(event=="start"){
			this.bHeadlines.tickerEvent = window.setInterval(this.smoothTicker.bind(this,"next"),this.bHeadlines.tickerSeconds*1000);
		} else if(event=="reset"){
			this.handleTicker2("stop");
			this.handleTicker2("start");
		} else if(event=="stop"){
			window.clearInterval(this.bHeadlines.tickerEvent);
			this.bHeadlines.tickerEvent = null;
		}
	},
	smoothTicker: function(direction,index){
		this.handleTicker2("reset");
		var entries = this.bHeadlines.entries;
		var active = false;
		var found = 0;
		var type = "";
		
		if(!Object.isElement(direction)){
			entries.each(function(e,i){
				if(e.visible()){
					e.hide();
					active = true;
				} else if(active){
					e.show();
					active = false;
					found = 1;
				} else {
					e.hide();
				}
			}.bind(this));
			
			if(found == 0){
				entries[0].show();
				type = "start";
			}
		} else {
			entries.invoke("hide");
			entries[index].show();
		}
		
		this.handleController(this.bHeadlines,direction,type);
	}
};
document.observe("dom:loaded",modul.init.bind(modul));
