$(document).ready(function(){
  
  /* Fix the IE z-index bug */
  if ($.browser.msie && $.browser.version.substr(0,1)<8) {
  }
  
  $( 'span.addthis_hover' ).click(function()
  {
    $('div.addthis_toolbox').toggle();
  })

  var zIndexNumber = 1000;

  $('div').each(function(){

  $(this).css('zIndex', zIndexNumber);

  zIndexNumber -= 1;

  });

  /* Accordeon jQuery effects */
  $('#accordeon li:first-child').css("width", 200);

  $('#accordeon li').mouseover(function(){
    if($(this).css('width') == '30px')
    {
      $('#accordeon li').animate( {width: "30px"}, {queue: false, duration: 400, easing: 'swing'});
      //$('#accordeon .accordeon_content').hide();

      $(this).animate( {width: "200px"}, {queue: false, duration: 400, easing: 'swing'});
      //$(this).find('.accordeon_content').show();
    }

    /* Track accordeon actions for Google Analytics */
    pageTracker._trackEvent('Topcases', 'Browse cases');
    
  });
  
  var fTimer;

  $('#mainmenu > ul > li').mouseover(function(){

    $('#mainmenu > ul > li').css("background", "none");
    $('#mainmenu > ul > li').find('a').css("color", "#CCC");
    $('#mainmenu > ul > li').find('ul').hide();

    clearTimeout(fTimer);
    $(this).find('a').css("color", "#517395");
    $(this).find('ul li a').css("color", "#FFF");
    $(this).css("background", "url(../../../images/main_menu_hover.png) no-repeat");
    $(this).css("backgroundPosition", "50% 0px");
    $(this).find('ul').show();

    /* Track menuevents for Google Analytics */
    pageTracker._trackEvent('Mainmenu', 'Browse mainmenu');

  });
  $('#mainmenu > ul > li').mouseout(function(){
    fTimer = setTimeout(function(){
      $('#mainmenu > ul > li').css("background", "none");
      $('#mainmenu > ul > li').find('a').css("color", "#CCC");
      $('#mainmenu > ul > li').find('ul').hide();
    }, 2000);
  });

  /* Resize first column, depending on highest available other column */
  var col_one_height = $(".column_one").height();
  var col_two_height = $(".column_two").height();
  var col_three_height = $(".column_three").height();
  var newHeight;
  if (!col_two_height)
  {
    col_two_height = $(".single_column").height();
  }

  if(col_two_height > col_three_height)
  {
    if(col_one_height < col_two_height)
    {
      newHeight = col_two_height;
      $(".column_one").css("height", newHeight + 30);
    }
  }
  else
  {
    if(col_one_height < col_three_height)
    {
      newHeight = col_three_height;
      $(".column_one").css("height", newHeight + 30);
    }
  }

  // Resize column after loading searchresults
  setTimeout(function(){

    var col_one_height = $(".column_one").height();
    var col_two_height = $(".column_two").height();
    var col_three_height = $(".column_three").height();
    var newHeight;
    if(col_one_height < col_two_height)
    {
      if(col_three_height > col_two_height)
      {
        newHeight = col_three_height;
        $(".column_two_height").css("height", newHeight);
        newHeight = col_three_height + 30;
        $(".column_one").css("height", newHeight);
      }
      if(col_two_height > col_three_height)
      {
        newHeight = col_two_height;
        $(".column_three_height").css("height", newHeight);
        newHeight = col_two_height + 30;
        $(".column_one").css("height", newHeight);
      }
    }
  }, 1200);

  /*
   * Monthpager behaviour
   */
  //alert($('.month_pager ul').css("marginLeft"));
  $(".pager_left").mousedown(function(){
    var currentMargin = parseInt($(this).parent().find("li").css("marginLeft"));
    var newMargin = currentMargin + 50;
    $(this).parent().find("li").eq(0).css("marginLeft", newMargin);
    
  });

  $(".pager_right").mousedown(function(){
    var currentMargin = parseInt($(this).parent().find("li").css("marginLeft"));
    var newMargin = currentMargin - 50;
    $(this).parent().find("li").eq(0).css("marginLeft", newMargin);
    
  });

  /*
   * Agenda registration introducee behaviour
   */
  if($("#agendaregistration_introducee"))
  {
    $("#agendaregistration_introducee_name").hide();
    $("label[for=agendaregistration_introducee_name]").hide();

    $("#agendaregistration_introducee_email").hide();
    $("label[for=agendaregistration_introducee_email]").hide();

    $("#agendaregistration_introducee_phone").hide();
    $("label[for=agendaregistration_introducee_phone]").hide();

    $("#agendaregistration_introducee").click(function(){
      if($("#agendaregistration_introducee_name").is(":hidden"))
      {
        $("#agendaregistration_introducee_name").fadeIn();
        $("label[for=agendaregistration_introducee_name]").fadeIn();

        $("#agendaregistration_introducee_email").fadeIn();
        $("label[for=agendaregistration_introducee_email]").fadeIn();

        $("#agendaregistration_introducee_phone").fadeIn();
        $("label[for=agendaregistration_introducee_phone]").fadeIn();
      }
      else
      {
        $("#agendaregistration_introducee_name").fadeOut();
        $("label[for=agendaregistration_introducee_name]").fadeOut();

        $("#agendaregistration_introducee_email").fadeOut();
        $("label[for=agendaregistration_introducee_email]").fadeOut();

        $("#agendaregistration_introducee_phone").fadeOut();
        $("label[for=agendaregistration_introducee_phone]").fadeOut();
      }

    });

    /*
     * Agenda behaviour
     */
    $("#show_for_clients").click(function(){

      if($("#show_for_clients").attr("checked"))
      {
        $(".for_clients").slideDown();
        $(".for_both").slideDown();
      }
      else
      {
        $(".for_clients").slideUp();
        if(!$("#show_for_employees").attr("checked"))
        {
          $(".for_both").slideUp();
        }
      }
    });
    $("#show_for_employees").click(function(){
      if($("#show_for_employees").attr("checked"))
      {
        $(".for_employees").slideDown();
        $(".for_both").slideDown();
      }
      else
      {
        $(".for_employees").slideUp();
        if(!$("#show_for_clients").attr("checked"))
        {
          $(".for_both").slideUp();
        }
      }
    });
  }

  setTimeout(function(){
    $('.msg').fadeOut();
  }, 2000);

  $('#all_jobs').click(function(){
    $('#job_vakgebied').val('');
    $('#job_trefwoord').val('');
    document.search_job.submit();
    return false;
  });

  $('#all_specialists').click(function(){
    $('#specialist_vakgebied').val('');
    $('#specialist_trefwoord').val('');
    document.search_specialist.submit();
    return false;
  });

  /*
   * Custom selectbox
   */

  var select_timer;

  // Show dropdown
  $('.custom_select input').click(function(){
    $(this).parent().find('ul').fadeIn();
  });

  $('.custom_select img').click(function(){
    $(this).parent().find('ul').fadeIn();
  });

  // Mouseovereffect selection
  $('.custom_select > ul > li').mouseover(function(){
    $(this).css('background', '#4B688A');
    $(this).css('color', '#FFF');
  });

  // Mouseouteffect selection
  $('.custom_select > ul > li').mouseout(function(){
    $(this).css('background', 'none');
    $(this).css('color', '#8E8479');
  });

  // Selection
  $('.custom_select ul li').click(function(){
    $(this).parent().parent().find('input').val($(this).attr('title'));
    $(this).parent().fadeOut();
  });

  $('.custom_select > ul').mouseout(function(){
    select_timer = setTimeout(function(){
        $('.custom_select > ul').fadeOut();
      }, 400);
  });
  $('.custom_select > ul').mouseover(function(){
    clearTimeout(select_timer);
  });

  if ($('#applicationform_origin').val() != 'anders')
  {
    $('#applicationform_origin_other').parent().hide();
  }

  $('#applicationform_origin').change(function(){
    if ($(this).val() == 'anders')
    {
      $('#applicationform_origin_other').parent().show();
    }
    else
    {
      $('#applicationform_origin_other').parent().hide();
    }
  });


});

