var HORIZONTAL = 1;
var VERTICAL = 2;
function scroller (_reset,_name, _content_name, _content_mode, _content_spacer, _speed, _stepping) {
	var name, content_name, content_spacer, scroller, clipper, button_up, button_down, button_left, button_right, speed, stepping, scroller_height, scroller_width, clipper_height, clipper_width, content, content_num, timer_horizontal, timer_vertical, i, initialised, thing, scroll_horizontal, scroll_vertical, scroller_size, clipper_size, scroller_pos, timer;
	this.name = _name;
	this.content_name = _content_name;
	this.content_mode = _content_mode;
	this.content_spacer = _content_spacer;
	this.scroller = get_obj('scroller_'+this.name+'');
	this.up = scroll_up;
	this.down = scroll_down;
	this.left = scroll_left;
	this.right = scroll_right;
	this.jump_up = scroll_jump_to_content_up;
	this.jump_down = scroll_jump_to_content_down;
	this.jump_left = scroll_jump_to_content_left;
	this.jump_right = scroll_jump_to_content_right;
	this.recalc = scroll_recalc_content_num;
	this.check_button = scroll_check_button;
	this.scroll = scroll;
	this.scroll_to_content_num = scroll_to_content_num;
	this.jump_to_content = scroll_jump_to_content;
	this.scroll_to_px = scroll_to_px;
	this.jump_to_px = scroll_jump_to_px;
	this.stop = scroll_stop;
	this.reset = scroll_reset;
	if (this.scroller) {
		this.scroller.css = get_style(this.scroller);
		this.clipper = get_obj('clipper_'+this.name+'');
		if (_speed) {
			this.speed = _speed;
		}
		else {
			this.speed = 30;
		}
		if (_stepping) {
			this.stepping = _stepping;
		}
		else {
			this.stepping = 10;
		}
		this.reset(_reset);
		this.timer_horizontal = "";
		this.timer_vertical = "";
		this.initialised = true;
		this.thing = this.name+'whatever';
		eval(this.thing + "= this");
	}
	else {
		this.initialised = false;
	}
	return this;
}
function scroll_reset (_reset) {
	this.scroller_height = get_height(this.scroller);
	this.scroller_width = get_width(this.scroller);
	this.clipper_height = get_height(this.clipper);
	this.clipper_width = get_width(this.clipper);
	this.content = new Array();
	this.content_num = 0;
	this.t_content = new Array();
	if (this.content_mode == VERTICAL) {
		this.button_up = get_obj('button_up_'+this.name+'');
		this.button_down = get_obj('button_down_'+this.name+'');
		this.button_jump_up = get_obj('button_jump_up_'+this.name+'');
		this.button_jump_down = get_obj('button_jump_down_'+this.name+'');
		this.scroller_size = this.scroller_height;
		this.clipper_size = this.clipper_height;
		this.get_size = get_height;this.scroller_pos  = 'this.scroller.css.top';
		this.timer = 'this.timer_vertical';
	}
	else if (this.content_mode == HORIZONTAL) {
		this.button_left = get_obj('button_left_'+this.name+'');
		this.button_right = get_obj('button_right_'+this.name+'');
		this.button_jump_left = get_obj('button_jump_left_'+this.name+'');
		this.button_jump_right = get_obj('button_jump_right_'+this.name+'');
		this.scroller_size = this.scroller_width;
		this.clipper_size = this.clipper_width;
		this.get_size = get_width;
		this.scroller_pos  = 'this.scroller.css.left';
		this.timer = 'this.timer_horizontal';
	}
	i = 1;
	this.scroller_size = 0;

	while(this.t_content[i] = get_obj(this.name + '_' + this.content_name + '_' + i)) {

		this.t_content[i].real_size = this.get_size(this.t_content[i]);
		//alert("origi "+this.t_content[i].real_size);
		if (this.content_spacer) {
			this.t_content[i].real_size += parseInt(this.content_spacer);
		}
		this.scroller_size += this.t_content[i].real_size;
//		alert("full "+this.scroller_size);
		i++;
	}
	this.scroller_size += 42;
	if (i > 1) {
		t_i = (i-1);
		this.content_num = t_i;
		if (this.content_spacer) {
			this.t_content[t_i].real_size -= parseInt(this.content_spacer);
			this.scroller_size -= parseInt(this.content_spacer);
		}
	}
	if (this.content_mode == VERTICAL) {
		if (this.button_up && this.button_down) {
			this.button_up.css = get_style(this.button_up);
			this.button_down.css = get_style(this.button_down);
		}
		if (this.button_jump_up && this.button_jump_down) {
			this.button_jump_up.css = get_style(this.button_jump_up);
			this.button_jump_down.css = get_style(this.button_jump_down);
		}
	}else if (this.content_mode == HORIZONTAL) {
		if (this.button_left && this.button_right) {
			this.button_left.css = get_style(this.button_left);
			this.button_right.css = get_style(this.button_right);
		}
		if (this.button_jump_left && this.button_jump_right) {
			this.button_jump_left.css = get_style(this.button_jump_left);
			this.button_jump_right.css = get_style(this.button_jump_right);
		}
	}
	this.check_button();
	this.clip_delta  = (this.clipper_size - this.scroller_size);
	this.content = this.t_content;
	if (!(this.scroll_horizontal && this.scroll_vertical)) {
		if(_reset) {
			eval(this.scroller_pos + ' = \'0px\';');
		}
	}
}
function scroll_up (_multiplier) {
	this.scroll ('up', _multiplier);
}
function scroll_down (_multiplier) {
	this.scroll ('down', _multiplier);
}
function scroll_left (_multiplier) {
	this.scroll ('left', _multiplier);
}
function scroll_right (_multiplier) {
	this.scroll ('right', _multiplier);
}
function scroll (_direction, _multiplier) {
	this.stop();
	if (this.initialised && _direction && (this.scroll_horizontal || this.scroll_vertical)) {
		
		var target_px;
		var div_px;
		div_px = parseInt(eval(this.scroller_pos));
		if (!_multiplier) {
			_multiplier = 1;
		}
		if (_direction == 'left' || _direction == 'up') {
			if(div_px <= (0 - (this.stepping*_multiplier))){
				scroll = true;
				target_px = (div_px + (this.stepping*_multiplier)) + 'px';
			}else if (div_px < 1) {
				scroll = false;
				target_px = 0 + 'px';
			}
		}
		else if (_direction == 'right' || _direction == 'down') {
			if(div_px >= (this.clip_delta + (this.stepping*_multiplier))){
				scroll = true;
				target_px = (div_px - (this.stepping*_multiplier)) + '';
			}
			else if (div_px > this.clip_delta) {
				scroll = false;
				target_px = this.clip_delta + '';
			}
		}
		if (target_px && this.scroller_pos) {

			eval (this.scroller_pos + ' = \'' + target_px + '\';');


			if (scroll) {
				eval(this.timer + ' = setTimeout(\'' + this.thing + '.' + _direction + '(' + _multiplier + ');\', ' + this.speed + ');');
			}
		}
	}
}
function scroll_recalc_content_num (_cell_id) {
	if (this.initialised && _cell_id) {
		pixel_old = parseInt(this.content[_cell_id].real_size);
		pixel_new = parseInt(this.get_size(this.content[_cell_id]));
		if (this.content_spacer) {
			if (_cell_id < (this.content.length-1)) {
				pixel_new += this.content_spacer;
			}
		}
		this.content[_cell_id].real_size = pixel_new;
		this.scroller_size += (pixel_new-pixel_old);
		this.clip_delta  = (this.clipper_size - this.scroller_size);
		this.check_button();
	}
}
function scroll_check_button () {
	if (this.content_mode == VERTICAL) {
		if (this.scroller_size > this.clipper_size) {
			this.scroll_vertical = true;
			if (this.button_up && this.button_down) {
				div_show(this.button_up);
				div_show(this.button_down);
			}
			if (this.content_num > 1) {
				if (this.button_jump_up && this.button_jump_down) {
					div_show(this.button_jump_up);div_show(this.button_jump_down);
				}
			}
			else {
				if (this.button_jump_up && this.button_jump_down) {
					div_hide(this.button_jump_up);
					div_hide(this.button_jump_down);
				}
			}
		}
		else {
			this.scroll_vertical = false;
			if (this.button_up && this.button_down) {
				div_hide(this.button_up);
				div_hide(this.button_down);
			}
			if (this.button_jump_up && this.button_jump_down) {
				div_hide(this.button_jump_up);div_hide(this.button_jump_down);
			}
		}
	}
	if (this.content_mode == HORIZONTAL) {
		if (this.scroller_size > this.clipper_size) {
			this.scroll_horizontal = true;
			var button_spacer_left;
			var button_spacer_right;
			button_spacer_left = get_obj("button_spacer_left");
			button_spacer_right = get_obj("button_spacer_right");
			if (this.button_left && this.button_right) {
				div_show(this.button_left);
				div_show(this.button_right);
				if(button_spacer_right && button_spacer_left) {
					button_spacer_right.css = button_spacer_right.style;
					button_spacer_left.css = button_spacer_left.style;
					div_show(button_spacer_left);
					div_show(button_spacer_right);
				}
			}
			if (this.content_num > 1) {
				if (this.button_jump_left && this.button_jump_right) {
					div_show(this.button_jump_left);
					div_show(this.button_jump_right);
				}
			}
			else {
				if (this.button_jump_left && this.button_jump_right) {
					div_hide(this.button_jump_left);
					div_hide(this.button_jump_right);
				}
			}
		}
		else {
			var button_spacer_left;
			var button_spacer_right;
			button_spacer_left = get_obj("button_spacer_left");
			button_spacer_right = get_obj("button_spacer_right");
			this.scroll_horizontal = false;
			if (this.button_left && this.button_right) {
				div_hide(this.button_left);
				div_hide(this.button_right);
				if(button_spacer_right && button_spacer_left) {
					button_spacer_right.css = button_spacer_right.style;
					button_spacer_left.css = button_spacer_left.style;
					div_hide(button_spacer_right);
					div_hide(button_spacer_left);
				}
			}
			if (this.button_jump_left && this.button_jump_right) {
				div_hide(this.button_jump_left);
				div_hide(this.button_jump_right);
			}
		}
	}
}
function scroll_jump_to_content_up () {
	this.jump_to_content('up');
}
function scroll_jump_to_content_down () {
	this.jump_to_content('down');
}
function scroll_jump_to_content_left () {
	this.jump_to_content('left');
}
function scroll_jump_to_content_right () {
	this.jump_to_content('right');
}
function scroll_jump_to_content (_mode) {
	pixel = 0;
	for (i = 1; i < this.content_num+1; i++) {
		pixel += parseInt(eval('this.content[' + i + '].real_size'));
		if (pixel > parseInt(eval(this.scroller_pos))*(-1)) {
			break;
		}
	}
	this.content_num_current = i;
	if (_mode == 'up' || _mode == 'left' ) {
		this.content_num_current--;
	}
	if (_mode == 'down' || _mode == 'right' ) {
		this.content_num_current++;
	}
	if (this.content_num_current < 1) {
		this.content_num_current = 1;
	}
	if (this.content_num_current > this.content_num) {
		this.content_num_current = this.content_num;
	}
	this.scroll_to_content_num(this.content_num_current, 1);
}
	
function scroll_to_content_num (_cell_id, _jump, _mode) {
	this.stop();
	if (this.initialised && _cell_id && (this.scroll_horizontal || this.scroll_vertical)) {
		pixel = 0;
		for (i = 1; i < this.content_num+1; i++) {
			if (i < _cell_id) {
				pixel += parseInt(eval('this.content[' + i + '].real_size'));
			}
			else {
				break;
			}
		}
		if (_mode == 'min') {
			pixel = (pixel - this.clipper_size + parseInt(eval('this.content[' + i + '].real_size')));
		}
		if (pixel > (this.scroller_size - this.clipper_size) && (this.scroller_size > this.clipper_size)) {
			pixel = (this.scroller_size - this.clipper_size);
		}
		else if (pixel < 0) {
			pixel = 0;
		}
		this.scroll_to_px(pixel, _jump);
	}
}
	
function scroll_to_px (_pixel, _jump) {
	this.stop();if (this.initialised && (this.scroll_horizontal || this.scroll_vertical)) {
	if (_pixel > (this.scroller_size - this.clipper_size) && (this.scroller_size > this.clipper_size)) {
		_pixel = (this.scroller_size - this.clipper_size);
	}
	else if (_pixel < 0) {
		_pixel = 0;
	}if (_jump) { 
		eval(this.scroller_pos + ' = \'-' + _pixel + 'px\';');
	}
	else {
		var pos, scroll, div_px;
		pos = (0 - _pixel);
		scroll = false;
		div_px = parseInt(eval(this.scroller_pos));
		if ((div_px - this.stepping) > (0 - _pixel)) {pos = (div_px-this.stepping);scroll = true;}else if ((div_px + this.stepping) < (0 - _pixel)) {pos = (div_px+this.stepping);scroll = true;}if (scroll) {eval(this.timer + ' = setTimeout(\'' + this.thing + '.scroll_to_px(' + _pixel + ');\', this.speed);');}eval(this.scroller_pos + ' = \'' + pos + 'px\';');}}}

function scroll_jump_to_px (_pixel) {this.scroll_to_px (_pixel, true);}
	
function scroll_stop () {clearTimeout(this.timer_horizontal);clearTimeout(this.timer_vertical);return true;}

var br5 = (document.getElementById) ? 1 : 0;var ns4 = (document.layers && !document.getElementById) ? 1 : 0;var ie4 = (document.all && !document.getElementById) ? 1 : 0;
	
function get_obj (obj_name) {
	if (ns4){
		obj = eval ("document."+obj_name);
		
		if (obj) {
			return obj;
		}
		else {
			return false;
		}
	}
	if (ie4){
		obj = eval ("document.all[\""+obj_name+"\"]");
		if (obj) {
			return obj;
		}
		else {
			return false;
		}
	}
	if (br5){		
		obj = document.getElementById(obj_name);
		if (obj) {
			return obj;
		}
		else {
			return false;
		}
	}
}

function div_show (obj) {if (ns4) {obj.css.visibility = "show";}if (ie4 || br5) {obj.css.visibility = "visible";obj.css.display = "block";}obj.css.display = "block";}function div_hide (obj) {if (ns4) {obj.css.visibility = "hide";}if (ie4 || br5) {obj.css.visibility = "hidden";obj.css.display = "none";}obj.css.display = "none";}function div_vis (obj) {if (ns4) {obj.css.visibility = "hide";}if (ie4 || br5) {obj.css.visibility = "hidden";}}

function get_style (obj) {if (ns4) {return obj;}if (ie4 || br5) {return obj.style;}}
	
function get_width(obj) {
	if (ns4 || ie4) {
		return obj.document.width;
	}
	if (br5) {
		return obj.offsetWidth;
	}
}
function get_height(obj) {
	if (ns4 || ie4) {
		return obj.document.height;
	}
	if (br5) {
		return obj.offsetHeight;
	}
}
