var animspeed = 800;
var animspeed_kort = 400;
var home_animdelay = 5000;
var home_animspeed = 2000;
var home_animdelay_start = 2000;
var home_to;
var scrollto;
var scrolltimeout = 10;
var tooltipto;
function show_tooltip(ref) {
	var jobj = $(ref);
	var left = jobj.position().left;
	var top = jobj.position().top;
	if ( ref.className == 'link' ) {
		left += 10;
		top += 12;
	} else {
		left += 400;
		top += 180;
	}
	jobj = $('#site').append("<div style='left:"+left+"px;top:"+top+"px;' class='tooltip'>u verlaat nu het gecertificeerde MVO jaarverslag</div>");
	if ( tooltipto != null ) clearTimeout(tooltipto);
	tooltipto = setTimeout( "$('.tooltip').fadeOut(animspeed,function(){ $('.tooltip').remove();tooltipto=null; });",3000);
}
function preload_mo() {
	var img_mo = [];
	for (var i=0; i<arguments.length; i++) {
		var ii=img_mo.length;
		img_mo[ii]=new Image;
		img_mo[ii].src='/_cms/images/'+arguments[i];
	}
}
var content_obj = {};
function pageLoaded(ref,new_maintopic) {
	content_obj = {
		subnav: $('#subnav',ref).html(),
		contents: $('#contents',ref).html(),
		id: $('#id',ref).html(),
		new_maintopic: new_maintopic
	}
	contentLoaded();
}
function contentLoaded(ref,new_maintopic) {
	if (ref && ref.getElementById) {
		content_obj = {
			subnav: ref.getElementById('subnav') ? ref.getElementById('subnav').innerHTML : '',
			contents: ref.getElementById('contents') ? ref.getElementById('contents').innerHTML : '',
			id: ref.getElementById('id') ? ref.getElementById('id').innerHTML : '',
			new_maintopic: new_maintopic
		}
	}
	if ( content_obj.new_maintopic == '' ) {
		selectHome();
	} else if ( content_obj.new_maintopic == 8 ) {
		// in cases
		selectCase();
	} else {
		// normal topic
		selectNormal();
	}
}
function selectNormal(selid) {
	var id = content_obj.id;
	if ( selid && id != selid ) return;
	if ( maintopic == '' ) {
		// komt van home
		if ( !selid ) {
			stop_animate_home();
			$('#beelden *:animated').hide(animspeed);
			$('#beelden *:visible').hide(animspeed);
			$('#topnav .text').fadeOut(animspeed,function() { selectNormal(id) });
			return;
		}
	} else if ( maintopic == '8' ) {
		// komt van case
		$('#case_contents_outer').scrollTop(0);
		if ( !selid ) {
			$('#case_blokje').fadeOut(animspeed);
			$('#beelden .beelden').css('top',130).css('height',220);
			$('#beelden *:animated').hide(animspeed);
			$('#beelden *:visible').hide(animspeed);
			$('#case_content').hide(animspeed,function() { selectNormal(id) });
			return;
		}
	} else {
		// blijft binnen Normal
		$('#contents_outer').scrollTop(0);
		if ( !selid ) {
			if (maintopic !=  content_obj.new_maintopic) {
				$('#content #b2, #content #b3, #content .scroll').fadeOut(animspeed);
				$('#content #content_inner').fadeOut(animspeed,function() { selectNormal(id); });
				return;
			}
		}
	}
	$('#content #subnav').html( content_obj.subnav );
	$('#content #contents').html( content_obj.contents );
	if ( maintopic == '' ||  maintopic == '8' ) {
		$('#content').show(animspeed,function() {
			$('#tab').show().attr( 'className', 'tab'+content_obj.new_maintopic );
		});
	} else {
		if (maintopic !=  content_obj.new_maintopic) {
			$('#content #b2, #content #b3, #content .scroll').fadeIn(animspeed);
			$('#content #content_inner').fadeIn(animspeed);
		}
		$('#tab').attr( 'className', 'tab'+content_obj.new_maintopic );
	}
	maintopic = content_obj.new_maintopic;
	$('#content .scroll').css('display',$('#contents').height() > 276 ? 'block' : 'none');
}
var caseColors = [
	'#0d1739',
	'#1689ca',
	'#c2c409',
	'#ffd10c',
	'#ee860d',
	'#b8b1ab'
];
function selectCase(selid) {
	var id = content_obj.id;
	if ( selid && id != selid ) return;
	if ( maintopic == '' ) {
		// komt van home
		if ( !selid ) {
			stop_animate_home();
			$('#topnav .text').fadeOut(animspeed);
		}
	} else if ( maintopic != '8' ) {
		// komt van normal
		$('#contents_outer').scrollTop(0);
		if ( !selid ) {
			$('#tab').hide();
			$('#content').hide(animspeed,function() { selectCase(id) });
			return;
		}
	} else {
		// blijft binnen cases
		$('#case_contents_outer').scrollTop(0);
	}
	if ( maintopic == '8' ) {
		$('#beelden'+id).css('top', 30).css('height',320);
	}
	// set new backgrounds
	$('#beelden [id!=beelden'+id+'][id!=beelden_back]:animated').hide(animspeed);
	$('#beelden [id!=beelden'+id+'][id!=beelden_back]:visible').hide(animspeed);
	$('#case_contents_outer').scrollTop(0);
	$('#case_content').hide(animspeed);
	$('#beelden'+id).show(animspeed,function() {
		$('#case_contents').html( content_obj.contents );
		$('#beelden'+id).animate({top: 30, height: 320}, animspeed);
		$('#beelden #beelden_back').show(animspeed,function() {
			$('#case_blokje').animate({'left': 130 * (id-8)}).css('backgroundColor',caseColors[id-9],animspeed_kort).show(animspeed);
			$('#case_content').css('left', 130 * (id >11 ? (id-11) : (id-7)));
			$('#case_content .scroll').css('display', $('#case_content').height() > 250 ? 'block' : 'none');
			$('#case_content').show(animspeed);
		});
	});
	maintopic = content_obj.new_maintopic;
}
function selectHome(selid) {
	var id = content_obj.id ? content_obj.id : -1;
	if ( selid && id != selid ) return;
	if ( maintopic == '' ) return;
	if ( maintopic == '8' ) {
		// komt van case
		$('#case_contents_outer').scrollTop(0);
		if ( !selid ) {
			$('#case_blokje').fadeOut(animspeed);
			$('#beelden .beelden').css('top',130).css('height',220);
			$('#beelden *:animated').hide(animspeed);
			$('#beelden *:visible').hide(animspeed);
			$('#case_content').hide(animspeed,function() { selectHome(id) });
			return;
		}		
	} else {
		// komt van normal
		$('#contents_outer').scrollTop(0);
		if ( !selid ) {
			$('#tab').hide();
			$('#content').hide(animspeed,function() { selectHome(id) });
			return;
		}
	}
	$('#topnav .text').fadeIn(animspeed);
	$('#beelden #beelden0').show(animspeed,function() {
		home_anim_current = 9;
		home_to = setTimeout( "start_animate_home();",home_animdelay_start);
	});
	maintopic = '';
}
function start_animate_home() {
	$('#beelden *:animated').fadeOut(home_animspeed);
	$('#beelden *:visible').fadeOut(home_animspeed);
	$('#beelden #beelden'+home_anim_current).fadeIn(home_animspeed);
	$('#case_blokje').animate({'left': 130 * (home_anim_current-8)},home_animspeed).css('backgroundColor',caseColors[home_anim_current-9]).show();

	if ( ++home_anim_current > 14 ) home_anim_current = 9;
	home_to = setTimeout( "start_animate_home();",home_animdelay);
}
function stop_animate_home() {
	if (home_to) {
		clearTimeout(home_to);
		home_to = null;
	}
}
function start_scroll(content, speed) {
	var code = "$('"+content+"').scrollTop($('"+content+"').scrollTop() + "+speed+");start_scroll('"+content+"',"+speed+");";
	scrollto = setTimeout(code,scrolltimeout);
}
function stop_scroll() {
	if ( scrollto==null) return;
	clearTimeout(scrollto);
	delete(scrollto);
}


// xAjax Stuff
function submitForm(e, additional_cf) {
	
	if (e && e.tagName == 'FORM') {
		
		if(!ValidateForm(e)) return false;
		
		var formElements = e.elements;
		var target_cf = formElements['target_cf'] ? formElements['target_cf'].value : additional_cf;
		var wait_button = formElements['wait_button'].value;
		
		if(target_cf) {
			
			var chksum = location.hostname;
			formElements['checksum'].setAttribute("value",chksum);
			
			if (xajax.$(wait_button)) {
				xajax.$(wait_button).disabled=true;
				xajax.$(wait_button).value="Even geduld aub...";
			}
			xajax.call(target_cf, new Array(xajax.getFormValues(e)), 1);
			
		} else {
			alert('submitForm: No valid arguments');
		}
	} else {
		alert('submitForm: No valid FORM object');
	}
	return false;
}


function ValidateForm(e) {
	var msg = '';
	var chk = '';
	var focusfld = '';
	var i,ii,x;
	var rclist = [];
	var val = '';
	for(i=0;i<e.length;i++) {
		if (chk = e[i].getAttribute('inpchk')) {
			e[i].className = e[i].className.replace('mandatory','');
			if ((chk == 'notempty')) {
				if (e[i].type == 'checkbox') {
					val = (e[i].checked) ? '1' : '';
				} else {
					val = e[i].value;
				}
				if (val == '') {
					if (focusfld == '') focusfld = e[i].name;
					msg += e[i].getAttribute('chkmsg')+"\n";
					e[i].className += ' mandatory';
				}
			}
			if ((chk == 'minmaxlen')) {
				var min = e[i].getAttribute('minlen');
				var max = e[i].getAttribute('maxlen');
				if (e[i].value.length < min) {
					msg += e[i].getAttribute('chkmsg')+"\n";
					if (focusfld == '') focusfld = e[i].name;
					e[i].className += ' mandatory';
				}
				if (e[i].value.length > max) {
					msg += e[i].getAttribute('maxmsg')+"\n";
					if (focusfld == '') focusfld = e[i].name;
					e[i].className += ' mandatory';
				}
			}
			if ((chk == 'radiovalue')) {
				if (rclist[e[i].name] == 1) continue;
				rclist[e[i].name] = 1;
				x = e[e[i].name];
				if (!!x[0]) {
					for(ii=0;ii<x.length;ii++) {
						x[ii].className = x[ii].className.replace('mandatory','');
					}
					val = false;
					for(ii=0;ii<x.length;ii++) {
						if (x[ii].checked) val = true;
					}
					if (!val) {
						for(ii=0;ii<x.length;ii++) {
							x[ii].className += ' mandatory';
						}
						if (focusfld == '') focusfld = i;
						msg += e[i].getAttribute('chkmsg')+"\n";
					}
				}
			}
		}
	}
	if (msg) {
		alert(msg);
		e[focusfld].scrollIntoView(false);
		e[focusfld].focus();
		return false;
	}
	return true;
}


