$(document).ready(function() {
  $('#slideshow img:first').fadeIn(1000, function() {
    $('#slideshow').cycle({
      pager:  '#slide_items',
      pagerAnchorBuilder: function(idx, slide) { 
          var img_title =  $(slide).attr('title');          
          var sec_title =  $(slide).attr('secondtitle');  
          return '<div class="item"><h3>' + img_title + '</h3><h4>' + sec_title + '</h4></div>'; 
      }
    });
  })
  $('#project').live('click', function(event) {
    clicky.goal('Clicked: Quote - Project');
  })
  $('.item').live('click', function(event) {
    slide = "Click on slide: " + $(this).find('h3').html();
    clicky.goal(slide);
  });

  $(window).scroll(function(){
          if  ($(window).scrollTop() == $(document).height() - $(window).height()){
             clicky.goal('Bezoeker heeft gescrollt');
          }
  });
  $('input[type=text]').usableForms();
	$('textarea').usableForms();
	$('input, textarea').live('focusin', function(event) {
    $(this).addClass('focus')
	});
	$('input, textarea').live('focusout', function(event) {
    $(this).removeClass('focus')
	});
	$('input, textarea').live('focusout', function(event) {
    if (!$(this).val() == $(this).prev('label').val()) {
      $(this).addClass('changed');
    };
	});
	$('#callback').live('click', function(event) {
	  if ($('#callback').attr('checked')) {
        $(this).parent().addClass('changed');
      }else{
        $(this).parent().removeClass('changed');
      }
	});
  // ajax handling contact form
  $("#sent").click(function(){
    $('.sending').show();
    $('input, textarea, .checkbox_holder').addClass('half-opacity');
    
    var name = $("input#name").val();
    var email = $("input#email").val();
    var phone = $("input#phone").val();
    var callback = []
    var msg = $("textarea#comment").val();
    if ($('#callback').attr('checked')) {
      callback = $('#callback').val()
    }else{
      callback = "nee"
    };
    $.ajax({
      type: "POST",
      url: "contact.php",
      data: "name=" + name + "&email=" + email + "&phone=" + phone + "&callback=" + callback + "&msg=" + msg,
      success: function(fb){
        if (fb == "true") {
          clicky.goal('Contact form: Succes');
          $('.sending').hide()
          $('.succes').fadeIn('slow').delay(2500).fadeOut('fast', function  () {
            $('input, textarea, .checkbox_holder').removeClass('half-opacity');
            $('input, textarea').val('').usableForms();;
            $('.checkbox_holder input').attr('')
          })
        }
        if (fb == "false") {
          clicky.goal('Contact form: Error');
          $('.sending').hide()
          $('.error').fadeIn('slow').delay(2500).fadeOut('fast', function  () {
            $('input, textarea, .checkbox_holder').removeClass('half-opacity');
          })
        }
      }
    });
  });
});


