var initNews = new Class({
	Implements: [Events, Options], 
	options: {
		appendNewsOnScroll: true,
		newsHeight: 537,
		newsWidth: 490,
		typeNum: 26, // 26 für StartNews, 27 für Video News
		id: null, // wenn id gesetzt ist dann wird diese beim AJAX Request migesendet. Für Parknews notwendig da dort die News über PageID kategorisiert sind, nicht über Kategorien!
		lang: 0
	},
	initialize: function(options) {
		this.setOptions(options);
		var tmpNewsInner = $("news_inner");
		if(tmpNewsInner) {
			this.curHeight = tmpNewsInner.getSize().y;
		}	
		if(this.curHeight > this.options.newsHeight) {
			// Scrolling ist erwünscht
            if(this.options.appendNewsOnScroll) {
                this.addLoadingEl();
            }
			this.setStylesForScrolling();	
			this.createHtmlForScrollBox();	
			this.scrollLoading = false;
			this.getNewsPage = 1;
			this.noMoreNewsToLoad = false;
			if(this.options.id) {
				var id = "&id="+this.options.id;
			}
			else {
				var id = "";
			}
			this.scrollRequest = new Request.HTML({"method": "get", "url": "/index.php?type="+this.options.typeNum+id+"&L="+this.options.lang, "evalScripts": false, onSuccess: this.sniScrollLoaded.bind(this)});
			this.scroller = new ScrollBar('news_pos_outer', 'scroller_outer', 'scroller_inner', {
				"onScrolledToBottom": this.scrolledToBottom.bind(this)
			});
		}
	},
	scrolledToBottom: function() {
		if(!this.scrollLoading && !this.noMoreNewsToLoad) {
			this.scrollLoading = true;
			this.scrollRequest.get({"tx_ttnews[pointer]": this.getNewsPage});
		}
	},
	sniScrollLoaded: function(responseTree, responseElements, responseHTML, responseJavaScript) {
		// this.scrollRequest onSuccess Funktion
		if(responseTree.length > 1) {
            this.loadingEl = this.loadingEl.dispose()
			$("news_inner").adopt(responseTree,this.loadingEl);
			this.scroller.update();
			Mediabox.scanPage(); // neuen Elemente zur Lightbox hinzufügen
			this.getNewsPage++;
		}
		else {
			this.noMoreNewsToLoad = true;
			this.hideLoading();
			this.scroller.update();
		}
        this.scrollLoading = false;
	},
	addLoadingEl: function() {
		this.loadingEl = new Element("div", {"class": "sni_scroll_loading"});
		$("news_inner").adopt(this.loadingEl);
	},
	hideLoading: function() {
		this.loadingEl.setStyle("display","none");
	},
	setStylesForScrolling: function() {
		// wird von manchen Browsern benötigt ?
        $("news_outer").setStyles({
            height: this.options.newsHeight+"px"
        }); 
        $("news_pos_outer").setStyles({
            width: this.options.newsWidth,
            height: this.options.newsHeight,
            overflow: "hidden"
        }); 
	},
	createHtmlForScrollBox: function() {
		var scroller = new Element("div",{
			"id": "scroller_right",
			"styles": {
				"height": this.options.newsHeight+"px"
			}
		}).adopt(new Element("div",{
			"id": "scroller_outer",
			"styles": {
				"height": this.options.newsHeight+"px"
			}
		}).adopt(new Element("div",{
			"id": "scroller_inner"
		})));
		scroller.inject($("newsbanner"),"top");
	}
});

	var ScrollBar = new Class({

		Implements: [Events, Options],

		options: {
			maxThumbSize: 10,
			wheel: 8
		},

		initialize: function(content, track, thumb, options){
			this.setOptions(options);

			this.content = $(content);
			this.track = $(track);
			this.thumb = $(thumb);

			this.bound = {
				'start': this.start.bind(this),
				'end': this.end.bind(this),
				'drag': this.drag.bind(this),
				'wheel': this.wheel.bind(this),
				'page': this.page.bind(this)
			};

			this.position = {};
			this.mouse = {};
			this.update();
			this.attach();
		},

		update: function(){

			this.contentSize = this.content.offsetHeight;
			this.contentScrollSize = this.content.scrollHeight;
			this.maxScrollTop = this.contentScrollSize - this.contentSize - 51; // 51px ist das Loading div hoch. Wenn dort angekommen sollen News nachgeladen werden.

			this.trackSize = this.track.offsetHeight;

			this.contentRatio = this.contentSize / this.contentScrollSize;

			this.thumbSize = (this.trackSize * this.contentRatio).limit(this.options.maxThumbSize, this.trackSize);

			this.scrollRatio = this.contentScrollSize / this.trackSize;

			this.thumb.setStyle('height', this.thumbSize);

			this.updateThumbFromContentScroll();
			this.updateContentFromThumbPosition();
		},

		updateContentFromThumbPosition: function(){
			this.content.scrollTop = this.position.now * this.scrollRatio;
			this.checkForBottom();
		},

		updateThumbFromContentScroll: function(){
			this.position.now = (this.content.scrollTop / this.scrollRatio).limit(0, (this.trackSize - this.thumbSize));
			this.thumb.setStyle('top', this.position.now);
			this.checkForBottom();
		},

		attach: function(){
			this.thumb.addEvent('mousedown', this.bound.start);
			if (this.options.wheel) this.content.addEvent('mousewheel', this.bound.wheel);
			this.track.addEvent('mouseup', this.bound.page);
		},

		wheel: function(event){
			this.content.scrollTop -= event.wheel * 7 * this.options.wheel;
			this.updateThumbFromContentScroll();
			event.stop();
		},
		checkForBottom: function() {
            if(this.content.scrollTop >= this.maxScrollTop) {
                this.fireEvent("onScrolledToBottom");
            }
		},
		page: function(event){
			if (event.page.y > this.thumb.getPosition().y) this.content.scrollTop += this.content.offsetHeight;
			else this.content.scrollTop -= this.content.offsetHeight;
			this.updateThumbFromContentScroll();
			event.stop();
		},

		start: function(event){
			this.mouse.start = event.page.y;
			this.position.start = this.thumb.getStyle('top').toInt();
			document.addEvent('mousemove', this.bound.drag);
			document.addEvent('mouseup', this.bound.end);
			this.thumb.addEvent('mouseup', this.bound.end);
			event.stop();
		},

		end: function(event){
			document.removeEvent('mousemove', this.bound.drag);
			document.removeEvent('mouseup', this.bound.end);
			this.thumb.removeEvent('mouseup', this.bound.end);
			event.stop();
		},

		drag: function(event){
			this.mouse.now = event.page.y;
			this.position.now = (this.position.start + (this.mouse.now - this.mouse.start)).limit(0, (this.trackSize - this.thumbSize));
			this.updateContentFromThumbPosition();
			this.updateThumbFromContentScroll();
			event.stop();
		}

	});


