var in_ie_hell;
var V3 = true;
window.addEvent('domready', function() {

	if((document.body.id=='home') && ($('home_slideshow')!=null)) { 
		HOME_SLIDESHOW_init();
		trigger_subscriber();
		if(V3==false) { init_scroll_bar(); }
	}
	if($('news_ticker')!=null) {
		make_fading_slideshow($('news_ticker'), 'div');
	}
	if($('page_slideshow')!=null) {
		make_fading_slideshow($('page_slideshow'), 'div.slideshow_slide');
	}
	insert_press_login_form();
	if(Browser.Engine.trident==true) { 
		in_ie_hell=true; 
		delete_swf_on_unload();
	}
	if($$('.direct_editable').length>0) {
		init_direct_edit();
	} else if($('resident_edit_profile')!=null) {
		init_direct_edit();
	}
	init_image_enlargements();

	// BACK LINK
	if($('back_link')!=null) {
		$('back_link').addEvent('click', function() {
			history.back();
			return false;
		});
	}
	check_content_types_adder();
	check_resident_galleries();
	check_fan_form();
	if(V3==true) { 
		//menu_tips();
		home_res_preview();
	}
});

var home_slideshow, home_slideshow_text_link, home_slideshow_img_link, home_slideshow_img, num_slides, slide_delay, page_header, home_slideshow_fade_props;
var slide_index = 0;
var slide_interval = 4000;
var slide_fade_speed = 500;
var english_version = false;
var lang_url_base = 'NL/';

function HOME_SLIDESHOW_init() {
	home_slideshow = $('home_slideshow');
	home_slideshow_text_link = $('home_slideshow_text_link');
	home_slideshow_img_link = $('home_slideshow_img_link');
	home_slideshow_img = $('home_slideshow_img');
	page_header = $('page_header');
	num_slides = home_slides.length;
	slide_delay = HOME_SLIDESHOW_show_next_slide.delay(slide_interval);
	var cur_href = window.location.toString();
	if(cur_href.indexOf('/ENG')!=-1) { 
		english_version = true; 
		lang_url_base = 'ENG/';
	}
	// alert('num_slides = '+num_slides);
	
	// SET FADE PROPERTIES
	home_slideshow_fade_props = {
		property: 'opacity',
		duration: slide_fade_speed,
		transition: 'linear',
		link: 'chain'
	};	
}
function HOME_SLIDESHOW_show_next_slide() {
	if((slide_index+1)<num_slides) {
		slide_index++;
	} else {
		slide_index = 0;
	}
	clearTimeout(slide_delay);
	var header_name = home_slides[slide_index][0];
	var main_img_src = home_slides[slide_index][1];
	var header_img_src = home_slides[slide_index][2];
	var slide_link = home_slides[slide_index][5];
	if(english_version==true) {
		if(home_slides[slide_index][3]!='') { header_name = home_slides[slide_index][3]; }
		if(home_slides[slide_index][4]!='') { header_img_src = home_slides[slide_index][4]; }
	}
	
	// LOAD MAIN
	var main_img_loader= new Image();
	main_img_loader.onload=function() { 
		// LOAD HEADER
		var header_img_loader= new Image();
		header_img_loader.onload=function() { 
			header_img_loaded = true; 
			// FADE CUR OUT, VERANDER, DAN FADE IN
			var slideshow_img_fade = new Fx.Tween('home_slideshow', home_slideshow_fade_props); 
			var slideshow_header_fade = new Fx.Tween('page_header', home_slideshow_fade_props); 
			slideshow_header_fade.start(1, 0);
			slideshow_img_fade.start(1, 0).chain(
			    function(){ 
					// laat veranderingen zien
					home_slideshow_img.src = main_img_src;
					page_header.src = header_img_src;
					home_slideshow_text_link.innerHTML = header_name;
					home_slideshow_text_link.href = lang_url_base+slide_link;
					home_slideshow_img_link.href = lang_url_base+slide_link;
					home_slideshow_img_link.title = header_name;
					// FADE IN
					slideshow_header_fade.start(0, 1);
					slideshow_img_fade.start(0, 1);
					// RETRIGGER
					slide_delay = HOME_SLIDESHOW_show_next_slide.delay(slide_interval);
			    }
			);
		}
		header_img_loader.src = header_img_src;
	}
	main_img_loader.src = main_img_src;
}

///////////////////////////////////////////////////////////////////////
///////////// MENU TIP IMAGES /////////////////////////////////
/////////////////////////////////////////////////////////////////////
function menu_tips() {
	///*
	var menu_tip_links = $$('a.menu_image');
	menu_tip_links.each(function(menu_tip, index) {
		menu_tip.store('tip:text', '<img src="'+menu_tip.rel+'" />');
	});
	var menu_tips = new Tips(menu_tip_links,{
		className: 'tip_menu',
		fixed: false,
		hideDelay: 50,
		showDelay: 50
	});
//	menu_tips.setText('<img src="'+this.rel+'" />');
	//*/
}
function home_res_preview() {
	var res_previews = $$('a.has_preview');

	if(res_previews.length>0) {
		var resident_names = [];
		var res_preview_index = -1;
		
		res_previews.each(function(res_preview, index) {
			res_preview.store('tip:text', '<img src="'+res_preview.rel+'" />');
			res_preview.store('his_large_img', res_preview.getProperty('rel').replace('-TH.jpg', '.jpg'));
			res_preview.store('his_index', index);
			resident_names[index] = res_preview.title;
		});
		var resident_tips = new Tips(res_previews,{
			className: 'tip_menu',
			fixed: false,
			hideDelay: 50,
			showDelay: 50
		});
		// RESIDENT PREVIEW BIJ HOME
		if($('res_preview')!=null) {
			var res_preview_div = $('res_preview');
			var res_preview_img = $('res_preview_large');
			var res_preview_name_EL = $('res_preview_name');
			var blanco_img = res_preview_img.src;
			
			res_previews.addEvent('mouseover', function() {
				var this_res_preview_index = this.retrieve('his_index');
				if(this_res_preview_index!=res_preview_index) {
					// EEM RESETTEN
					res_preview_img.src=blanco_img;
					res_preview_div.addClass('res_preview_filled');
					// Naam
					var res_name = resident_names[this.retrieve('his_index')];
					res_preview_name_EL.innerHTML = res_name;
					// Plaatje
					var res_img_large = this.retrieve('his_large_img');//this.getProperty('rel').replace('-TH.jpg', '.jpg');
					res_preview_img.src=res_img_large;
				}
			});
			res_previews.addEvent('mouseout', function() {
				//res_preview_div.removeClass('res_preview_filled');
				//res_preview_img.src=blanco_img;
			});
		}
	}
}
///////////////////////////////////////////////////////////////////////
///////////// SUBSCRIBER /////////////////////////////////
/////////////////////////////////////////////////////////////////////
function trigger_subscriber() {
	if($('subscribe_holder')!=null) {
		var subscribe_holder_EL = $('subscribe_holder');
		var subscribe_opener = $('subscribe_nb');
		var subscribe_closer = $('close_subscribe_nb');
		
		subscribe_holder_EL.setStyle('display', 'none');
		subscribe_opener.addEvent('click', function() {
			subscribe_holder_EL.setStyle('display', 'block');
			return false;
		});
		subscribe_closer.addEvent('click', function() {
			subscribe_holder_EL.setStyle('display', 'none');
			return false;
		});
	}
}

///////////////////////////////////////////////////////////////////////
///////////// FADING SLIDESHOW /////////////////////////////////
/////////////////////////////////////////////////////////////////////
var news_change_interval = 5000;
var fade_speed = 100;
var news_ticker_fade_speed = 500;
var fade_delay;
var auto_play;
function make_fading_slideshow(slideshow_holder, element_tag) {
	var slideshow_holder_id = slideshow_holder.id;
	var slides = $$('#'+slideshow_holder_id+' '+element_tag);
	var num_slides = slides.length;

	slideshow_holder.his_slides = slides; 
	slideshow_holder.num_slides = num_slides; 
	slideshow_holder.cur_index = 0;
	
	slides.each(function(slide, index) {
		slide.store('his_index', index);
		slide.store('his_z_index', num_slides);
		slide.setStyles({
			position: 'absolute',
			top: 0,
			left: 0,
			'z-index': num_slides
		});
		num_slides--;
	});

	// CONTROL BUSINESS  / FADE
	slideshow_holder.store('fading', false);
	
	var slideshow_controls = $$('#'+slideshow_holder_id+' a.slider_control');
	if(slideshow_controls.length>0) {
		if(slideshow_holder.hasClass('autoplay')) {
			output('Auto Play: '+slideshow_holder_id)
			slideshow_holder.store('auto_play', true);
		} else {
			slideshow_holder.store('auto_play', false);
		}
		slideshow_controls.each(function(slide_control, index) {
			if(slide_control.hasClass('prev_slide')) {
				slide_control.addEvent('click', function() {
					var show_is_fading = slideshow_holder.retrieve('fading');
					if(show_is_fading==false) {
						fade_slide_V2(slideshow_holder, 'prev');
					}
					return false;
				});			
			} else if(slide_control.hasClass('next_slide')) {
				slide_control.addEvent('click', function() {
					var show_is_fading = slideshow_holder.retrieve('fading');
					if(show_is_fading==false) {
						fade_slide_V2(slideshow_holder, 'next');
					}
					return false;
				});
			}
		});
	} else {
		slideshow_holder.store('auto_play', true);
	}
	// ZET DE DELAY AAN ALS AUTOPLAY IS
	if(slideshow_holder.retrieve('auto_play')==true) {
		slideshow_holder.store('fade_delay', fade_slide_V2.delay(news_change_interval, slideshow_holder));	
	}
}

function fade_slide_V2(active_slideshow, slideshow_direction) {
	if(active_slideshow==undefined) {
		active_slideshow = this;
	}
	active_slideshow.store('fading', true);
	if(slideshow_direction==undefined) {
		slideshow_direction = 'next';
	}
	if(active_slideshow.id=='news_ticker') {
		active_slideshow.store('this_fade_speed', news_ticker_fade_speed);
	} else {
		active_slideshow.store('this_fade_speed', fade_speed);
	}
	var NEXT_reset_top = false;
	var PREV_reset_top = false;
	var cur_index = active_slideshow.cur_index;
	var slides = active_slideshow.his_slides;
	var num_slides = active_slideshow.num_slides;
	var next_index;
	var index_to_fade;

	if(slideshow_direction=='next') {
		var top_opacity_start = 1;
		var top_opacity_end = 0;
		if((cur_index+1)<num_slides) {
			NEXT_reset_top = false;
			next_index = cur_index+1;
		} else {
			NEXT_reset_top = true;
			next_index = 0;
			top_opacity_end = 1;
		}	
		// RESET INBETWEENERS ALS INDEX: 0 IS
		if(next_index==1) { NEXT_reset_inbetweeners(slides); }
		index_to_fade = cur_index;
	} else if(slideshow_direction=='prev') {
		if(cur_index==0) {
			PREV_reset_inbetweeners(slides);
			var top_opacity_start = 1;
			var top_opacity_end = 0;
			next_index = num_slides - 1;
			index_to_fade = cur_index;
		} else {
			var top_opacity_start = 0;
			var top_opacity_end = 1;
			next_index = cur_index - 1;					
			index_to_fade = next_index;
		}
	
	}
	// if(active_slideshow.id=='page_slideshow') {	output('cur_index = '+cur_index+' | next_index = '+next_index); }

	var fade_top = new Fx.Tween(slides[index_to_fade], {property: 'opacity', duration: active_slideshow.retrieve('this_fade_speed')});
	fade_top.start(top_opacity_start, top_opacity_end).chain(
			function(){ 
				if(NEXT_reset_top==false) {
					if(slideshow_direction=='next') {
						active_slideshow.cur_index++;
					} else {
						if(cur_index==0) {
							active_slideshow.cur_index=num_slides - 1;
						} else {
							active_slideshow.cur_index--;
						}
					}
				} else {
					active_slideshow.cur_index = 0;
				}
				active_slideshow.store('fading', false);
				var this_auto_play = active_slideshow.retrieve('auto_play');
				if(this_auto_play==true) {
					clearTimeout(active_slideshow.retrieve('fade_delay'));
					active_slideshow.store('fade_delay', fade_slide_V2.delay(news_change_interval, active_slideshow));
				}
			}
	);
	if((NEXT_reset_top==true) && (slides[next_index].getStyle('opacity')==0)) {
		//output('NEXT_reset_top | next_index = '+next_index);
		var fade_bottom = new Fx.Tween(slides[next_index], {property: 'opacity', duration: active_slideshow.retrieve('this_fade_speed')});
		fade_bottom.start(0, 1);
	} 
}
function NEXT_reset_inbetweeners(slides) {
	// ZET TUSSENLIGGENDE SLIDES WEER OP 1
	slides.each(function(slide, slide_index) {
		if(slide_index>0) {
			slide.setStyle('opacity', 1);
		}
	});
}
function PREV_reset_inbetweeners(slides) {
	// ZET TUSSENLIGGENDE SLIDES WEER OP 1
	slides.each(function(slide, slide_index) {
		if((slide_index>0) && (slide_index<(slides.length-1))) {
			slide.setStyle('opacity', 0);
		} else if(slide_index==(slides.length-1)) {
			slide.setStyle('opacity', 1);
		}
	});
}
var output_div_available;
var output_div;
function output(string) {
	if(output_div_available==undefined) {
		if($('output')!=null) {
			output_div_available=true;
			output_div = $('output');
		} else {
			output_div_available=false;		
		}
	}
	if(output_div_available==true) {
	//$('output').appendText("<br />"+string);	
		var cur_content = output_div.innerHTML;
		output_div.innerHTML = cur_content+'<br />'+string;
	}
	return false;
}

function check_resident_galleries() {
	if($$('a.resident_gallery').length>0) {
		$$('a.resident_gallery').each(function(pic_gallery, index) {
			var gal_data = pic_gallery.id.split('-');
			var gal_type = gal_data[0];
			var gal_id = gal_data[1];

			gal_images = resident_galleries[gal_id].images;
			gal_captions = resident_galleries[gal_id].captions;
			gal_ws = resident_galleries[gal_id].img_widths;
			gal_hs = resident_galleries[gal_id].img_heights;
			var another_image_gallery = new modal_gallery({
				gallery_link: pic_gallery,
				gallery_images: gal_images,
				gallery_captions: gal_captions,
				gallery_img_ws: gal_ws,
				gallery_img_hs: gal_hs
			});
		});
	}
}

//////////////////////////////////////////////////////////////////
/////////////////// MODAL EN AJAX STUFF //////////////////////////
//////////////////////////////////////////////////////////////////

function insert_press_login_form() {
	var press_login_links = $$('.press_login');
	press_login_links.addEvent('click', load_press_login);
}
var login_insert_id;
var load_press_login = function () {
	// SHOW LOADER
	link_insert_loader(this);
	var cur_href = escape(this.href);
	login_insert_id = this.rel;
	load_file = '_inc/_content_readers/content_ajax.inc.php?part=press_login&ref_url='+cur_href;
	// MAKE ZE CALL
	sendRequest(load_file, insert_press_login);
	return false;
}

var modal_to_show = '';
var modal_pic_height=0;
var image_to_enlarge;

function show_modal(the_link) {
	// SHOW LOADER
	link_insert_loader(the_link);
	// LOAD FILE
	// INSERT MODAL HOLDING HTML
	insert_modal_html();
	// MODAL ID
	modal_to_show = 'rabk_modal';
	var load_file = the_link.href;
	// MAKE ZE CALL
	sendRequest(load_file, handleRequest);
}

function init_image_enlargements() {
	var large_img_links = $$('.enlarge_image');
	large_img_links.addEvent('click', function() {
		enlarge_image(this);
		return false;
	});
}

function enlarge_image(the_link) {
	// SHOW LOADER
	link_insert_loader(the_link);
	// INSERT MODAL HOLDING HTML
	insert_modal_html();
	// LOAD FILE
	image_to_enlarge = the_link.href;
	// MODAL ID
	modal_to_show = 'enlarge_img';
	
	// PRELOAD IMAGE
	var load_img = new Image();
	load_img.onload = function() {
		var his_width = load_img.width;
		var his_height = load_img.height;
		var html_to_show='<div id="enlarge_img" style="width:'+his_width+'px"class="page_modal"><a name="close_werk_modal" id="close_modal"><img src="'+image_to_enlarge+'" id="modal_enlarge_image" border="0" width="'+his_width+'" height="'+his_height+'" /></a></div>';
		var container = document.getElementById('modal_holder');
		container.innerHTML = html_to_show;

		hide_inserted_loader();
		launch_modal(modal_to_show);
	}
	load_img.src=image_to_enlarge;
}

function insert_msg_modal(the_link, msg) {
	// SHOW LOADER
	link_insert_loader(the_link);
	// INSERT MODAL HOLDING HTML
	insert_modal_html();
	// MODAL ID
	modal_to_show = 'de_vorm_message';
	
	var html_to_show='<div id="de_vorm_message" class="page_modal"><a name="close_werk_modal" id="close_modal" class="close_modal"></a><hr class="clear" style="height: 10px;">'+msg+'</div>';
	
	var container = document.getElementById('modal_holder');
	container.innerHTML = html_to_show;

	hide_inserted_loader();
	launch_modal(modal_to_show);
}

function insert_modal_html() {
	// INSERT HTML AT TOP OF PAGE
	var body_tag = document.getElementsByTagName("body").item(0);
	// -- MODAL
	var page_modal = document.createElement("div");
	page_modal.setAttribute('id','modal_holder');
	body_tag.insertBefore(page_modal, body_tag.firstChild);
	// -- OVERLAY
	var overlay_div = document.createElement("div");
	overlay_div.setAttribute('id','overlay');
	body_tag.insertBefore(overlay_div, page_modal.nextSibling);
}
///////// SHOW LOADING /////////
var active_link;
function toggle_opacity(link, state) {
	active_link = link;
	var linked_img=link.getElement('img');

//	var linked_img = link.childNodes[1];
	if(state=='full') {
		opacity_val=1;
	} else {
		opacity_val=0.5;
	}
	if(in_ie_hell==false) {
		linked_img.style.opacity = opacity_val;
	} else {
		linked_img.style.filter = 'alpha(opacity=' + opacity_val*100 + ')';
	}
}
var active_loading_link;
var active_loading_link_html;
var mix_loader_html = ' <img src="_img/ajax_loader.gif" alt="Loading..." border="0" />';

function link_insert_loader(the_link) {
	active_loading_link = the_link;
	active_loading_link_html = the_link.innerHTML;
	the_link.innerHTML = active_loading_link_html + mix_loader_html;
}
function hide_inserted_loader() {
	if((active_loading_link!='') && (active_loading_link!=null)) {
		active_loading_link.innerHTML = active_loading_link_html;
	} 
}

/* -----------------------------------------------------
	INSTANT EDIT CALLBACK FUNCTIONS
 -----------------------------------------------------*/
function handleRequest(req) {
	var html_to_show = req.responseText;
	var container = document.getElementById('modal_holder');
	container.innerHTML = html_to_show;
	hide_inserted_loader();
	launch_modal(modal_to_show);
}
function insert_press_login(req) {
	var html_to_show = req.responseText;
	if($(login_insert_id)!=null) {
		$(login_insert_id).innerHTML = html_to_show;
	}
	active_loading_link.innerHTML = '&gt; Login';
	active_loading_link.removeEvent('click', load_press_login);
	active_loading_link.onclick=function() { return false; }
}
/* -----------------------------------------------------
	XMLHTTP Functions
	As found on Quirksmode. You hero.
 -----------------------------------------------------*/

function sendRequest(url,callback,postData) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
		//	alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

function XMLHttpFactories() {
	return [
		function () {return new XMLHttpRequest()},
		function () {return new ActiveXObject("Msxml2.XMLHTTP")},
		function () {return new ActiveXObject("Msxml3.XMLHTTP")},
		function () {return new ActiveXObject("Microsoft.XMLHTTP")}
	];
}

function createXMLHTTPObject() {
	var xmlhttp = false;
	var factories = XMLHttpFactories();
	for (var i=0;i<factories.length;i++) {
		try {
			xmlhttp = factories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}
/* -----------------------------------------------------
	SET OVERLAY STUFF
 -----------------------------------------------------*/
var array_page_size;
var overlay_div;
var scroll_x, scroll_y, window_w, window_h;


function get_page_size(){
	var scroll_position = window.getScroll();
	scroll_x = scroll_position.x;
	scroll_y = scroll_position.y;

	var body_size =  $(document.body).getScrollSize();
	body_w = body_size.x;
	body_h = body_size.y;
	
	var window_size = window.getSize();
	window_w = window_size.x;
	window_h = window_size.y;
	
	// for small pages with total height less then height of the viewport
	if(body_h < window_h){
		page_h = window_h;
	} else { 
		page_h = body_h;
	}

	// for small pages with total width less then width of the viewport
	if(scroll_x < window_w){	
		page_w = window_w;
	} else {
		page_w = body_w;
	}
	array_page_size = new Array(page_w, page_h, window_w, window_h);
	return array_page_size;
}

function show_overlay(overlay_height_int) {
	var overlay_height = overlay_height_int + 'px';
	overlay_div.setStyle('height', overlay_height);
	overlay_div.setStyle('display', 'block');
	overlay_div.setStyle('opacity', '0');
	overlay_div.fade(0, 1);
}
var overflow_hidden = false;
var direct_editing = false;
function launch_modal(modal_to_display) {

	array_page_size=get_page_size();

	var target_modal_div = $(modal_to_display);
	// ZET OP ZICHTBAAR
	target_modal_div.setStyle('opacity', '0');
	target_modal_div.style.display = "block";
	//  MODAL DIMENSIONS
	var modal_size = target_modal_div.getSize();
	var modal_div_w = modal_size.x;
	var modal_div_h = modal_size.y;
	
	var page_height = array_page_size[1];
	var overlay_height = page_height;
	if(modal_div_h>page_height) {
		overlay_height = modal_div_h;
	}	

	// CENTER THE MODAL AND MAKE SURE LEFT AND TOP VALUES ARE NOT NEGATIVE
	var modal_div_x = Math.round(((array_page_size[2] - modal_div_w) / 2));
	var modal_div_y = Math.round(scroll_y + ((array_page_size[3] - modal_div_h) / 2));

	target_modal_div.style.top = (modal_div_y < 0) ? "0px" : modal_div_y + "px";
	target_modal_div.style.left = (modal_div_x < 0) ? "0px" : modal_div_x + "px";

 	// OVERLAY
	overlay_div = $('overlay');
	show_overlay(overlay_height);
	target_modal_div.fade(0, 1);
	
	// ACTIVATE GALLERY
	if($('modal_gallery')) {
		activate_gallery('modal_gallery');
	}
	// ENLARGE IMAGE BUSINESS	
	if(modal_to_display=='de_vorm_enlarge_img') {
		
	}
	// RESIDENT EDITING DOCUMENT OVERFLOW HIDDEN ZETTEN
	if((document.body.id=='resident') && (direct_editing==true)) {
		document.body.setStyle('overflow', 'hidden');
		overflow_hidden = true;
	}
	// CLOSE BUSINESS
	var close_link = $('close_modal');
	if(close_link) {
		close_link.onclick=function() {
			close_cur_modal();
		}
	}
	overlay_div.onclick=function() {
		close_cur_modal();
	}
}
var div_fader = new Fx;

function close_cur_modal() {
	if (document.getElementById) {
		// REMOVE THE DYNAMIC DIVS
		if ($(modal_to_show)) { 	
			var modal_to_show_to_del = $(modal_to_show);
			div_fader = new Fx.Tween(modal_to_show_to_del, { property: 'opacity', duration: 150 } ).start(1, 0);
		}
		if ($('overlay')) { 	
			var overlay = $('overlay');
			div_fader = new Fx.Tween(overlay, { property: 'opacity', duration: 150 } ).start(1, 0).chain(
				function() {  
				  	document.body.removeChild(overlay);
			  		var modal_holder = $('modal_holder');
				  	document.body.removeChild(modal_holder);
				  	if(overflow_hidden==true) {
			  			document.body.setStyle('overflow', 'auto');
			  			direct_editing = false;
						overflow_hidden = false;
					}
				}
			);
		}
	}
}
function start_key_functions() {
 	document.onkeydown = check_key;
}
function check_key(event) {
	// IE hack
	if (window.event) { event = window.event; }
	var keycode = event.keyCode;
	var escapeKey;
	if (event.DOM_VK_ESCAPE) {  // mozilla
		escapeKey = event.DOM_VK_ESCAPE;
	} else { // ie
		escapeKey = 27;
	}
	var key = String.fromCharCode(keycode).toLowerCase();
	if (keycode == escapeKey){ // close lightbox
			close_cur_modal();
	}
}
start_key_functions();


function delete_swf_on_unload() {
	window.addEvent ('beforeunload', function() {
		var swf_holders = $$('video_holder');
		// alert('weg halen anders faalt IE ZO HARD!')
		swf_holders.each(function() {
			this.dispose();
		});
	}); 
}
////////////////////////////////////////////////////////////////
//////////////////// HOME SCROLLBAR ////////////////////
////////////////////////////////////////////////////////////
var el_to_scroll, scroll_up, scroll_down;
var div_scrolling = false;
var scroll_step = 20;
function init_scroll_bar() {
	el_to_scroll = $('rabk_residents');
	var scroll_up = $('scroll_up');
	var scroll_down = $('scroll_down');
	scroll_up.addEvent('mousedown', function() {
		div_scrolling=true;
		scroll_div(-1*scroll_step);	
	});
	scroll_down.addEvent('mousedown', function() {
		div_scrolling=true;
		scroll_div(scroll_step);	
	});
	// STOP SCROLLING
	scroll_up.addEvent('mouseup', stop_scrolling_div);
	scroll_down.addEvent('mouseup', stop_scrolling_div);
	scroll_up.addEvent('mouseout', stop_scrolling_div);
	scroll_down.addEvent('mouseout', stop_scrolling_div);
	// STOP CLICK
	scroll_up.addEvent('click', function() { return false; });
	scroll_down.addEvent('click', function() { return false; });
}
var scroll_delay;
var active_scroll_step;

function scroll_div(cur_scroll_step) {
	output('- scrolling baby, yeah...');
	if(cur_scroll_step==undefined) {
		cur_scroll_step = active_scroll_step;
	} else {
		active_scroll_step = cur_scroll_step;
	}
	el_to_scroll.scrollTop+=cur_scroll_step;
	scroll_delay = setTimeout('scroll_div()',100); // scrolls every 100 milliseconds
	// CHECK WANNEER MOUSE UP IS
	document.body.addEvent('mouseup', stop_scrolling_div);
}

function stop_scrolling_div() {
	output('CHECK of ie moet stoppen met scrollen! div_scrolling='+div_scrolling);
	if(div_scrolling==true) {
		output('stop eens met scrollen!');
		div_scrolling = false;
		clearTimeout(scroll_delay);
		// CLEAR EVENT
		document.body.removeEvent('mouseup', stop_scrolling_div);
	}
}

function check_fan_form() {
	if($('fan_option_bank')!=null) {
		var fan_option_bank_radio = $('fan_option_bank');
		var fan_option_cc_radio = $('fan_option_cc');
		var fan_option_bank_form_el = $('fan_option_bank_form');
		if(fan_option_bank_form_el.hasClass('opened')==false) {
			fan_option_bank_form_el.setStyle('display', 'none');
		}
		fan_option_bank_radio.addEvent('click', function() {
			fan_option_bank_form_el.setStyle('display', 'block');
		});
		fan_option_cc_radio.addEvent('click', function() {
			fan_option_bank_form_el.setStyle('display', 'none');
		});
	}
}

////////////////////////////////////////////////////////////////
//////////////////// VALIDATE BUSINESS ////////////////////
////////////////////////////////////////////////////////////

function validate_login(theform) {
	var login_email=theform.RABK_login_email;
	if(!default_check(login_email)) { return false; }
	if (echeck(login_email.value)==false){
		login_email.focus()
		return false;
	}
	var login_pass=theform.RABK_login_pass;
	if(!default_check(login_pass)) { return false; }
}
function validate_email(theform) {
	var login_email=theform.RABK_user_email;
	if(!default_check(login_email)) { return false; }
	if (echeck(login_email.value)==false){
		login_email.focus()
		return false;
	}
}
function validate_nb_subscribe(theform) {
	if(!default_check(theform.subscriber_name)) { return false; }
	if(!default_check(theform.subscriber_mail)) { return false; }
	if (echeck(theform.subscriber_mail.value)==false){
		theform.subscriber_mail.focus()
		return false;
	}
	return true;
}

function validate_search(theform) {
	if(!default_check(theform.search_val)) { return false; }
	return true;
}

var default_border_color = '#000000';
function default_check(input) {
	if ((input.value==null)||(input.value=="")){
    	input.style.borderColor="#ff0000";
    	input.style.backgroundColor='#ffc2c2';
		input.focus();
		return false;
	} else {
        input.style.borderColor=default_border_color;
    	input.style.backgroundColor='#ffffff';
        return true;
	}
}
function validate_new_slide(theform) {
	var file_to_upload = theform.file1.value;
	if(file_to_upload!='') {
		if(!default_check(theform.slideshow_image_name)) { return false; }
		if(!default_check(theform.slideshow_image_name_eng)) { return false; }
	}
	return true;
}
function validate_new_content(theform) {
	// CONTENT TYPE
	var content_type_options=theform.content_type;
    var content_type_options = validate_radio_button(content_type_options);
    if (content_type_options == null) {
        return false;
    } else if(content_type_options=='textfield') {
		if(!default_check(theform.text_field_name)) { return false; }
    } else if(content_type_options=='image') {
		if(!default_check(theform.file1)) { return false; }
    } else if(content_type_options=='video') {
		if(!default_check(theform.video_url)) { return false; }
    } else if(content_type_options=='serie_name') {
		if(!default_check(theform.serie_title)) { return false; }
    }
	return true;
}
function validate_new_serie_image(theform) {
	// UPLOAD 
	if(!default_check(theform.file1)) { return false; }
	return true;
}
function validate_fan_1(theform) {
	var amount_field;
	var minimum_val = 15;
	
	var interval_val = validate_radio_button(theform.payment_interval);
	if(interval_val == null) {
		alert('Please select your donation interval: once, monthly or annual.')
		return false;
	}
	if(interval_val=='once') {
		amount_field = theform.once_amount;
	} else if(interval_val=='monthly') {
		amount_field = theform.monthly_amount;
		minimum_val = 5;
	} else if(interval_val=='year') {
		amount_field = theform.year_amount;
	}
	if(!default_check(amount_field)) { return false; }
	if(!check_val_is_numeric(amount_field)) { 
		alert('Please enter numbers only');
		amount_field.value='';
		amount_field.focus();
		return false;
	}
	if(amount_field.value<minimum_val) { 
		alert('The minimum amount is '+minimum_val); 
		amount_field.value='';
		amount_field.focus();
		return false; 
	}
	if(!default_check(theform.first_name)) { return false; }
	if(!default_check(theform.name)) { return false; }
	if(!default_check(theform.address)) { return false; }
	if(!default_check(theform.postcode_city)) { return false; }
	if(!default_check(theform.country)) { return false; }
	var login_email=theform.email;
	if(!default_check(login_email)) { return false; }
	if (echeck(login_email.value)==false){
		login_email.focus()
		return false;
	}
	var payment_option_chosen = validate_radio_button(theform.payment_option);
	if(payment_option_chosen == null) {
		alert('Please select your method of payment.')
		return false;
	}
	if(payment_option_chosen=='bank') {
		if(!default_check(theform.account_name)) { return false; }
		if(!default_check(theform.account_number)) { return false; }
		if(!check_val_is_numeric(theform.account_number)) { 
			alert('Please enter numbers only');
			theform.account_number.value='';
			theform.account_number.focus();
			return false;
		}
	} else if(payment_option_chosen=='credit_card') {
		if(interval_val!='once') {
			alert('You can only make a one time donation when you pay with a credit card');
			theform.payment_interval[0].checked = true;
			if(amount_field.value<15) {
				amount_field.value='';
				theform.once_amount.value='';
				theform.once_amount.focus();
				return false;
			}
			return false;
		}
	}
	
	return true;
}

function validate_radio_button(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {
            cnt = i; i = -1;
        }
    }
    if (cnt > -1) { 
        return btn[cnt].value;
    } else {
        return null;
    }
}

function check_val_is_numeric(input_element){
	var numeric_expression = /^[0-9]+$/;
	if(input_element.value.match(numeric_expression)){
		return true;
	}else{
		return false;
	}
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid e-mail adress")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid e-mail adress")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid e-mail adress")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid e-mail adress")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid e-mail adress")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid e-mail adress")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid e-mail adress")
		    return false
		 }
 		 return true					
}
/***** * script voor de show/hide divs  ****/
function toggle_div(whichLayer)    {
    if (document.getElementById) {
        // this is the way the standards work
        var target_div = document.getElementById(whichLayer);
        var cur_display = target_div.style.display;
        if(cur_display=='block') {
        	target_div.style.display = 'none';
        	// CHECK OF ER EEN IMAGE_BANK IN OPEN STAAT, DAN VERWIJDEREN
        	if(beeldbank_container!=undefined) {
        		beeldbank_container.innerHTML='';
        	}
        } else {
        	target_div.style.display = 'block';
	    }
    } 
}

/* ----------------------------------
CONTENT TYPES TOGGLING
------------------------------------- */
function toggle_div_array(div_array, index_to_show) {
	for(var i=0;i<div_array.length;i++) {
		var cur_display;
		if(i==index_to_show) {
			cur_display='block';
		} else {
			cur_display='none';		
		}
		div_array[i].style.display=cur_display;
	}
}

function check_content_types_adder() {
	var toggle_links = $$('.show_more_options');
	var toggled_content = $$('.more_options');
	init_toggling(toggle_links, toggled_content);
}
function init_toggling(triggers, content) {
	if(triggers.length>0) {
		for(var i=0; i<triggers.length;i++) {
			// divs op onzichtbaar
			if(content[i]) {
				content[i].style.display='none';	
			}
			triggers[i].his_index = i;
			triggers[i].onclick=function() {
				toggle_div_array(content, this.his_index);
			}
		}
	}
	
}
///////// SCROLL TO ACTIVE ALBUM ////////////
function check_active_content() {
	if($('active_serie')!=null) {
		var EL_content_listing = $('scrolling_videos');
		var EL_active_item = $('active_serie');
		var scroll_to_active = new Fx.Scroll(EL_content_listing, {duration: 1}).toElement(EL_active_item);
	}
}
