/* 	
    Plugin: iframe autoheight jQuery Plugin 
    Author: original code by NATHAN SMITH; converted to plugin by Jesse House
    File: jquery.iframe-auto-height.plugin.js
    Description: when the page loads set the height of an iframe based on the height of its contents
    Remarks: original code from http://sonspring.com/journal/jquery-iframe-sizing    
    Version: 1.0.0 - see README: http://github.com/house9/jquery-iframe-auto-height
*/
(function ($) {
    $.fn.iframeAutoHeight = function (options) {
        // set default option values
        var options = $.extend({
            heightOffset: 0
        }, options);

        // iterate over the matched elements passed to the plugin
        $(this).each(function () {
            // Start timer when loaded.
            var iframe = this,delayedResize = function(){resizeHeight(iframe);},intervalID = setInterval(delayedResize, 1000);					

            // Check if browser is Opera or Safari(Webkit so Chrome as well)
            if ($.browser.safari || $.browser.opera) {
                // Safari and Opera need a kick-start.
                var source = $(this).attr('src');
                $(this).attr('src', '');
                $(this).attr('src', source);
            }

            // resizeHeight
            function resizeHeight(iframe) {
                // Set inline style to equal the body height of the iframed content plus a little
				var newHeight = 0;
				//newHeight = $(iframe.contentWindow.document.body).height();                
				jQuery.map($(iframe.contentWindow.document.body).children(),function(el, i){
					newHeight += $(el).height();
				});				
                newHeight += options.heightOffset;
                $(iframe).height(newHeight);
            }

        }); // end
    }
})(jQuery);
