// innerlabel
$(function(){
  $('.mainsearch input, .panel-jobsearch input, input.innerlabel').innerLabel();
  
  if( $('#tab-vacancy').length > 0 ){
    $('#tab-vacancy').infoTabs({
      tabContentClass: 'tabs-container',
      cancelLinkAction: true
    });
  }
  
   if( $('#tab-columnist').length > 0 ){
    $('#tab-columnist').infoTabs({
      tabContentClass: 'tab-content',
      cancelLinkAction: true
    });
  }
  
  if( $('#searchbanner div.tabs').length > 0 ){
    $('#searchbanner div.tabs').infoTabs({
      tabContentClass: 'tab-content',
      cancelLinkAction: true
    });
  }

  if( $('#MeerArtikelenContainer').length > 0 ){
    $('#MeerArtikelenContainer').infoTabs({
      tabContentClass: 'tabs-container',
      cancelLinkAction: true
    });
  }
  
  for(var i=0; i<11; i++) { 
     if( $('#tab-vacatureresult-pos'+i).length > 0 ){
		  $('#tab-vacatureresult-pos'+i).infoTabs({
			tabContentClass: 'tabs-vacatureresult-container',
			cancelLinkAction: true
		  });
	  }
  }
  
  $('#searchbanner ul.tabs li a').mouseover(function() {
  		if( $(this).parent('li').hasClass('active') ) return;
	    var lefty = $(this).find('span');
	    lefty.stop().animate({left: -10},250);
	  }
	 ).mouseout(function() {
	  	if( $(this).parent('li').hasClass('active') ) return;
	    var lefty = $(this).find('span');
	    lefty.stop().animate({left: -lefty.outerWidth()},250);
	  }
  ).click(function() {
  		$(this).find("span").css({left:"auto"});
  		var newleft = $('#searchbanner ul.tabs li:first a span').outerWidth();
  		$('#searchbanner ul.tabs li a').not(this).find('span').stop().animate({left: -newleft},250)
  });
  
  	// shows and hide select fields in search form
  	$('.rowtoggler .row.hide').removeClass('hide').hide();
  
	$('.rowtoggler :button.plus_button').live('click', function(){
		$(this).closest('.rowtoggler').find('.row:hidden:first').show();
	});
  	$('.rowtoggler :button.min_button').live('click', function(){
		$(this).closest('.row').hide().find('select option:first').attr('selected', 'selected');
	});
  
  
  /**
  * function to expand the read more link in a h2
  *
  */
   $('.rule .toggler a').click(function(){
  	if( $(this).parents('div:first').hasClass('closed') ){
  		$(this).parents('div:first')
	  		.removeClass('closed')
				.addClass('open')
				.find('.foldable').slideDown(250);
				return false;
	  	}
	  	else {
	  		$(this).parents('div:first')
	  		.removeClass('open')
				.addClass('closed')
				.find('.foldable').slideUp(250);
				return false;
	  	}
		});
		
	/**
  * function to unfold the mail form in the thesis/rule block
  *
  */
   $('.rule .emphasis a.toggle').click(function(){
  	if( $('.emphasis .foldable').hasClass('closed') ){
  		$('.emphasis .foldable')
	  		.removeClass('closed')
				.slideDown(250);
				$(this).parents('.emphasis')
				.removeClass('closed')
				.addClass('open');
				return false;
	  	}
	  	else {
	  		$('.emphasis .foldable')
	  		.addClass('closed')
	  		.slideUp(250);
	  		$(this).parents('.emphasis')
				.removeClass('open')
				.addClass('closed');
				return false;
	  	}
		});
		
		
		/** 
		 * function mouseover foundation table
		 * this function gives the title a mouseover when hovering the logo image.
		*/
		$('table.foundations tr img').mouseover(function(){
			$(this).parents('tr:first')
			.find('h4 a')
			.css('border-bottom', '1px dotted #169AE9');
		});
		$('table.foundations tr img').mouseout(function(){
			$(this).parents('tr:first')
			.find('h4 a')
			.css('border-bottom', 'none');
		});


		/** 
		 * function vote
		 * this function animates the colored bars after visitors vote
		*/

	$('form.voteform').each(function(i, elem){
		var btn_agree = $(elem).find('.true');	
		var btn_disagree = $(elem).find('.false');
		
		// op welk vlak wordt geklikt?
  		btn_agree.click(function(){
  			vote('svar1', elem);
  			$(this).find('span').addClass('active');
  		});
  		btn_disagree.click(function(){
  			vote('svar2', elem);
  			$(this).find('span').addClass('active');
  		});							 
	});
  	
  	function vote(choice, form){
		//var id_val			= $(form).find(':hidden[name=id]').val();
		var btn_agree		= $(form).find('.true');	
		var btn_disagree	= $(form).find('.false');
		
		$(form).find(':hidden[name=vote]').val(choice);
		
		var formdata = $(form).serializeArray();
		
		$.post($(form).attr('action'), formdata, function(obj) {
			if( obj.agree >= obj.disagree ){
				btn_agree.add($(form).find('.vote')).animate({height:btn_agree.height() + obj.agree}, 300 );
				btn_disagree.animate({height:btn_disagree.height() + obj.disagree}, 300 );
			} else {
				btn_disagree.add($(form).find('.vote')).animate({height:btn_disagree.height() + obj.disagree}, 300 );
				btn_agree.animate({height:btn_agree.height() + obj.agree}, 300 );
			}												
			btn_agree.add(btn_disagree).unbind("click");
			btn_agree.add(btn_disagree).css('cursor', 'text');
			
			$(btn_agree).find('span').text(obj.agree + '% eens (' + obj.votesyes + ' stemmen)');
			$(btn_disagree).find('span').text(obj.disagree + '% oneens (' + obj.votesno + ' stemmen)');								
		}, 'json');
  	};

  	

/**
 * Cycles trough the books in books-preview on link click
 * 
 * @param {Object} '.books-preview'
 */
  $('.books-preview').each(function(){
    elem = $(this);
    
    var speed    = 500;
    var items    = elem.find('li.item').removeClass('hide').hide(); // put in array, clear hide class and hide
    var current  = 0;
    elem.find('li.item:first').show(); // show the first again
    
    elem.find('ul').css({
      'height': getItemHeight(0) + 'px',
      'position': 'relative'
    });
    items.css('position', 'absolute');
    
    elem.find('.books-preview-cycler a.prev').click(function(){
      if( current == 0 )
        show(items.length-1);
      else
        show(current-1);
      
      return false;
    });
    
    elem.find('.books-preview-cycler a.next').click(function(){
      if( current == items.length-1 )
        show(0);
      else
        show(current+1);
      
      return false;
    });
    
    
    /**
     * Transition to the next image
     */
    function show(id)
    {
      $(items[current]).fadeOut(speed);
      $(items[id]).fadeIn(speed, function(){
        $(this).parent().height(getItemHeight(id));
      });
      current = id;
    }
    
    /**
     * Get the height of item including the padding
     */
    function getItemHeight(id)
    {
      return $(items[id]).height() + parseInt($(items[id]).css('padding-top')) + parseInt($(items[id]).css('padding-bottom'));
    }
  });
});


/**
 * mouse-over for resume
 */
$('.panel-resume').each(function(){
  var panel  = $(this);
  var padding;
  
  panel.find('.top-linkinfo').hide();
  
  $(this).find('.top h4 a').hover(
    function(){
      padding = parseInt(panel.find('.body').css('padding-top'));
      panel.find('.top-linkinfo').show();
      panel.find('.body').css('padding-top', (padding + 20) +'px');
    },
    function(){
      panel.find('.top-linkinfo').hide();
      panel.find('.body').css('padding-top', padding +'px');
    }
  );
});


/**
 * Activate the panels
 */
$(function(){
  $('.fold-panel').foldpanel({collapseTimeout: 500});
});


/**
 * Foldout menu
 * 
 */
$(function(){
  $('#foldout-menu').each(function(){
    var menu  = $(this);
    var speed = 250;
    var fixImage = menu.css('background-image');
    menu.css('background-image', 'none');
    
    menu.css({
      'position': 'absolute',
      'overflow': 'hidden'
    });
    
    menu.removeClass('hide');
    menu.hide();
    var height = menu.height();
    menu.height(0);
    
    $('#foldout-menu-toggler a').click(function(){
      if( menu.hasClass('open') )
        closeMenu();
      else
        openMenu();
      
      return false;
    });
    
    $('#foldout-menu a.close').click(function(){
      closeMenu();
      return false;
    });
    
    
    /**
     * Open the menu panel
     */
    function openMenu()
    {
      menu.show();
      $('#foldout-menu-toggler').addClass('open');
      menu.animate({height: height}, speed, 'swing', function(){
        $(this).addClass('open');
        menu.css('background-image', fixImage); // bg fix for FF
      });
    }
    
    /**
     * Close the menu panel
     */
    function closeMenu()
    {
      menu.animate({height: 0}, speed, 'swing', function(){
        menu.css('background-image', 'none');  // bg fix for FF
        $('#foldout-menu-toggler').removeClass('open');
        menu.hide();
        $(this).removeClass('open');
      });
    }
    
    
      


    
  });
});


/**
 * Toggle foldable panel plugin for jQuery
 * Created for VKBanen
 * @copyright 2008 info.nl
 * @author Jorn Luiten
 * @version 1.0
 * 
 * Searches for foldable divs inside a parent div and toggles them by an event
 *
 * ----------
 * HOW TO USE
 * ----------
 * $("").foldpanel();
 * 
 * or to set options:
 * $("input").foldpanel({
 *   option: 'value'
 * });
 * 
 *
 * -------
 * OPTIONS
 * -------
 * className : [string] The class of the field when it contains the label text
 *  
 * 
 */
(function($){
	$.fn.foldpanel = function(opts) {
		var settings = jQuery.extend({
      speed : 500,
      collapseTimeout : 0,
      customPadding : ''
		}, opts);
		
    var heights = []; // will be filled with heights of foldable divs within the panels
    var timers = [];
    var animating = false;
    
		return this.each(function(i){
			var elem = $(this);
      elem.addClass('open'); // add open class
      
      var content = elem.find('.content');
      var hContent = elem.find('.content.foldable');
			var allContentHidden = content.length <= hContent.length? true: false; // check if all content will be made invisible
			var body = elem.find('.body');
      var bodyPadding = [body.css('padding-top'), body.css('padding-right'), body.css('padding-bottom'), body.css('padding-left')];
      
      // store heights
      hContent.each(function(){
        if( !heights[i] )
          heights.push([]);
          
        heights[i].push($(this).height());
      });
      
      // Bind the click function to toggler
      elem.find('.toggler a').unbind('click').click(function(){
        $(this).hide();
        
        if( elem.hasClass('open') )
          closePanel(allContentHidden);
        else
          openPanel(i, allContentHidden);
        
        return false; // cancel default link action
      });
      
      // Fold the panel after timeout if necessary
      if( settings.collapseTimeout > 0 && !settings.startClosed ){
        setTimeout(function(){
          elem.find('.toggler a').hide();
          closePanel(allContentHidden);
        }, settings.collapseTimeout);
      }
      
      
      // Function block below
      // --------------------
			
			/**
			 * Opens the panels
			 */
			function openPanel(id, allContentHidden) {
        if( !animating ){
          if( allContentHidden ){ // IE doesn't support 'padding' with jquery
            elem.find('.body').css('padding-top', bodyPadding[0]);
            elem.find('.body').css('padding-right', bodyPadding[1]);
            elem.find('.body').css('padding-bottom', bodyPadding[2]);
            elem.find('.body').css('padding-left', bodyPadding[3]);
          }
          
  				hContent.each(function(i){
            $(this).animate({
              height : heights[id][i]
            },
            settings.speed,
            'swing',
            function(){ // callback
              elem.addClass('open');
              elem.removeClass('closed');
              elem.find('.toggler a').show();
            });
          });
        }
			};
      
      /**
			 * Closes the panels
			 */
			function closePanel(allContentHidden){
				if( !animating ){
          if( allContentHidden )
            elem.find('.body').css('padding', '0');
          
  				hContent.each(function(){
            $(this).animate({
              height : 0
            },
            settings.speed,
            'swing',
            function(){ // callback
              elem.addClass('closed');
              elem.removeClass('open');
              elem.find('.toggler a').show();
            });
          });
        }
			};
      
      
		});	
	};
})(jQuery);



/**
 * Inner label plugin for jQuery
 * @copyright 2008 info.nl
 * @author Arno Wolkers
 * @version 1.0.1
 * 
 * Puts the text of a label inside a text field if is has no value set
 *
 *
 * ----------------------------------------
 * HOW TO USE
 * ----------------------------------------
 * $("input").innerLabel();
 * $("input").innerLabel({option: 'value'});
 * 
 * $.fn.innerLabel.defaults.className = 'yada';
 * $("input").innerLabel();
 *
 *
 * ----------------------------------------
 * OPTIONS
 * ----------------------------------------
 * className : [string] The class of the field when it contains the label text
 *  
 */
(function($) {
	$.fn.innerLabel = function(options) {
		// Extend our default options with those provided.
		var opts = $.extend({}, $.fn.innerLabel.defaults, options);
		
		return this.each(function() {
			var elem   = $(this);
			var label  = $("label[for="+elem.attr('id')+"]:last");
      elem.label = label.text();
	  	
	    if (elem.label) {
				addLabel(elem);
//        label.remove();
        
				$(elem).focus(function() {
					removeLabel(elem);
				}).blur(function() {
					addLabel(elem);
				});
				
				$.fn.innerLabel.submitForm(elem);
			}
		});
		
		// PRIVATE FUNCTIONS
		/**
		 * If the value of the field is empty then put the label text inside the field
		 */
		 function addLabel(elem) {
			if ($.trim(elem.val()) == "") {

				elem.val(elem.label).addClass(opts.className);
			}
		};
		
		/**
		 * If the value of the field equals the label text then empty the field
		 */
		function removeLabel(elem) {
			if (elem.val() == elem.label) {
				elem.val("").removeClass(opts.className);
			}
		};
	};
	
	
	// PUBLIC FUNCTIONS
	$.fn.innerLabel.submitForm = function(elem) {
		// delete the inner label on form submit
		elem.parents('form:first').bind('submit',function() {
			if ($.trim(elem.val()) == elem.label) {
				elem.val("");
			}
		});
	};
	 
	// plugin defaults 
	$.fn.innerLabel.defaults = {
		className : 'innerlabel',
    hideLabel : true
	};
	
})(jQuery); 
;

