
function cmsgetpos(e) {

	var pos = new Array(2);
	
	if (e.pageX || e.pageY) {
		pos[0] = e.pageX;
		pos[1] = e.pageY;
	} else if (e.clientX || e.clientY) 	{
		pos[0] = e.clientX;
		pos[1] = e.clientY;
		
		if (document.body.scrollTop) {
			pos[0]=pos[0]+document.body.scrollLeft;
			pos[1]=pos[1]+document.body.scrollTop;
		} else if (document.documentElement.scrollTop) {
			pos[0]=pos[0]+document.documentElement.scrollLeft;
			pos[1]=pos[1]+document.documentElement.scrollTop;
		}
	}
	
	return pos;
}

var scrolling;
scrolling=false;

var lastscrollbox;
lastscrollbox="";

var dragging;
dragging=false;

var dragscreeninitial;
var dragtopinitial;

var scrollmarkermargin=8;
var scrollmarkerheight=15;

var delta=0;

function movemarker(newpos) {
	var newtop;
	newtop=newpos-scrollmarkerheight;
		
	if (newtop<0) {
		newtop=0;
		delta=0;
	}
	if (newtop>scrollbarplay()) {
		newtop=scrollbarplay();
		delta=0;
	}
		
	getmarker().style.top=(newtop+scrollmarkermargin)+"px";
		
	if (viewheight()>=contentheight()) newtop=0;
		
	getscrollcontent().style.top=-newtop*(contentheight()-viewheight())/scrollbarplay()+"px";
}

function onscrollmousemove(e) {
	if (!e) e=window.event;
	
	if (dragging) {
		var pos=cmsgetpos(e);

		var loop=getmarker().offsetParent;
		while (loop) {
			if (loop.offsetTop) pos[1]-=loop.offsetTop;
			loop=loop.offsetParent;
		}	
		
		movemarker(pos[1]);
	}
}

function scrollby() {
	var marker = getmarker();
	
	if (delta!=0)  {
		if (marker.style.top) {
			movemarker(parseInt(marker.style.top)+delta+scrollmarkermargin);
		}
		else {
			if(marker.offsetTop) {
				movemarker(parseInt(marker.offsetTop)+delta+scrollmarkermargin);
			}
			else {
				alert("I have no top!");
			}
		}
		setTimeout("scrollby();",50);
	}
}

function scrollup(boxid) {
	lastscrollbox=boxid;
	delta=5;
	scrollby();
}

function scrolldown(boxid) {
	lastscrollbox=boxid;
	delta=-5;
	scrollby();
}


function cancelclick(event) {
	if (!event) return;
	if (event.stopPropagation)	event.stopPropagation(); else event.cancelBubble=true;
}

// objects

function getmarker() {
	return document.getElementById("markerscroll"+lastscrollbox);
}

function getscrollcontent() {
	return document.getElementById("scroll"+lastscrollbox);
}

function getscrollviewport() {
	return document.getElementById("scrollviewport"+lastscrollbox);
}

function getscrollbar() {
	return document.getElementById("scrollbar"+lastscrollbox);
}

// metrtics

function objectheight(obj) {
	var h;
	h=obj.clientHeight;
	if (h==0) h=obj.offsetHeight;
	
	return h;
}

function viewheight() {
	var viewport=getscrollviewport();
	return objectheight(viewport);
}

function contentheight() {
	var content=getscrollcontent();
	return objectheight(content);
}

function scrollbarheight() {
	var scrollbar=getscrollbar();
	return objectheight(scrollbar);
}

function scrollbarplay() {
	return scrollbarheight()-scrollmarkerheight*2-1;
}

// movement

function scrolldrag(e,boxid) {			
	if (!e) e=window.event;
	lastscrollbox=boxid;
	dragging=true;
}

// init

function onmouseup() {
	delta=0;
	dragging=false;
}

function onthumbnailclick(link) {
	lastscrollbox="thumbnails";
	link.href=link.href+"?scroll="+getmarker().style.top+"&lastscrollbox="+lastscrollbox;
}

function initscroll() {
	if(arguments.length == 0) {
		if (window.location.search) {
			var matches=window.location.search.match(/scroll=([0-9]+)/)
			if (matches) {
				
				lastscrollbox="thumbnails";
				if (!getmarker()) {
					setTimeout("initscroll();",100);
				} else {
					movemarker(matches[1]-scrollmarkermargin+scrollmarkerheight);
				}
			}
		}
	}
	else {
		var name = arguments[0];
		var temp=lastscrollbox;
		lastscrollbox=name;
		if (viewheight()>=contentheight()-3) {
			document.getElementById("scrollbar"+name).style.visibility="hidden";
		}
		lastscrollbox=temp;
		
		document.body.onmouseup=onmouseup;
	}
}