/*global iefix:true, document: true, eventHandler: true, window: true, navigator: true */
/*
    Document   : decor.js
    Created on : 2008-jan-28, 18:40:42
    Author     : Andreas Jorneus
*/
/*jslint type:true*/
iefix = {
    init: function init() {

        eventHandler.registerEventListener(window, 'load', iefix.ieFix);
        if (document.selection) {
            var appVersion = parseFloat(navigator.appVersion.match(/MSIE (\d\.\d)/)[1]);
            if (appVersion < 7) {
                eventHandler.registerEventListener(window, 'resize', iefix.ieFix);
            }
        }
    },
    myParseFloat: function myParseFloat(num) {
        var ret = parseFloat(num);
        if (isNaN(ret)) {
            return 0;
        }
        return ret;
    },
    evalFixHeight: function (obj, extra) {
        var offsetHeight, ret;
        if (obj.offsetParent.tagName === "HTML") {
            offsetHeight = document.documentElement.clientHeight;
        }
        else {
            offsetHeight = obj.offsetParent.offsetHeight -
                iefix.myParseFloat(obj.offsetParent.style.borderTopWidth) -
                iefix.myParseFloat(obj.offsetParent.style.paddingTopWidth) -
                iefix.myParseFloat(obj.offsetParent.style.paddingBottomWidth) -
                iefix.myParseFloat(obj.offsetParent.style.borderBottomWidth);
        }
        ret = 
            (
                offsetHeight +
                extra
            )
            ;
        if (ret < 0) {
            ret = 0;
        }

        return ret + 'px';
    },
    beforeFixHeight: function (obj) {
        var bottom = obj.style.bottom;

        return -(iefix.myParseFloat(obj.style.top) +
                    iefix.myParseFloat(bottom) +
                    iefix.myParseFloat(obj.style.borderTopWidth) +
                    iefix.myParseFloat(obj.style.borderBottomWidth) +
                    iefix.myParseFloat(obj.style.paddingBottom) +
                    iefix.myParseFloat(obj.style.paddingTop) +
                    iefix.myParseFloat(obj.style.marginTop) +
                    iefix.myParseFloat(obj.style.marginBottom));
    },
    evalFixWidth: function (obj, extra) {
        var offsetWidth, ret;
        if (obj.offsetParent.tagName === "HTML") {
            offsetWidth = document.documentElement.clientWidth;
        }
        else {
            offsetWidth = obj.offsetParent.offsetWidth -
                iefix.myParseFloat(obj.offsetParent.style.borderLeftWidth) -
                iefix.myParseFloat(obj.offsetParent.style.paddingLeftWidth) -
                iefix.myParseFloat(obj.offsetParent.style.paddingRightWidth) -
                iefix.myParseFloat(obj.offsetParent.style.borderRightWidth);
        }
        ret = 
            (
                offsetWidth +
                extra
            );
        if (ret < 0) {
            ret = 0;
        }
        return ret + 'px';
    },
    beforeFixWidth: function (obj) {
        var right = obj.style.right;
        return -(iefix.myParseFloat(obj.style.left) +
                    iefix.myParseFloat(right) +
                    iefix.myParseFloat(obj.style.borderLeftWidth) +
                    iefix.myParseFloat(obj.style.borderRightWidth) +
                    iefix.myParseFloat(obj.style.paddingRight) +
                    iefix.myParseFloat(obj.style.paddingLeft) +
                    iefix.myParseFloat(obj.style.marginLeft) +
                    iefix.myParseFloat(obj.style.marginRight));
    },

    recursiveFixIEProblem: function recursiveFixIEProblem(obj) {

        var i;
        if ((obj.style && obj.style.left && obj.style.right) || typeof(obj.beforeFixHeight) !== 'undefined') {
            if (typeof(obj.beforeFixWidth) === 'undefined') {
                obj.beforeFixWidth = iefix.beforeFixWidth(obj);
            }
            obj.style.width = iefix.evalFixWidth(obj, obj.beforeFixWidth);
//            obj.style.right ="";

        }

        if ((obj.style && obj.style.top && obj.style.bottom) || typeof(obj.beforeFixHeight) !== 'undefined') {
            if (typeof(obj.beforeFixHeight) === 'undefined') {
                obj.beforeFixHeight = iefix.beforeFixHeight(obj);
            }
            obj.style.height = iefix.evalFixHeight(obj, obj.beforeFixHeight);
  //          obj.style.bottom = "";
        }

        for (i = 0; i < obj.children.length; i++) {
            recursiveFixIEProblem(obj.children[i]);
        }
       

    },

    ieFixSpecific: function ieFixSpecific(obj) {
        if (document.selection) {
            var appVersion = parseFloat(navigator.appVersion.match(/MSIE (\d\.\d)/)[1]);
            if (appVersion < 7) {
                iefix.recursiveFixIEProblem(obj);
            }
        }
    },

    ieFix: function ieFix() {
        iefix.ieFixSpecific(document.body);
    }
};
