var basehref; // set with jQuery $('base').attr('href');

var ShBoxOptions = {
		language:				'nl',
		counterType:			'skip',//'default',//'skip',
		overlayOpacity:	   		'0.6',
		//handleUnsupported:  	'remove',
		//onFinish:				galleryElemDesc,
		initialHeight:			'450',//'160',
		initialWidth:			'550',//'320',
		resizeDuration:			'0.2',//'0.55',
		fadeDuration:			'0.15',//'0.35',
		animSequence:			'sync',//'wh',
		handleOversize:			'resize',//'drag',
		continuous:				true,
		flvPlayer:				'gui/media/mediaplayer2.swf'
};
// Shadowbox start
Shadowbox.init(ShBoxOptions);

// init & start
$(function() {

	// set base url var
	basehref = $('base').attr('href');
	
	// geen javascript == geen ui-prefs
	$('.javascriptui').css({display:'inline-block'});
	
	// pngfix
	$(document).pngFix(); 
	
	// searchfield
	initsearchfield();
	
	// media
	initmedia();
	
	// faq
	initFaq();
});

// contrastSwitch
$(function() {
	
	var cookieName = cprefix+'contrast';
	var cookieOptions = { path: '/', expires: 10 };
	
	var contrast = 0;
	
	// cookiewaarde?
	try {
		var CookieContrast = $.cookie(cookieName);
		if(CookieContrast && CookieContrast != null && CookieContrast != 'NaN'){
			contrast = CookieContrast;
		} 
	} catch(err){
		$.cookie(cookieName,contrast,cookieOptions);
	}
	
	if(contrast == 1){
		$(".contrast").html($(".contrast").html().replace('Zwart-wit ','Normale kleur'));
		$('body').addClass('highcontrast');
	}
	
	// Contrast ui
	$(".contrast")
	.click(function(){
		
		if(contrast == 1){
			contrast = 0;
			$(this).html($(this).html().replace('Normale kleur','Zwart-wit '));
			
			$('body').removeClass('highcontrast');
		} else {
			contrast = 1;
			$(this).html($(this).html().replace('Zwart-wit ','Normale kleur'));
			$('body').addClass('highcontrast');
		}
		
		$.cookie(cookieName,contrast,cookieOptions);
		
		return false;
	});
	
	
});

// fontSizer
$(function() {
	
	var increment = fontincr;
	var fontBase = 100; // $('html').css('fontSize');
	
	var cookieName = cprefix+'fontsize';
	var cookieOptions = { path: '/', expires: 10 };
	
	var fontPerc = fontBase;
	
	// cookiewaarde?
	try {
		var CookiefontPerc = $.cookie(cookieName);
		if(CookiefontPerc && CookiefontPerc != null && CookiefontPerc != 'NaN'){
			fontPerc = CookiefontPerc;
		} 
	} catch(err){
		$.cookie(cookieName,fontPerc,cookieOptions);
	}
	
	if(fontPerc <= fontBase){
		$(".smaller").hide();
	} else {
		$('html')
		.css({
			'fontSize': fontPerc+'%',
			'lineHeight': fontPerc+'%'
		});
	}
	
	// Decrease Font Size
	$(".smaller")
	.click(function(){
	
		fontPerc = (fontPerc /1) - (increment /1);
		scaleFont();
		return false;
	});
	
	// Increase Font Size
	$(".bigger").click(function(){
	
		fontPerc = (fontPerc /1) + (increment /1);
		scaleFont();
		return false;
	});
	
	function scaleFont(){
		
		$('html')
		.css({
			'fontSize': fontPerc+'%',
			'lineHeight': fontPerc+'%'
		});
		
		if(fontPerc == fontBase){
			$(".smaller").hide();
		}
		
		if(fontPerc > fontBase){
			$(".smaller").show();
		}
		
		$.cookie(cookieName,fontPerc,cookieOptions);
	}
	
	
});

/*
// functions
*/
function initsearchfield(){
	
	$('#searchform form')
  	.each(function(){
  	
		label = $('td.searchlabel label',$(this)).hide().text();
		
		var txt = label.replace(/^\s\s*/,'').replace(/\s\s*$/,'');
		var theform = $(this);
		var theinput = $('td input.search',$(this));
		var val = $(theinput).val();
		
		if(val == ''){
			$(theinput).val(txt);
		}
		
		$(theinput)
		.mouseover(function(){
			val = $(theinput).val();
			if(val == txt){
				$(theinput)
				.animate({ 
					"opacity":0.0
				},'fast','',function(){
					$(theinput)
					.val('')
					.animate({
						"opacity":1.0
					},1)
					.focus()
					.select();
				});
			} else {
				$(theinput)
				.focus()
				.select();
			}
		})
		.mouseout(function(){
			val = $(theinput).val();
			if(val == ''){
				$(theinput)
				.stop()
				.val(txt);
			}
		})
		.focus(function(){
			$(theinput)
			.unbind("mouseout")
			.select();
		})
		.mouseup(function(){
			$(theinput).unbind("mouseout");
			$(theinput).select();
		})
		.blur(function(){
			val = $(theinput).val();
			if(val == ''){
				$(theinput).val(txt);
			}
			
			$(theinput).mouseout(function(){
				val = $(theinput).val();
				if(val == ''){
					$(theinput).val(txt);
				}
			});
		});
		
	});
	
	// Add this behavior to all text fields
	$("input[type=text]").focus(function(){
		// Select field contents
		this.select();
	});
	
}

// searchfield showlabel
function field_showlabel(theinput){
	
	var val = $(theinput).val();
	
	$(theinput)
	.stop();
	 
	if(val == ''){
		
		$(theinput)
		.animate({ 
			"opacity":'1.0'
		},'fast','',function(){
			$(this)
			.blur()
			.val(label);
		});
	}
}


// Faq
function initFaq(){
	
	var anchors = $('#pagecontent a[name]:not([href])');
	
	if( anchors.length > 3){
		
	}

}

// media
function initmedia(){
	
	var theHolder = 'div.video p';
	var bg = $('a',$(theHolder)).attr('rel');
	
	//window.console.log( bla );
	
	$.fn.media.defaults.flvPlayer = basehref+'gui/media/flvplayer.swf';
	
	$('a',$(theHolder)
		.css({
			'backgroundImage':"url('"+bg+"')",
			'backgroundRepeat':'no-repeat'
		})
	)
	.media({ 
		width:  	434,//430, //4
		height: 	343,//343, //3 + 21 controlbar
		autoplay:	false, 
		src:		$(this).attr('href'),
		bgColor:	'transparent', // background color
		caption:	false, // supress caption text
		attrs:		{ 
						autoplay:  false, 
						autostart:  false//, 
						//controlbar: 'none' 
					},  // object/embed attrs 
		params: 	{ 
						autoplay: false, 
						autostart: false//, 
						//controlbar: 'none' 
					}//, // object params/embed attrs 
	}); 
	
	
	//$('div.media p div embed')
	//.css('backgroundColor','#ff9600')
	//.attr('bgcolor','#ff9600');
}
