var IFrameHelper = {

    getBodyHeight : function(doc) {
        var height = null;
        var scrollHeight;
        var offsetHeight;
        if (doc.height) {
            height = doc.height;
        } else if (doc.body) {
            if (doc.body.scrollHeight) height = scrollHeight = doc.body.scrollHeight;
            if (doc.body.offsetHeight) height = offsetHeight = doc.body.offsetHeight;
            if (scrollHeight && offsetHeight) height = Math.max(scrollHeight, offsetHeight);
        }
        return height;
    }

};

// Usage: <iframe ... onload="IFrameParent.resize(this);">
var IFrameParent = {

    resize : function(iframe) {
        if (iframe == undefined) {
            iframe = document.getElementsByTagName("iframe")[0];
        }
        var height = IFrameParent.retrieveHeight(iframe);
        if (height != null && !isNaN(height)) iframe.style.height = height.toString() + "px";
    },

    retrieveHeight : function(iframe) {
        var height = null;
        try {
            height = IFrameHelper.getBodyHeight(iframe.contentWindow.document);
            // we are same-domain so don't need to bother with cross-domain hacks
        } catch (e) {
            // we are cross-domain so grab the height that the child put in our window.location
            if (window.location.hash.length <= 1) return null;
            var hashValue = window.location.hash.substring(1);
            if (hashValue == null || hashValue.length == 0) return null;
            height = parseInt(hashValue);
            if (!isNaN(height)) {
                if (navigator.appName == 'Microsoft Internet Explorer') window.location.hash = '#';
                else history.go(-1);
            }
        }
        if (height != null && !isNaN(height)) height += 15;
        return height;
    }

};

// Usage: <body onload="IFrameChild.publishHeight();">
var IFrameChild = {

    publishHeight : function() {
        if (IFrameChild.parentIsCrossDomain()) {
            var height = IFrameHelper.getBodyHeight(document);
            var parentUrl = document.referrer + '#' + height.toString();
            window.top.location = parentUrl;
        }
    },

    publishHeightAndNotify : function() {
        IFrameChild.publishHeight();
        if (IFrameChild.parentIsCrossDomain()) {
            window.focus()
            window.top.focus();
        } else {
            window.top.IFrameParent.resize();
        }
    },

    parentIsCrossDomain : function() {
        try {
            if (window.top.document != undefined) return false;
        } catch (e) { }
        return true;
    }

};
