/*
Additional JavaScript that can be useful.
*/

function show(id){
	document.getElementById(id).style.display = 'block';
}

function getObject(id){
	return document.getElementById(id);
}

/*
JavaScript needed for the scrollbar.

Created by: TvL Design
Date: 05 january 2009
Version: 0,1 * Beta
*/

var height = 0;
var mousex = 0;
var mousey = 0;
var grabx = 0;
var graby = 0;
var orix = 0;
var oriy = 0;
var elex = 0;
var eley = 0;
var algor = 0;
var maxHeight = 0;
var scrollHeight; //totalheight - height venster
var slidebarHeight;
var textboxObj;
var sliderObj;
var scroll = true;

var dragobj = null;

if (window.addEventListener){
	window.addEventListener('DOMMouseScroll', wheel, false);
}
window.onmousewheel = document.onmousewheel = wheel;

function init(box_id, box_content_id, slider_id, slidebar_id){
	
	box_height = getObject(box_id).offsetHeight;
	box_content_height = getObject(box_content_id).offsetHeight;
	slider_height = getObject(slider_id).offsetHeight;
	slidebar_height = getObject(slidebar_id).offsetHeight;
	
	if(box_content_height <= box_height){
		scroll = false;
		getObject(slidebar_id).style.display = 'none';
		getObject(slider_id).style.display = 'none';
	}
	
	scrollHeight = (box_content_height - box_height);
	slidebarHeight = (slidebar_height - slider_height);
	textboxObj = box_content_id;
	sliderObj = slider_id;
	document.onmousemove = getMouseXY;
	getMouseXY();
	if(maxHeight == 0){
		obj = getObject(sliderObj);
		pos = findPos(obj);
		maxHeight = pos[1];
	}
}

function handle(delta) {
	if(scroll != false){
		var difference = 20;
		if(navigator.userAgent.indexOf("Opera") == 0){
			difference = -20;
		}
		var s = delta*difference;
		height += s;
		sliderHeight = maxHeight - (height*(slidebarHeight/scrollHeight));
		if(height >= 0){
			height = 0;
		}
		if(height <= -scrollHeight){
			height = -scrollHeight;
		}
		if(sliderHeight <= maxHeight){
			sliderHeight = maxHeight;
		}
		if(sliderHeight >= (maxHeight + slidebarHeight)){
			sliderHeight = (maxHeight + slidebarHeight);
		}
		getObject(textboxObj).style.marginTop = height + 'px';
		getObject(sliderObj).style.top = sliderHeight + 'px';
	}
}

function wheel(event){
	if(scroll != false){
		var delta = 0;
		if(!event){
			event = window.event;
		}
		if (event.wheelDelta){
			delta = event.wheelDelta/120; 
			if (window.opera){
				delta = -delta;
			}
		}
		else if (event.detail){
			delta = -event.detail/3;
		}
		if(delta){
			handle(delta);
		}
	}
}

function falsefunc(){
	return false;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function getMouseXY(e){
	if(scroll != false){
		if(!e){
			e = window.event;
		}
		if(e){ 
			if(e.pageX || e.pageY){
				mousex = e.pageX;
				mousey = e.pageY;
				algor = '[e.pageX]';
				if (e.clientX || e.clientY){
					algor += ' [e.clientX] ';
				}
			}
			else if(e.clientX || e.clientY){
				mousex = e.clientX + document.body.scrollLeft;
				mousey = e.clientY + document.body.scrollTop;
				algor = '[e.clientX]';
				if(e.pageX || e.pageY){
					algor += ' [e.pageX] ';
				}
			}
		}
	}
}

function grab(){
	if(scroll != false){
		document.onmousedown = falsefunc;
		dragobj = getObject(sliderObj);
		//dragobj = context;
		//dragobj.style.zIndex = 100;
		document.onmousemove = drag;
		document.onmouseup = drop;
		grabx = mousex;
		graby = mousey;
		pos = findPos(dragobj);
		
		elex = orix = pos[0];
		eley = oriy = pos[1];
		getMouseXY();
	}
}

function drag(e){
	if(scroll != false){
		if (dragobj){
			document.body.style.cursor = 'pointer';
			elex = orix + (mousex-grabx);
			eley = oriy + (mousey-graby);
			//dragobj.style.position = "absolute";
			//dragobj.style.left = elex + 'px';
			if(eley <= maxHeight){
				height = 0;
				dragobj.style.top = maxHeight + 'px';
				getObject(textboxObj).style.marginTop = 0 + 'px';
			}
			else if(eley >= (maxHeight + slidebarHeight)){
				height = -scrollHeight;
				dragobj.style.top = (maxHeight + slidebarHeight) + 'px';
				getObject(textboxObj).style.marginTop = -scrollHeight + 'px';
			}
			else{
				height = -((scrollHeight/slidebarHeight)*(eley-maxHeight));
				dragobj.style.top = eley + 'px';
				getObject(textboxObj).style.marginTop = height + 'px';
			}
		}
		getMouseXY(e);
		return false;
	}
}

function drop(){
	if(scroll != false){
		if (dragobj){
			document.body.style.cursor = 'default';
			//dragobj.style.zIndex = 0;
			dragobj = null;
		}
		getMouseXY();
		document.onmousemove = getMouseXY;
		document.onmouseup = null;
		document.onmousedown = null;
	}
}