//JavaScript Document
// Copyright (c) 2009+ Andrew Pautler

$(function() {
  //show contact page
  $("#contact-page").css('display', 'block')
  
  //for smooth scrolling
  $.localScroll();

  //for contact hide/show
  $('a#button-contact').click(function() { $('#contact-page').slideToggle(400); return false;});
  // for contact arrow
  $('#nav-contact a#button-contact').click(function () {$(this).toggleClass('arrow-down');});
  //on home subsections
  $('#contact-page').hide(); $('a.show-contact').click(function() { $('#contact-page').slideToggle(400); return false;});
  $('a.show-contact').click(function () {$('#nav-contact a#button-contact').toggleClass("arrow-down");});
  
  //zebra stripe capabilities
  $("#capabilities ul li:nth-child(odd)").addClass("odd");
  $(".project-description ul li:nth-child(odd)").addClass("odd");

  //alternate sides for portfolio section
  $("#portfolio-pieces .website:nth-child(even), #portfolio-pieces .print:nth-child(even), #portfolio-pieces .identity:nth-child(even), #portfolio-pieces .other:nth-child(even), #portfolio-pieces .download:nth-child(even)").addClass("odd");
  //add class to last piece in each section to remove line
  $('#portfolio-pieces #web-design-pieces div.portfolio-piece:last, #portfolio-pieces #print-design-pieces div.portfolio-piece:last, #portfolio-pieces #identity-design-pieces div.portfolio-piece:last, #portfolio-pieces #other-pieces div.portfolio-piece:last, #portfolio-pieces #download-pieces div.portfolio-piece:last').addClass("last");

  //portfolio slider
  $('.slider').nivoSlider({effect: 'fade', slices: 1, controlNav: true, directionNav:true, manualAdvance:true, keyboardNav:true});

  //home page slider
  $('#slider').nivoSlider({effect: 'fade', slices: 1, controlNav: true, directionNav:true, keyboardNav:true, pauseOnHover:true, animSpeed:500, pauseTime:4500});

  //download hover
  $('a.download-image').hover(
    function () {$('a.download-button').addClass("download-hover");}, 
    function () {$('a.download-button').removeClass("download-hover");
  });
  
  $('a.download-button').hover(
    function () {$('a.download-image').addClass("download-hover-image");}, 
    function () {$('a.download-image').removeClass("download-hover-image");
  });

  //Contact Form Validation
  // Place ID's of all required fields here.
  required = ["name", "email", "message"];
  // If using an ID other than #email or #error then replace it here
  email = $("#email");
  errornotice = $("#error");
  // The text to show up within a field when it is incorrect
  emptyerror = "This field is required.";
  emailerror = "Please enter a valid e-mail.";

  $("#contact-form").submit(function(){
  //Validate required fields
  for (i=0;i<required.length;i++) {
    var input = $('#'+required[i]);
    if ((input.val() == "") || (input.val() == emptyerror)) {
      input.addClass("needsfilled");
      input.val(emptyerror);
    } else {
      input.removeClass("needsfilled");
    }
  }

  // Validate the e-mail.
  if (!/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
      email.addClass("needsfilled");
      email.val(emailerror);
  }

    //if any inputs on the page have the class 'needsfilled' the form will not submit
    if ($(":input").hasClass("needsfilled")) {
      return false;
    } else {
      errornotice.hide();
      return true;
    }
  });

  // Clears any fields in the form when the user clicks on them
  $(":input").focus(function(){
     if ($(this).hasClass("needsfilled") ) {
      $(this).val("");
      $(this).removeClass("needsfilled");
     }
  });




  //capabilities slider
  // var top = $('#capabilities').offset().top - parseFloat($('#capabilities').css('margin-top').replace(/auto/, 0));
  // $(window).scroll(function (event){
  //   // what the y position of the scroll is
  //   var y = $(this).scrollTop();
  //   // whether that's below the form
  //   if (y >= top) {
  //     // if so, add the fixed class
  //    $('#capabilities').addClass('fixed');
  //   } else {
  //    // otherwise remove it
  //     $('#capabilities').removeClass('fixed');
  //   }
  // });
});
