ConsolleSimpleWP = Class.create();
Object.extend(ConsolleSimpleWP.prototype, 
{
	initialize: function(el) {	
		this.container = $(el);
		if (this.container == undefined) return 0;
		
		this.scrollable = this.container.down("div.consolle_s_items");
		this.firstlevelitems = this.container.select("div.consolle_s_item");

		var count = 0;
		this.currentWidth = 0;
		this.firstlevelitems.each((function(fli) {
			fli.number = count;
			count ++;
			fli.controller = this;
			
			fli.child = fli.next(0);
			fli.child.elements = fli.child.select("div.consolle_s_childitem");
						
			fli.childopenedwidth = (fli.child.elements.length*180);
			fli.totalopenedwidth = 180 + fli.childopenedwidth;
			
			fli.child.setStyle({width:fli.childopenedwidth+"px"});
			fli.child.down("div.consolle_s_child").setStyle({width:fli.childopenedwidth+"px"});
			fli.child.parent = fli;
			fli.child.status = "open";
			
			this.currentWidth += fli.totalopenedwidth;
			
			fli.child.elements.each(function(e) {
				e.observe("mouseover", (function() {this.addClassName("consolle_s_itemhover");}).bind(e), false);
				e.observe("mouseout", (function() {this.removeClassName("consolle_s_itemhover");}).bind(e), false);
			});
			fli.observe("mouseover", (function() {this.addClassName("consolle_s_itemhover");}).bind(fli), false);
			fli.observe("mouseout", (function() {this.removeClassName("consolle_s_itemhover");}).bind(fli), false);
		}).bind(this));
		
		this.scrollable.setStyle({width:this.currentWidth+"px"});
		
		this.checkscrollactivation = function() {
			if (this.currentscrollvalue < 0 && this.currentWidth > 900) {
				this.leftscroller.observe("mouseover", this.boundstartleft, false);
				this.leftscroller.observe("mouseout", this.boundstopleft, false);
				this.leftscroller.addClassName("lc_on");
			} else {
				Event.stopObserving(this.leftscroller, 'mouseover', this.boundstartleft);
				Event.stopObserving(this.leftscroller, 'mouseout', this.boundstopleft);
				this.leftscroller.removeClassName("lc_on");
			}
			
			if (Math.abs(this.currentscrollvalue) + 900 < this.currentWidth && this.currentWidth > 900) {
				this.rightscroller.observe("mouseover", this.boundstartright, false);
				this.rightscroller.observe("mouseout", this.boundstopright, false);
				this.rightscroller.addClassName("rc_on");
			} else {
				Event.stopObserving(this.rightscroller, 'mouseover', this.boundstartright);
				Event.stopObserving(this.rightscroller, 'mouseout', this.boundstopright)
				this.rightscroller.removeClassName("rc_on");
			}
		}
		
		this.scrollto = function(to) {
			if (this.scrolling) return;
			this.scrolling = true;
			this.currentscrollvalue = to;
			new Effect.Move(this.scrollable, {x:this.currentscrollvalue, y:9, duration: 0.5, mode: 'absolute', afterFinish:(function(){this.scrolling=false}).bind(this)});
		}
		
		this.startrightscroll = function() {
			if (this.intervalrightscroll != undefined) return;
			this.intervalrightscroll = setInterval(this.scrollright.bind(this), 100)
		}
		
		this.stoprightscroll = function() {
			clearInterval(this.intervalrightscroll);
			this.intervalrightscroll = undefined;
			this.checkscrollactivation();
		}
		
		this.scrollright = function() {
			if (this.scrolling) return;
			
			this.currentscrollvalue -= 180;
			if (Math.abs(this.currentscrollvalue) + 900 > this.currentWidth) {
				this.currentscrollvalue = 900 - this.currentWidth;
				this.stoprightscroll();
			} else {
				this.scrolling = true;
				new Effect.Move(this.scrollable, {x:this.currentscrollvalue, y:9, duration: 0.5, mode: 'absolute', afterFinish:(function(){this.scrolling=false}).bind(this)});
			}
		}
				
		this.startleftscroll = function() {
			if (this.intervalleftscroll != undefined) return;
			this.intervalleftscroll = setInterval(this.scrollleft.bind(this), 100)
		}
		
		this.stopleftscroll = function() {
			clearInterval(this.intervalleftscroll);
			this.intervalleftscroll = undefined;
			this.checkscrollactivation();
		}
		
		this.scrollleft = function() {
			if (this.scrolling) return;
			
			this.currentscrollvalue += 180;
			if (this.currentscrollvalue > 0) {
				this.currentscrollvalue = 0;
				this.stopleftscroll();
			} else {
				this.scrolling = true;
				new Effect.Move(this.scrollable, { x:this.currentscrollvalue, y:9, duration: 0.5, mode: 'absolute', afterFinish:(function(){this.scrolling=false}).bind(this)});
			}
		}
		
		this.stopanyscroll = function() {
			this.stoprightscroll();
			this.stopleftscroll();
		}
		
		this.currentscrollvalue = 0;
		this.rightscroller = this.container.down("div#consolle_s_controller_right");
		this.leftscroller = this.container.down("div#consolle_s_controller_left");
		
		this.boundstartright = this.startrightscroll.bind(this);
		this.boundstopright = this.stoprightscroll.bind(this);
		this.boundstartleft = this.startleftscroll.bind(this);
		this.boundstopleft = this.stopleftscroll.bind(this);
		
		this.checkscrollactivation();
	}
});