$(function() {
  if (!$('#work')[0] || $('#work.identity')[0]) {
    return false;
  }
  
  var xml;
  
  $.ajax({
    url: 'work.xml',
    type: 'GET',
    dataType: 'xml',
    success: function(data) {
      xml = $(data);
      var startid = $('#work-select li.selected').attr('id').substr(5);
      bindPagination(xml.find('item[id=' + startid + '] page'));
    }
  });
  
  $('#work-select .work-view a').click(function() {
    var parent = $(this).parent().parent();
    var newid = parent.attr('id').substr(5);
    var curXML = xml.find('item[id=' + newid + '] page');
    
    $('#content-inside h3').eq(0).text(parent.find('h3').text());
    $('#work-select li').removeClass('selected');
    parent.addClass('selected');
    
    $('div.work-image').remove();
    curXML.eq(0).children().each(function() {
      $('#content-inside article').append($('<div class="work-image"></div>').html('<img src="' + $(this).attr('src')  + '" alt="' + $(this).attr('alt')  + '" height="' + $(this).attr('height')  + '" width="' + $(this).attr('width')  + '" />'));
    });
    
    $('#pagination li:not(.prev):not(.next)').remove();
    $('#pagination li').filter('.prev').addClass('disabled').end().filter('.next').removeClass('disabled');
    curXML.each(function(i) {
      $('#pagination li.next').before('<li><a href="#">' + (i+1) + '</a></li>');
    });
    $('#pagination li:not(.prev):not(.next)').eq(0).addClass('selected');
    bindPagination(curXML);
    
    return false;
  });
  
  var bindPagination = function(xml) {
    var pagination = $('#pagination');
    pagination.find('li.prev').unbind('click').click(function() {
      if ($(this).hasClass('disabled')) {
        return false;
      }
      
      pagination.find('li.selected').prev().click();
      
      return false;
    }).end().find('li.next').unbind('click').click(function() {
      if ($(this).hasClass('disabled')) {
        return false;
      }
      
      pagination.find('li.selected').next().click();
      
      return false;
    }).end().find('li:not(.prev):not(.next)').click(function() {
      if ($(this).hasClass('selected')) {
        return false;
      }
      
      var curIndex = pagination.find('li:not(.prev):not(.next)').index(this);
      
      $('div.work-image').remove();
      xml.eq(curIndex).children().each(function() {
        $('#content-inside article').append($('<div class="work-image"></div>').html('<img src="' + $(this).attr('src')  + '" alt="' + $(this).attr('alt')  + '" height="' + $(this).attr('height')  + '" width="' + $(this).attr('width')  + '" />'));
      });
      
      pagination.find('li').removeClass('selected').removeClass('disabled');
      $(this).addClass('selected');
      if (curIndex === 0) {
        pagination.find('li.prev').addClass('disabled');
      } else if (curIndex+1 === pagination.find('li:not(.prev):not(.next)').length) {
        pagination.find('li.next').addClass('disabled');
      }
      
      return false;
    });
  };
});

$(function() {
  if (!$('#contact')[0]) {
    return false;
  }
    
  $('input.default, textarea.default', '#content-inside').actsAsDefault();
  
  $('#contact form').submit(function() {
    var formValid = true;
    
    $('div.required input.text, div.required textarea').each(function() {
      if ($(this).hasClass('default') || $.trim($(this).val()).length < 5) {
        $(this).parent().addClass('error');
        formValid = false;
      }
      
      if ($(this).parent().hasClass('email') && !(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/).test($.trim($(this).val()))) {
        $(this).parent().addClass('error');
        formValid = false;
      }
    });
    
    if (!formValid) {
      if (!$('p.error')[0]) {
        $('#contact form').before('<p class="error"><strong>Please fill in all the required fields below.</strong></p>');
      }
      
    }
    
    return formValid;
  });
});

$.fn.actsAsDefault = function() {
  $(this).each(function() {
    var defaultValue = $(this).val();
    $(this).focus(function() {
      if ($(this).val() === defaultValue) {
        $(this).removeClass('default').val('');
      }
    }).blur(function() {
      if ($.trim($(this).val()) === '') {
        $(this).addClass('default').val(defaultValue);
      }
    });
  });
};