var waitInterval=6000;  //this is the interval between each fade
var fadeSpeed=2000;    //this is the speed of the fade action
var timer;

$(function(){
	var $menu = $('ul.sf-menu').superfish({
		//animation: { width:'show' },
		delay: 350,
		onBeforeShow: function() {
			this.parent().parent().children('li').each(function(i) { $(this).css('z-index',100-i); });
			this.children('li').width(this.width());
			if ( !this.hasClass('mainsub') ) {
				this.css({left:this.parent().parent().width()});
			}
		}
	});
	$menu.find("ul").each(function(i){
		$(this).hover(
			function(){ $(this).parent("li").children("a").addClass("pselected"); },
			function(){ $(this).parent("li").children("a").removeClass("pselected"); }
		);
	});	

	
	// font replacement
	Cufon.replace('h1');
	Cufon.replace('h2');
	Cufon.replace('h3');
	Cufon.replace('h4', { hover: true });
	
	// handlers
	$(".searchbox input").click( function() {
		if ( $(this).hasClass('clicked') ) return;
		this.value='';
		$(this).addClass('clicked')
	});
	
	// fading
	fade();
	
	// p-issues
	$('.p-inline p:last-child').css({'margin-bottom':0,'display':'inline'});

});
 
function fade( fs ){
	if (fs==null) fs = fadeSpeed;
	
	if (timer) clearTimeout(timer);
	var $sel = $('#fader .selected');
	$sel.removeClass("selected").fadeOut(fs);
	
	$sel = $sel.next();
	if ( $sel.length == 0 ) $sel = $('#fader .imageHolder:first');

	$sel.addClass("selected").fadeIn(fs);

	timer = setTimeout("fade()", waitInterval);
}

function login_status(frm_status, frm_error) {
	if (frm_status) {
		location.reload();
	} else {
		alert(frm_error);
	}
}
logout_status = login_status;

var save_button;
var save_button_label;

function submitForm(e, additional_cf) {
	if (e && e.tagName == 'FORM') {
		
		if(!checkForm(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 ? formElements.wait_button.value : '';
		
		if(target_cf) {
			
			var chksum = location.hostname;
			formElements.checksum.setAttribute("value",chksum);

			save_button = xajax.$(wait_button);
			if ( save_button ) {
				save_button_label = save_button.value;
				save_button.disabled=true;
				save_button.value="One moment...";
			}
			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 enable_submit_button() {
	if (save_button) {
		save_button.disabled=false;
		save_button.value=save_button_label;
	}
}
function show_status(status,errormsg,link) {
	enable_submit_button();
	if (status) {
		var ref = xajax.$('form_content');
		if (ref) ref.style.display = 'none';
		ref = xajax.$('form_result');
		if (ref) ref.style.display = 'block';
		if (link) {
			//$('#regdwnl').attr('href',link);
			if ($.browser.msie) {
				//$('#regdwnl')[0].click();
				window.location.href=location.href+"&dlok=1";
			} else {
				window.location.href=link;
			}
		}
	} else {
		alert(errormsg);
	}
	window.scrollTo(0,0);
}
function showTable(id,cnt) {
	var outer=xajax.$(id+'_outer');
	var table=xajax.$(id);
	var body=xajax.$(id+'_body');
	for (var i=0; i<body.rows.length; i++) body.rows[i].style.display= (i<cnt) ? '' : 'none';
	if (table.offsetHeight >400) {
		outer.style.height=400+'px';
		outer.style.overflow='scroll';
	} else {
		outer.style.height=table.offsetHeight+'px';
		outer.style.overflow='';
	}
}
function checkForm(frm) {
	var e = frm.elements;
	var msg = '';
	var chk = '';
	var focusfld = '';
	var i;
	for(i=0;i<e.length;i++) e[i].className = e[i].className.replace('mandatory','');
	for(i=0;i<e.length;i++) {
		if (chk = e[i].getAttribute('inpchk')) {
			var el=e[i];
			if (chk == 'notempty') {
				var value=el.value;
				if (el.type=='radio') {
					var rcol=e[el.name];
					value=false;
					for(ii=0;!value && ii<rcol.length;ii++) value=rcol[ii].checked;
					if (!value) for(ii=0;ii<rcol.length;ii++) rcol[ii].className += ' mandatory';
				} else {
					if (value == '') {
						value=false;
						el.className += ' mandatory';
					}
				}
				if (!value) {
					if (focusfld == '') focusfld = i;
					msg += el.getAttribute('chkmsg')+"\n";
				}
			}
		}
	}
	if (msg) {
		alert(msg);
		e[focusfld].scrollIntoView(false);
		e[focusfld].focus();
		return false;
	}
	e['checksum'].setAttribute("value",location.hostname);
	return true;
}
function prtwnd() {
	var url=location.href;
	if (url.substr(-1)=='/') url+='print/';
	else url+='/print/';
	var pwnd = window.open(url,"prtwnd");
	if (!pwnd) alert('Het is niet mogelijk om een printwindow te openen. Heeft u mogelijk popup-windows geblokkeerd?');
}
