$(function() {
	

// TWEEDEHANDS
if ($('a.tweedehands')) {
$('a.tweedehands').click(function(event) {
	event.preventDefault();
$.ajax({
  url: this.href,
  cache: false,
  beforeSend: function () {
  $('#tweedehands_lijst').empty().addClass('ajax-loading');	
  },
  success: function(html) {
    $('#tweedehands_lijst').html(html);
	},
  complete: function() {
  	$('#tweedehands_lijst').removeClass('ajax-loading');
  	//$.getScript("js/load.js");
  }
});
});
}


// MIEUWSBERICHT REAGEREN
if ($('#reageren_submit_submit')) {
$('#reageren_submit_submit').click(function(event) {
	event.preventDefault();
	var queryString = $('#reageren_submit').formSerialize(); 
 	var url = $('#reageren_submit').attr('action');
$.ajax({
  url: url,
  type: "POST",
  data: queryString,
  cache: false,
  beforeSend: function () {
  $('#reageren').empty().addClass('ajax-loading');	
  },
  success: function(html) {
    $('#reageren').removeClass('ajax-loading');
	},
  complete: function() {
  	window.location.reload(true);
  }
});
});
}
   

// POLL REAGEREN
if ($('#reageren_submit_submit_poll')) {
$('#reageren_submit_submit_poll').click(function(event) {
	event.preventDefault();
	var queryString = $('#reageren_submit_poll').formSerialize(); 
 	var url = $('#reageren_submit_poll').attr('action');
$.ajax({
  url: url,
  data: queryString,
  cache: false,
  beforeSend: function () {
  $('#reageren').empty().addClass('ajax-loading');	
  },
  success: function(html) {
    $('#reageren').removeClass('ajax-loading');
	},
  complete: function() {
  	window.location.reload(true);
  }
});
});
}


// CAPTCHA REGISTREREN PARTICULIER
if ($('#regform_particulieren')) {

$("#regform_particulieren").validate({
		rules: {
			naam: {
				required: true				
			},
			weergavenaam: {
				equalTo: "#weergavenaam_registreren"
			},
			emailadres: {
				required: true,
				email: true
			},
			emailadres_bevestig: {
				required: true,
				email: true,
				equalTo: "#emailadres_registreren"
			},

			wachtwoord: {
				required: true				
			},
			wachtwoord_bevestig: {
				required: true,
				equalTo: "#wachtwoord"
			}

		},
		messages: {
			naam: {
				required: "Voer uw naam volledige in"
			},
			emailadres: {
				required: "Voer je emailadres in",
				email: "Voer een geldig emailadres in"
			},
			emailadres_bevestig: {
				required: "Voer het emailadres nogmaals in",
				email : "Voer een geldig emailadres in",
				equalTo: "Het emailadres moet gelijk zijn"
			},
			wachtwoord: "Voer een wachtwoord in",
			wachtwoord_bevestig: {
				required: "Voer het wachtwoord nogmaals in",
				equalTo: "Het wachtwoord moet gelijk zijn"
			}
		}
	});
	
	// check if confirm password is still valid after password changed
	$("#wachtwoord").blur(function() {
		$("#wachtwoord_bevestig").valid();
	});
}
    
   
// CAPTCHA REGISTREREN BEDRIJF
if ($('#regform_bedrijven')) {
$("#regform_bedrijven").validate({
		rules: {
			bedrijfsnaam: "required",
			contactpersoon: "required",
			emailadres: {
				required: true,
				email: true
			},
			emailadres_bevestig: {
				required: true,
				email: true,
				equalTo: "#emailadres_registreren"
			},

			wachtwoord: {
				required: true
				
			},
			wachtwoord_bevestig: {
				required: true,
				equalTo: "#wachtwoord"
			}

		},
		messages: {
			bedrijfsnaam: "Voer je bedrijfsnaam in",
			contactpersoon: "Voer je naam in",
			emailadres: {
				required: "Voer je emailadres in",
				email: "Voer een geldig emailadres in"
			},
			emailadres_bevestig: {
				required: "Voer het emailadres nogmaals in",
				email: "Voer een geldig emailadres in",
				equalTo: "Het emailadres moet gelijk zijn"
			},
			wachtwoord: "Voer een wachtwoord in",
			wachtwoord_bevestig: {
				required: "Voer het wachtwoord nogmaals in",
				equalTo: "Het wachtwoord moet gelijk zijn"
			}
		}
	});
	
	// check if confirm password is still valid after password changed
	$("#wachtwoord").blur(function() {
		$("#wachtwoord_bevestig").valid();
	});
}


// wordt het emailadres al gebruikt?
$("#emailadres_inloggen").focus(function() { 
this.value=''; });

if ($('#emailadres_registreren')) {

$("#emailadres_registreren").change( function() {
  var a = this;
$.get("modules/registreren/ajax/index.php", { emailadres: this.value },
function(data){
if (data == 1) { $("#span_email").html("<strong style='color:red;'>Dit emailadres bestaat al!</strong>");
a.value='';  	
} else {
$("#span_email").html("* emailadres is tevens je gebruikersnaam");
} });
});
}

// wordt de weergavenaam al gebruikt?
if ($('#weergavenaam_registreren')) {

$("#weergavenaam_registreren").change( function() {
  var a = this;
$.get("modules/registreren/ajax/index2.php", { weergavenaam: this.value },
function(data){
if (data == 1) { $("#span_weergavenaam").html("<strong style='color:red;'>Deze weergavenaam bestaat al!</strong>");
a.value='';  	
} else {
$("#span_weergavenaam").html("* emailadres is tevens je gebruikersnaam");
} });
});
}




	
// INLOGGEN WEBSITE / WIJZIGEN INLOG
$("#wachtwoord_inloggen").focus(function() { 
if (this.value == 'wachtwoord') {
$("#verander").html("<input name=\"wachtwoord\" type=\"password\" id=\"wachtwoord_inloggen2\" style=\"font-size: 11px;\" size=\"13\">") ;
$("#wachtwoord_inloggen2").focus();
} });



// VAN WIES IS DAT ER IEN FUNCTIES
if ($('a.wies_toggle')) {
$("a.wies_toggle").click(function() { //alert(this.id);
$("#"+this.rel).toggle("slow");
}); }

if ($('.wies_nummering')) {
$('.wies_nummering').click(function(event) {
	event.preventDefault();
$.ajax({
  url: this.href,
  cache: false,
  beforeSend: function () {
  $("#bladeren").fadeOut('slow');	
  },
  success: function(html) {
    $("#bladeren").html(html);
	},
  complete: function() {
  	$("#bladeren").fadeIn('fast');
  	$.getScript("js/load.js");
  }
});
});
}

// WIST U DATJES FUNCTIES
if ($('.datjes_nummering')) {
$('.datjes_nummering').click(function(event) {
	event.preventDefault();
$.ajax({
  url: this.href,
  cache: false,
  beforeSend: function () {
  $("#bladeren").fadeOut('slow');	
  },
  success: function(html) {
    $("#bladeren").html(html);
	},
  complete: function() {
  	$("#bladeren").fadeIn('fast');
  	$.getScript("js/load.js");
  }
});
});
}


// GALLERIA FUNCTIES
if ('ul.gallery') {
	
		$('ul.gallery').galleria(
{
			history   : true, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Volgende plaatje >>>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		});
	
}


// ZOEKEN FUNCTIES
if ($("#zoekwoord")) {	
	$("#zoekwoord").focus();
	$("#zoekwoord").keyup(function() { $("#zoekwoord").css("border", "1px solid #000000"); });
}
if ($("#zoekbutton")) {
	$("#zoekbutton").click(function() {
	var data = document.getElementById('zoekwoord').value;
	if (data == "") {
	$("#zoekwoord").css("border","1px solid #FF0000");
	}else {
	var url ="/modules/zoeken/zoeken.php";
$.ajax({
  url: url,
  type: "POST",
  data: "zoekwoord="+data,
  cache: false,
  beforeSend: function () {
  $('#zoekresultaten').empty().addClass('ajax-loading');	
  },
  success: function(html) {
    $('#zoekresultaten').html(html);
	},
  complete: function() {
  	$('#zoekresultaten').removeClass('ajax-loading');
  }
});
}
		
});
}


// HOOFDMENU > SUBMENU'S
function megaHoverOver(){
$(this).find(".sub").stop().fadeTo('fast', 1).show();
	
//Calculate width of all ul's
(function($) { 
	jQuery.fn.calcSubWidth = function() {
		rowWidth = 0;
		//Calculate row
		$(this).find("ul").each(function() {					
			rowWidth += $(this).width(); 
		});	
	};
})(jQuery); 

if ( $(this).find(".row").length > 0 ) { //If row exists...
	var biggestRow = 0;	
	//Calculate each row
	$(this).find(".row").each(function() {							   
		$(this).calcSubWidth();
		//Find biggest row
		if(rowWidth > biggestRow) {
			biggestRow = rowWidth;
		}
	});
	//Set width
	$(this).find(".sub").css({'width' :biggestRow});
	$(this).find(".row:last").css({'margin':'0'});
	
} else { //If row does not exist...
	
	$(this).calcSubWidth();
	//Set Width
	$(this).find(".sub").css({'width' : rowWidth});
	
}
}

function megaHoverOut(){ 
$(this).find(".sub").stop().fadeTo('fast', 0, function() {
  $(this).hide(); 
});
}


var config = {    
 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
 interval: 10, // number = milliseconds for onMouseOver polling interval    
 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
 timeout: 300, // number = milliseconds delay before onMouseOut    
 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
};

$("ul#topnav li .sub").css({'opacity':'0'});
$("ul#topnav li").hoverIntent(config);



});