var Site = {
    init: function() {
        Site.overlay.init();
    },

    VideoFacts: {
        init: function() {
            var toggles = $$("#afibFacts .question");
            var togd = $$("#afibFacts .answer");

            togd.hide();
            toggles.each(function(el, i) {
                el.addEvent("click", function() {
                    var how = togd[i].getStyle("display") == "none";
                    if (how) {
                        el.getElement(".toggle").set("html", "&ndash;");
                        togd[i].show();
                    }
                    else {
                        el.getElement(".toggle").set("html", "+");
                        togd[i].hide();
                    }
                })
            });
        }
    },

    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;

                var href = el.get("href");
                var videoid = el.get("rel") || "1"

                el.store("overhref", href);
                el.store("videoid", videoid);

                el.set({
                    href: "javascript:void(0);",
                    target: ""
                });

                el.addEvent("click", function(e) {
                    e.stop();
                    this.launch(el);
                } .bind(this))

                if (num == videoid) autostart = el;

            }, this)

            if (autostart) this.launch(autostart);
        },

        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([
                new Element("div", { "class": "overlayHead" }).adopt([
                    this.emailBtn = new Element("div", { "class": "overEmail", html: "<a href='javascript:void'>E-mail</a>" }),
                    this.shareBtn = new Element("div", { "class": "overShare", html: "<a href='javascript:void(0);' onmouseout='addthis_close()'>Share</a>" })
                ]),
                new Element("div", { "class": "overlayBody" }).adopt(
                    this.flashHolder = new Element("div", { "class": "flashHolder" })
                ),
                new Element("div", { "class": "overlayFoot" }).adopt([
                    new Element("div", { "class": "footLegal", html: "&copy; " + new Date().getFullYear() + " sanofi-aventis U.S. LLC. All rights reserved." }),
                    this.pdfBox = new Element("div", { "class": "footPdf" })
                ])
            ]);

        },

        launch: function(el) {
            var href = el.retrieve("overhref");
            this.videoid = el.retrieve("videoid");

            this.scrolled.toTop();
            window.addEvent("resize", this.resizeOverlay.bind(this)).addEvent("scroll", this.resizeOverlay.bind(this));
            this.resizeOverlay();
            this.toggleLoader(true);
            this.overlay.show();

            var req = new Request({
                url: href,
                onSuccess: function(txt, xml) {
                    this.toggleLoader();
                    this.setContent(txt);
                } .bind(this),
                onFailure: function() {
                    console.log("fail")
                    this.collapse();
                } .bind(this)
            });

            req.send();

        },

        collapse: function() {
            this.overlay.hide();
            this.toggleLoader();
            this.wrap.hide();
            this.flashHolder.empty();
            window.removeEvents("resize").removeEvent("scroll")
        },

        setContent: function(els) {
            this.flashHolder.empty();

            var dummy = new Element("div", { "html": els + "", styles: { display: "none"} });
            this.pdfBox.set("html", dummy.getElement(".pdfWrap").innerHTML || "");

            this.setFlash();

            //update overlay util buttons
            this.emailBtn.getElement("a").set("href", window.root + "Email_Colleague.aspx?videoid=" + this.videoid);
            var anchor = this.shareBtn.getElement("a") || new Element("div"); //.set("onmouseover", "return addthis_open(this, " + "''" + ", " + "'" + window.location.href + "?videoid=" + this.videoid + "'" + ", " + "'[TITLE]')");
            var pageurl = window.location.href + "?videoid=" + this.videoid;
            anchor.removeEvents("mouseover").addEvent("mouseover", function() {
                return addthis_open(this, '', pageurl, '[TITLE]');
            });

            this.resizeOverlay();
            this.overlay.show();
            this.wrap.show();
        },

        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 page = $A(window.location.href.split("/")).getLast();
            var flashvars = {
                xmlConfigUrl: window.root + "docs/swf/xml/videoPlayerConfig.xml",
                pageUrl: page,
                playerName: page.split(".")[0],
                pageName: page.split(".")[0],
                autoStartId: this.videoid.toInt()
            };
            var params = {
                menu: "true",
                wmode: "opaque",
                bgcolor: "#ffffff",
                base: window.root + "docs/swf/",
                swliveconnect: "true",
                allowscriptaccess: "always",
                allowfullscreen: "true"
            };
            var attributes = {
                id: "videoSection",
                name: "videoSection"
            };
            swfobject.embedSWF(window.root + "../../docs/swf/videoSection.swf", "flashContent", "640", "386", "9.0.0", window.root + "../../docs/swf/expressInstall.swf", flashvars, params, attributes);

            SWFFormFixAuto();
            //SWFFormFix("videoSection");
            //alert(flashvars.playerName);
        }
    },

    autoStart: function() {
        var chk = Cookie.read("showIwindow");
        if (chk == null) {
            $$(".iwindow")[0].fireEvent("click");
            Cookie.write("showIwindow", "t", { duration: false });
        };

    },

    educator: {
        init: function() {
            this.centeredOver = $$(".sitewrap")[0] || $(document.body);
            this.scrolled = new Fx.Scroll($(document.body))

            this.build();

            var toggles = $$(".eduVideo");
            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;

                var href = el.get("href");
                var videoid = el.get("rel") || "1";

                el.store("overhref", href);
                el.store("videoid", videoid);

                el.set({
                    href: "javascript:void(0);",
                    target: ""
                });

                el.addEvent("click", function(e) {
                    e.stop();
                    this.launch(el);
                } .bind(this))

                if (num == videoid) autostart = el;

            }, this)

            this.resizeBound = this.resizeOverlay.bind(this);

            if (autostart) this.launch(autostart);
        },

        build: function() {
            var targeted = $(document.body);
            this.overlay = new Element("div", { "id": "eduBoxOverlay", opacity: 0.75, styles: { "display": "none"} }).addEvent("click", function() { this.collapse(); } .bind(this)).inject(targeted, "bottom");
            this.wrap = new Element("div", { "id": "eduBoxWrap", styles: { "display": "none"} }).inject(targeted, "bottom");
            this.closeBtn = new Element("div", { "id": "eduBoxClose" }).addEvent("click", function() { this.collapse(); } .bind(this)).inject(this.wrap);
            this.wrapContent = new Element("div", { "class": "eduBoxContent" }).inject(this.wrap);

            // build the content for the overlay
            this.wrapContent.adopt([
                new Element("div", { "class": "overlayHead" }).adopt([
                    this.emailBtn = new Element("div", { "class": "overEmail", html: "<a href='javascript:void'>E-mail</a>" }),
                    this.shareBtn = new Element("div", { "class": "overShare", html: "<a href='javascript:void(0);' onmouseout='addthis_close()'>Share</a>" })
                ]),
                new Element("div", { "class": "overlayBody" }).adopt(
                    this.flashHolder = new Element("div", { "class": "flashHolder" })
                ),
                new Element("div", { "class": "overlayFoot" }).adopt([
                    new Element("div", { "class": "footLegal", html: "&copy; " + new Date().getFullYear() + " sanofi-aventis U.S. LLC. All rights reserved." })
                ])
            ]);

        },

        launch: function(el) {
            var href = el.retrieve("overhref");
            this.videoid = el.retrieve("videoid");

            this.scrolled.toTop();

            window.removeEvent("resize", this.resizeBound).addEvent("resize", this.resizeBound);
            window.removeEvent("scroll", this.resizeBound).addEvent("scroll", this.resizeBound);

            this.resizeOverlay();
            this.toggleLoader(true);
            this.overlay.show();

            this.setContent();
            this.setFlash();

        },

        collapse: function() {
            this.overlay.hide();
            this.toggleLoader();
            this.wrap.hide();
            this.flashHolder.empty();
            window.removeEvent("resize", this.resizeBound).removeEvent("scroll", this.resizeBound);
        },

        setContent: function() {
            //update overlay util buttons
            this.emailBtn.getElement("a").set("href", window.root + "Email_Colleague.aspx?videoid=" + this.videoid);
            var anchor = this.shareBtn.getElement("a") || new Element("div"); //.set("onmouseover", "return addthis_open(this, " + "''" + ", " + "'" + window.location.href + "?videoid=" + this.videoid + "'" + ", " + "'[TITLE]')");
            var pageurl = window.location.href + "?videoid=" + this.videoid;
            anchor.removeEvents("mouseover").addEvent("mouseover", function() {
                return addthis_open(this, '', pageurl, '[TITLE]');
            });

            this.resizeOverlay();
            this.overlay.show();
            this.wrap.show();
        },

        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(id) {
            this.flashHolder.empty();
            this.flashHolder.adopt(new Element("div", { "id": "eduFlashContent" }));

            var page = $A(window.location.href.split("/")).getLast();

            var flashvars = {
                xmlConfigUrl: window.root + "docs/swf/xml/videoPlayerConfig.xml",
                pageUrl: page,
                playerName: "AFIB Educator",
                pageName: "AFIB Educator",
                autoStartId: this.videoid.toInt()
            };

            var params = {
                menu: "true",
                wmode: "opaque",
                bgcolor: "#ffffff",
                base: window.root + "docs/swf/",
                swliveconnect: "true",
                allowscriptaccess: "always",
                allowfullscreen: "true"
            };
            var attributes = {
                id: "videoSection",
                name: "videoSection"
            };
            swfobject.embedSWF(window.root + "docs/swf/videoSectionAfibCustom.swf", "eduFlashContent", "330", "440", "9.0.0", window.root + "docs/swf/expressInstall.swf", flashvars, params, attributes);

            SWFFormFixAuto();

            //alert(flashvars.playerName);
        }


    }

};

window.addEvent('domready', function() {
    Site.init();
});


function thisMovie(movieName) {
	 if (navigator.appName.indexOf("Microsoft") != -1) {
		 return document.getElementById(movieName);//window[movieName];
	 } else {
		 return document[movieName];
	 }
}
function closePopupWindow()
{
	Site.educator.collapse();
}
function playVideo()
{
	thisMovie("videoSection").playVideo();
}
function resumeVideo()
{
	thisMovie("videoSection").resumeVideo();
}
function pauseVideo()
{
	thisMovie("videoSection").pauseVideo();
}
function stopVideo()
{
	thisMovie("videoSection").stopVideo();
}
function seekVideo(offset)
{
	thisMovie("videoSection").seekVideo(offset);
}
function setVideoVolume(level)
{
	thisMovie("videoSection").setVideoVolume(level);
}
function fullScreenVideo()
{
	thisMovie("videoSection").fullScreenVideo();
}
function loadVideoById(id)
{
	thisMovie("videoSection").loadVideoById(id);
}
function loadVideoPlayList(stringArray)
{
	thisMovie("videoSection").loadVideoPlayList(stringArray);
}

function MM_showHideLayers() { //v6.0
    var i, p, v, obj, args = MM_showHideLayers.arguments;
    for (i = 0; i < (args.length - 2); i += 3) if ((obj = MM_findObj(args[i])) != null) {
        v = args[i + 2];
        if (obj.style) { obj = obj.style; v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v; }
        obj.visibility = v;
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

