﻿var Main =
{
    init: function() {
        if ($("ctl00_languageId")) {
            this.languageId = $F("ctl00_languageId");
            this.pageId = $F("ctl00_pageId");
        }

        this.languageWnd = new Window("languagewnd");

        if ($("helpwnd")) {
            this.helpWnd = new Window("helpwnd");
            this.windowHelp = this.helpWnd.getContent();
        }
        if ($("infownd")) {
            this.infoWnd = new Window("infownd");
        }
        if ($("errorwnd")) {
            this.errorWnd = new Window("errorwnd");
        }
        this.languageButton = $("ctl00_languageButton");
        this.helpReadButton = $("ctl00_helpReadButton");
        this.helpListenButton = $("ctl00_helpListenButton");
        this.localHelpButtons = $$(".localhelpbutton");
        this.localLargeHelpButtons = $$(".locallargehelpbutton");

        if (!this.languageButton) {
            this.languageButton = $("languageButton");
        }

        for (var i = 0; i < this.localHelpButtons.length; i++) {
            Event.observe(this.localHelpButtons[i], "click", this.localHelpClick.bindAsEventListener(this));
        }

        for (var j = 0; j < this.localLargeHelpButtons.length; j++) {
            Event.observe(this.localLargeHelpButtons[j], "click", this.localLargeHelpClick.bindAsEventListener(this));
        }

        Event.observe(this.languageButton, "click", this.languageClick.bindAsEventListener(this));

        if (this.helpReadButton) {
            Event.observe(this.helpReadButton, "click", this.helpReadClick.bindAsEventListener(this));
        }
        if (this.helpListenButton) {
            Event.observe(this.helpListenButton, "click", this.helpListenClick.bindAsEventListener(this));
        }

        swfobject.embedSWF("flash/audioplayer.swf", "audioplayer", 1, 1, "9");

        var showFlag = $("ctl00_preview");
        if (showFlag && showFlag.value == "true") {
            this.showPreview();
        }

        this.handleStatusMessage();
    },

    handleStatusMessage: function() {
        var message = $("ctl00_statusMessage") || $("statusMessage");
        var type = $("ctl00_statusMessageType") || $("statusMessageType");
        if (message && type) {
            var messageType = type.value;
            var messageVal = message.value;

            switch (messageType) {
                case "error":
                    this.showError(messageVal);
                    break;
                case "info":
                    this.showInfo(messageVal);
                    break;
            }
        }
    },

    showInfo: function(message, keepOthers) {
        this.infoWnd.setText(message);
        this.infoWnd.show({ keepOthers: keepOthers });
    },

    showError: function(message, keepOthers) {
        this.errorWnd.setText(message);
        this.errorWnd.show({ keepOthers: keepOthers });
    },

    showPreview: function() {
        var lang = $F("ctl00_languageId");

        var url = "pdfshow.aspx?preview=true&lang=" + lang + "&ac=" + (new Date().getTime()) + "&file=kompetencemappe.pdf";
        var width = 800;
        var height = 600;
        var left = parseInt((screen.availWidth / 2) - (width / 2));
        var top = parseInt((screen.availHeight / 2) - (height / 2));
        var features = "resizable=1,scrollbars=1,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top;

        Main.tryPopup(url, $F("ctl00_previewWndName").replace(/\s+/g, ""), features, $F("ctl00_previewFailureMessage"), $F("ctl00_previewSuccessMessage"));
    },

    tryPopup: function(url, title, features, errorMessage, successMessage) {
        var wnd = window.open(url, title, features);
        if (
            (!wnd) || // The typical result
            (typeof (wnd) == "undefined") || // Safari
            (wnd.closed) ||
            (typeof (wnd.closed) == "undefined")
            ) {
            errorMessage = errorMessage.replace("{0}", url);
            Main.infoWnd.setText(errorMessage);
            Main.infoWnd.show();
        }
        else {
            successMessage = successMessage("{0}", url);
            Main.infoWnd.setText(successMessage);
            Main.infoWnd.show();
        }
    },

    languageClick: function(e) {
        Event.stop(e);
        this.languageWnd.show();
    },

    localHelpClick: function(e) {
        Event.stop(e);
        var ele = Event.element(e);

        // Get help text variable name from element id:
        var textKey = ele.id;
        Element.removeClassName(this.helpWnd.wnd, "largehelp");
        this.helpWnd.setContent(window[textKey]);

        // Show window next to button:
        this.helpWnd.show({ x: 25, y: 0, offsetEle: Event.element(e) });
    },

    localLargeHelpClick: function(e) {
        Event.stop(e);
        var ele = Event.element(e);

        // Get help text variable name from element id:
        var textKey = ele.id;
        this.helpWnd.setContent(window[textKey]);
        Element.addClassName(this.helpWnd.wnd, "largehelp");
        // Show window next to button:
        this.helpWnd.show({ x: 25, y: 0, offsetEle: Event.element(e) });
    },

    helpListenClick: function(e) {
        Event.stop(e);
        this.playSound("audio/" + this.languageId + "/" + this.pageId + ".mp3");
    },

    helpReadClick: function(e) {
        Event.stop(e);
        this.helpWnd.setContent(this.windowHelp);
        this.helpWnd.show({ x: 750, y: 150 });
    },

    playSound: function(url) {
        if ($("audioplayer") && $("audioplayer").playSound) {
            $("audioplayer").playSound(url);
        }
        else {
            setTimeout(function() { Main.playSound(url); }, 200);
        }
    },

    stopSound: function() {
        $("audioplayer").stopSound();
    },

    log: function(message) {
        if (window.console && window.console.firebug) {
            console.log(message);
        }
    }
}

Event.observe(document, "dom:loaded", Main.init.bindAsEventListener(Main));
