﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("VoxWeb.Web");

VoxWeb.Web.WindowFaq = function(element) {
    VoxWeb.Web.WindowFaq.initializeBase(this, [element]);
    this._answers = null;
    this._questionHandler = null;
    this._answerPanelID = null;
    this._answerPanel = null;
    this._questionPanel = null;
    this._questionPanelID = null;
}

VoxWeb.Web.WindowFaq.prototype = {
    initialize: function() {
        VoxWeb.Web.WindowFaq.callBaseMethod(this, 'initialize');

        //create the Question handler to maintain the context
        this._questionHandler = Function.createDelegate(this, this._showAnswer);
        $addHandler(this.get_element(), 'click', this._questionHandler);

        // get the array of the answers from the string
        this._answers = this._answers.split(";|;");

        // get the answer panel handle
        this._answerPanel = $get(this._answerPanelID);

        // select the first question, and answer
        try {
            var qPanel = $get(this._questionPanelID);
            if (qPanel != undefined && qPanel != null) {
                var ancs = qPanel.getElementsByTagName("A");
                if (ancs == null) {
                    return;
                }
                this._resetQuestions(ancs[0]);
                ancs[0].parentNode.className = "SelectedQuestion";
                this._answerPanel.innerHTML = ancs[0].innerHTML;
                this._answerPanel.innerHTML += this.get_answers()[0];
            }
        } catch (ex) { }
    },
    dispose: function() {
        $removeHandler(this.get_element(), 'click', this._questionHandler);

        // dispose the base class
        VoxWeb.Web.WindowFaq.callBaseMethod(this, 'dispose');
    },
    _showAnswer: function(args) {
        if (args !== undefined && args.target.tagName == "A") {
            var qId = Number.parseInvariant(args.target.attributes["questionId"].value);
            this._resetQuestions(args.target);
            args.target.parentNode.className = "SelectedQuestion";
            var answer = this.get_answers()[qId];
            this._answerPanel.innerHTML = args.target.innerHTML;
            this._answerPanel.innerHTML += answer;
        }
    },
    get_answers: function() {
        return this._answers;
    },
    _resetQuestions: function(anchor) {
        if (this._questionPanel == null) {
            this._questionPanel = anchor.parentNode.parentNode;
        }

        var paras = this._questionPanel.getElementsByTagName("P");
        if (paras) {
            for (var i = 0; i < paras.length; i++) {
                paras[i].className = "NoMargin";
            }
        }
    }
}

VoxWeb.Web.WindowFaq.registerClass('VoxWeb.Web.WindowFaq', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
