/* className.replace(/\bMyClass\b/','') */
var ps_object, ps_objcls, ps_coord;

/* Internals: */var jss_nratio = 1, mouse_off = 0, obj_off = 0, jss_hori = 0;
/* Pointers: */var jss_area = 0, jss_scroller = 0, jss_slider = 0;


function objCoords(ev, obj) {//unused afterall
	var e = mouseCoords(ev);
	var o = findPos(obj);
	return { x: e.x - o.x, y: e.y - o.y };
}
function mouseCoords(ev) { //this function was taken from the wast internets  
    if (ev.pageX || ev.pageY) return { x:ev.pageX, y:ev.pageY };
    return { x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        y:ev.clientY + document.body.scrollTop  - document.body.clientTop };
}
function findPos(obj) { //http://www.quirksmode.org/js/findpos.html
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do { curleft += obj.offsetLeft; curtop += obj.offsetTop; }
		while (obj = obj.offsetParent);
	}
	return { x:curleft, y:curtop };
}


function dlog(txt) { var o = document.getElementById('dlog');	o.value = o.value + txt + '\n'; }
function dlogc() {	var o = document.getElementById('dlog');	o.value = ''; }


var scroller_w, scroller_h;
function scrollhand(id, hori, scroll_class, slide_class) {
	if (!scroll_class) scroll_class = 'scroller';
	if (!slide_class) slide_class = 'slider';
if (!jss_area) {
	jss_area = document.getElementById(id);
	if (!jss_area) return;
	jss_hori = hori;
	jss_scroller = document.createElement("div");
	jss_slider = document.createElement("div");
	jss_scroller.className = scroll_class;
	jss_slider.className = slide_class;
	jss_scroller.appendChild(jss_slider);
	document.getElementById('wrap').appendChild(jss_scroller);

	jss_scroller.onclick = jss_goto;
	jss_slider.onmousedown = jss_hold;
	jss_area.onmousedown = ps_hold;
	jss_area.onselectstart = cancelEvent; 
	jss_slider.onselectstart = cancelEvent;

	scroller_w = jss_scroller.clientWidth;	
	scroller_h = jss_scroller.clientHeight;
	
	if (jss_area.addEventListener)
		jss_area.addEventListener('DOMMouseScroll', jss_wheel, false);
	jss_area.onmousewheel = jss_wheel;
}
	if (!hori) {
		var total_h = jss_area.scrollHeight;
		var visible_h = jss_area.clientHeight;
		//if (total_h < scroller_h) total_h = 0;

		jss_slider.style.height = Math.floor((visible_h / total_h) * scroller_h) + 'px';
		jss_nratio = ( (scroller_h)  / total_h );
	} else {
		var total_w = jss_area.scrollWidth;
		var visible_w = jss_area.clientWidth;

		if (total_w < scroller_w) return;

		jss_slider.style.width = Math.floor((visible_w / total_w) * scroller_w) + 'px';
		jss_nratio = ( (scroller_w)  / total_w );	
	}
	jss_nratio = Math.floor( jss_nratio*10000 ) / 10000;

	return scroll_class+','+slide_class;	
}

function jss_wheel(e) {
	var ev = e || window.event;
	var delta = 0;
	if (ev.wheelDelta) {
		delta = ev.wheelDelta/2;
	} else if (ev.detail) {
		delta = -ev.detail*20;
	}

	var old = 0;	
	if (jss_hori) {
		old = (jss_slider.style.left ? parseInt(jss_slider.style.left) : 0);
	} else {
		old = (jss_slider.style.top ? parseInt(jss_slider.style.top) : 0);
	}

	var new_top = old - delta;
	jss_set(new_top, 0);
	
	return cancelEvent(ev);
}

function jss_set(base, mouse) {
	var max = 0;
	if (jss_hori)
		max = jss_scroller.clientWidth - jss_slider.clientWidth;
	else
		max = jss_scroller.clientHeight - jss_slider.clientHeight;

	max -= 0;/*tiny hack*/

	var stopped = 0;
	if (base < 0) { stopped = base; base = 0; }
	if (base > max) { stopped = -(max - base); base = max; }
	if (stopped && mouse) { mouse_off += stopped; }

	if (jss_hori) 
	{
		/* Set it */
		jss_slider.style.left = base + 'px';
	
		/* Scroll area by 'progress' (like 20px / 0.25) */
		jss_area.scrollLeft = Math.min(Math.ceil(base / jss_nratio), jss_area.scrollWidth - jss_area.clientWidth );
	} else {
		/* Set it */
		jss_slider.style.top = base + 'px';
	
		/* Scroll area by 'progress' (like 20px / 0.25) */
		jss_area.scrollTop = Math.min(Math.ceil(base / jss_nratio), jss_area.scrollHeight - jss_area.clientHeight );
	}
}

function jss_move(e) {
	var mouse_cur;
	if (jss_hori) {
		//current mouse coordinate:
		mouse_cur = mouseCoords(e || window.event).x;
	} else {
		//current mouse coordinate:
		mouse_cur = mouseCoords(e || window.event).y;
	}

	//alert("Mouse currently: " + mouse_cur);
	var mouse_mov = mouse_cur - mouse_off;

	//alert("Moved by : " + mouse_mov + " pixels totally");
	var new_top = obj_off + mouse_mov

	jss_set(new_top, 1);
}
function jss_release(e) {
	document.onmousemove = null;
	document.onmouseup = null;
}

function jss_goto(e) {
	var ev = e || window.event;
	if (ev.target != jss_scroller) return;

	var full_off = objCoords(ev, jss_scroller);

	if (jss_hori) {
		var half = full_off.x - Math.round(jss_slider.clientWidth/2);
		jss_set(half, 0); 
	} else {
		var half = full_off.y - Math.round(jss_slider.clientHeight/2);
		jss_set(half, 0);
	}
}

function jss_hold(e) {
	if (jss_hori) {
		//current mouse coordinate:
		mouse_off = mouseCoords(e || window.event).x;
		//object's offset from top:
		obj_off = jss_slider.offsetLeft;
	} else {
		//current mouse coordinate:
		mouse_off = mouseCoords(e || window.event).y;
		//object's offset from top:
		obj_off = jss_slider.offsetTop;
	}
	document.onmousemove = jss_move;
	document.onmouseup = jss_release;
	return cancelEvent(e || window.event);
}

function ps_auto_init() {
	var i = 0, obj;
	var objs = document.getElementsByTagName('*');
	while ((obj = objs[i++])) {	if (obj.className &&
		obj.className.search(/pan/) != -1) obj.onmousedown = ps_hold; }
}
function ps_release(e) {
	var ev = e || window.event;
	var ps_target = ev.target || ev.srcElement;
	document.onmouseup = null;
	document.onmousemove = null;
	ps_object.className = ps_objcls;
	ps_object = 0;
}


function ps_move(e) {
	var cur = mouseCoords(e || window.event);

	if (jss_hori) {
		/* Scroll */		
		ps_object.scrollLeft -= (cur.x - ps_coord.x);
		/* Set it */
		jss_slider.style.left = Math.floor(ps_object.scrollLeft * jss_nratio) + 'px';
	} else {
		/* Scroll */		
		ps_object.scrollTop -= (cur.y - ps_coord.y);
		/* Set it */
		jss_slider.style.top = Math.floor(ps_object.scrollTop * jss_nratio) + 'px';
	}
	ps_coord = cur;	
}

function ps_hold(e) {
	var ev = e || window.event;
	var targ = ev.target || ev.srcElement;
	if (targ.nodeName == 'INPUT') return true;
	while (targ != jss_area && targ) targ = targ.parentNode;
	if (!targ) return;
	/* Input hack: */
	ps_object = jss_area;
	document.onmouseup = ps_release;
	document.onmousemove = ps_move;
	ps_coord = mouseCoords(e || window.event);	
	ps_objcls = ps_object.className;
	ps_object.className += ' drag';
	return cancelEvent(ev);
}


function stopEvent(e) {
	if (e.stopPropagation) e.stopPropagation();
	else e.cancelBubble = true;
	return false;
}

function cancelEvent(e) {
	if (!e) e = window.event;
	if (e.preventDefault) e.preventDefault(); 
	else e.returnValue = false;
	return stopEvent(e);
}