// site specific functions

createMailto = function(pers, domain, customText) {
  var email = pers + '@' + domain;
  (customText == null) ? lnk = email : lnk = customText;
  document.write('<a href="mailto:'+email+'">'+lnk+'</a>');
}

validateEmail = function(add) {
  var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
  return emailPattern.test(add);
}

initAlignedImages = function() {
  $('img').each(function(e) { $(this).addClass($(this).attr('align')); });
}

initPhotoOverlays = function() {
  var cnt = 1;
  $('.photos').each(function(e) {
    $(this).find('img').each(function(e) {
      $(this).wrap('<a href="'+$(this).attr('src')+'" rel="sect'+cnt+'" title="'+$(this).attr('alt')+'"></a>')
    });

    $(this).find('a').each(function(e) {
      $(this).colorbox({
          transition:"elastic",
          current: "foto {current} / {total}",
          onOpen:function(){ $('object').css('visibility', 'hidden'); },
          onClosed:function(){ $('object').css('visibility', 'visible'); }
      });
    });
    cnt++;
  });
}

initContactForm = function() {
  $('form .disabled').each(function(e) {
    $(this).attr('originalValue', $(this).attr('value'));
  });
  $('form .disabled').focus(function(e) {
    if ($(this).attr('value') == $(this).attr('originalValue')) {
      $(this).attr('value', '');
      $(this).attr('class', $(this).attr('class').replace('disabled', 'active'));
    }
  });
  $('form .disabled').blur(function(e) {
    if ($(this).attr('value') == '') {
      $(this).attr('value', $(this).attr('originalValue'));
      $(this).attr('class', $(this).attr('class').replace('active', 'disabled'));
    }
  });
}

initNav = function() {
  $('#nav1 .has-sub').mouseover(function(e) { this.className = "has-sub hover"; });
  $('#nav1 .has-sub').mouseout (function(e) { this.className = "has-sub"; });
}

initFormFieldHints = function (){
  $('input[type=text], textarea').inputhint();
}

initDownloadForm = function (){
  // Show the extra fields once the e-mail field gets focus.
  $('.register-download input[name=e-mail]').focus(function (){
    var block = $(this).closest('.register-download');
    var extra = block.find('.extra-fields');
    if (extra.is(':hidden')){ extra.slideDown(); }
  });
  
  // Add validation to the download form.
  $('.register-download').submit(function (){
    var errors = [];
    var form = $(this);
    // Make the fields empty that just show the input hint.
    form.find('input[type=text]').each(function (){
      var field = $(this);
      if (field.val() == field.attr('title')){
        field.val('');
      }
    });
    if (!form.find('input[name=name]').val().match(/\S/)){ errors.push("Je naam is verplicht."); }
    if (!form.find('input[name=e-mail]').val().match(/\S/)){ errors.push("Je e-mail is verplicht."); }
    if (errors.length > 0){
      // Put back the hints in the empty fields. Then show the errors and cancel the submit.
      form.find('input[type=text]').inputhint();
      alert("- " + errors.join("\n- "));
      return false;
    }
  });
}

initTweets = function (){
  $('a.twitter').each(function (index, el){
    var link = $(this);
    var subject = link.find('.username,.hashtag'); // Assumes that there is only one username or hashtag within the link.
    if (subject.length == 0){
      return;
    }
    var query = subject.html();
    var tweetsContainer = $('<div>').addClass('tweets').insertAfter(link);
    if (query.indexOf('@') == 0) {
      tweetsContainer.tweet({
        username: query,
        avatar_size: 36,
        count: 3,
        loading_text: "loading tweets..."
      });
    } else {
      tweetsContainer.tweet({
        avatar_size: 36,
        count: 3,
        query: query,
        loading_text: "loading tweets..."
      });
    }
  });
}

$(document).ready(function() {
  initPhotoOverlays();
  initAlignedImages();
  initContactForm();
  initFormFieldHints();
  initNav();
  initDownloadForm();
  initTweets();
});





