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 = {
	prevHeight : 200,
	
    resize : function(iframe) {	
	    items = document.getElementsByTagName("iframe");
	
        if (iframe == undefined) {
            // Get the last iframe instance as we seem to end up with 2.
            items = document.getElementsByTagName("iframe");
            if (items.length > 0) {
            	iframe = document.getElementsByTagName("iframe")[items.length - 1];
            }	                                                
        }
        if (iframe == undefined) return;
        var height = IFrameParent.retrieveHeight(iframe);        
        // Ignore map button button jitter
        if (height == null || height - IFrameParent.prevHeight <= 60) return;  
        if (height != null && !isNaN(height)) iframe.style.height = height.toString() + "px";
        IFrameParent.prevHeight = height;
    },

    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)) {
              	window.location.hash = "#";
            }
        }
        if (height != null && !isNaN(height)) height += 15;
        return height;
    }

};

// Usage: <body onload="setInterval(IFrameChild.publishHeightAndNotify, 2000);">
var IFrameChild = {

    previousHeight : null,

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

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

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

};

