// JavaScript Document
// Copyright J Pollock, Inc. 2011+

$(function() {
  //Contact Form Watermarks
  $('#contact-form textarea').watermark('Tell us more about your specific problems or request further information', {className: 'watermark'});

  //add /clients/jpollock to links
  //if ($('a[href^=mailto:]')) {
  //  return false;
  //} else {
    //$('a').each(function() {
    //  var href = $(this).attr('href');
    //  $(this).attr('href', '/clients/jpollock' + href);
    //});
  //};


  //homepage slider
  $('#slider-nav div:first').addClass('current');
  $('#slider-images div.slide-frame').hide();
  $('#slider-images div..slide-frame:first').addClass('current').show();

  $('#slider-nav h1').click(function(){
    var id        = $(this).parent().attr('id');
    var sliderImg = '#slider-images div.slide-frame';
    $(sliderImg).removeClass('current');
    $(sliderImg).fadeOut('slow');
    $("#slider-images div." + id).fadeIn('slow');

    $("#slider-nav div").removeClass('current');
    $(this).parent().addClass('current');
    return false;
  });

  //setInterval(autoProgress, 8000);

  var hovering = 0;

  //autoProgressing Function
  function autoProgress() {
      if (hovering > 0) {return;}

      var $next = $('#slider-nav div.current h1').parent().next().children('h1');
      if ($next.length==0) $next = $('#slider-nav div:first h1')
      $next.trigger('click');
  }

  //Pause on Hover
  $("#slider").hover(startHover, endHover);
  function startHover() {
      // Increment the hovering flag
      ++hovering;
  }

  function endHover() {
      // Decrement the hovering flag, clamping at zero out of paranoia
      if (hovering > 0) {
          --hovering;
      }
  }

  //keyboard navigation
  $(document.documentElement).keyup(function (event) {
    // handle cursor keys
    if (event.keyCode == 37) {
      // go left
      $('#slider-nav div.current').prev().find('h1').click();
    } else if (event.keyCode == 39) {
      // go right
      $('#slider-nav div.current').next().find('h1').click();
    }
  });


  // Primary Dropdown Show/Hide
  $('.dropdown .expanded-content').css('display','block');
  $('.dropdown h4').click(function() {
    $(this).next().slideToggle('medium').parent().siblings().children('div.expanded-content').slideUp('medium');
    $(this).parent().siblings().children('h4').removeClass('minus-sign');
    $(this).toggleClass('minus-sign');
    return false;
  }).next().hide();

  // Case Study Show/Hide
  $('.case-study .expanded-case').hide();
  $('.case-study h5').click(function() {
    $(this).next().slideToggle('medium');
    $(this).toggleClass('minus-sign');
    return false;
  });

  // Open Dropdown from Homepage
  if (location.hash) { // This will only run if the adress has is "something#enedrop"
    $currentArticle  = location.hash;
    $($currentArticle + ' .expanded-content').slideToggle('medium');
    $($currentArticle + ' h4').toggleClass('minus-sign');
    return false;
  }

  // Terms & Keywords Accordion
  $('#accordion.term-definitions .expanded-content').hide();
  $('.terms-dropdown h4 span').click(function() {
    $(this).parent().next().slideToggle('medium').parent().siblings().children('div.expanded-content').slideUp('medium');
    $(this).parent().parent().siblings().children('h4').children('span').removeClass('minus-sign');
    $(this).toggleClass('minus-sign');
    return false;
  });

  //Gray out form until field is entered
  $('.radio-select,.radio span').addClass('faded-out');
  //alert($('input#phone').val().length);
  $('input#phone').blur(function() {
    if( $('input#phone').val().length > 0 ) {
      $('.radio-select,.radio span').removeClass('faded-out');
    }
  });


  //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.";

  $("#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");
     }
  });

});

