/*jslint white: true, indent: 2, onevar: false, browser: true, undef: true, nomen: false, eqeqeq: true, plusplus: false, bitwise: true, regexp: true, strict: false, newcap: true, immed: true */
/*global window, console, jQuery, setTimeout */

/*
  Plugin: iframe autoheight jQuery Plugin
  Version: 1.5.0  
  Author and Contributors
  ========================================
  NATHAN SMITH (http://sonspring.com/)
  Jesse House (https://github.com/house9)
  aaron manela (https://github.com/aaronmanela)
  Hideki Abe (https://github.com/hideki-a)
  Patrick Clark (https://github.com/hellopat)
  ChristineP2 (https://github.com/ChristineP2)
  Mmjavellana (https://github.com/Mmjavellana)
  
  File: jquery.iframe-auto-height.plugin.js  
  Remarks: original code from http://sonspring.com/journal/jquery-iframe-sizing      
  Description: when the page loads set the height of an iframe based on the height of its contents
  see README: http://github.com/house9/jquery-iframe-auto-height

*/
(function ($) {
  $.fn.iframeAutoHeight = function (spec) {
    
    // set default option values
    var options = $.extend({
        heightOffset: 0, 
        minHeight: 0, 
        callback: function (newHeight) {}
      }, spec);
    // ******************************************************
    // iterate over the matched elements passed to the plugin
    $(this).each(function () {
      // for use by webkit only
      var loadCounter = 0;

      // resizeHeight
      function resizeHeight(iframe) {
        // get the iframe body height and set inline style to that plus a little
        var newHeight = $(iframe, window.top.document).contents().find('body').prop('scrollHeight') + options.heightOffset;

        if (newHeight < options.minHeight) {
          newHeight = options.minHeight + options.heightOffset;
        }

        $(iframe).css({'height': newHeight + 2, 'width': $(iframe).parent().width()});
        options.callback({newFrameHeight: newHeight});
      }
      
      // Check if browser is Opera or Safari (Webkit really, so includes Chrome)
      if ($.browser.safari || $.browser.opera) {
        // Start timer when loaded.
        $(this).load(function () {
          var delay = 0;
          var iframe = this;  
          // Reset iframe height to 0 to force new frame size to fit window properly          
          // iframe.style.height = '0px';
          var delayedResize = function () {
            resizeHeight(iframe);
          };
          
          if (loadCounter === 0) {
            delay = 500; // delay the first one
          }
          
          setTimeout(delayedResize, delay);
          loadCounter++;          
        });

        // Safari and Opera need a kick-start.
        var source = $(this).attr('src');
        $(this).attr('src', '');
        $(this).attr('src', source);
      } else {
        // For other browsers.
        $(this).load(function () {
          resizeHeight(this);
        });
      } // if browser
             
    }); // $(this).each(function () {    
  }; // $.fn.iframeAutoHeight = function (options) {
}(jQuery)); // (function ($) {
