//Scroll
var s_control = {
	interval_time : 20,	//Milliseconds between move function calls 
						//(the smaller value - the smoother movement)
	max_speed : 10,		//Max. amount of scroll per iteration (pixels)
	fixed_speed : 1,	//Fixed speed. Set to 0 if not required
	active_zone : 8,	//Width of active zone (hover on which causes scroll)
						//In percents; value should be in range [0..50]
	h_zone : 25,		//Width (in pixels) of horizontal zone where scrolling is active
	debug : false,		//Display debug messages
	cursor : true,		//Change cursor to (n|s)-resize
	is_ie : false,		//Are we in stoopid IE?

	//Don't touch anything below :)
	active : false,
	dir_up : false,
	scroll_amount : 0,
	current_scroll : 0,
	min_scroll : 0,
	max_scroll : 0,
	s_width : 0
};
function init_scroll(e){
	s_control.active = true;
	proc_scroll(e);
}
function halt_scroll(e){
	s_control.active = false;
}
function proc_scroll(e){
	if(!e) e = window.event;
	if(!e) return;
	var debug = '';

	//Acquire event target
	var targ = e.target || e.srcElement;
	if(targ.nodeType == 3)
		targ = targ.parentNode;
	//while(targ.id != 'scroller_content'){
	//	targ = targ.parentNode;
	//	if(targ.tagName == 'BODY')
	//		return;
	//}
	debug += targ.tagName + '<br />';
	
	//First, check if we are in scrollable area
	var wh = _.get('scroller').clientHeight;	//Wrapper height
	var ch = _.get('scroller_content').clientHeight;	//Content height
	var ms = ch - wh;	//Max possible scroll
	var ypos = e.layerY || e.y;	//Mouse Y position
	var xpos = e.layerX || e.x;	//Mouse Y position
	if(!s_control.is_ie)
		ypos += s_control.current_scroll;
	if(!s_control.s_width)
		s_control.s_width = _.get('scroller').offsetWidth;
	var az = Math.min(50, Math.max(0, s_control.active_zone));	//Active zone (%)
	var az_real = Math.floor(wh * az / 100);	//Active zone (px)
	var xc;	//Scroll excess (just nevermind it :)

	debug += 'yPos: ' + ypos + '; curScroll: ' + s_control.current_scroll + '; ';
	debug += 'xPos: ' + xpos + '; width: ' + _.get('scroller_content').offsetWidth + '; ';
	debug += 'sWidth: ' + s_control.s_width + '; ';

	//Unscrollable
	s_control.max_scroll = Math.max(0, ms);
	var too_left = (s_control.s_width - xpos > s_control.h_zone);
	if(ms <= 0 || too_left){
		if(s_control.cursor)
			_.get('scroller').style.cursor = 'auto';
		s_control.scroll_amount = 0;
		if(s_control.debug)
			_.get('for_ajax').innerHTML = debug;
		return;
	}

	//Scroll up
	if(ypos <= az_real){
		s_control.dir_up = true;
		if(s_control.cursor)
			_.get('scroller').style.cursor = 'pointer'; //n-resize
	}
	//Scroll down
	else if(ypos >= wh - az_real){
		s_control.dir_up = false;
		ypos = wh - ypos;
		if(s_control.cursor)
			_.get('scroller').style.cursor = 'pointer'; //s-resize
	}
	//Out of scroll zone
	else{
		s_control.scroll_amount = 0;
		if(s_control.cursor)
			_.get('scroller').style.cursor = 'auto';
		if(s_control.debug)
			_.get('for_ajax').innerHTML = debug;
		return;
	}

	//Calculate speed
	s_control.scroll_amount = Math.round(Math.pow((az_real - ypos) / az_real, 1) * s_control.max_speed);
	if(s_control.fixed_speed != 0 && s_control.scroll_amount != 0){
		if(s_control.scroll_amount < 0)
			s_control.scroll_amount = -s_control.fixed_speed;
		else
			s_control.scroll_amount = s_control.fixed_speed;
	}

	//Debug output
	if(s_control.debug)
		_.get('for_ajax').innerHTML = debug;
}
function dodo_scroll(){
	if(!s_control.active || s_control.scroll_amount == 0) return;
	if(s_control.dir_up)
		s_control.current_scroll = Math.min(s_control.min_scroll, s_control.current_scroll + s_control.scroll_amount);
	else
		s_control.current_scroll = Math.max(-s_control.max_scroll, s_control.current_scroll - s_control.scroll_amount);
	_.get('scroller_content').style.top = s_control.current_scroll + 'px';
}
function scroll_this_page(){ setInterval(dodo_scroll, s_control.interval_time); }
function reset_scroll(){
	_.get('scroller_content').style.top = 0;
	s_control.current_scroll = 0;
}