var zoek_expert_txt        = 'Zoek naar een Expert';
var do_not_show_optionpick = 0;
var form_verder_locatie    = '';
var form_numajax_calls     = 0;

var aanbeveling_sortveld = 'datum';
var aanbeveling_sorttype = 'desc';
var aanbeveling_limit    = 4;
var aanbeveling_currnum  = 0;
var aanbeveling_maxRest  = 10;

var aanbeveling_thumbs   = 0;

var slideUpDownisat      = 0;

$(document).ready(function(){
  
  //old niet ok
  /*
  if($("a.nietok.replacelink").length > 0)
  {
    $("a.nietok.replacelink").attr('href', $("a.nietok.replacelink").attr('rel'));
  }
  */
  
  //new niet ok
  if($("a.nietok.replacelink").length > 0)
  {
    $("#action_niet_ok").attr('action', $("#action_niet_ok").attr('rel'));
  }
  
  $('a.nietok.replacelink').click(function() {
    $('#nietokvak').animate({
      height: "298px"
    }, 300, function() {
      // Animation complete.
    });
    return false;
  });
  
  $('#rapporteernietok').click(function() {
    $("form").submit();
    //alert('hoi');
    return false;
  });
  
  if($("div.aanbeveling-container").length > 0)
  {
    $("div.aanbeveling-container input").bind('keydown', function(e){
      if(e.keyCode)
      {
        if(e.keyCode == 13)
        {
          $("a.aanbevelingverzenden").click();
        }
      }
    });
    
    $("a.aanbevelingverzenden").removeAttr('href').click(function(){
      $("img.statusimg.red").hide().prev().prev().css({ 'font-weight' : 'normal' });
      post_aanbeveling();
      /*
      if($("p.error:eq(1)").length > 0)
      {
        if($("p.error:eq(1)").is(":visible"))
        {
          //$("p.error:eq(1)").hide();
        }
      }
      if($("p.error:eq(0)").length > 0)
      {
        if($("p.error:eq(0)").is(":visible"))
        {
          //$("p.error:eq(0)").hide(0, function(){
            post_aanbeveling();
          //});
        }else{
          post_aanbeveling();
        }
      }else{
        post_aanbeveling();
      }
      */
    });
    
    $("div.form div.thumbs span.rate").hover(function(){
      $("div.form div.thumbs").removeClass('thumb1').removeClass('thumb2').removeClass('thumb3').removeClass('thumb4').removeClass('thumb5');
      $("div.form div.thumbs").addClass('thumb' + substr($(this).attr('id'),1));
      updatetxtaanbeveling(substr($(this).attr('id'),1));
    },function(){
      $("div.form div.thumbs").removeClass('thumb1').removeClass('thumb2').removeClass('thumb3').removeClass('thumb4').removeClass('thumb5');
      $("div.form div.thumbs").addClass('thumb' + aanbeveling_thumbs);
      updatetxtaanbeveling(aanbeveling_thumbs);
    }).click(function(){
      aanbeveling_thumbs = substr($(this).attr('id'),1);
      updatetxtaanbeveling(aanbeveling_thumbs);
    });
    
    $("input[name='naam']").focus();
  }
  
  $("a.leesmeer").live('click', function(){
    $(this).parent().parent().parent().find('div.tekst').hide();
    $(this).parent().parent().parent().find('div.tekst-full').show();
  });
  
  if($('div.persoon-aanbeveling-loading').length > 0)
  {    
    zoek_aanbeveling(aanbeveling_limit,aanbeveling_sortveld,aanbeveling_sorttype);
    
    $("div.aanbeveling div.header div.kolom-1").click(function(){      
      fieldTo = 'thumbs';
      
      $("span.sorting").removeClass('nosort').removeClass('asc').removeClass('desc').addClass('nosort');
      if(aanbeveling_sortveld !== fieldTo)
      {
        aanbeveling_sorttype = 'desc';
      }else{
        if(aanbeveling_sorttype == 'desc')
        {
          aanbeveling_sorttype = 'asc';
        }else{
          aanbeveling_sorttype = 'desc';
        }
      }
      aanbeveling_sortveld = fieldTo;
      $("div.aanbeveling div.header div.kolom-1 span.sorting").addClass(aanbeveling_sorttype).removeClass('nosort');
      zoek_aanbeveling(aanbeveling_limit,aanbeveling_sortveld,aanbeveling_sorttype);
    });
    $("div.aanbeveling div.header div.kolom-4").click(function(){
      fieldTo = 'datum';
      
      $("span.sorting").removeClass('nosort').removeClass('asc').removeClass('desc').addClass('nosort');
      if(aanbeveling_sortveld !== fieldTo)
      {
        aanbeveling_sorttype = 'desc';
      }else{
        if(aanbeveling_sorttype == 'desc')
        {
          aanbeveling_sorttype = 'asc';
        }else{
          aanbeveling_sorttype = 'desc';
        }
      }
      aanbeveling_sortveld = fieldTo;
      $("div.aanbeveling div.header div.kolom-4 span.sorting").addClass(aanbeveling_sorttype).removeClass('nosort');
      zoek_aanbeveling(aanbeveling_limit,aanbeveling_sortveld,aanbeveling_sorttype);
    });
  }
  
  $(".ttw").tipsy({gravity: 'w', fade: true, live: true, html: true });
  $(".ttn").tipsy({gravity: 'n', fade: true, live: true, html: true });
  $(".tts").tipsy({gravity: 'e', fade: true, offset: 5, live: true, html: true });
  $(".fillit").tipsy({gravity: 'w', fade: true, offset: 11, live: true, html: true });
  
  if($("div.aanbeveling-container").length < 1)
  {
    $("form.standaard input:eq(0)").focus();
    $("img.statusimg.red:eq(0)").prev().focus();
    $("input.focus").focus();
    $("label.contact-error:eq(0)").next().focus();
    $("label.contact-error.next:eq(0)").next().next().focus();
    $("form.standaard.nodefaultselect input:eq(0)").blur();
  }
    
  $('input[type="password"].spc-password').pstrength();

  $(".savethemall").click(function(){
    var theClickForom = $(this).parent();
    form_verder_locatie = theClickForom.attr('action');
    
    $("form").each(function(){
      if(/* $(this).attr('action') !== form_verder_locatie && */ $(this).attr('action') !== '/zoek/expert-zoeken/quick/')
      {
        // alert($(this).attr('action'));
        form_numajax_calls++
      }
    });
    
    var is_at_form = 1;
    $("form").each(function(){
      if(/* $(this).attr('action') !== form_verder_locatie && */ $(this).attr('action') !== '/zoek/expert-zoeken/quick/')
      {
        // alert($(this).attr('action'));
        $.post($(this).attr('action'), $(this).serializeArray(), function(d){
          
          if(is_at_form == form_numajax_calls)
          {
            theClickForom.submit();
            // alert('submit');
          }
          
          is_at_form++;
        }, 'html');
      }
    });
    
    return false;
  });

  
  $(".ookopslaan").click(function(){
    form_verder_locatie = $(this).attr('href');
    
    $("form").each(function(){
      if($(this).attr('action') !== '/zoek/expert-zoeken/quick/')
      {
        form_numajax_calls++
      }
    });
    
    var is_at_form = 1;
    $("form").each(function(){
      if($(this).attr('action') !== '/zoek/expert-zoeken/quick/')
      {
        $.post($(this).attr('action'), $(this).serializeArray(), function(d){
          
          if(is_at_form == form_numajax_calls)
          {
            document.location.href = form_verder_locatie;
          }
          
          is_at_form++;
        }, 'html');
      }
    });
    return false;
  });
  
  $("a.faq .faq-inhoud p").hide();
  
  $("a.faq .faq-inhoud").click(function(){
    if($(this).find("p").is(':visible'))
    {
      $(this).find("p").slideUp();
    }else{
      $(this).find("p").slideDown();
    }
  });
  
  $("select#wgvr").change(function(){
    if($(this).val() < 0)
    {
      $(".werkgever-overig").hide();
    }
    if($(this).val() == 0)
    {
      $(".werkgever-overig").show().find("input").focus();
    }
    if($(this).val() > 0)
    {
      $(".werkgever-overig").hide();
    }
  });
  
  $("input[name='zzpr']").click(function(){
    if($(this).val() == 'j')
    {
      $(".werkgever-zzp").hide();
    }else{
      $(".werkgever-zzp").show();
    }
  });
  
  $("#top-menu input.zoeken, input.snel-zoeken").focus(function(){
    if($(this).val() == zoek_expert_txt)
    {
      $(this).val('').css({ 'color' : '#000', 'font-weight' : 'bold' });
    }
  }).blur(function(){
    if($(this).val() == '')
    {
      $(this).val(zoek_expert_txt).css({ 'color' : '#A2A2A2', 'font-weight' : 'normal' });
    }    
  });
  
  $("#captcha").click(function(){
    $(this).attr('src', '/captcha?change=' + mktime() + rand(100,999)).next().focus();
  });
  
  $("div#container div#content div.block div.extra div.text a.checkbox").removeAttr('href').click(function(){
    if($(this).filter(".checked").length > 0)
    {
      $(this).removeClass('checked');
    }else{
      $(this).addClass('checked');
    }
  });
  
  $(".block a.button-zoeken").click(function(){
    $(this).attr('rel', $(this).attr('href')).removeAttr('href');
    
    var add = '';
    
    $(this).parent().find("a.checkbox.checked").each(function(){
      add = add + $(this).attr('rel') + '/';
    });
    
    document.location.href = $(this).attr('rel') + '/' + add;
    
    return false;
  });
  
  // $(".geen-verdieping-beschikbaar").parent().parent().parent().addClass('halftrans');  
  
  if($(".wo-addrule .wo-desc").length > 0)
  {
    if($(".wo-addrule .wo-desc").val() == 'Geef een omschrijving op...')
    {
      $(".wo-addrule .wo-desc").css({'color':'#ccc'});
    }
    $(".wo-addrule .wo-desc").live('focus', function(){
      if($(this).val() == 'Geef een omschrijving op...')
      {
        $(this).val('').css({'color':'#000'});
      }
    });
    $(".wo-addrule .wo-desc").live('blur', function(){
      if($(this).val() == '')
      {
        $(this).val('Geef een omschrijving op...').css({'color':'#ccc'});
      }
    });
  }
  
  $(".wo-addrule button.add").parent().parent().submit(function(){
    var prefix = '#' + $(this).attr('id') + ' ';
    
    var obj   = $(prefix + ".wo-addrule:last");
    var from  = obj.find("select:eq(0)").val();
    var till  = obj.find("select:eq(1)").val();
    var desc  = obj.find("input[type='text']:eq(0)").val();
    
    var error = '';
    
    if(desc == '' || desc == 'Geef een omschrijving op...') error = 'Geef een omschrijving op';
    
    if(from !== 'Beginjaar' && from !== '"Heden"' && till !== 'Eindjaar' && till !== '"Heden"')
    {
      if(till < from) error = 'Het beginjaar kan niet groter zijn dan het eindjaar';
    }
    
    if(till == 'Eindjaar')  error = 'Geef het eindmomment op';
    if(from == 'Beginjaar') error = 'Geef het beginmoment op';
    
    if(error !== '')
    {
      if(obj.find("div.error").length > 0)
      {
        obj.find("p.error").slideUp('fast', function(){
          obj.find("div.error").remove();
          insertError(error,obj);
        });
      }else{
        insertError(error,obj);
      }
      return false;
    }
  });
  
  $(".pfrule span.edit a").removeAttr('href').click(function(){
    var prefix = '#' + $(this).parent().parent().parent().attr('id') + ' ';
    
    $(prefix + "div.wo-addrule").slideUp('fast');
    $(prefix + "div.hr").hide().prev().hide();

    if($(prefix + "div.wo-addrule.marginbottom3").length > 0)
    {
      $(prefix + "div.wo-addrule.marginbottom3").slideUp(function(){
        $(this).prev().slideDown();
        $(this).remove();
      });
    }
    var obj = $(this).parent().parent();
    
    obj.slideUp(function(){      
      var oldcolor = $(this).parent().parent().parent().find(".wo-addrule .wo-desc").css('color');
      $(this).parent().parent().parent().find(".wo-addrule .wo-desc").css({'color' : '#000'});
      var newHtml = $(prefix).find(".wo-addrule").html();
      
      $(this).parent().parent().parent().find(".wo-addrule .wo-desc").css({'color' : oldcolor });
      
      newHtml = newHtml.replace(/rightfloat add/, 'rightfloat save');
      newHtml = newHtml.replace(/>Toevoegen</, '>Opslaan<');
      
      newvallabel = 'forlabel_afas_' + mktime();
      newHtml = newHtml.replace(/id="forlabel_afas/,  'id="' + newvallabel);
      newHtml = newHtml.replace(/for="forlabel_afas/, 'for="' + newvallabel);
      
      newHtml = newHtml.replace(/Geef een omschrijving op.../, htmlentities($(this).find("span.desc").html()));
      
      obj.after('<div class="wo-addrule">' + newHtml + '<input type="hidden" name="edit" value="' + $(this).attr('rel') + '" /><br clear="both" /></div>');
      obj.next().hide().addClass('marginbottom3').slideDown();
      obj.next().find("select:eq(0)").val($(this).find("span.from").html());
      obj.next().find("select:eq(1)").val($(this).find("span.till").html().replace(/Heden/, '"Heden"'));
      obj.next().find("input.checkbox").attr('checked', ($(this).find("input[type='hidden']").length > 0));
      obj.next().find("button").addClass('saveEdit');
    });
  });
  
  $(".pfrule span.delete a").hover(function(){
    $(this).find("img").attr('src', 'images/icons/delete.png');
  },function(){
    $(this).find("img").attr('src', 'images/icons/delete_fade.png');
  });
  
  $("button.uploadfoto").click(function(){
    $("form").submit();
    $("input.file").attr('disabled', true);
    $(this).prev().remove();
    $(this).hide().next().remove();
    $("img.loader").show();
    return false;
  });
  
  $("input.kennischeckbox").change(function(){
    if($(this).attr('checked'))
    {
      $(this).next().addClass('bold');
    }else{
      $(this).next().removeClass('bold');
    }
  });
  
  if($('#resizeimage').length > 0)
  {
    var w = $('img#resizeimage').width();
    var h = $('img#resizeimage').height();
    
    if(w > h)
    {
      var size = h;
      var n_size = size/10*8;
      var ss = [ ((w-n_size)/2) , (size/10) , (n_size+size/10) , (n_size+size/10) ];
    }else{
      var size = w;
      var n_size = size/10*8;
      var ss = [ (size/10), ((h-n_size)/2) , (n_size+size/10) , (n_size+size/10) ];
    }
    
    $('#resizeimage').Jcrop({
        onChange: showPreview,
        onSelect: showPreview,
        aspectRatio: 1,
        setSelect: ss
    });
  }
  
  if($("select#kennisgebied").length > 0)
  {
    $("select#kennisgebied").hide().after('<div id="select"><span class="grey">Maak een keuze...</span></div><input type="hidden" name="kennisgebied_jquery" value="" />');
    
    $("div.categorie-zoeken").prepend(
      '<div id="selectpick">' +
      '  <div id="outclick"></div>' +
      '  <div class="top"></div>' +
      '  <div class="main">' +
      '    <ul>' +
      '    </ul>' +
      '  </div>' +
      '  <div class="bottom"></div>' +
      '</div>'
    );
    
    $("div.categorie-zoeken #selectpick").hide();
    
    $("select#kennisgebied option").each(function(a,b){
      $("div.categorie-zoeken .main ul").append('<li rel="' + $(this).attr('value') + '"><img src="dbpics/icons/' + $(this).attr('value') + '/small" /><span>' + $(this).html() + '</span></li>');
    });
    
    $("a.zoek-button").removeAttr('href').click(function(){
      // alert($("input[name='kennisgebied_jquery']").val());
      // alert($(".opties-zoeken ul li.active").find("a").attr('rel'));
      var v = {};
      v.kennisgebied = $("input[name='kennisgebied_jquery']").val();
      v.subonderdeel = [];
      $(".opties-zoeken ul li.active").each(function(){
        v.subonderdeel[v.subonderdeel.length] = $(this).find("a").attr('rel');
      });
      v.zoeken = $("input.snel-zoeken").val();
      

      v.provincies = [];
      v.kaartjezichtbaar = $("#kaartjezichtbaar").val();
      $("#checkboxes input[type=checkbox]").each(function(){ v.provincies[v.provincies.length] = $(this).val(); });
      
      if(v.zoeken === "%"){ $("input.snel-zoeken").val(zoek_expert_txt).css({ 'color' : '#A2A2A2', 'font-weight' : 'normal' }) };
      
      $("div#searchwarnmsg").hide();
      $(".persoon-details-zoekpagina").remove();
      $(".groepstitel").remove();
      $("center#searchloader").show();
      if( (v.zoeken == '' || v.zoeken == zoek_expert_txt) ){ v.zoeken = "%" };
      
      $("div.zoekresultaten").fadeIn(function(){
        $.post('/ajax/zoekactie/', v , function(d){
          $("center#searchloader").stop().fadeOut(function(){
          
              if(d.data.length > 0)
              {
                var PrevTitle = '';
                $.each(d.data, function(a,b){
                  if(PrevTitle !== b.titel && v.zoeken != "%")
                  {
                    $(".zoekresultaten").append('<div class="groepstitel"><br clear="both" /><h2>Gevonden binnen <b>' + b.titel + '</b></h2></div>');
                  }
                  PrevTitle = b.titel;

                  
                  $(".zoekresultaten").append(
					'<div class="persoon-details-zoekpagina">' + 
					'	<h1><a href="/persoon/details/' + b.id + '/' + b.naam_url + '">' + b.naam + '</a></h1>' +
                                        '	<a href="/persoon/details/' + b.id + '/' + b.naam_url + '" class="leesmeer">  </a>' + 
                                        '	<a href="/persoon/details/' + b.id + '/' + b.naam_url + '"><img class="foto" src="' + b.picture + '" alt="" /></a>' + 
					
					
					'	<div class="informatie">' + 
					'		<div class="ttw" original-title="Richtlijn, kan afwijken per opdracht/project.">' + b.uurtarief.replace(/([0-9]{1,})/g, '<b>$1</b>').replace(/(&euro;)/g, '<b>$1</b>') + '</div>' + 
					'		<div class="ttw" original-title="Vestigingsgebied (niet per definitie het werkgebied)">Provincie: <b>' + b.provincie + '</b></div>' + 
					
					'		<div class="sterren ster' + b.sterren + '"></div>' +
                                        '		<div>' + (b.bedrijfsnaam == 'ZZP\'er' ? '<span><b>' + b.bedrijfsnaam + '</b></span>' : 'Werkzaam bij ' + '<span class="bedrijfsnaamurl">' + (b.url == '' ? '' : '<a href="' + (substr(b.url, 0, 4) == 'http' ? '' : 'http://') + b.url + '" class="" target="blank">') + b.bedrijfsnaam +  (b.url == '' ? '' : '</a>')  + '</span>') + '</div>' + 
					'	</div>' + 
					'	<div class="logo ' + b.zzplogo + '">' + (b.url == '' ? '' : '<a href="' + (substr(b.url, 0, 4) == 'http' ? '' : 'http://') + b.url + '" target="blank">') + '<img src="dbpics/bedrijfslogo/' + b.hash + '" alt="" />' + (b.url == '' ? '' : '</a>') + '</div>' + 
					
					'</div>'
                  );
                  
                });
                
              }else{
                $("div#searchwarnmsg p").removeClass('common').removeClass('dev').removeClass('error').addClass('dev');
                $("div#searchwarnmsg p span").html('Er zijn geen AFAS Experts gevonden die voldoen aan de opgegeven kennisgebieden en/of trefwoorden. <br /><br /><span class="todolist">Wanneer u meerdere specialisaties aanvinkt wordt er met een <u>en</u>-relatie gezocht. <br />In dit geval kunt u een of meerdere opties uitvinken om het zoekgebied te verkleinen.</span>');
                $("div#searchwarnmsg").stop().fadeIn();
              }

          });         
        }, 'json');
      });
    });
    
    $("div#outclick, div#selectpick .main ul li").click(function(){
      var t_this = this;
      if($(this).attr('rel'))
      {
        $(".opties-zoeken .zoek-button").animate({'margin-top' : '10px'});
        $(".opties-zoeken .hidehandler").slideUp(function(){
          $("img#qualloader").hide().fadeIn('fast');
          $(".opties-zoeken").addClass('verberg');
          $().show();

          $("div#select").html($(t_this).find("span").html());
          $("input[name='kennisgebied_jquery']").val($(t_this).attr('rel'));
          $.post('/ajax/subcatsqualificatie/' + $(t_this).attr('rel'), {}, function(d){
            if(d.data){
              $("img#qualloader").show().stop().fadeOut('fast',function(){
                $(this).hide();
              });
              $(".opties-zoeken ul li").remove();
              $.each(d.data, function(a,b){
                var is_def_act = '';
                if($("input.dsel[value='" + b.id + "']").length > 0){
                  is_def_act = 'active ';
                }
                $(".opties-zoeken ul").append(
                  '<li class="' + is_def_act + (a == (d.data.length-1) ? ' laatste ' : '') + (a == 0 ? ' eerste ' : '') + (d.data.length == 1 ? ' eerste laatste ' : '') + '">' +
                  '<a class="pointer" rel="' + b.id + '">' +
                  b.titel +
                  '</a></li>'
                );
              });
              
              $("input.dsel").remove();
              
              if(d.data.length < 1)
              {
                $(".opties-zoeken ul").append('<li class="geenspecialisaties">Er zijn nog geen specialisaties opgegeven...</li>');
              }
              
              if(d.data.length > 2)
              {
                var newh = (((d.data.length * 31)-74)/2) + 25;
              }else{
                var newh = 10;
              }
              $(".opties-zoeken .zoek-button").animate({'margin-top' : newh + 'px'});
              $(".opties-zoeken.verberg").removeClass('verberg').find(".hidehandler").hide().slideDown(function(){
                
                if(do_not_show_optionpick > 0){
                  $(".opties-zoeken a.zoek-button").click();
                }
                
                do_not_show_optionpick = 0;                
              });
            }
          }, 'json');
        });
      }
      
      if(do_not_show_optionpick < 1)
      {
        $("div.categorie-zoeken #selectpick").animate({
          "height" : "toggle"
        }, 150);
      }
    });
    
    if(do_not_show_optionpick < 1)
    {
      $("div.categorie-zoeken #select").click(function(){
        $("div.categorie-zoeken #selectpick").animate({
          "height" : "toggle"
        }, 300);
      });
    }
    
    if($("select#kennisgebied option[rel='sel']").length > 0)
    {
      do_not_show_optionpick = 1;
      $("div.categorie-zoeken .main ul li[rel='" + $("select#kennisgebied option[rel='sel']").val() + "']").click();
    }
    
    $("div.zoeken div.opties-zoeken ul.zoek-opties li").live('click', function(){
      if($(this).filter(".active").length > 0){
        $(this).removeClass('active');
      }else{
        $(this).addClass('active');
      }
      
    });
    
  }
  
    
  if($("a#neemcontactop").length > 0)
  {
    $("a#neemcontactop").click(function(){
      if($("div.form-contact").is(':visible'))
      {
        $("div.form-contact").fadeOut(function(){
          $("div.form-contact form.accept").remove();
          $("div.form-contact form").show();
          $("div.form-contact form input").val('');
          $("div.form-contact form textarea").val('');
          $("div.form-contact form label").removeClass('error');
          $("div.form-contact").css({ 'height' : 'auto' });
        });
      }else{
        $("div.form-contact").fadeIn().find("input:eq(0)").focus();
      }
    });
    $("form.contact").submit(function(){
      naam = $("form.contact input:eq(0)");
      tel  = $("form.contact input:eq(1)");
      mail = $("form.contact input:eq(2)");
      ber  = $("form.contact textarea");
      
      if(ber.val().length < 10){
        ber.focus().prev().prev().addClass('error');
      }else{
        ber.prev().prev().removeClass('error');
      }
      
      patroon = /[a-zA-Z]{2,}.*@.*\.[a-zA-Z]{2,}/
      if(!patroon.test(mail.val())){
        mail.focus().prev().prev().addClass('error');
      }else{
        mail.prev().prev().removeClass('error');
      }

      patroon = /[0-9]{2,}/
      if(!patroon.test(tel.val()) || tel.val().length < 9){
        tel.focus().prev().prev().addClass('error');
      }else{
        tel.prev().prev().removeClass('error');
      }
      
      patroon = /[a-zA-Z]{2,}/
      if(!patroon.test(naam.val())){
        naam.focus().prev().prev().addClass('error');
      }else{
        naam.prev().prev().removeClass('error');
      }
      
      if($("form.contact label.error").length < 1)
      {
        $(".form-contact form").fadeOut('fast');
        $(".form-contact").animate({
          'height' : '126px'
        }, function(){
          $(this).append('<form class="contact accept"><br clear="both" /><br clear="both" /><center><b>Bedankt!</b><br /><br />Uw reactie is verzonden. <br /><br />' + $('.persoon-details h1').html() + ' neemt zo spoedig mogelijk contact met u op.</center></form>');
          $.post('/ajax/react', { 'safekey' : $("form.contact").attr('rel'), 'naam' : naam.val(), 'tel' : tel.val(), 'ber' : ber.val(), 'mail' : mail.val(), 'expert' : $("form.contact").parent().attr('rel') }, function(d){}, 'text');
        });
      }
      
      return false;
    });
  }
  
  $(".snel-zoeken-button").click(function(){
    //document.location.href = '/zoek/expert-zoeken/quick/' + urlencode(str_replace("/", "", $("input.snel-zoeken").val()));
    $("a.zoek-button").click();
  });
  
  $("input.snel-zoeken").bind('keyup', function(e){
    if(e.keyCode == 13)
    {
      $(".snel-zoeken-button").click();
    }
  });
  
  if($("input.snel-zoeken").length > 0)
  {
    if($("input.snel-zoeken").val() !== zoek_expert_txt)
    {
      $("input.snel-zoeken").css({ 'color' : '#000', 'font-weight' : 'bold' });
      $(".zoek-button").click();
    }
  }
  
  setTimeout(function(){
    $(".slowfadeout").animate({
      "height" : "toggle",
      "opacity" : "toggle"
    }, 600);
  }, 3000);
  
});

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  function post_aanbeveling()
  {
    if(slideUpDownisat < 1)
    {
      slideUpDownisat = 1;
      $.post('/ajax/add-aanbeveling/' + $("input[name='pid']").val(), {
        'naam' : $("input[name='naam']").val(),
        'mail' : $("input[name='mail']").val(),
        'functie' : $("input[name='functie']").val(),
        'organisatie' : $("input[name='organisatie']").val(),
        'plaats' : $("input[name='plaats']").val(),
        'waardering' : aanbeveling_thumbs,
        'url' : $("input[name='url']").val(),
        'aanbeveling' : $("textarea").val(),
        'rid' : $("input[name='rid']").val()
      }, function(d){
        if(d.errors > 0)
        {
          isFocussed = 0;
          $("p.error").removeClass('hidden').hide()
          $.each(d.error, function(a,b){
            $("input[name='" + a + "']").next().filter("img").show().prev().prev().css({ 'font-weight' : 'bold' });
            
            //if($("p.error.pos" + b[0]).is(':visible'))
            //{
            //  //alert('v');
            //}else{
              //$("p.error.pos" + b[0]).html(b[1]).slideDown();
              //alert('nv');
              $("p.error.pos" + b[0]).html(b[1]).show();
              if(isFocussed < 1)
              {
                if($("input[name='" + a + "']").length > 0)
                {
                  $("input[name='" + a + "']").focus();
                }
                if(a == 'aanbeveling' || a == 'waardering')
                {
                  $("textarea").focus();
                }
              }
            //}
            // alert(a + ' - ' + b[0] + ' - ' + b[1]);
          });
        }else{
          var theMail = $("input[name='mail']").val();
          $("div.aanbeveling-container").html('<h1 class="nopadding">Bedankt! We hebben uw aanbeveling ontvangen.</h1><p>We hebben een e-mail gestuurd naar <b>' + theMail + '</b> met een bevestigingslink. Wanneer u op deze link klikt wordt uw aanbeveling geplaatst op de profielpagina van <b>' + $("div#breadcrumbs a:last").html() + '</b>.</p><p><a class="button-zoeken normal ietsrechts" href="' + $("div#breadcrumbs a:last").attr('href') + '" type="submit">Terug</a></p>');
        }
        slideUpDownisat = 0;
      }, 'json');
    }
  }

  function zoek_aanbeveling(xa,xb,xc)
  {
    aanbeveling_currnum = 0;
    $("div.profielvak-inhoud").addClass('loading');
    
    $.post('/ajax/review/' + $("div.profielvak.aanbeveling").attr('id') + '/L:' + xa  + '/S:' + xb  + '/M:' + xc + '/' , {}, function(d){
      aanbevelingenOk = false;
      if(d.data)
      {
        l = d.data.length;
        n = aanbeveling_limit;
        if(l > 0)
        {
          aanbevelingenOk = true;
          abv_html        = '';
          $.each(d.data, function(a, b){
            laatste = (l == a+1 ? 'L' : 'N');
            if(a < n)
            {
              aanbeveling_currnum++;
              
              abv_html += (
                '<div class="persoon-aanbeveling'+(a==0?' first':'')+'">' +
                '  <div class="kolom-1">' +
                '    <div class="thumbs thumb' + b.thumbs + '"></div>' +
                ( b.url !== '' ? '    <a href="' + b.url + '" class="website" target="blank">' : '' ) + (b.haslogo > 0 ? '<img class="foto" src="images/logo_ipub.jpg" alt="" style="width:100px;" />' : '' ) + ( b.url !== '' ? '</a>' : '' ) +
                '  </div>' +
                '  <div class="kolom-2">' +
                '    <h4>' + b.naam + '</h4>' +
                '    <span>' + b.functie + '</span>' +
                ( b.url !== '' ? '    <a href="' + b.url + '" class="website" target="blank">' : '' ) + b.bedrijf + ( b.url !== '' ? '</a>' : '' ) +
                ( b.plaats !== '' ? '    <a class="locatie">' + b.plaats + '</a>' : '') +
                '  </div>' +
                '  <div class="kolom-3">' +
                '    <div class="tekst">' + b.intro_txt + '</div>' +
                '    <div class="tekst tekst-full">' + b.comment + '</div>' +
                '  </div>' +
                '  <div class="kolom-4">' +
                '    <b>' + b.datum + '</b>' +
                '  </div>' +
                '  ' + (laatste == 'N' ? '<div class="rand"></div>' : '') +
                '</div>'
              );
            }            
          });
          if($("div.persoon-aanbeveling-loading").length > 0)
          {
            $("div.persoon-aanbeveling-loading").before(abv_html);
            $("div.persoon-aanbeveling").show();
            $("div.persoon-aanbeveling-loading").remove();
          }else{
            $("div.persoon-aanbeveling").addClass('removeNext');
            $("div.persoon-aanbeveling:eq(0)").before(abv_html);
            $("div.persoon-aanbeveling").show();
            $("div.persoon-aanbeveling.removeNext").remove();
            // $("div.aanbeveling div.header").next().after(abv_html);
            // $("div.persoon-aanbeveling").show();
          }
          $("div.profielvak-inhoud").removeClass('loading');
          $("div.ToonMeer").hide();
          if(d.aantal)
          {
            if(d.aantal > 0)
            {
              if(aanbeveling_currnum > 0)
              {
                if(d.aantal > aanbeveling_currnum)
                {
                  rest = d.aantal - aanbeveling_currnum;
                  if(rest > aanbeveling_maxRest) rest = aanbeveling_maxRest;
                  
                  $("span.ClickMore").unbind('click').click(function(){
                    aanbeveling_limit = aanbeveling_limit + rest;
                    zoek_aanbeveling(aanbeveling_limit,aanbeveling_sortveld,aanbeveling_sorttype);
                  });
                  
                  $("div.ToonMeer").show();
                  $("div.ToonMeer span.aantal").html(rest);
                }
              }
            }
          }
        }
      }
      if(!aanbevelingenOk)
      {
        $("div.profielvak.aanbeveling").html(
          '<img class="rand" src="images/profiel_bg_top.png" alt="" />' + 
          '<div class="profielvak-inhoud aanbeveling">' +
          '  <div class="header">' +
          '    <div class="kolom-5">Nog geen aanbevelingen</div>' +
          '  </div>' +
          '  <div style="clear:both;"></div>' +
          '  ' +
          '  <div class="persoon-aanbeveling persoon-aanbeveling-loading">' +
          '    <div class="loading">' +
          '      Er zijn nog geen aanbevelingen geplaatst' +
          '    </div>' +
          '    <a href="' + $("a.addAanbeveling").attr('href') + '">Klik hier om als eerste een aanbeveling toe te voegen</a>' +
          '    <div class="rand last"></div>' +
          '  </div>' +
          '  ' +
          '  <div style="clear:both;"></div>' +
          '  ' +
          '  <img class="randbottom" src="images/aanbeveling_bgbottom.jpg" alt="" />' +
          '  <div style="clear:both;"></div>' +
          '</div>' +
          '<img class="rand" src="images/profiel_bg_bottom.png" alt="" />'
        );
      }
    }, 'json');
  }
  
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function updatetxtaanbeveling(theid)
{
  newtxt = '';
  if(theid == 0) newtxt = '<i>Nog geen waardering opgegeven...</i>';
  if(theid == 1) newtxt = 'Redelijk';
  if(theid == 2) newtxt = 'Voldoende';
  if(theid == 3) newtxt = 'Goed';
  if(theid == 4) newtxt = 'Uitmuntend';
  if(theid == 5) newtxt = 'Excellent';
  
  $("span.waarderingtxt").html(newtxt);
}


function insertError(error,obj)
{
  obj.prepend('<div class="error"><p class="error inform subtiel">' + error + '</p><br clear="both" /></div>');
  obj.find("p.error").hide().animate({
    "height" : "toggle",
    "opacity" : "toggle"
  }, 200);
}

function updateCoords(c)
{
  $('#x').val(c.x);
  $('#y').val(c.y);
  $('#w').val(c.w);
  $('#h').val(c.h);
};

function showPreview(coords)
{
  updateCoords(coords);
  
  var rx = 81 / coords.w;
  var ry = 81 / coords.h;

  $("body.msie .jcrop-hline:eq(0)").css({'padding-top': '1px'});

  $('#preview img').css({
    width: Math.round(rx * $("#resizeimage").width()) + 'px',
    height: Math.round(ry * $("#resizeimage").height()) + 'px',
    marginLeft: '-' + Math.round(rx * coords.x) + 'px',
    marginTop: '-' + Math.round(ry * coords.y) + 'px'
  });
};

function mktime() {
  var d = new Date(), r = arguments, i = 0,
    e = ['Hours', 'Minutes', 'Seconds', 'Month', 'Date', 'FullYear'];

  for (i = 0; i < e.length; i++) {
    if (typeof r[i] === 'undefined') {
      r[i] = d['get' + e[i]]();
      r[i] += (i === 3); // +1 to fix JS months.
    } else {
      r[i] = parseInt(r[i], 10);
      if (isNaN(r[i])) {
        return false;
      }
    }
  }
  r[5] += (r[5] >= 0 ? (r[5] <= 69 ? 2e3 : (r[5] <= 100 ? 1900 : 0)) : 0);
  d.setFullYear(r[5], r[3] - 1, r[4]);
  d.setHours(r[0], r[1], r[2]);
  return (d.getTime() / 1e3 >> 0) - (d.getTime() < 0);
}

function rand (min, max) {
  var argc = arguments.length;
  if (argc === 0) {
    min = 0;
    max = 2147483647;
  } else if (argc === 1) {
    throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
  }
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function get_html_translation_table (table, quote_style) {   
    var entities = {}, hash_map = {}, decimal = 0, symbol = '';
    var constMappingTable = {}, constMappingQuoteStyle = {};
    var useTable = {}, useQuoteStyle = {};
    
    // Translate arguments
    constMappingTable[0]      = 'HTML_SPECIALCHARS';
    constMappingTable[1]      = 'HTML_ENTITIES';
    constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
    constMappingQuoteStyle[2] = 'ENT_COMPAT';
    constMappingQuoteStyle[3] = 'ENT_QUOTES';

    useTable       = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
    useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';

    if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
        throw new Error("Table: "+useTable+' not supported');
        // return false;
    }

    entities['38'] = '&amp;';
    if (useTable === 'HTML_ENTITIES') {
        entities['160'] = '&nbsp;';
        entities['161'] = '&iexcl;';
        entities['162'] = '&cent;';
        entities['163'] = '&pound;';
        entities['164'] = '&curren;';
        entities['165'] = '&yen;';
        entities['166'] = '&brvbar;';
        entities['167'] = '&sect;';
        entities['168'] = '&uml;';
        entities['169'] = '&copy;';
        entities['170'] = '&ordf;';
        entities['171'] = '&laquo;';
        entities['172'] = '&not;';
        entities['173'] = '&shy;';
        entities['174'] = '&reg;';
        entities['175'] = '&macr;';
        entities['176'] = '&deg;';
        entities['177'] = '&plusmn;';
        entities['178'] = '&sup2;';
        entities['179'] = '&sup3;';
        entities['180'] = '&acute;';
        entities['181'] = '&micro;';
        entities['182'] = '&para;';
        entities['183'] = '&middot;';
        entities['184'] = '&cedil;';
        entities['185'] = '&sup1;';
        entities['186'] = '&ordm;';
        entities['187'] = '&raquo;';
        entities['188'] = '&frac14;';
        entities['189'] = '&frac12;';
        entities['190'] = '&frac34;';
        entities['191'] = '&iquest;';
        entities['192'] = '&Agrave;';
        entities['193'] = '&Aacute;';
        entities['194'] = '&Acirc;';
        entities['195'] = '&Atilde;';
        entities['196'] = '&Auml;';
        entities['197'] = '&Aring;';
        entities['198'] = '&AElig;';
        entities['199'] = '&Ccedil;';
        entities['200'] = '&Egrave;';
        entities['201'] = '&Eacute;';
        entities['202'] = '&Ecirc;';
        entities['203'] = '&Euml;';
        entities['204'] = '&Igrave;';
        entities['205'] = '&Iacute;';
        entities['206'] = '&Icirc;';
        entities['207'] = '&Iuml;';
        entities['208'] = '&ETH;';
        entities['209'] = '&Ntilde;';
        entities['210'] = '&Ograve;';
        entities['211'] = '&Oacute;';
        entities['212'] = '&Ocirc;';
        entities['213'] = '&Otilde;';
        entities['214'] = '&Ouml;';
        entities['215'] = '&times;';
        entities['216'] = '&Oslash;';
        entities['217'] = '&Ugrave;';
        entities['218'] = '&Uacute;';
        entities['219'] = '&Ucirc;';
        entities['220'] = '&Uuml;';
        entities['221'] = '&Yacute;';
        entities['222'] = '&THORN;';
        entities['223'] = '&szlig;';
        entities['224'] = '&agrave;';
        entities['225'] = '&aacute;';
        entities['226'] = '&acirc;';
        entities['227'] = '&atilde;';
        entities['228'] = '&auml;';
        entities['229'] = '&aring;';
        entities['230'] = '&aelig;';
        entities['231'] = '&ccedil;';
        entities['232'] = '&egrave;';
        entities['233'] = '&eacute;';
        entities['234'] = '&ecirc;';
        entities['235'] = '&euml;';
        entities['236'] = '&igrave;';
        entities['237'] = '&iacute;';
        entities['238'] = '&icirc;';
        entities['239'] = '&iuml;';
        entities['240'] = '&eth;';
        entities['241'] = '&ntilde;';
        entities['242'] = '&ograve;';
        entities['243'] = '&oacute;';
        entities['244'] = '&ocirc;';
        entities['245'] = '&otilde;';
        entities['246'] = '&ouml;';
        entities['247'] = '&divide;';
        entities['248'] = '&oslash;';
        entities['249'] = '&ugrave;';
        entities['250'] = '&uacute;';
        entities['251'] = '&ucirc;';
        entities['252'] = '&uuml;';
        entities['253'] = '&yacute;';
        entities['254'] = '&thorn;';
        entities['255'] = '&yuml;';
    }

    if (useQuoteStyle !== 'ENT_NOQUOTES') {
        entities['34'] = '&quot;';
    }
    if (useQuoteStyle === 'ENT_QUOTES') {
        entities['39'] = '&#39;';
    }
    entities['60'] = '&lt;';
    entities['62'] = '&gt;';


    // ascii decimals to real symbols
    for (decimal in entities) {
        symbol = String.fromCharCode(decimal);
        hash_map[symbol] = entities[decimal];
    }
    
    return hash_map;
}

function htmlentities (string, quote_style) {
    var hash_map = {}, symbol = '', tmp_str = '', entity = '';
    tmp_str = string.toString();
    
    if (false === (hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style))) {
        return false;
    }
    hash_map["'"] = '&#039;';
    for (symbol in hash_map) {
        entity = hash_map[symbol];
        tmp_str = tmp_str.split(symbol).join(entity);
    }
    
    return tmp_str;
}

function urlencode (str) {
    str = (str+'').toString();
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
                                                                    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

function substr (str, start, len) {
    var i = 0, allBMP = true, es = 0, el = 0, se = 0, ret = '';
    str += '';
    var end = str.length;

    this.php_js = this.php_js || {};
    this.php_js.ini = this.php_js.ini || {};
    switch(
        (this.php_js.ini['unicode.semantics'] && 
            this.php_js.ini['unicode.semantics'].local_value.toLowerCase())) {
        case 'on': // Full-blown Unicode including non-Basic-Multilingual-Plane characters
            // strlen()
            for (i=0; i < str.length; i++) {
                if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i+1))) {
                    allBMP = false;
                    break;
                }
            }

            if (!allBMP) {
                if (start < 0) {
                    for (i = end - 1, es = (start += end); i >= es; i--) {
                        if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i-1))) {
                            start--;
                            es--;
                        }
                    }
                }
                else {
                    var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
                    while ((surrogatePairs.exec(str)) != null) {
                        var li = surrogatePairs.lastIndex;
                        if (li - 2 < start) {
                            start++;
                        }
                        else {
                            break;
                        }
                    }
                }

                if (start >= end || start < 0) {
                    return false;
                }
                if (len < 0) {
                    for (i = end - 1, el = (end += len); i >= el; i--) {
                        if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i-1))) {
                            end--;
                            el--;
                        }
                    }
                    if (start > end) {
                        return false;
                    }
                    return str.slice(start, end);
                }
                else {
                    se = start + len;
                    for (i = start; i < se; i++) {
                        ret += str.charAt(i);
                        if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i+1))) {
                            se++; // Go one further, since one of the "characters" is part of a surrogate pair
                        }
                    }
                    return ret;
                }
                break;
            }
        case 'off': // assumes there are no non-BMP characters;
        default:
            if (start < 0) {
                start += end;
            }
            end = typeof len === 'undefined' ? end : (len < 0 ? len + end : len + start);
            return start >= str.length || start < 0 || start > end ? !1 : str.slice(start, end);
    }
    return undefined; // Please Netbeans
}
