var modul = {
	topnews: {
		view: null,
		entriesView: null,
		entries: null,
		entryWidth: 0,
		left:0,
		ticker:true,
		tickerEvent:null,
		tickerSeconds:5,
		carrousel:true,
		carrouselEvent:null
	},
	build: false,
	init: function(){
		this.initTopnews();
	},
	initTopnews: function(){
		this.topnews.view = $$("div.topnews_container")[0];
		this.topnews.entriesView = this.topnews.view.down("div.entries");
		this.topnews.entries = this.topnews.view.select("div.entry");
//		var dimension = this.topnews.view.down("div.view").getDimensions();
		// karussell
		if(this.topnews.carrousel){
//			var tmp = null;
			this.topnews.entriesView.insert(
				tmp = new Element("div",{className:"entry"}).insert(
					this.topnews.entries[0].innerHTML
				)
			);
//			tmp.down("img",0).setStyle({width:dimension.width+"px",height:dimension.height+"px"});
		}
		// float clearer
		this.topnews.entriesView.insert(
			new Element("div",{className:"clearer"})
		);
		
		this.setViewWidth();
		
		// direction buttons
		var btns = this.topnews.view.down("div.direction_navi").select("div");
		if(this.topnews.entries.size()>1){
			btns.each(function(e,i){
				if(e.className.endsWith("left")){
					e.observe("click",this.scroll.bind(this,"prev"));
				} else if(e.className.endsWith("right")){
					e.observe("click",this.scroll.bind(this,"next"));
//					if(Prototype.Browser.IE){
//						e.setStyle({right:"-8px"});
//					}
				}
			}.bind(this));
			
			// page buttons
			this.topnews.view.down("div.controller_wrap").select("div").each(function(e,i){
				e.observe("click",function(){
					var left = (this.topnews.entryWidth*i)*-1;
					this.topnews.left = left;
					this.scroll(e);
				}.bind(this));
			}.bind(this));
			
		} else {
			btns.invoke("hide");
		}
		// transperenz & größe
		
		this.topnews.entries.each(function(e,i){
			e.down("div.background").setOpacity(0.5);
//			e.down("div.image_wrap").down("img",0).setStyle({width:dimension.width+"px",height:dimension.height+"px"});
		});
		// ticker
		this.handleTicker("start");
	},
	handleTicker: function(event){
		if(!this.topnews.ticker)
			return false;
		
		if(event=="start"){
			this.topnews.tickerEvent = window.setInterval(this.scroll.bind(this,"next"),this.topnews.tickerSeconds*1000);
		} else if(event=="reset"){
			this.handleTicker("stop");
			this.handleTicker("start");
		} else if(event=="stop"){
			window.clearInterval(this.topnews.tickerEvent);
			this.topnews.tickerEvent = null;
		}
	},
	setViewWidth: function(){
		this.topnews.entryWidth = this.topnews.entries[0].getWidth();
		if(this.topnews.entryWidth==0){
			this.setViewWidth();
			return false;
		}
		if(this.topnews.carrousel){
			var size = this.topnews.view.select("div.entry").size();
		} else {
			var size = this.topnews.entries.size();
		}
		var width = size*this.topnews.entryWidth;		
		this.topnews.entriesView.setStyle({width:width+"px"});
	},
	scroll: function(direction){
		this.handleTicker("reset");
		if(!Object.isElement(direction)){
			var	left = direction=="next"?this.topnews.left-this.topnews.entryWidth:this.topnews.left+this.topnews.entryWidth;
			var type = "";
			
			if(this.topnews.carrousel){
				var width = this.topnews.entriesView.getWidth()-this.topnews.entryWidth;
			} else {
				var width = this.topnews.entriesView.getWidth();
			}
			
			if(left==(width*-1)){
				left = 0;
				type = "start";
			} else if(left>0){
				left = (width-this.topnews.entryWidth)*-1;
				type = "end";
			}
			this.topnews.left = left;
		} else {
			var	left = this.topnews.left;
		}
		// karussell
		if(this.topnews.carrousel){
			if(type=="start"){
				left = (this.topnews.entriesView.getWidth()-this.topnews.entryWidth)*-1;
				this.topnews.left;
				this.handleCarrousel("start",type);
			}
			
			this.topnews.entriesView.morph({left:left+"px"});
			this.handleController(direction,type);
		} else {
			this.topnews.entriesView.morph({left:left+"px"});
			this.handleController(direction,type);
		}		
	},
	handleCarrousel: function(event,type){
		if(event=="start"){
			this.topnews.carrouselEvent = window.setInterval(this.handleCarrousel.bind(this,"check",type),(this.topnews.tickerSeconds*1000)/2);
		} else if(event=="stop"){
			window.clearInterval(this.topnews.carrouselEvent);
			this.topnews.carrouselEvent = null;
		} else if(event=="check"){
//			var lastleft = (this.topnews.entriesView.getWidth()-this.topnews.entryWidth)*-1;
//			var nowleft = parseInt(this.topnews.entriesView.getStyle("left").gsub("px",""));
//			if(lastleft==nowleft){
				this.handleCarrousel("stop");
				this.topnews.entriesView.setStyle({left:"0px"});
//			}
		}
	},
	handleController: function(direction,type){
		var entries = this.topnews.view.down("div.controller_wrap").select(".page");
		if(!Object.isElement(direction)){
			var active = this.topnews.view.down("div.controller_wrap").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("div").addClassName("active");
			} else {
				active.removeClassName("active");
				active.previous("div").addClassName("active");
			}
		} else {
			entries.invoke("removeClassName","active");
			direction.addClassName("active");
		}
	}
};
document.observe("dom:loaded",modul.init.bind(modul));