var Site = {
    init: function() {
        Site.overlay.init();
    },

    overlay: {
        init: function() {
            this.centeredOver = $$(".sitewrap")[0] || $(document.body);
            this.scrolled = new Fx.Scroll($(document.body))

            this.build();

            var toggles = $$(".videoItem");
            var autostart = null;
            var _href = window.location.href;
            var arr = null, num;
            if (_href.indexOf("videoid=") != -1) {
                var exp = /^.*\?.*videoid=(\d{1,})&?.*$/i;
                arr = _href.match(exp);
                if (arr && arr.length > 1) {
                    num = arr[1];
                }
            }

            toggles.each(function(el, i) {
                if ($type(el) != "element") return;

                el.set({
                    href: "javascript:void(0);",
                    target: ""
                });

                el.addEvent("click", function(e) {
                    e.stop();
                    this.launch(el);
                } .bind(this))

            }, this)

        },

        build: function() {
            var targeted = $(document.body);
            this.overlay = new Element("div", { "id": "boxOverlay", opacity: 0.75, styles: { "display": "none"} }).addEvent("click", function() { this.collapse(); } .bind(this)).inject(targeted, "bottom");
            this.wrap = new Element("div", { "id": "boxWrap", styles: { "display": "none"} }).inject(targeted, "bottom");
            this.closeBtn = new Element("div", { "id": "boxClose" }).addEvent("click", function() { this.collapse(); } .bind(this)).inject(this.wrap);
            this.wrapContent = new Element("div", { "class": "boxContent" }).inject(this.wrap);

            // build the content for the overlay
            this.wrapContent.adopt(this.flashHolder = new Element("div", { "class": "flashHolder" }));

        },

        launch: function(el) {

            this.scrolled.toTop();
            window.addEvent("resize", this.resizeOverlay.bind(this)).addEvent("scroll", this.resizeOverlay.bind(this));
            this.resizeOverlay();
            this.toggleLoader(true);
            this.overlay.show();
            this.setContent();

        },

        collapse: function() {
            this.overlay.hide();
            this.toggleLoader();
            this.wrap.hide();
            this.flashHolder.empty();
            window.removeEvents("resize").removeEvent("scroll")
        },

        setContent: function() {
            this.flashHolder.empty();
            
            this.setFlash();
            this.resizeOverlay();
            this.overlay.show();
            this.wrap.show();
            this.toggleLoader();
        },

        resizeOverlay: function() {
            this.overlay.setStyle("height", window.getScrollSize().y)

            var wrapStyle = {}
            wrapStyle.left = ($(document.body).getSize().x / 2).toInt() - (this.wrap.getStyle("width").toInt() / 2).toInt() + "px"
            wrapStyle.top = "100px";

            this.wrap.setStyles(wrapStyle);

        },

        toggleLoader: function(state) {
            var fn = (state) ? "addClass" : "removeClass";
            this.overlay[fn]("overlayLoading");
        },

        setFlash: function() {
            this.flashHolder.adopt(new Element("div", { "id": "flashContent" }));
                       
            var flashvars = {};
            var params = {
                menu: "true",
                wmode: "opaque",
                bgcolor: "#ffffff",
                base: "http://downloads.buystreaming.edgesuite.net/anon.buystreaming/intouch/SanofiAventis/nasacort/video/",
                swliveconnect: "true",
                allowscriptaccess: "always",
                allowfullscreen: "true"
            };
            var attributes = {
                id: "videoSection",
                name: "videoSection"
            };
            swfobject.embedSWF("http://downloads.buystreaming.edgesuite.net/anon.buystreaming/intouch/SanofiAventis/nasacort/video/Nasacort_PatientEd_032509.swf", "flashContent", "800", "600", "9.0.0", window.root + "Common/Docs/Swf/expressInstall.swf", flashvars, params, attributes);
        }
    }
}

window.addEvent('domready', function() {
    Site.init();
});
