$(document).ready(function() {
  $('.contactAanvraagLink').click(function(e) {
    e.preventDefault();
    
    var type = $(this).text();
    var huis = $(this).attr('title');
    
    $.fancybox({
      type: 'iframe',
      href: '/php/verwerk/contact.php?type=' + type + '&huis=' + huis
    });
  });
  
  $('.contactTarieven').click(function(e) {
    e.preventDefault();
    
    var type = $(this).attr('title');
    
    $.fancybox({
      type: 'iframe',
      href: '/php/verwerk/contactTarieven.php?type=' + type
    });
  });  
  
  $('.internationaalContact').click(function(e) {
    e.preventDefault();

    $.fancybox({
      type: 'iframe',
      href: '/php/verwerk/contactInternationaal.php'
    });
  });
  
	//Execute the slideShow
  if ($('#homeGallery').length > 0) {
	  slideShow();
  }  
  
  // Nieuwsbrief
    $("#nieuwsbriefNaam").click(function() {
        if($("#nieuwsbriefNaam").val() == 'Uw naam') {
            $("#nieuwsbriefNaam").val('');
        }    
    });
    
    $("#nieuwsbriefNaam").blur(function() {
        if($("#nieuwsbriefNaam").val() == '') {
            $("#nieuwsbriefNaam").val('Uw naam');
        }    
    });     
    
    $("#nieuwsbriefEmail").click(function() {
        if($("#nieuwsbriefEmail").val() == 'Uw e-mail adres') {
            $("#nieuwsbriefEmail").val('');
        }    
    });
    
    $("#nieuwsbriefEmail").blur(function() {
        if($("#nieuwsbriefEmail").val() == '') {
            $("#nieuwsbriefEmail").val('Uw e-mail adres');
        }    
    });
    
    $(".nieuwsbriefAanmelden").submit(function (event) {
        nieuwsbriefVerzenden();
        
        return false;
    });    
  
  
  // Zoekservice
    $('#zoekservice_soort1').click(function() {
      if ($(this).attr('checked') == true) {
        $('#formulier_rij_woonhuis_soort:hidden').fadeIn();
      } else {
        $('#formulier_rij_woonhuis_soort:visible').fadeOut();
      }
    });
    
    $('#zoekservice_soort2').click(function() {
      if ($(this).attr('checked') == true) {
        $('#formulier_rij_appartement_soort:hidden').fadeIn();
      } else {
        $('#formulier_rij_appartement_soort:visible').fadeOut();
      }
    });
    
    $('#zoekservice_soort3').click(function() {
      if ($(this).attr('checked') == true) {
        $('#formulier_rij_anders:hidden').fadeIn();
      } else {
        $('#formulier_rij_anders:visible').fadeOut();
      }
    });
  
  // Enquete
    $('.enqueteKeuzes input[name="vraag1"]').click(function() {
      if($(this).val() == 'anders, namelijk') {
        $(this).parent().parent().find('.textbox').show();
      } else {
        $('.enqueteKeuzes .textbox').hide();
      }
    }); 
    
  
  // Footer menu 
	  /*
    $('.extraFooterMenu .topmenuItem').hover(function() {
      $(this).find('.submenu:hidden').slideDown('fast');
    }, function() {
      $(this).find('.submenu:visible').slideUp('fast');
    });
		*/
   
});

function slideShow() {

	//Set the opacity of all images to 0
	$('#homeGallery a').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#homeGallery a:first').css({opacity: 1.0});
	
	//Set the caption background to semi-transparent
	$('#homeGallery .caption').css({opacity: 0.8});

	//Resize the width of the caption according to the image width
	$('#homeGallery .caption').css({width: 428});
	
	//Get the caption of the first image from REL attribute and display it
	$('#homeGallery .content').html($('#homeGallery a:first').find('img').attr('rel'))
	.animate({opacity: 1}, 800);
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',6000);
	
}

function gallery() {
	//if no IMGs have the show class, grab the first image
	var current = ($('#homeGallery a.show')?  $('#homeGallery a.show') : $('#homeGallery a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#homeGallery a:first') :current.next()) : $('#homeGallery a:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#homeGallery .caption').animate({opacity: 0.0}, { queue:false, duration:50 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#homeGallery .caption').animate({opacity: 0.8},100 ).animate({height: '60px'},500 );
	
	//Display the content
	$('#homeGallery .content').html(caption);
	
	
}

function nieuwsbriefVerzenden(){   
    var params = {
        'nieuwsbriefVerzenden': 1,
        'naam': $("#nieuwsbriefNaam").val(),
        'email': $("#nieuwsbriefEmail").val(),
        'type': $('.nieuwsbriefType[name=type]:checked').val() 
    };

    $.ajax({
        type: "POST",
        url: webadres + "php/verwerk/formulier/nieuwsbrief.php",
        data: params,
        dataType: "xml",
        success: function(xml) { nieuwsbriefVerzendenResultaat(xml); }
    });
    
    return false;
}

function nieuwsbriefVerzendenResultaat(xml) {
    var resultaat = $(xml).find("status").attr("code");
    var melding = $(xml).find("status").attr("melding");

    if (resultaat == 100) {
        $('.nieuwsbriefAanmelden').fadeOut(function() {
            $('.nieuwsbriefAanmelden').html(melding).addClass('veldResultaat');
            $('.nieuwsbriefAanmelden').fadeIn();           
        });
    } else {  
        $('.nieuwsbriefAanmelden .veldResultaat').fadeOut(function() {
            $('.nieuwsbriefAanmelden .veldResultaat').html(melding);
            $('.nieuwsbriefAanmelden .veldResultaat').fadeIn();
        });
    }
}

