(function($) {

  $.fn.sameHeight = function(options) {
    var opts = $.extend({}, $.fn.sameHeight.defaults, options);

    var maxHeight = Math.max.apply(Math, this.map(function() {
      return $(this).height();
    }).get());

    this.each(function(index) {
      $(this).height(maxHeight + opts.adjustment);
    });
    return this;
  };

  $.fn.sameHeight.defaults = {
    adjustment: 0
  };


/***************************************
   Big Links Plugin for increasing the target area of links to a containing element
   @author Karl Swedberg
   @version 0.1 (11/19/2008)
   @requires jQuery v1.2.6+
   
***************************************/
$.fn.biglinks = function(options) {
  var opts = $.extend({}, $.fn.biglinks.defaults, options);

  return this.each(function() {
    var $this = $(this).addClass(opts.biglinkClass);
    $this.click(function() {
      if (opts.preventDefault === true) {
        $('a:first', this).triggerHandler('click');
      } else {
        var url = $this.find('a')[0].href;
        window.location.href = url;
      }
    }).hover(function() {
      $this.addClass(opts.biglinkHoverClass);
    }, function() {
      $this.removeClass(opts.biglinkHoverClass);
    });
  });
};

// default options
$.fn.biglinks.defaults = {
  preventDefault: false,
  biglinkClass: 'biglink',
  biglinkHoverClass: 'biglink-hover'
};

/***************************************
  Columns jQuery Plugin
   @author Karl Swedberg
   @version 1.0 (November 25, 2009)
   @requires jQuery v1.2.6+

***************************************/
$.fn.columns = function(options) {
  var opts = $.extend(true, {}, $.fn.columns.defaults, options);

  var roundUp = Math.ceil,
      roundDown = Math.floor;

  this.each(function(event) {
    var $container = $(this),
      containerWidth = opts.containerWidth || $container.width(),
      nname = this.nodeName,
      cname = this.className ? this.className + ' ' : '',
      $kids = $container.children(),
      col = 1,
      item = 1,
      items = [],
      colClass = opts.columnClass;

    if (opts.columnWrapper) {
      var cw = opts.columnWrapperClass;
      var $wrap = $(opts.columnWrapper)
        .addClass(cw + ' ' + cw + '-' + opts.columns)
        .insertBefore($container);
    }
    for (var i=0, klength = $kids.length; i < klength; i++) {
      items.push($kids[i]);
      var itemStart = (klength/opts.columns)*col;
      if (i+1 >= itemStart || i === klength-1) {
        item = Math.ceil(itemStart);
        var $parent = $('<' + nname + ' class="' + cname + colClass + ' ' + colClass + '-' + col + '"></' + nname + '>')
          .append($(items));
        var lastCol = (i === klength-1);
        
        calculateWidths.call($parent, containerWidth, lastCol);
          
        if (lastCol) {
          $parent.addClass(colClass + '-last last');
        }
        if (opts.columnWrapper) {
          $parent.appendTo($wrap);
        } else {
          $parent.insertBefore($container);
        }
        if ($parent[0].nodeName == 'OL' && col > 1) {
          $parent.attr('start', start);
        }
        col++;
        items = [];
      }
      var start = item+1;
    }

    $container.remove();

  });
  function calculateWidths(containerWidth, last) {
    if (!opts.autoWidth) { return; }
    var fluff = 0, 
        $parent = this;
    $.each(['paddingLeft', 'paddingRight', 'marginLeft', 'borderLeftWidth', 'borderRightWidth'], function(index, val) {
      fluff += ( parseInt($parent.css(val), 10) || 0);
    });

    fluff = (fluff * opts.columns) + (opts.gutter * (opts.columns-1));
    var cssProps = {
      width: roundDown( (containerWidth - fluff)  / opts.columns ),
      marginRight: last ? 0 : opts.gutter
    };
    $parent.css(cssProps);
  }
  
  return this;
};

$.fn.columns.defaults = {
  containerWidth: null, // set to an integer to force a width for the columns' container element.
  columns: 3,
  autoWidth: false, // set to true if you want the plugin to automatically define column widths for you (otherwise, use CSS classes, etc.).
  gutter: 0, // if autoWidth is true, set margin-right on all but last column
  columnClass: 'floatcols',
  columnWrapper: '<div></div>', // set to null if you don't want a wrapper around all columns
  columnWrapperClass: 'column-wrapper'
};

})(jQuery);

/***************************************
* Default Text Plugin for inputs
* @author Karl Swedberg
* @version 1.2 (March 29, 2010)
* @requires jQuery v1.3+
************************************** */

(function($){

  $.fn.defaulttext = function(options) {

    var elText = {
      title: function(input) {
        return $(input).attr('title');
      },
      placeholder: function(input) {
        return $(input).attr('placeholder');
      },
      label: function(input) {
        return $('label[for=' + input.id +']').text();
      }
    },
    delay = 50, loadDelay = 100,
    selector = this.selector,
    $form = this.closest('form');

    $form
    .bind('blurText.dt', function(event) {
      var $tgt = $(event.target),
          prevClass = $tgt.data('dtInfo') && $tgt.data('dtInfo').prevClass || '';
      if ($.trim($tgt.val()) === '') {
        $tgt.prev(prevClass).show();
      } else {
        $tgt.prev(prevClass).hide();
      }
    })
    .bind('focusText.dt', function(event, el) {
      var tgt = event.target;
      $(tgt).prev().hide();
      if (!$(el).is(':dtinput')) {
        tgt.focus();
      }
    });

    this.filter(':dtinput').each(function() {

      var $input = $(this);
      var opts = $.extend({}, $.fn.defaulttext.defaults, options || {}, $.metadata ? $input.metadata() : $.meta ? $input.data() : {});

      // set the default text based on the value of the text option
      if (opts.text.constructor === Function) {
        opts.text = opts.text.call(this);
      } else if (opts.text && opts.text.constructor === String) {
        if (opts.text === 'label') {
          $('label[for= '+ this.id + ']').css({position: 'absolute', left: '-4000em'});
        }

        opts.text = (/(title|label|placeholder)/).test(opts.text) ? elText[opts.text](this) : opts.text;
      }

      if (!opts.text || $.support.placeholder) { return; }

      $input.data('dtInfo', {text: opts.text, prevClass: opts.defaultClass ? '.' + opts.defaultClass : ''});

      if ($input.parent().css('position') == 'static') {
        $input.parent().css({position: 'relative'});
      }
      $(opts.tag).html($input.data('dtInfo').text)
      .addClass(opts.defaultClass)
      .css({
        position: 'absolute',
        top: $input.position().top,
        left: $input.position().left,
        width: $input.width(),
        display: 'none'
      })
      .insertBefore($input);

      // hide default text on focus
      var focused;
      $input
      .bind('focus', function(event) {

        $input.trigger('focusText.dt', event.target);
        focused = setTimeout(function() {
          $input.trigger('focusText.dt', event.target);
        }, delay);
      });
      $input.prev($input.data('dtInfo').prevClass)
      .bind('click', function(event) {
        $input.trigger('focusText.dt', event.target);
      });

      // conditionally show default text on input blur
      $input
      .bind('blur', function(event) {
        clearTimeout(focused);
        $input.trigger('blurText.dt');
      });

      // trigger the focus and blur when the window has loaded
      // trigger is delayed to work around a race condition in Safari's autofill
      $(window).bind('load', function() {
        setTimeout(function() {
          $input.trigger('focusText.dt', $input);
          $input.trigger('blurText.dt');
        }, delay+loadDelay);
      });
    });

    function dtHide(el) {
      el.prev().hide();
    }

    return this;
  };

  $.fn.defaulttext.defaults = {
    tag: '<span></span>',
    defaultClass: 'default-text',
    text: 'label'             // 'label' uses text of input's label
                              // 'title' uses input's title attribute
                              // 'placeholder' uses HTML5 "placeholder" attribute
                              //  otherwise, use some other string or return a value from a function
  };

  $.extend($.expr[':'], {
    dtinput: function(element, index, matches, set) {
      var tag = element.nodeName.toLowerCase();
      return ( tag === 'input' && !(/file|checkbox|radio/i).test(element.type) ) || tag === 'textarea';
    }
  });

  var inp = document.createElement('input');
  $.extend($.support, {
    placeholder: 'placeholder' in inp
  });
  inp = null;
})(jQuery);

/** =pixel-jog fix for <body> bg image
************************************************************/


(function() {

  if (typeof window.resizeBy === 'undefined') { return; }

  $(window)
  .bind('load', function() {
    resizeIfOdd();
  })
  .bind('resize', function() {

  });

  function resizeIfOdd() {
    if(document.documentElement.clientWidth % 2 === 1) {
      window.resizeBy(-1,0);
    }
  }

  function forceBrowserEven(){
    window.setTimeout(resizeIfOdd, 50);
  }

})();
