﻿var swffit = function() {
    var win = window,
    doc = document,
    html = doc.getElementsByTagName("html")[0],
    AGENT = navigator.userAgent.toLowerCase(),
    WK = /webkit/.test(AGENT),
    IE = /msie/.test(AGENT) && !win.opera,
    FF = /firefox/.test(AGENT) && !win.opera,
    UNDEF = "undefined",
    _ft, _re, _t, _mw, _mh, _xw, _xh, _hc, _vc, _ow, _oh, _subH, _fancy_w, _fancy_h;
    swfobject.createCSS("object", "position:relative");

    function recreate() {
        win = window,
        doc = document,
        html = doc.getElementsByTagName("html")[0],
        AGENT = navigator.userAgent.toLowerCase(),
        WK = /webkit/.test(AGENT),
        IE = /msie/.test(AGENT) && !win.opera,
        FF = /firefox/.test(AGENT) && !win.opera,
        UNDEF = "undefined",
        _ft = null, _re = null, _t = null, _mw = null, _mh = null, _xw = null, _xh = null, _hc = null, _vc = null, _ow = null, _oh = null, _subH = null, _fancy_w = null, _fancy_h = null;
        swfobject.createCSS("object", "position:relative");
    }

    function fit(t, mw, mh, xw, xh, hc, vc, subH) {
        var mw = (mw) ? mw : _ow,
        mh = (mh) ? mh : _oh,
        xw = (xw) ? xw : null,
        xh = (xh) ? xh : null,
        hc = (hc || hc == null),
        vc = (vc || vc == null),
        subH = (subH) ? subH : null;
        configure({ target: t,
            minWid: mw,
            minHei: mh,
            maxWid: xw,
            maxHei: xh,
            hCenter: hc,
            vCenter: vc,
            subtractH: subH
        });

    }
    function configure(o) {
        var evalNum = function(v, p) { return (typeof o[p] != UNDEF) ? o[p] : v; };
        _mw = evalNum(_mw, "minWid");
        _mh = evalNum(_mh, "minHei");
        _xw = evalNum(_xw, "maxWid");
        _xh = evalNum(_xh, "maxHei");
        _subH = evalNum(_subH, "subtractH");
        var evalBool = function(v, p) { return (o[p] || (v && typeof o[p] == UNDEF)); };
        _hc = evalBool(_hc, "hCenter");
        _vc = evalBool(_vc, "vCenter");
        if (o.target && (o.target != _t)) {
            _t = o.target;
            swfobject.addDomLoadEvent(initFit);
            if (IE) {
                swfobject.addLoadEvent(initFit);
            }
        }
        else {
            startFit();
        }
    }

    function initFit() {
        if (!html.style.overflowX) {
            controlScroll(0, 0);
        }
        if (!html.style.overflowY) {
            controlScroll(0, 1);
        }
        doc.body.style.margin = doc.body.style.padding = 0;
        var st = "width:100%; height:100%";
        st += (IE) ? "; overflow:hidden" : "";
        swfobject.createCSS("#" + _t, st);
        _ft = doc.getElementById(_t);
        _ft = (_ft != UNDEF && FF && /object/.test(_ft.innerHTML)) ?
            doc.getElementById(_t).getElementsByTagName("object")[0] :
            _ft;
        _ow = _ft.width;
        _oh = _ft.height;
        _mw = (_mw) ? _mw : _ow;
        _mh = (_mh) ? _mh : _oh;
        startFit();
    }

    function startFit() {
        setSize();
        if (!_re) {
            swffit.addResizeEvent(setSize);
            _re = 1;
        }
    }

    function stopFit(w, h) {
        if (_re) {
            swffit.removeResizeEvent(setSize);
            _re = 0;
            setStyle("top", "auto");
            setStyle("left", "auto");
            setStyle("marginTop", 0);
            setStyle("marginLeft", 0);
            var w = (w) ? w : "100%", h = (h) ? h : "100%";
            setWidth(w);
            setHeight(h);
            forceRedraw();
        }
    }

    function forceRedraw() {
        if (WK) {
            html.focus();
        }
    }

    function controlResizeEvent(a, fn) {
        var p = (a) ? ["addEventListener", "attachEvent"] : ["removeEventListener", "detachEvent"];
        if (win[p[0]]) {
            win[p[0]]("resize", fn, false);
        }
        else {
            if (win[p[1]]) {
                win[p[1]]("onresize", fn);
            }
        }
    }

    function setWidth(w) {
        var v = (isNaN(w)) ? w : w + "px";
        setStyle("width", v);
        var v = getBodyWidth();

        if (document.getElementById('header') != null) {
            document.getElementById('header').style["width"] = (v - 40) + "px";
        }
        if (document.getElementById('video') != null) {
            document.getElementById('video').style["width"] = (v - 50) + "px";
        }
        if (document.getElementById('backgroundImage') != null) {
            document.getElementById('backgroundImage').style["width"] = (v) + "px";
        }
        if (document.getElementById('logo') != null) {
            document.getElementById('logo').style["width"] = (v - 50) + "px";
        }
        if (document.getElementById('overviewFooter') != null) {
            document.getElementById('overviewFooter').style["width"] = (v - 50) + "px";
        }

        try {
            var fancy = document.getElementById('fancy_outer');
            if (fancy != null) {
                if (_fancy_w == null || isNaN(_fancy_w)) {
                    _fancy_w = new String(fancy.style["width"]);
                    if (_fancy_w.indexOf('px') > 0) {
                        _fancy_w = _fancy_w.substring(0, _fancy_w.length - 2);
                    }
                    _fancy_w = parseInt(_fancy_w);

                    _fancy_h = new String(fancy.style["height"]);
                    _fancy_h = _fancy_h.substring(0, _fancy_h.length - 2);
                    _fancy_h = parseInt(_fancy_h);
                }

                var neww, newh;
                if (_fancy_w > v - 60) {
                    neww = parseInt(v - 60);
                    newh = parseInt(neww * _fancy_h / _fancy_w);
                }
                else {
                    neww = _fancy_w;
                    newh = _fancy_h;
                }
                fancy.style["width"] = (neww - 4) + 'px';
                fancy.style["height"] = (newh) + 'px';
                document.getElementById('fancy_inner').style.width = neww + 'px';
                document.getElementById('fancy_inner').style.height = newh + 'px'; ;
                document.getElementById('fancy_frame').style.width = (neww - 20) + 'px';
                document.getElementById('fancy_frame').style.height = (newh - 20) + 'px';
            }
        }
        catch (exception) {
        }

    }

    function setHeight(h) {
        var v = (isNaN(h)) ? h : h + "px";
        setStyle("height", v);
        if (document.getElementById('video') != null) {
            document.getElementById('video').style["height"] = v;
        }
    }

    function setStyle(p, v) {
        _ft.style[p] = v;
    }

    function getBodyWidth() {
        var v = (win.innerWidth) ? win.innerWidth : (IE) ? doc.documentElement.clientWidth : doc.body.clientWidth;
        /*var o = _ft.parent;
        //alert(document.getElementById('video').offsetWidth);
        if (v == 0 || v == undefined) {
            v = o.offsetWidth;
        }*/
        return v;
    }

    function getBodyHeight() {
        var v = (win.innerHeight) ? win.innerHeight : (IE) ? doc.documentElement.clientHeight : doc.body.clientHeight;
        if (v == 0 || v == undefined) {
            v = _ft.parent.offsetHeight;
        }
        return v;
    }


    function setSize() {
        var iw = getBodyWidth(), ih = getBodyHeight();

        ih -= (!IE && iw <= _mw) ? 18 : 0;
        ih -= _subH;
        if (_subH > 0) {
            iw = (ih / _xh * _xw) + 10;
        }

        if (_xw && iw > _xw) {
            iw = _xw;
        }
        else {
            if (_mw && iw < _mw) {
                iw = _mw;
            }
        }
        setWidth(iw);
        setPosition(0, 1, iw);

        if (_xh && ih >= _xh) {
            ih = _xh;
        }
        else {
            if (_mh && ih < _mh) {
                ih = _mh;
            }
        }
        setHeight(ih);
        setPosition(1, 0, iw);
        forceRedraw();
    }

    function setPosition(t, x, w) {
        var p, m;
        if (t) {
            p = (x && _vc) ? "50%" : "auto";
            m = (x && _vc) ? -(_xh * 0.5) + "px" : 0;
            setStyle("top", p);
            setStyle("marginTop", m);
        }
        else {
            p = (x && _hc) ? "50%" : "auto";
            m = (x && _hc) ? (-(w * 0.5)) : 0;
            setStyle("left", p);
            if (doc.getElementById('video').getElementsByTagName("object")[0] == null) {
                m *= -1;
            }
            m += "px";
            setStyle("marginLeft", m);
        }
    }

    function controlScroll(s, v) {
        var p = (v) ? "overflowY" : "overflowX";
        html.style[p] = (s) ? "scroll" : "auto";
    }

    function getValueOf(p) {
        var o = { target: _t, minWid: _mw, minHei: _mh, maxWid: _xw, maxHei: _xh, hCenter: _hc, vCenter: _vc, subtractH: _subH };
        return o[p];
    }
    return {
        recreate: recreate,
        fit: fit,
        configure: configure,
        startFit: startFit,
        stopFit: stopFit,
        getValueOf: getValueOf,
        addResizeEvent: function(fn) { controlResizeEvent(1, fn); },
        removeResizeEvent: function(fn) { controlResizeEvent(0, fn); },
        showScrollH: function() { controlScroll(1, 0); },
        showScrollV: function() { controlScroll(1, 1); }
    };
} ();