function getPosition(ele) {
    var pos = { x: 0, y: 0 };
    do {
        pos.x += ele.offsetLeft;
        pos.y += ele.offsetTop;
        ele = ele.offsetParent;
    } while (ele != null)
    return pos;
}

var Formbox = {
    current: null,
    align: null,
    init: function (options) {
        // init default options
        this.options = Object.extend({
            resizeDuration: 400, // Duration of height and width resizing (ms)
            initialWidth: 825, 	// Initial width of the box (px)
            initialHeight: 0, 	// Initial height of the box (px)
            defaultWidth: 550, 	// Default width of the box (px)
            defaultHeight: 350	// Default height of the box (px)
        }, options || {});

        if (this.overlay == undefined)
            this.overlay = new Element('div').setProperty('id', 'lbOverlay').injectInside(document.forms[0]);
       
        if (this.center == undefined)
            this.center = new Element('div').setProperty('id', 'lbCenter').setStyles({ width: this.options.initialWidth + 'px', height: this.options.initialHeight + 'px', display: 'none' }).injectInside(document.forms[0]);

        this.anchors = [];
        $A($$('a')).each(function (el) {
            if (el.rel && el.href && el.rel.test('^formbox', 'i')) {
                var parts = el.rel.split(" ");
                if (parts.length > 1) {
                    var align = null;
                    $(parts[1]).setStyle('display', 'none').injectInside(this.center);
                    if (parts.length > 2) align = parts[2];
                    if ($$("#" + parts[1] + " div.required-fields-error").length > 0 || $$("#" + parts[1] + " div.confirm").length > 0) {
                        this.current = parts[1];
                        if (align != null) this.align = $(align);
                    }
                    if (parts.length > 2) {
                        if (document.getElementById("filename") != undefined)
                            document.getElementById("filename").setAttribute("value", parts[2]);
                        //$$("#filename").setText(parts[2]);
                    }
                    el.addEvent('click', function (e) {
                        e = new Event(e);
                        e.stop();
                        this.click(el, parts[1], align);
                    } .bind(this));
                    this.anchors.push(el);
                }
            }
        }, this);
        this.overlay.onclick = this.close.bind(this);
        var nextEffect = this.nextEffect.bind(this);
        this.fx = {
            overlay: this.overlay.effect('opacity', { duration: 500 }).hide(),
            center: this.center.effects({ duration: 500, transition: Fx.Transitions.sineInOut, onComplete: nextEffect })
        };
        if (this.current != null) this.open("", "", "", this.current);
    },
    click: function (link, form, alignid) {
        var pos = null;
        if (alignid != null && $(alignid)) {
            pos = getPosition($(alignid));
            this.align = $(alignid);
        }
        this.current = form;
        return this.open(link.href, link.title, link.rel, form, pos);
    },
    openonload: function (form) {
        this.current = form;
        return this.open("", "", null, form, null);
    },
    open: function (sLinkHref, sLinkTitle, sLinkRel, form, pos) {
        $(form).setStyle('display', 'block');
        this.rel = sLinkRel;
        this.position();
        this.setup();

        var pos = null;
        if (this.align != null) pos = getPosition(this.align);

        if (pos != null && pos.x > -1) this.left = pos.x;
        else this.left = (Window.getWidth() - this.options.defaultWidth) / 2;

        if (pos != null && pos.y > -1) this.top = pos.y;
        else this.top = Window.getScrollTop() + (Window.getHeight() / 15);

        this.center.setStyles({ left: this.left + 'px', top: this.top + 'px', display: '' });
        this.fx.overlay.start(0.5);
        this.step = 1;
        this.fx.center.start({ 'width': [$(form).offsetWidth], 'height': [$(form).offsetHeight] });
    },
    setup: function () {
        if (this.rel != undefined) {
            var aDim = this.rel.match(/[0-9]+/g);
            this.options.contentsWidth = (aDim && (aDim[0] > 0)) ? aDim[0] : this.options.defaultWidth;
            this.options.contentsHeight = (aDim && (aDim[1] > 0)) ? aDim[1] : this.options.defaultHeight;
        }
    },
    position: function () {
        if (this.overlay != undefined)
            this.overlay.setStyles({ 'top': window.getScrollTop() + 'px', 'height': window.getHeight() + 'px' });
    },
    nextEffect: function () {
        switch (this.step++) {
            case 1:
                break;
            case 2:
                if (this.current) { $(this.current).setStyle('display', 'none'); this.current = null; this.align = null; }
                this.center.style.display = 'none';
                break;
        }
    },
    close: function () {
        this.fx.center.start({ 'height': [this.options.initialHeight] });
        this.fx.overlay.start(0);
        return false;
    }
};
window.addEvent('domready', Formbox.init.bind(Formbox));

