/**
 * file default.js
 *
 * contains javascript for PME website
 *
 * @require jQuery version 1.3.2
 */



$(document).ready(function(){	
	initChangeFontSize();
	initSearchForm();
	initButtonPME();
	initCarousel();
	initReadMoreLinks()
	initFAQ();
	initNieuwsoverzicht();
	initDownloads();
	initBegrippen();
	initSubmitButtons();
});


$(window).load(function(){

});



/**
 * initSearchForm
 *
 * Submit search, show/hide search hint text &
 * add/remove searchHint class on load, focus & blur
 *
 * @author Gijs Oliemans <gijs.oliemans{AT}efocus.nl>
 * @return void
 */
function initSearchForm() {
	if ($('form.zoeken input').length < 1) return;
	
	if($('form.zoeken input').val() == ''){
		$('form.zoeken input').val($('.zoekenHint').text());
		$('form.zoeken input').addClass('zoekenHintActive');
	}else if($('form.zoeken input').val() == $('.zoekenHint').text()){
		$('form.zoeken input').addClass('zoekenHintActive');
	}

	$('form.zoeken input').focus(function(){
		if($(this).val() == $('.zoekenHint').text()){
			$(this).val('');
			$(this).removeClass('zoekenHintActive');
		}
	});
	
	$('form.zoeken input').blur(function(){
		var curZoekString = $.trim($(this).val());
		if(curZoekString == ''){
			$(this).val($('.zoekenHint').text());
			$(this).addClass('zoekenHintActive');
		}
	});
	
	$('form.zoeken a.button').click(function(){
		if($(this).prev('div').children('input').val() != $('.zoekenHint').text()){
			$(this).parent('fieldset').parent('form').trigger('submit');
		}
	});

}



/**
 * initButtonPME
 *
 * Hide PME "button dropdown" and create a dropdown onlick when more than 1 link
 *
 * @author Gijs Oliemans <gijs.oliemans{AT}efocus.nl>
 * @return void
 */
function initButtonPME() {
	if($('a.button_inloggen').length < 1) return;
	
	
	if($("ul.pme_inloggen").children().length > 1){
		$('a.button_inloggen').click(function(){
			//$('ul.pme_inloggen').slideToggle('fast'); // Doesn't work in #@$!% IE8 due to a terrible bug
						
			if($('ul.pme_inloggen').css('display') == 'none'){
				if($.browser.msie && $.browser.version.substring(0,1) == 8){
					$('ul.pme_inloggen').show();
				}else{
					$('ul.pme_inloggen').slideDown('fast');
				}
			}
			else{
				$('ul.pme_inloggen').slideUp('fast');
			}
			
			this.blur();
			return false;
		});
	}else{
		var just1Link = $("ul.pme_inloggen li a").attr('href');
		$('a.button_inloggen').attr('href', just1Link);
	}
}






/**
 * initCarousel
 *
 * Initialise carousel
 * 
 * @author Gijs Oliemans <gijs.oliemans{AT}efocus.nl>
 * @require jquery.cycle.js
 * @return void
 */
function initCarousel(){

	if($('#f5-carousel').length < 1) return;
		
	$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
		$(pager).find('li').removeClass('car_control_active')
			.filter('li.car_control_'+currSlideIndex).addClass('car_control_active');
	}; 
	
	$('#carousel_slides').cycle({ 
		fx:     'fade', 
		speed:  '1500', 
		pager:  '#carousel_controls', 
		before: function(currSlide, nextSlide, opts) {
			// Set timeout for individual slides
			var index = $(this).parent().children().index(this) + 1;
			var currTimeOutValue = parseInt($('#carousel_slides li:nth-child('+ index +')>a').attr('rel'));
			opts.timeout = !currTimeOutValue ? 3000 : currTimeOutValue;
		} 
	});

	$('#carousel_controls').children().each(
			 
		function( intIndex ){			
			$(this).addClass('car_control_' + intIndex);
			
			$(this).bind("click",function(){
				$('#carousel_controls li').removeClass('car_control_active');	
				$('#carousel_controls>a:eq('+ intIndex +')').trigger('click'); 	
				$(this).addClass('car_control_active');			
			});			
		}
	 
	);
		
	// Hide pager links
	$('#carousel_controls>a').hide();
	// Start with first control active
	$('li.car_control_0').addClass('car_control_active');
	
};



/**
 * initNieuwsoverzicht
 *
 * Show/hide news sections
 * 
 * @author Gijs Oliemans <gijs.oliemans{AT}efocus.nl>
 * @require function tabControl()
 * @return void
 */
function initNieuwsoverzicht(){
	if($('#f14_nieuwsoverzicht').length < 1) return;
	
	tabControl('#f14_nieuwsoverzicht');

}



/**
 * initReadMoreLinks
 *
 * Show/hide readmore paragraphs
 * 
 * @author Gijs Oliemans <gijs.oliemans{AT}efocus.nl>
 * @return void
 */
function initReadMoreLinks(){
	if($('div.f11_pagina-kop_contentbox').length < 1 || $('div.readmore').length < 1) return;
	
	$('div.readmore').hide();

	$("a.readmore").click(function(){
		$(this).parent('p').next('div.readmore').slideToggle("slow");
		this.blur();
		return false;
	});	
	
}



/**
 * initDownloads
 *
 * Show/hide downloads sections
 * 
 * @author Gijs Oliemans <gijs.oliemans{AT}efocus.nl>
 * @require function tabControl()
 * @return void
 */
function initDownloads(){
	if($('.f17_downloads').length < 1) return;
	
	tabControl('.f17_downloads');

}



/**
 * initFAQ
 *
 * Show/hide FAQ's onclick
 * 
 * @author Gijs Oliemans <gijs.oliemans{AT}efocus.nl>
 * @return void
 */
function initFAQ(){

	if($('#f16_faq').length < 1) return;
	
	$("#f16_faq dd").hide();
	
	$("#f16_faq dt").bind("click", function() {
		$('this').children('a').blur();
		$("#f16_faq dd:visible").slideUp('fast');
		$("#f16_faq dt").removeClass('faq_active');		
		if($(this).next('dd').is(':hidden')){
			$(this).addClass('faq_active');
		}					
		$(this).next('dd:hidden').slideDown('fast');
		return false;
	});	
	
}



/**
 * initChangeFontSize
 *
 * Increase, decrease or reset font-size and save setting in a cookie
 * 
 * @author Jonny Kamaly
 * @editor Gijs Oliemans <gijs.oliemans{AT}efocus.nl>
 * @require jquery.cookie.js
 * @return void
 */
function initChangeFontSize() {
		if($('.fontSize').length < 1) return;
	
		var $cookie_name = "fontSize";
		var originalFontSize = $("body").css("font-size");
		
		// if exists load saved value, otherwise store it
		if($.cookie($cookie_name)) {
			var $getSize = $.cookie($cookie_name);
			if($getSize<11)$(".fontSizeDec").addClass('act');
      if($getSize=='11px')$(".fontSizeReset").addClass('act');
      if($getSize>11)$(".fontSizeInc").addClass('act');
			$("body").css({fontSize : $getSize + ($getSize.indexOf("px")!=-1 ? "" : "px")});
		} else {
			$.cookie($cookie_name, originalFontSize);
		}
		
		// font reset link
		$(".fontSizeReset").bind("click", function() {
			this.blur();
			$("div.f1_header ul li.fontSize a").removeClass('act');
			$(".fontSizeReset").addClass('act');
			$("body").css("font-size", originalFontSize);
			$.cookie($cookie_name, originalFontSize);
		});
		
		// font increase link
		$(".fontSizeInc").bind("click", function() {
			this.blur();
			$("div.f1_header ul li.fontSize a").removeClass('act');
			$(".fontSizeInc").addClass('act');
			//var currentFontSize = $("body").css("font-size");
			var currentFontSize = 11; // hardcoded to allow to only enlarge once
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*1.2;
			if (newFontSize, 11) {
				$("body").css("font-size", newFontSize);
				$.cookie($cookie_name, newFontSize);
			}
			return false;	
		});
		
		// font decrease link
		$(".fontSizeDec").bind("click", function() {
			this.blur();
			$("div.f1_header ul li.fontSize a").removeClass('act');
			$(".fontSizeDec").addClass('act');
		  //var currentFontSize = $("body").css("font-size");
		  var currentFontSize = 11; // hardcoded to allow to only decrease once
		  var currentFontSizeNum = parseFloat(currentFontSize, 10);
		  var newFontSize = currentFontSizeNum*0.8;
		  if (newFontSize, 11) {
		    $("body").css("font-size", newFontSize);
		    $.cookie($cookie_name, newFontSize);
		  }
		  return false;
		});
}



/**
 * begrippen/faq onderwerpen
 *
 * open/close each begrip
 * also used for the categories of FAQ's
 * 
 * @author Bas van der Togt <bas.vander.togt{AT}efocus.nl> 
 * @editor Gijs Oliemans <gijs.oliemans{AT}efocus.nl>
 * @return void
 */
function initBegrippen() {
	if($('.begrippenlijst').length < 1 && $('#f16_faq').length < 1) return;
	
	
	$(".toggle_container").hide();

	$("h2.trigger").toggle(function(){
		$(this).addClass("active"); 
	}, function () {
		$(this).removeClass("active");
	});
	
	$("h2.trigger").click(function(){
		$(this).next(".toggle_container").slideToggle("slow");
		$(this).children('a').blur();
	});

}



/**
 * 
 *
 * Automagically make buttons/links with class="submit" submit their parent form
 * 
 * @author Gijs Oliemans <gijs.oliemans{AT}efocus.nl>
 * @return void
 */
function initSubmitButtons() {
	
	$('a.submit').click(function(){
		$(this).parent("fieldset").parent("form").trigger("submit");
		return false;
	});
	
}


/**
 * tabControl
 *
 * Show tab-content depending on clicked tab
 * 
 * @author Gijs Oliemans <gijs.oliemans{AT}efocus.nl>
 * @param el; parent html element class or id (e.g. '#downloads' or ".downloads") 
 * @return void
 */
function tabControl(el){
	
	$(el+' div.tabcontainer div.tabcontent').hide()
	$(el+' div.tabcontainer div.tabcontent:eq(0)').show();
			
	$(el+" ul.tabs li").each(			 
		function( intIndex ){
			$(this).bind("click",function(){
				$(el+" ul.tabs li").removeClass('active');
				$(this).addClass('active');
				$(el+' div.tabcontainer div.tabcontent').hide();
				$(el+' div.tabcontainer div.tabcontent:eq('+ intIndex +')').show();
				$(this).children('a').blur();	
			});
		}
	);
	
}
