/* 	
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
*/
/*IMPORTANT.... THIS SCRIPT HAS BEEN DOCTORED TO WORK SPECIFICALLY WITH BMS*/
(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 () {
            // Check if browser is Opera or Safari(Webkit so Chrome as well)
            if ($.browser.safari || $.browser.opera) {
                // Start timer when loaded.
                $(this).load(function () {
                    var iframe = this;
                    var delayedResize = function () {
                        resizeHeight(iframe);
                    };
                    setTimeout(delayedResize, 0);
                });

                // 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);
                });
            }

            // resizeHeight
            function resizeHeight(iframe) {
                // Set inline style to equal the body height of the iframed content plus a little.

                // BMS SPECIFIC EDITS BELOW.

                // Set the document title correctly.
                var documentTitle = jQuery("#mainProductTitle", parent.document).val();

                var currentTitle = parent.document.title;

                var index = currentTitle.indexOf(":");

                var formattedTitle = documentTitle + currentTitle.substring(index - 1);

                parent.document.title = formattedTitle;

                // Populate the form with the correct machine name.
                $('input[id$="_form_Machine"]', iframe.contentWindow.document).val(documentTitle);

                // var newHeight = iframe.contentWindow.document.body.offsetHeight + options.heightOffset;
                // Edited as the wrong height was not being passed though.
                var newHeight = $("#bmsShopFormFlag #dnn_ContentPane", iframe.contentWindow.document).height();
                iframe.style.height = newHeight + 'px';

                // Change the body background off the iframe. Not IE.
                if ((Peach.getIEVersion() === 0)) {
                    $(iframe.contentWindow.document.body).addClass("clearBody");
                }
            }

        }); // end
    }
})(jQuery);
