$(document).ready(function(){
  /* Preload hover images */
  preloadImages('../images/logo-pentasz-em.png','../images/logo-pentasz-m.png','../images/logo-pentasz-gw.png','../images/logo-pentasz-v.png','../images/submenu-bottom-left.png', '../images/submenu-bottom-right.png', '../images/submenu-hole-top.png', '../images/submenu-left.png', '../images/submenu-right.png', '../images/submenu.png', '../images/news-sub-active-bg.png', '../images/pdf.png');
  Cufon.replace('h1')('h2')('.title');
  Cufon.replace('#top-menu > ul > li > a', {hover: true, fontFamily: 'Century Gothic'});
  
  /* Search form */
  toggleMultipleFocus('#search', '#newsletter-name', '#newsletter-email');
  $('#content-middle .p hr:last').hide();
  
  /* Newsletter form */
  toggleFocus('#sidebar form #name', 'Naam');
  toggleFocus('#sidebar form #email', 'E-mail');
  
  /* Start sidebar slideshow */
  if($('#header-holder img').length > 1) {
    slideshowIndex = $('#header-holder img').length - 1;
    setTimeout(startSlideshow, 5000);
  }
  
  /* Gemeenten */
  $('map area').click(function(e) {
    e.preventDefault();
  });
  $('map area').each(function() {
    $(this).hover(function() {
      $('#top-logo img').attr('src', 'images/logo-pentasz-' + $(this).attr('id') + '.png');
    }, function() {
      $('#top-logo img').attr('src', 'images/logo-pentasz.png');    
    });
  });
  
  /* FAQ */
  $('#content-middle .vraag').click(function() {
    if(!$(this).next().is(':visible')) {
      $(this).css({borderBottom:'none', background:"url('../images/list-arrow-down.png') 0px 6px no-repeat"});
      $(this).next().slideDown();
    } else {
      $(this).css({borderBottom:'#9ca1a6 1px solid', background:"url('../images/list-arrow.png') 0px 6px no-repeat"});
      $(this).next().slideUp(200);
    }
  });
  
  /* Fotoalbum */ 
  if($('.fotoalbum').length > 0) {
    $('.fotoalbum').click(function() {
      window.location.href += $(this).attr('rel');
    });
  }
  if($('.fotoalbum-detail').length > 0) {
    var w = $('.fotoalbum-detail .thumbs .mask .holder .thumb').length * 76;
    $('.fotoalbum-detail .thumbs .left').click(function() {
      if($('.fotoalbum-detail .thumbs .mask .holder').css('left').replace('px', '') < 0) {
        $('.fotoalbum-detail .thumbs .mask .holder').animate({left:'+=80px'}, 400);
      }
    });
    $('.fotoalbum-detail .thumbs .right').click(function() {
      if(Number($('.fotoalbum-detail .thumbs .mask .holder').css('left').replace('px', '')) > 560 - w) {
        $('.fotoalbum-detail .thumbs .mask .holder').animate({left:'-=80px'}, 400);
      }
    });
    $('.fotoalbum-detail .thumbs .mask .holder .thumb img').click(function() {
      var newSrc = $(this).attr('src').replace('_thumb', '');
      $('.fotoalbum-detail .groot .foto img').fadeOut(200, function() {
        $(this).attr('src', newSrc).fadeIn(400);
      });
      $('.fotoalbum-detail .groot .titel').html($(this).attr('alt'));
    });
  }
  
  /* Sidebar subnav */
  $('#sidebar-news-middle > ul > li > ul > li > ul > li.active').each(function() {
    $(this).parent().parent().addClass('active');
  });
  $('#sidebar-news-middle > ul > li > ul > li.active').each(function() {
    $(this).parent().parent().addClass('active');
  });
  $('#sidebar-news-middle > ul > li > ul > li:last > div').css({background:'none'});

  /* News scroll actions */
  if($('.news-item').length > 0) {
    $('.news-item h2').click(function() {
      $('.news-item .holder').not($(this).parent().find('.holder')).hide();
      $(this).parent().find('.holder').toggle();
    });
    checkHash();
    $('#sidebar-news-middle > ul > li > ul.news > li > div > a').click(function() {
      setTimeout(checkHash, 100);
    });
    $('#sidebar-highlight-corners').click(function() {
      setTimeout(checkHash, 100);
    });
    $('#sidebar-agenda-middle ul li a').click(function() {
      setTimeout(checkHash, 100);
    });
  }
  
  /* Footer */
  $('#content-bottom .print').click(function(e) {
    e.preventDefault();
    window.print();
  });
  
  /* Submenu */
  $('.submenu-holder > .submenu').each(function() {
    $(this).css({top:'-' + $(this).height() +'px'});
  });
  $('.submenu-holder').parent().hover(function() {
    $(' > a', this).css('background', "url('../images/submenu-hole-top.png') no-repeat");
    $(' > .submenu-holder > .submenu', this).stop().animate({top:'0px'}, 200);
    $(' > .submenu-holder', this).css({width:$(' .submenu', this).width() + 'px', height:$(' .submenu', this).height() + 'px'});
  }, function() {
    $(' > a', this).css('background', '');
    $(' > a.active', this).css('background', "url('../images/menu-active.png') 5px 3px no-repeat");
    $(' > .submenu-holder > .submenu', this).stop().animate({top:'-' + $(' > .submenu-holder > .submenu', this).height() + 'px'}, {duration:100, complete:function() {
      $(this).parent().css({width:0, height:0});
    }});
  });
  $('#top-menu > ul > li > a.active').css({background:"url('../images/menu-active.png') 5px 3px no-repeat", color:'#9c0058'});
});

function checkHash() {
  var h = window.location.hash;
  if(h != '' && $(h).length > 0) {
    $('.news-item .holder').hide();
    scrollTo(h);
  }
}
var slideshowIndex = 0;
function startSlideshow() {
  $('#header-holder img').eq(slideshowIndex).fadeOut(1000, function() {
    slideshowIndex--;
    if(slideshowIndex < 1) {
      setTimeout(resetSlideshow, 5000);
    } else {
      setTimeout(startSlideshow, 5000);
    }
  });
}
function resetSlideshow() {
  slideshowIndex = $('#header-holder img').length - 1;
  $('#header-holder img').eq(slideshowIndex).fadeIn(1000, function() {
    $('#header-holder img').show();
    setTimeout(startSlideshow, 5000);
  });
}
function scrollTo(id) {
  $(id + ' .holder').show();
  $('html, body').animate({scrollTop:$(id).offset().top - 25}, {queue:false, duration:1200, easing:'easeInOutCubic'});
}
function preloadImages() {
  for(var i = 0; i < arguments.length; i++) {
    var oImg = new Image();
    oImg.src = arguments[i];
  }
}
function toggleMultipleFocus() {
  for(var i = 0; i < arguments.length; i++) {
    toggleFocus(arguments[i]);
  }
}
function toggleFocus(id) {
  var val = $(id).val();
  $(id).focus(function() {
    if($(this).val() == val) {$(this).val('');}
  }).blur(function() {
    if($(this).val() == '') {$(this).val(val);}
  });
}
