// gives up and down scroll buttons to images, spans, ... named up_name, down_name, respectively.
// will keep the default scroll_box's style overflow if it encounters errors (so make overflow: auto;)

// set global var for Scroll collaboration between scrollwheel function and click function
var scrollPositie = 0;

// usage: put this after the scrollbox div:  var div_scroll1 = new TextScroll('div_scroll1', 'scroll_box');
function TextScroll(scrollname, div_name, up_name, down_name, visible_height) {
    this.div_name = div_name;
    this.name = scrollname;
    scrollPositie = 0;
    this.speed = 10;
    this.timeoutID = 0;
    this.div_obj = null;
    this.up_name = up_name;
    this.dn_name = down_name;
    this.vis_height = visible_height + 20; 

    {	
        if (document.getElementById) {
        	div_obj = document.getElementById(this.div_name);

       		if (div_obj) {
       			this.div_obj = div_obj;
       			this.div_obj.style.overflow = 'hidden';
       		}

       		// check visible height against real height
       		//alert('tekst hoogte: ' + this.div_obj.scrollHeight + ' vak hoogte: '+ this.vis_height);
       		if ( visible_height && this.div_obj.scrollHeight > this.vis_height) {
        	
	            div_up_obj = document.getElementById(this.up_name);
	            div_dn_obj = document.getElementById(this.dn_name);
	            if (div_up_obj && div_dn_obj) {
					div_up_obj.onmouseover = function() { eval(scrollname + ".scrollUp();") };
					div_up_obj.onmouseout = function() { eval(scrollname + ".stopScroll();") };
					
					div_dn_obj.onmouseover = function() { eval(scrollname + ".scrollDown();") };
					div_dn_obj.onmouseout = function() { eval(scrollname + ".stopScroll();") };
	            }
        	}
        	else {
        		document.getElementById('prev_next_news').style.display = "none";
        	}
        }
    }

this.stopScroll = function() {
        clearTimeout(this.timeoutID);
    }

this.scrollUp = function() {
        if (this.div_obj) {
            scrollPositie = (scrollPositie - this.speed) < 0 ? 0 : scrollPositie - this.speed;
            this.div_obj.scrollTop = scrollPositie;
            this.timeoutID = setTimeout(this.name + ".scrollUp()", 60);
        }
    }

this.scrollDown = function() {
		if (this.div_obj) {
			scrollPositie += this.speed;
			this.div_obj.scrollTop = scrollPositie;
			if (this.div_obj.scrollTop == scrollPositie) {
				this.timeoutID = setTimeout(this.name + ".scrollDown()", 60);
			} else {
				scrollPositie = this.div_obj.scrollTop;
			}
		}
	}

this.resetScroll = function() {
        if (this.div_obj) {
            this.div_obj.scrollTop = 0;
            scrollPositie = 0;
        }
    }
}

/**
 * functies voor scrollen met een muiswiel
 * 
 */
window.onload = function() {
    //adding the event listener for Mozilla
    if(window.addEventListener)
        document.addEventListener('DOMMouseScroll', moveObject, false);
 
    //for IE/OPERA etc
    document.onmousewheel = moveObject;

}

function moveObject(event) {
	
    var delta = 0;
 
    if (!event) event = window.event;
 
    // normalize the delta
    if (event.wheelDelta) {
 
        // IE and Opera
        delta = event.wheelDelta / 60;
 
    } else if (event.detail) {
 
        // W3C
        delta = -event.detail / 2;
    }
    
    // check which element
    var elm = null;
    if( document.getElementById('cv_content')) {
    	elm = 'cv_content';
    }
    if( document.getElementById('news_content')) {
    	elm = 'news_content';
    }
    if( document.getElementById('fiftyPct_news_content')) {
    	elm = 'fiftyPct_news_content';
    }
    if( document.getElementById('twentyfivePct_news_content')) {
    	elm = 'twentyfivePct_news_content';
    }
    
    if( elm ) {
	    var currPos=document.getElementById(elm).scrollTop;
	    //calculating the next position of the object
	    currPos=parseInt(currPos)-(delta*10);
	    //alert(currPos);
	 
	    //moving the position of the object
	    if(currPos >= 0) {
	    	document.getElementById(elm).scrollTop = currPos;
	    	scrollPositie = currPos;
	    }
	    else {
	    	document.getElementById(elm).scrollTop = 0;
	    	scrollPositie = 0;	    	
	    }
    }
}

/**
 * showHideDivs
 * @return
 */
function fotoShow(n,dir) {
	
	var current = 0;
	// first check which one is visible
	for( i=0; i<n; i++ ) {
		if ( document.getElementById('werk_img'+i).style.visibility == 'visible') {
			// make it invisible
			document.getElementById('werk_img'+i).style.visibility = "hidden";
			
			// check direction
			if ( dir == 'prev' ) {
				current = i-1;
			}
			else {
				current = i+1;
			}

			// show next or first
			if ( current >= n ) {
				current = 0;
			}
			if ( current < 0 ) {
				current = n-1;
			}
			document.getElementById('werk_img'+current).style.visibility = 'visible';
			document.getElementById('work_numerator').innerHTML = parseInt(current + 1) + '/' + n;
			break;
		}
	}
	
}

/**
 * set caption nicely under clipped photo
 * 
 * @param n integer number of images
 * @param a integer height of container
 * @return void
 */
function setCaptionPos(n,a) {
	
	for( i=0; i<n; i++) {
		// check for existence of elements
		if(document.getElementById('cap'+i) && document.getElementById('werkimg'+i) ) {
			// measure height of photo
			var h = document.getElementById('werkimg'+i).offsetHeight;
			// if img is higher than allowed height 'a', set it to allowed height
			var cappos = h > a ? a : h;
			//alert(cappos);
			document.getElementById('cap'+i).style.top = parseInt(cappos) + 'px';
			//alert(document.getElementById('cap'+i).style.top);
		}
	}
}

function showHideNews(deze) {
	
	var elm_small	= document.getElementById('item_'+deze+'_small');
	var elm_big		= document.getElementById('item_'+deze+'_big');
	var readmore	= document.getElementById('readmore_link_'+deze);
	
	if(elm_small.style.display == 'block') {
		elm_small.style.display = 'none';
		elm_big.style.display = 'block';
		readmore.innerHTML = 'read&nbsp;less';
	}
	else {
		elm_small.style.display = 'block';
		elm_big.style.display = 'none';
		readmore.innerHTML = 'read&nbsp;more';
	}
}
