var el = document.getElementById("textbody");
var elY = 0;
var doScroll = false;
var upDown;

function scroll() {
	if (doScroll == true) {
		if (upDown == "up") {
			if (elY < 0) {
				elY = elY + 3;
			}
			document.getElementById("textbody").style.top = elY + "px"
		} else {
			elY = elY - 3;
			document.getElementById("textbody").style.top = elY + "px"
		}
		setTimeout("scroll()", 25);
	}
}

function startScroll(direction) {
	doScroll = true;
	upDown = direction;
	scroll();
}

function stopScroll() {
	doScroll = false;
}

function setTarget(target) {
	locationHref = location.href;
	var thissite = new Array();
	thissite = locationHref.split('/');
	thissite = thissite[2];
	
	l = document.links
	for(a = 0; a < l.length; a++) {
		lnk = l[a];
		linkhost = lnk.hostname;
		
		if(thissite != linkhost) {
			lnk.target = target;
		}
	}
}


// Random images
var number_images = 42;
var min_delay = 3000;
var max_delay = 5000;

var block_1;
var block_2;
var block_3;
var block_4;
var block = new Array(1,2,3,4);
var div = new Array();
var visible_images = new Array();
var between_delay = max_delay - min_delay;

function init_rimages() {
	div[1] = document.getElementById('block_top_1');
	div[2] = document.getElementById('block_top_2');
	div[3] = document.getElementById('block_menu_1');
	div[4] = document.getElementById('block_menu_2');
	
	// Construct the array with visible images
	for (i = 0; i <= number_images; i++) {
		visible_images['Beeld_'+i+'.jpg'] = false;
	}
	
	// Set images for the containers by default
	get_random_ri(1)
	set_random_ri(block[1], true);
	style_ri(div[1], block[1]);
	
	get_random_ri(2)
	set_random_ri(block[2], true);
	style_ri(div[2], block[2]);
	
	get_random_ri(3)
	set_random_ri(block[3], true);
	style_ri(div[3], block[3]);
	
	get_random_ri(4)
	set_random_ri(block[4], true);
	style_ri(div[4], block[4]);
	
	// Set timeout for first change
	setTimeout("set_ri()", delay_ri());
}

function delay_ri() {
	return rand_ri(between_delay) + min_delay;
}

function style_ri(el, st) {
	el.style.backgroundImage = 'url(/images/'+st+')';
}

function get_random_ri(b) {
	var rand = rand_ri(number_images);
	
	if (visible_images['Beeld_'+rand+'.jpg'] != true) {
		block[b] = 'Beeld_'+rand+'.jpg';
	} else {
		get_random_ri(b);
	}
}

function set_random_ri(key, state) {
	visible_images[key] = state;
}

function rand_ri(limit) {
	var rand_unrounded = Math.random() * (limit + 1);
	return Math.floor(rand_unrounded);
}

function set_ri() {
	// Choose a block
	var use_block = rand_ri(3) + 1;
	
	// Unset previous image
	set_random_ri(block[use_block], false);
	
	// Get random image
	var use_image = get_random_ri(use_block);
	set_random_ri(block[use_block], true);
	
	// Show new image
	style_ri(div[use_block], block[use_block]);
	
	// Set timeout for next change
	setTimeout("set_ri()", delay_ri());
}
