jQuery(document).ready(function($){



  // Helps with media query dev work
  /*
  $('body').prepend('<div id="window-width" style="background:rgba(0,0,0,0.5);color:white;padding:10px;font-size:12px;position:fixed;top:0;left:0;width:100%;z-index:20;" />');
  $('#window-width').text($(window).width()+'px');
  $(window).resize(function() {
    $('#window-width').text($(window).width()+'px');
  });
  */

  /* Prevent orphans/widows in headings */
  /*
  $("body.home .heading").each(function() {
    var wordArray = $(this).text().split(" ");
    if(wordArray.length > 2){
      wordArray[wordArray.length-2] += "&nbsp;" + wordArray[wordArray.length-1];
      wordArray.pop();
      $(this).html(wordArray.join(" "));
    }
  });
  */


  /* Style select boxes */
  $('select').uniform();
  $('select#subject').before('<div class="icon">&nbsp;</div>');



  /* Form validation */
  $("form#freeform").validate({});



  /* Image galleries */
  var imageGalleries = $('.gallery-images');
  if($.isFunction($.fn.cycle)) {

    var imageGalleryConfig = { 
      activePagerClass: 'active', // class name used for the active pager link 
      autostop: true, // true to end slideshow after X transitions (where X == slide count) 
      autostopCount: 20, // number of transitions (optionally used with autostop to define X)
      fx: 'fade', // name of transition effect (or comma separated names, ex: fade,scrollUp,shuffle)
      pager: '.gallery-nav', // selector for element to use as pager container 
      pagerAnchorBuilder: function(idx, slide) { // callback fn for building anchor links: function(index, DOMelement)
        var thumb = $(slide).data('thumb');
        return '<li><a href="#"><img src="'+thumb+'" alt="Image '+(idx + 1)+'"><span class="magnify">&nbsp;</span></a></li>';
      },
      // pause: true, // true to enable "pause on hover" 
      // pauseOnPagerHover: true, // true to pause when hovering over pager link 
      speed: 900, // speed of the transition (any valid fx speed value)
      timeout: 0
    };
    imageGalleries.each(function(){
      var self = $(this);
      var items = self.children('li').length;
      if (items > 1){
        self.after('<ul class="gallery-nav" />').cycle(imageGalleryConfig);
      }
    });
  }



  /* Fix a navigation bug */
  $('body.contact .site-nav li.last').addClass('parent-here');



  /* Fix some IE bugs */
  $('.gallery-nav').find('li:last-child').addClass('last').end().find('li:first-child').addClass('first');


  /* Create a different version of the homepage's headline for vertical tablet orientation */
  var headline = $('body.home h2.heading').clone();
  headline.addClass('vertical-tablet-only').prependTo('.columns');

});
