//SLALEEUW-114 RRB: Aanpassing reactie formulier 
$(document).ready(function() {
	// hides the comments as soon as the DOM is ready
	if ($("#morecomments").length > 0) { //check if morecomments element is available in the DOM
		$('#morecomments').hide();
	}
	// hides the commentform as soon as the DOM is ready
	if ($("#comment-form").length > 0) {
		$('#comment-form').hide();
	}
	// shows the commentform, morecomments and goes to the commentform anchor
	$('a.commentlink').click(function() {
		$('#morecomments').show('slow');
		$('#comment-form').show('slow');
		// get the top offset of the target anchor
		var target_offset = $("#comment-form").offset();
		var target_top = target_offset.top;
		// goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate( {
			scrollTop : target_top
		}, 500);
		return false;
	});
	//shows commentform and morecomments
	$('a#morecommentslink').click(function() {
		$('#morecomments').show('slow');
		$('#comment-form').show('slow');
		return false;
	});
});

jQuery.fn.collapse = function() {
 $(this).fadeOut(100).prevAll('a').removeClass('active');
}
var firstopen=0;
$(function() {
  var hasSmoothing = function() {
  if (typeof(screen.fontSmoothingEnabled) != 'undefined') {
   return screen.fontSmoothingEnabled;
  } else {
   try {
    var canvasNode = document.createElement('canvas');
    canvasNode.width = '35';
    canvasNode.height = '35'
    canvasNode.style.display = 'none';
    document.body.appendChild(canvasNode);
    var ctx = canvasNode.getContext('2d');
    ctx.textBaseline = 'top';
    ctx.font = '32px Arial';
    ctx.fillStyle = 'black';
    ctx.strokeStyle = 'black';
    ctx.fillText('O', 0, 0);
    for (var j = 8; j <= 32; j++) {
     for (var i = 1; i <= 32; i++) {
      var imageData = ctx.getImageData(i, j, 1, 1).data, alpha = imageData[3];
      if (alpha != 255 && alpha != 0) {
       return true;
      }
     }
    }
    return false;
   } catch (ex) {
    return null;
   }
  }
 }
 var result = hasSmoothing();
 if (result == true) {
  $('html').addClass('smoothing-true');
 } else if (result == false) {
  $('html').addClass('smoothing-false');
 } else {
  $('html').addClass('smoothing-unknown');
 }

 var navLists = $('#navigation li ul');
 navLists.each(function() {
  $(this).parent().addClass('has-toggle').children('a').before($('<a href="#" class="toggle" title="Submenu tonen">Toon submenu</a>')).mouseover(function() {
   navLists.collapse();
   var list = $(this).next();
   if (list.is(':visible')) {
    list.collapse();
   } else {
    list.slideDown(150);
    $(this).addClass('active');
   }
   return false;
  }).hover(function() {
   $(this).addClass('hovering');
  }, function() {
   $(this).removeClass('hovering');
  }).focus(function() {
   $(this).addClass('hovering');
  }).blur(function() {
   $(this).removeClass('hovering');
  })
 });
 
 $('body').click(function() {
  navLists.collapse();
 });

 if ($('#carrousel').length) {
  var carrouselNav = $('#carrousel-nav'), carrousel = $('#carrousel ul');
  $('a', carrouselNav).click(function() {
   var href = $(this).attr('href');
   if (carrousel.data('href') != href) {
    $(href).hide().appendTo(carrousel).fadeIn(200);
    $(this).parent().siblings().removeClass('active').end().addClass('active');
    carrousel.data('href', href);
   }
   return false;
  }).eq(0).click();
  var playCarrousel = true;
  function cancelPlay() {
   playCarrousel = false;
  }
  function startPlay() {
   playCarrousel = true;
  }
  carrouselNav.add(carrousel).hover(cancelPlay, startPlay).find('a').focus(cancelPlay).blur(startPlay);
  function rotate() {
   if (playCarrousel) {
    var active = $('.active', carrouselNav);
    if (active.next().length) {
     active.next().find('a').click();
    } else {
     active.siblings().eq(0).find('a').click();
    }
   }
  }
  setInterval(rotate, 5000);
 }

 $('#theme-navigation a').click(function() {
  $(this).parent().addClass('active').siblings().removeClass('active');
  $($(this).attr('href')).fadeIn().siblings().hide();
  return false;
 }).eq(0).click();

$('.collapsable').each(function() {
	  var that = this, heading = $('h2', that);
	  $('.simple ul', that).masonry({singleMode: true, resizeable: false});
	  heading.keypress(function(e) {
	   if (e.keyCode == 13 || e.keyCode == 0) {
	    $(this).parent().toggleClass('open').end().next('.section').slideToggle();
	   }
	  }).click(function() {
	   $(this).parent().toggleClass('open').end().next('.section').slideToggle();
	  }).attr('tabindex', 0);
	  
	  if (!$(that).hasClass('open')) {
      //standaard gesloten
		   if (firstopen>=2){
		     heading.next('.section').slideUp();
		   }
       else{
    	   $(this).toggleClass('open').end().next('.section').slideToggle();
		     firstopen=firstopen+1;
       }
	  } 
	  else {
		   //Standaard al een open
		  if (firstopen>=2){
			 // $(this).toggleClass('open').end().next('.section').slideToggle();
		  }
	    firstopen=firstopen+1;
	  }
	 });

});


if (!!Drupal.jsAC) { // this condition tests if autocomplete has been enabled 
    Drupal.jsAC.prototype.hidePopup = function (keycode) {
      // Select item if the right key or mousebutton was pressed
      if (this.selected && ((keycode && keycode != 46 && keycode != 8 && keycode != 27) || !keycode)) {
        this.input.value = this.selected.autocompleteValue;
      	 if (this.input.id=="edit-keys" || this.input.id=="edit-search-block-form-1") {
	 	document.getElementById(edit-search-block-form-1).value=this.selected.autocompleteValue;
        	$('#search-block-form').unbind('submit'); // replace '#search-theme-form' with your search form id
        	$('#search-block-form').submit();
	 }
      }
      // Hide popup ONLY if autocomplete
        var popup = this.popup;
        if (popup) {
          if (this.popup.id=='autocomplete') {
            this.popup = null;
            $(popup).fadeOut('fast', function() { $(popup).remove(); });
          }
        }
        this.selected = false;
    };
}
//SLALEEUW-29 retain apachesolr search filters 
if ($('#edit-apachesolr-search-retain-filters').length){
  $('#edit-apachesolr-search-retain-filters').attr('checked', 'checked');
}
//show gmap if javascript is available 
if ($('#gmap-auto1map-gmap0').length){
  $('#gmap-auto1map-gmap0').show();
}

