$(document).ready(function()
{
  // cookies

  var COOKIE_NAME = 'aanbod';
  var options = {path: '/', expires: 10};

  if(!$.cookie(COOKIE_NAME))
  {
    $.cookie(COOKIE_NAME, 4, options);
  }
  // select first personeel from unsorted list
  
  $('.page_personeel_desc').css("display", "block");

  var titel = $('#personeel_lijst li').eq(0).children('.personeel_titel').html();
  var text  = $('#personeel_lijst li').eq(0).children('.personeel_text').html();
  var foto  = $('#personeel_lijst li').eq(0).children('.personeel_foto').html();

  $('.page_personeel_desc').html(titel);
  $('#page_personeel_text').html(text);
  $('.page_personeel_photo').html(foto);

  // jquery switch met personeel

  $('#personeel_lijst li').click(function()
  {
    $('#personeel_lijst li').css('background', '#8c8273');

    $('.page_personeel_desc').css("display", "block");

    var titel = $(this).children('.personeel_titel').html();
    var text  = $(this).children('.personeel_text').html();
    var foto  = $(this).children('.personeel_foto').html();

    $(this).css('background', '#FF931E');

    $('.page_personeel_desc').html(titel);
    $('#page_personeel_text').html(text);
    $('.page_personeel_photo').html(foto);
    
  });

  // hovers voor aanbod

  $('.active').css("background", "#FF931E");

  $('#hover1').hover(function()
  {
    $('#hover1 img').css("background", "#FF931E");
  }, function (){
    $('#hover1 img').css("background", "#FFFFFF");
    $('.active').css("background", "#FF931E");
  });

  $('#hover2').hover(function()
  {
    $('#hover2 img').css("background", "#FF931E");
  }, function (){
    $('#hover2 img').css("background", "#FFFFFF");
    $('.active').css("background", "#FF931E");
  });

  $('#hover3').hover(function()
  {
    $('#hover3 img').css("background", "#FF931E");
  }, function (){
    $('#hover3 img').css("background", "#FFFFFF");
    $('.active').css("background", "#FF931E");
  });

  $('#hover4').hover(function()
  {
    $('#hover4 img').css("background", "#FF931E");
  }, function (){
    $('#hover4 img').css("background", "#FFFFFF");
    $('.active').css("background", "#FF931E");
  });

  $('#hover5').hover(function()
  {
    $('#hover5 img').css("background", "#FF931E");
  }, function (){
    $('#hover5 img').css("background", "#FFFFFF");
    $('.active').css("background", "#FF931E");
  });

  $("#aanbod").animate({opacity: 0}, 1);
  
  $("#aanbod_click").hover(function()
  {
    $("#aanbod").animate({opacity: 0.7}, 900);
    clearTimeout(aanbodtimer);
  },function()
  {
    aanbodtimer = setTimeout(function()
    {
      $("#aanbod").animate({opacity: 0}, 900);
    }, 8000);
  });

  $("#nieuws").animate({opacity: 0}, 1);

  $("#weblog_click").hover(function()
  {
    $("#nieuws").animate({opacity: 0.7}, 900);
    clearTimeout(nieuwstimer);
  },function()
  {
    nieuwstimer = setTimeout(function()
    {
      $("#nieuws").animate({opacity: 0}, 900);
    }, 8000);
  });

  // checkboxes

  var aanbodItemType1 = $(".aanbod_item.type1");
  var aanbodItemType2 = $(".aanbod_item.type2");

  var aanbodItemRefType1 = $(".aanbod_item_ref.type1");
  var aanbodItemRefType2 = $(".aanbod_item_ref.type2");

  // Type keuze
  function showKoop()
  {
    aanbodItemType2.show();
    aanbodItemRefType2.show();
    aanbodItemType1.hide();
    aanbodItemRefType1.hide();
    $('.verhuurd').parent().parent().hide();
    $('.verkocht').parent().parent().show();
  }
  function showHuur()
  {
    aanbodItemType1.show();
    aanbodItemRefType1.show();
    aanbodItemType2.hide();
    aanbodItemRefType2.hide();
    $('.verhuurd').parent().parent().show();
    $('.verkocht').parent().parent().hide();
  }
  function showBeide()
  {
    aanbodItemType1.show();
    aanbodItemRefType1.show();
    aanbodItemType2.show();
    aanbodItemRefType2.show();
    $('.verhuurd').parent().parent().show();
    $('.verkocht').parent().parent().show();
  }
  
  if($.cookie('type') == null)
  {
    $.cookie('type', 'beide', { path: '/'});
  }

  if($.cookie('type') == 'koop')
  {
    $("#choose2").attr('checked', 'checked');
    showKoop();
  }
  if($.cookie('type') == 'huur')
  {
    $("#choose1").attr('checked', 'checked');
    showHuur();
  }
  if($.cookie('type') == 'beide')
  {
    $("#choose4").attr('checked', 'checked');
    showBeide();
  }
  // Alleen koop
  $("#choose2").click(function(){
    showKoop();
    $.cookie('type', 'koop', { path: '/'});
  });

  // Alleen huur
  $("#choose1").click(function(){
    showHuur();
    $.cookie('type', 'huur', { path: '/'});
  });

  // Alles
  $("#choose4").click(function(){
    showBeide();
    $.cookie('type', 'beide', { path: '/'});
  });
  
  // colorbox

  $("a[rel='example1']").colorbox();

  // Trackitems carrousel

  function showTrackerItem(trackerBox, total, current)
  {
    if(total > 1)
    {
      if(current == (total - 1))
      {
        current = 0;
      }
      else
      {
        current++;
      }
      setTimeout(function(){
        showTrackerItem(trackerBox, total, current );
      }, 4500);
    
    trackerBox.find('li').hide();
    trackerBox.find('li').eq(current).fadeIn();
    }
  }
  $('.tracker_box').find('li').hide();
  $('#tracker_box_1').find('li').eq(0).show();
  $('#tracker_box_2').find('li').eq(0).show();
  $('#tracker_box_3').find('li').eq(0).show();

  setTimeout(function(){
    showTrackerItem( $('#tracker_box_1'), $('#tracker_box_1').find('li').length, 0);
  }, 2000);
  setTimeout(function(){
    showTrackerItem( $('#tracker_box_2'), $('#tracker_box_2').find('li').length, 0);
  }, 3000);
  setTimeout(function(){
    showTrackerItem( $('#tracker_box_3'), $('#tracker_box_3').find('li').length, 0);
  }, 4000);

  $('.objectimage img').each(function() {
      var maxWidth = 247; 
      var maxHeight = 150;   
      var ratio = 0;  
      var width = $(this).width();    
      var height = $(this).height();  

      if(width > maxWidth){
          ratio = maxWidth / width;   
          $(this).css("width", maxWidth); 
          $(this).css("height", height * ratio); 
          height = height * ratio;    
          width = width * ratio;    
          if((maxWidth - width) / 2 > 0)
          {
            $(this).css('marginLeft', (maxWidth - width) / 2);
          }
          if((maxHeight - height) / 2 > 0)
          {
            $(this).css('marginTop', (maxHeight - height) / 2);
          }
      }
      if(height > maxHeight){
          ratio = maxHeight / height; 
          $(this).css("height", maxHeight); 
          $(this).css("width", width * ratio);  
          width = width * ratio;   
          if((maxWidth - width) / 2 > 0)
          {
            $(this).css('marginLeft', (maxWidth - width) / 2);
          }
          if((maxHeight - height) / 2 > 0)
          {
            $(this).css('marginTop', (maxHeight - height) / 2);
          }
      }
  });

});

var nieuwstimer;
var aanbodtimer;

