var Scroll=Class.create();
Scroll.prototype={
	scrolled:null,
	current_dot:0,
	one_width:0,
	one_height:0,
	pwidth:0,
	pheight:0,
	number:0,
	no_displayed:0,
	scroll_type:"vertical",
	disabledClass:'off',
	overClass:'over',
	locked:false,
	chevrons:[],
	chevrons_disabled:false,
	status:undefined,
	stats:undefined,
	initialize:function(id,chevrons,status,stats,type,chevrons_disabled){
		/* required */
		this.scrolled=$(id);
		this.scroll_type=type;
		/* these aren't required */
		if(chevrons!=undefined){this.chevrons=chevrons;}
		if(chevrons_disabled!=undefined){this.chevrons_disabled=chevrons_disabled;}	//for showing chevrons even at the ends, but shown disabled
		if(status!=undefined){this.status=status;}
		if(stats!=undefined){this.stats=$(stats);}
		// dimension of the parent
		this.init();
	},
	init:function(){
		this.pwidth=this.scrolled.up().getDimensions().width;
		this.pheight=this.scrolled.up().getDimensions().height;
		// no of children
		this.number=this.scrolled.childElements().length;
		// dimensions of a child
		if (this.number > 0)
		{
			this.one_width=this.scrolled.childElements()[0].getDimensions().width;
			this.one_height=this.scrolled.childElements()[0].getDimensions().height;
		}
		// is it horizontal or vertical scroll
		if(this.scroll_type=="horizontal"){
			this.no_displayed = this.pwidth / this.one_width;
			this.moveby=this.pwidth;
			this.scrolled.setStyle({width:(this.one_width*this.number)+'px',height:this.one_height+'px',left:'0px'});
		}else{
			this.no_displayed = this.pheight / this.one_height;
			this.moveby=this.pheight;
			this.scrolled.setStyle({width:this.one_width+'px',height:(this.one_height*this.number)+'px',top:'0px', width:'445px'});
		}

		if ((this.no_displayed >= this.number) || (this.no_displayed == Infinity) ) {	//when only one page or no slides exists
			this.chevrons_disabled = false;		//this will always hide chevrons 
			if(this.status!=undefined){$(this.status).setStyle({display:'none'});}	//hide page dots
		}
		
		/* setup */
		if(this.status!=undefined) {
			//need to build the dots here based on the content inside, repeating it and wrapping it
			var inside = $(this.status).firstDescendant();
			for(var i=0; i<(Math.ceil(this.number/this.no_displayed)); i++) {
				var clone = inside.cloneNode(true);
				clone.title = 'Jump to page ' + (i+1);
				$(this.status).appendChild(clone);
				Event.observe(clone, 'click', this.jump.bindAsEventListener(this,clone));
			}
			inside.remove();	//take out original
			this.change_dot(1);	
		}
		if(this.stats!=undefined)
			this.show_stats(0);
		
		if(this.chevrons!=undefined) {
			//bind in event handlers (for non 'a' tags, allowing backwards compatibility)
			if(this.chevrons[0].tagName!='A') {
				Event.observe(this.chevrons[0], 'click', this.slidelink.bindAsEventListener(this,1));
				Event.observe(this.chevrons[0], 'mouseover', this.over.bindAsEventListener(this,this.chevrons[0]));
				Event.observe(this.chevrons[0], 'mouseout', this.out.bindAsEventListener(this,this.chevrons[0]));
			}
			if(this.chevrons[1].tagName!='A') {
				Event.observe(this.chevrons[1], 'click', this.slidelink.bindAsEventListener(this,-1));
				Event.observe(this.chevrons[1], 'mouseover', this.over.bindAsEventListener(this,this.chevrons[1]));
				Event.observe(this.chevrons[1], 'mouseout', this.out.bindAsEventListener(this,this.chevrons[1]));
			}
		}
		this.manage_buttons(0);
	},
	manage_buttons:function(no)
	{
		if(this.chevrons==undefined){return;}
		/* setup for vertical by default */
		var y=this.scrolled.positionedOffset()[1];
		var h=this.scrolled.getDimensions().height;
		var ph=this.pheight;
		/* make sure it is not horizontal */
		if(this.scroll_type=="horizontal"){
			y=this.scrolled.positionedOffset()[0];
			h=this.scrolled.getDimensions().width;
			ph=this.pwidth;
		}
		
		//button disabling
		$(this.chevrons[0]).removeClassName(this.disabledClass);
		$(this.chevrons[0]).removeClassName(this.overClass);
		$(this.chevrons[1]).removeClassName(this.disabledClass);
		$(this.chevrons[1]).removeClassName(this.overClass);
		if(y+no+h>ph){
			$(this.chevrons[1]).writeAttribute('disabled',false);
			$(this.chevrons[1]).setStyle({cursor:'pointer'});
			$(this.chevrons[1]).setStyle({display:'block'});
		}else{
			$(this.chevrons[1]).writeAttribute('disabled',true);
			$(this.chevrons[1]).setStyle({cursor:'default'});
			if(this.chevrons_disabled) {
				$(this.chevrons[1]).addClassName(this.disabledClass);
			}else{
				$(this.chevrons[1]).setStyle({display:'none'});
			}
		}
		if(y+no>=0){
			$(this.chevrons[0]).writeAttribute('disabled',true);
			$(this.chevrons[0]).setStyle({cursor:'default'});
			if(this.chevrons_disabled) {
				$(this.chevrons[0]).addClassName(this.disabledClass);
			}else{
				$(this.chevrons[0]).setStyle({display:'none'});
			}
		}else{
			$(this.chevrons[0]).writeAttribute('disabled',false);
			$(this.chevrons[0]).setStyle({cursor:'pointer'});
			$(this.chevrons[0]).setStyle({display:'block'});
		}
	},
	slidelink:function(e)
	{
		this.slide($A(arguments)[1]);
	},
	slide:function(dir)
	{
		/* don't do a thing if already in motion */
		if(this.locked){return;}
		var newx=0;
		var newy=0;
		var no=dir*(this.moveby);
		if(this.scroll_type=="horizontal"){
			newx=no;
		}else{
			newy=no;
		}
		/* check for overflow */
		if(this.status!=undefined){
			var newdot = 0;
			if(no<0){newdot = this.current_dot+1;}else{newdot = this.current_dot-1;}
			if(newdot<1 || newdot>Math.ceil(this.number/this.no_displayed)){return;}
		}
		/* Move */
		new Effect.Move(this.scrolled,{x:newx,y:newy,duration:0.5,mode:'relative',beforeStart:this.lock.bind(this),afterFinish:this.unlock.bind(this)});
		/* update the buttons */
		this.manage_buttons(no);
		/* update the dots */
		if(no<0){this.change_dot(1);}else{this.change_dot(-1);}
		/* output the current vals */
		this.show_stats(no);
	},
	jump:function(link)
	{
		var pos=link.element().previousSiblings().length;
		var newx=-1*pos*(this.moveby);
		var xnow=this.scrolled.positionedOffset()[0];
		var no=newx-xnow;
		/* don't if already in motion */
		if(this.locked){return;}
		new Effect.Move(this.scrolled,{x:(no),y:0,duration:0.5,mode:'relative',beforeStart:this.lock.bind(this),afterFinish:this.unlock.bind(this)});
		/* update the buttons */
		this.manage_buttons(no);
		/* update the dots */
		this.current_dot=(pos+1);
		this.change_dot(0);
		/* output the current vals */
		this.show_stats(no);
	},
	change_dot:function(no)
	{
		if(this.status==undefined){return;}
		var c = this;
		$(this.status).childElements().each(function(e){e.addClassName(c.overClass);});
		this.current_dot+=no;
		if ($(this.status).childElements()[this.current_dot-1])
			$(this.status).childElements()[this.current_dot-1].removeClassName(this.overClass);
	},
	show_stats:function(nextpos)
	{
		if(this.stats==undefined){return;}
		var y=this.scrolled.positionedOffset()[1];
		var h=this.scrolled.getDimensions().height;
		/* make sure it is not horizontal */
		if(this.scroll_type=="horizontal"){
			y=this.scrolled.positionedOffset()[0];
			h=this.scrolled.getDimensions().width;
		}
		var next_pos=(y+nextpos);
		var start_index=Math.abs(next_pos/(this.moveby/this.no_displayed));
		var start_no=start_index+1;
		var finish_no=((start_index+this.no_displayed)>this.number)?this.number:start_index+this.no_displayed;
		/* display the stats */
		if (this.number==0) {
			this.stats.update('0');
		} else {
			this.stats.update(start_no+' - '+finish_no+' of '+this.number);
		}
		
	},
	lock:function(){this.locked=true;},
	unlock:function(){this.locked=false;},
	over:function(obj)
	{
		if ($(obj).element().readAttribute('disabled')) {return;};
		$(obj).element().addClassName(this.overClass);
	},
	out:function(obj)
	{
		if ($(obj).element().readAttribute('disabled')) {return;};
		$(obj).element().removeClassName(this.overClass);
	}
};