// JavaScript Document
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
	
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}


function handle(delta) {
	if(document.getElementById(activeScrollField)){
		if (delta < 0){
			scrollTim(activeScrollField,15*scrollSpeed*-1);
		}else{
			scrollTim(activeScrollField,15*scrollSpeed);
		}
	}
}

function wheel(event){
	if(activeScrollField){
		var delta = 0;
		if (!event){
			event = window.event;
		}
		if (event.wheelDelta) { /* IE/Opera. */
			delta = event.wheelDelta/120;
			
			if (window.opera){
				delta = -delta;
			}
		}else if(event.detail) {
			delta = -event.detail/3;
		}
		if(delta){
			handle(delta);
		}
		if(event.preventDefault){
			event.preventDefault();
		}
		event.returnValue = false;
	}
}

if (window.addEventListener){
	window.addEventListener('DOMMouseScroll', wheel, false);
}
window.onmousewheel = document.onmousewheel = wheel;

var activeScrollField = false;
var scrollSpeed = 0;
function createScroll(id, width, height, speed, style){
	if(!style) var style = '';
	var el = document.getElementById(id);
	var divArray = el.getElementsByTagName("div");
	loaded = false;
	for(var i=0; i<divArray.length; i++){
		if(divArray[i].id == id+"scrollTekst"){
			var tekst = divArray[i].innerHTML;
			loaded = true;
		}
	}
	
	el.style.height 	= height 	+ 'px';
	el.style.width 		= width 	+ 'px';	
	
	
	if(!loaded){
		var tekst = el.innerHTML;		
		el.innerHTML 		= '';
		el.innerHTML 		= '<div id="'+id+'_scrollTekst" rel="scrollTekst" class="scrollTekst" style="position:absolute;top:0px;width:'+(width)+'px"><div class="scrollContentTekst">'+tekst+'<div></div>';
	}
	el.onmouseover = function(){
		activeScrollField=id;
		scrollSpeed=speed
	}	
	setTimeout('determinScroll(\''+id+'\','+speed+','+loaded+',\''+style+'\')',10);	
}

function determinScroll(id,speed,loaded,style){
	var el = document.getElementById(id);
	var divArray = el.getElementsByTagName("div");
	el.style.display = 'block';
	for(var i=0; i<divArray.length; i++){
		if(divArray[i].id == id+"_scrollTekst"){
			
			if(!loaded){
				if(style) var style = '_'+style;
				else var style = '';
				var controls 	= '';				
				controls += '<div id="'+id+'_scrollBar" 	class="scroll_bar'+style+'"></div>';				
				controls += '<div id="'+id+'_scrollTop" 	class="scroll_up'+style+'" onMouseDown="startScroll(\''+id+'\','+speed+');"  onMouseup="stopScroll();"  onmouseout="stopScroll();"></div>';
				controls += '<div id="'+id+'_scrollPane" 	class="scroll_pane'+style+'">';				
					controls += '<div id="'+id+'_scrollPaneTop" class="scroll_pane_top'+style+'"></div>';				
					controls += '<div id="'+id+'_scrollPaneCenter" class="scroll_pane_center'+style+'"></div>';				
					controls += '<div id="'+id+'_scrollPaneBottom" class="scroll_pane_bottom'+style+'"></div>';				
				controls += '</div>';				
				controls += '<div id="'+id+'_scrollBot" 	class="scroll_down'+style+'" onMouseDown="startScroll(\''+id+'\','+(speed * -1)+');" onMouseup="stopScroll();" onmouseout="stopScroll();"></div>';						
				el.innerHTML = el.innerHTML + controls;
			}

			var divArray2 = el.getElementsByTagName("div");
			for(var j=0; j<divArray2.length; j++){
				if(divArray2[j].id == id+"_scrollTekst"){
					var el_container = divArray2[j];	
				}else if(divArray2[j].id == id+"_scrollPane"){
					var el_pane = divArray2[j];	
				}else if(divArray2[j].id == id+"_scrollPaneTop"){
					var el_paneTop = divArray2[j];	
				}else if(divArray2[j].id == id+"_scrollPaneCenter"){
					var el_paneCenter = divArray2[j];	
				}else if(divArray2[j].id == id+"_scrollPaneBottom"){
					var el_paneBottom = divArray2[j];	
				}else if(divArray2[j].id == id+"_scrollTop"){
					var el_scrollup = divArray2[j];	
				}else if(divArray2[j].id == id+"_scrollBot"){
					var el_scrolldown = divArray2[j];	
				}else if(divArray2[j].id == id+"_scrollBar"){
					var el_scrollbar = divArray2[j];	
				}
			}			
			

			var maxpos 					= -1 * (parseInt(divArray[i].offsetHeight) - parseInt(getStyle(el,'height')));				
			
			
			if(maxpos < 0){	
			el_container.style.width 	= (parseInt(getStyle(el,'width'))-parseInt(getStyle(el_scrollbar,'width'))-parseInt(getStyle(el_scrollbar,'margin-left'))) + 'px';
			el_pane.style.left 			= (parseInt(getStyle(el,'width'))-parseInt(getStyle(el_scrollbar,'width'))) + 'px';
			
				var scrollarea 	= parseInt(getStyle(el,'height')) - parseInt(getStyle(el_scrollup,'height')) - parseInt(getStyle(el_scrolldown,'height'));	
				var newsize 	= Math.round((parseInt(getStyle(el,'height')) / parseInt(divArray[i].offsetHeight)) * scrollarea);
				if(newsize <= (parseInt(getStyle(el_paneTop,'height'))+parseInt(getStyle(el_paneBottom,'height')))){
					newsize = (parseInt(getStyle(el_paneTop,'height'))+parseInt(getStyle(el_paneBottom,'height')));
				}
						
				if(getStyle(el_pane,'height') == 'auto' || parseInt(getStyle(el_pane,'height')) == (parseInt(getStyle(el_paneTop,'height'))+parseInt(getStyle(el_paneBottom,'height'))) ){
					var resizescrollpane = true;
		
					el_pane.style.height = newsize + 'px';
				}
				
				var newpaneheight = (newsize-parseInt(getStyle(el_paneTop,'height'))-parseInt(getStyle(el_paneBottom,'height')));

				if(!loaded){
					el_pane.style.top 				= parseInt(getStyle(el_scrollup,'height')) + 'px';	
					if(resizescrollpane){
						el_paneCenter.style.height 		= newpaneheight + 'px';
					}
				}else{
					mover(el_paneCenter,false,false,false,newpaneheight+'',3);					
					if(parseInt(getStyle(el_pane,'top')) > parseInt(getStyle(el,'height')) - parseInt(getStyle(el_scrollup,'height')) - parseInt(getStyle(el_pane,'height'))){
						mover(el_pane,false,parseInt(getStyle(el,'height')) - parseInt(getStyle(el_scrollup,'height')) - parseInt(getStyle(el_pane,'height')),false,false,3);		
					}else if(parseInt(getStyle(el_pane,'top')) < parseInt(getStyle(el_scrollup,'height'))){
						mover(el_pane,false, parseInt(getStyle(el_scrollup,'height')),false,false,3);		
					}
					
					if(parseInt(getStyle(el_container,'top')) < maxpos){
						el_container.style.top = maxpos + 'px'
					}					
				}
				
				Drag.init(
							el_pane,
							el_pane, 
							parseInt(getStyle(el,'width')) - parseInt(getStyle(el_pane,'width')),
							parseInt(getStyle(el,'width')) - parseInt(getStyle(el_pane,'width')),
							parseInt(getStyle(el_scrollup,'height')),
							parseInt(getStyle(el,'height')) - parseInt(getStyle(el_scrolldown,'height')) - parseInt(getStyle(el_pane,'height'))
				);
				
				
				el_scrollbar.style.visibility 	= 'visible';
				el_pane.style.visibility 		= 'visible';
				el_scrollup.style.visibility	= 'visible';				
				el_scrolldown.style.visibility 	= 'visible';								
			}else{
				el_container.style.top = '0px'
				el_scrollbar.style.visibility 	= 'hidden';
				el_pane.style.visibility 		= 'hidden';
				el_scrollup.style.visibility	= 'hidden';				
				el_scrolldown.style.visibility 	= 'hidden';												
			}
			if(loaded){
				scrollTim(id,0)
			}

		}
	}
			if(myLightbox){
				myLightbox.updateImageList();
			}	
}

function startScroll(id,speed){
	scrollTimer = setInterval('scrollTim(\''+id+'\',\''+speed+'\')',20);
}

function scrollTimTo(id,targetY){
	var divArray = document.getElementById(id).getElementsByTagName("div");

	for(var i = 0; i<divArray.length; i++){		
		if(divArray[i].id == id+"_scrollTekst"){
			var el_container = divArray[i];	
		}
	}	
	var speed = parseInt(parseInt(targetY) - parseInt(getStyle(el_container,'top')));
	scrollTim(id,speed);
}


function scrollTim(id,speed){

		var divArray = document.getElementById(id).getElementsByTagName("div");
	
		for(var i = 0; i<divArray.length; i++){		
			if(divArray[i].id == id+"_scrollTekst"){
				var el_container = divArray[i];	
			}else if(divArray[i].id == id+"_scrollPane"){
				var el_pane = divArray[i];	
			}else if(divArray[i].id == id+"_scrollTop"){
				var el_scrollup = divArray[i];	
			}else if(divArray[i].id == id+"_scrollBot"){
				var el_scrolldown = divArray[i];	
			}
		}

	if(el_container){

		if(el_scrollup.style.visibility != 'hidden'){		

			if(el_pane){

				var newheight		= parseInt(parseInt(getStyle(el_container,'top'))+parseInt(speed));
				var maxpos 			= -1 * (parseInt(el_container.offsetHeight) - parseInt(getStyle(document.getElementById(id),'height')));
				
				if(maxpos > 0){ // CONTENT GEEN SCROLL NODIG
					var maxpos = 0;
				}else{				
					if(newheight > 0) 		newheight = 0;
					if(newheight < maxpos) 	newheight = maxpos;
					mover(el_container,false, newheight + '',false,false,3);
				}

				var panepos = Math.round(
											parseInt(getStyle(el_scrollup,'height')) + 
											(
												(newheight/maxpos) * 
												(
													parseInt(getStyle(document.getElementById(id),'height')) - 
													parseInt(getStyle(el_scrollup,'height')) - 
													parseInt(getStyle(el_scrolldown,'height')) - 
													parseInt(getStyle(el_pane,'height'))
												)
											 )
										);
				mover(el_pane,false, panepos+'',false,false,3);		

			}
		}
	}
}

function updateScroll(){
	if(activeScrollField){
		var divArray = document.getElementById(activeScrollField).getElementsByTagName("div");
		id = document.getElementById(activeScrollField).id;
		
		for(var i = 0; i<divArray.length; i++){
			if(divArray[i].id == id+"_scrollTekst"){
				var el_container = divArray[i];	
			}else if(divArray[i].id == id+"_scrollPane"){
				var el_pane = divArray[i];	
			}else if(divArray[i].id == id+"_scrollTop"){
				var el_scrollup = divArray[i];	
			}else if(divArray[i].id == id+"_scrollBot"){
				var el_scrolldown = divArray[i];	
			}
		}
		if(getStyle(el_scrollup,'visibility') != 'hidden'){
			
			var maxpos 			= -1 * (parseInt(el_container.offsetHeight) - parseInt(getStyle(document.getElementById(activeScrollField),'height')));
		
			if(maxpos > 0){ // CONTENT GEEN SCROLL NODIG
				maxpos = 0;
			}	
			
			var newpos = (((parseInt(getStyle(el_pane,'top')) - parseInt(getStyle(el_scrollup,'height'))) /  (parseInt(getStyle(document.getElementById(activeScrollField),'height')) - parseInt(getStyle(el_scrollup,'height')) - parseInt(getStyle(el_scrolldown,'height')) - parseInt(getStyle(el_pane,'height')))) * maxpos);
			mover(el_container,false, newpos+'',false,false,3);	
			
		}
	}
}

var scrollTimer = false

function stopScroll(){
	clearInterval(scrollTimer);
}
