/**
 * Automatically set height to <iframe> and/or <object>
 *
 * Copyright (c) 2008, Alert Investor Relations AB. All rights reserved.
 * Code licensed under the BSD License:
 * http://www.alertir.com/license.txt
 *
 * @version 2008-05-06
 * @author Alert Investor Relations AB <alertir@alertir.com>
 * @copyright Alert Investor Relations AB <alertir@alertir.com>
 *
 * Usage
 *
 * Page holding <iframe src="/foo/bar/" id="FOOBAR"></iframe> or
 * <object type="text/html" data="/foo/bar/" id="FOOBAR"></object>
 *
 * [...]
 * <head>
 * <script type="text/javascript">
 * document.domain = 'www.foo.se';
 * </script>
 * </head>
 * [...]
 * <iframe src="/foo/bar/" id="FOOBAR"></iframe>
 * <script type="text/javascript" src="afw_element_resizer.js"></script>
 * [...]
 *
 * The page living inside an iframe or object
 *
 * [...]
 * <script type="text/javascript">
 * document.domain = 'www.foo.se';
 * try {
 *     if (parent.AFWElementResizer) {
 *         parent.AFWElementResizer.addEvent(window, 'load', function() {
 *             parent.AFWElementResizer.autoHeight('FOOBAR', window)
 *         }, false);
 *     }
 * } catch (e) {}
 * </script>
 * [...]
 *
 * An alternative to document.domain = 'www.foo.se' is
 *
 * document.domain = document.domain.replace(/[^.]*\./, '');
 */
var afw_debug = (window.location.hash == '#afw_debug') ? true : false;
if(!afw_debug) {
    try {
        afw_debug = (parent.window.location.hash == '#afw_debug') ? true : false;
    } catch(e){}
}
var AFWElementResizer = {
	autoHeight: function(sElementId, oWin, bBubble) {
		var oResizeElement = document.getElementById ? document.getElementById(sElementId) : document.all ? document.all[sElementId] : null;
		var iChildDocumentHeight = AFWElementResizer.getDocumentHeight(oWin.document);
		var bubble = bBubble || false;
		if (oResizeElement && iChildDocumentHeight) {
			oResizeElement.style.height = (iChildDocumentHeight + 60) + 'px';
		}
		if (bubble) {
			AFWElementResizer.bubble(sElementId, oWin);
		}
	},
	getDocumentHeight: function(oDocument) {
        function _getHighest()
        {
            var result = false;

            for(var i = 0, m = arguments.length; i < m; ++i)
            {
                if((typeof(arguments[i]) == 'number' && isFinite(arguments[i])))
                {
                    if(result === false || arguments[i] > result)
                    {
                        result = arguments[i];
                    }
                }
            }

            return result;
        }
        var quirk = (document.compatMode !== 'CSS1Compat') ? true : false;
		var iHeight = 0;
        if(quirk)
        {
            iHeight = _getHighest(oDocument.body.scrollHeight, oDocument.documentElement.scrollHeight);
        }
        else
        {
            iHeight = _getHighest(oDocument.documentElement.scrollHeight, oDocument.documentElement.clientHeight);
        }
/*		if (oDocument.body) {
			if (document.addEventListener) {
				iHeight = oDocument.documentElement.offsetHeight;
			} else {
				iHeight = oDocument.body.scrollHeight;
			}
		}*/
        if(afw_debug){ alert(iHeight); }
		return iHeight;
	},
	addEvent: function(oElement, eventType, fn, useCapture){
		if (oElement.addEventListener) {
			oElement.addEventListener(eventType, fn, useCapture);
			return true;
		} else if (oElement.attachEvent) {
			var evt = oElement.attachEvent('on' + eventType, fn);
			return evt;
		} else {
			oElement['on'+eventType] = fn;
			return true;
		}
	},
	bubble: function(sElementId, oWin) {
		try {
			if (parent.AFWElementResizer) {
				parent.AFWElementResizer.addEvent(window, 'load', function() {
					parent.AFWElementResizer.autoHeight(sElementId, window, true)
				}, false);
			}
		} catch (e) {}
	}
};


