﻿var QA = Class.create();

QA.prototype = {
    initialize:function(containingElement,ajaxUrl,options) {
        this.containingElement = $(containingElement);
        this.ajaxUrl = ajaxUrl;
        this.options = options;
        this.qaImagePath = this.options.qaImagePath;
        this.progressImage = this.options.progressImage || '/images/indicator.gif';
        this.endColor = this.options.endColor || '#ffffff';
        this.Elems.container = $(document.createElement('div'));
        this.Elems.container.id = 'qa_inner_container';
        //this.Elems.container.setStyle({ width:'100%',margin:'10px 0px 0px 0px' });
        this.Elems.tag = $(document.createElement('p'));
        this.Elems.tag.id = 'qa_tag';
        //this.Elems.tag.setStyle({ fontWeight:'bold' });
        if(this.qaImagePath) {
            qaImage = $(document.createElement('img'));
            qaImage.setAttribute('src',this.qaImagePath);
            qaImage.setStyle({ verticalAlign:'bottom' });
            this.Elems.tag.appendChild(qaImage);
        }
        this.Elems.tag.appendChild(document.createTextNode('Have a question about this course? Please ask it below:'));
        this.Elems.progressIndicator = $(document.createElement('div'));
        this.Elems.progressIndicator.id = 'qa_progress_indicator';
        //this.Elems.progressIndicator.setStyle({ width:'100%',textAlign:'center' });
        progressIndicatorImage = $(document.createElement('img'));
        progressIndicatorImage.id = 'qa_image_indicator';
        progressIndicatorImage.setAttribute('src',this.progressImage);
        this.Elems.progressIndicator.appendChild(progressIndicatorImage);
        this.Elems.progressIndicator.hide();
        this.Elems.updatePanel = $(document.createElement('div'));
        this.Elems.updatePanel.id = 'qa_update_panel';
        this.construct();
    },
    construct:function() {
        this.Elems.container.appendChild(this.Elems.tag);
        this.Elems.container.appendChild(this.Elems.progressIndicator);
        this.Elems.container.appendChild(this.Elems.updatePanel);
        this.constructUpdatePanel(this.Elems.updatePanel);
        this.containingElement.appendChild(this.Elems.container);
    },
    constructUpdatePanel:function(elem) {
        this.Elems.updateBlock = $(document.createElement('div'));
        this.Elems.updateBlock.id = 'qa_update_block';
        //this.Elems.updateBlock.setStyle({ width:'100%',textAlign:'left',margin:'0px 0px 25px 0px' });
        this.Elems.questionBlock = $(document.createElement('div'));
        this.Elems.questionBlock.id = 'qa_question_block';
        this.Elems.questionBlock.setStyle({ width:'100%',textAlign:'left' });
        this.constructUpdateBlock(this.Elems.updateBlock);
        this.constructQuestionBlock(this.Elems.questionBlock);
        elem.appendChild(this.Elems.updateBlock);
        elem.appendChild(this.Elems.questionBlock);
    },
    constructUpdateBlock:function(elem) {
        blockquote = $(document.createElement('blockquote'));
        this.Elems.answerSpan = $(document.createElement('span'));
        this.Elems.answerSpan.id = 'qa_answer_span';
        this.Elems.answerSpan.hide();
        this.Elems.helpful = $(document.createElement('div'));
        this.Elems.helpful.id = 'qa_helpful_panel';
        this.Elems.helpful.hide();
        helpful_para = $(document.createElement('p'));
        helpful_para.id = 'qa_helpful';
        //helpful_para.setStyle({ margin:'0px 0px 25px 0px',fontWeight:'bold' });
        helpful_para.appendChild(document.createTextNode('Was this answer helpful?'));
        this.constructRadioButtons(helpful_para);
        //divBreak = $(document.createElement('div'));
        //divBreak.setStyle({ clear:'both' });
        this.Elems.thankyou = $(document.createElement('p'));
        this.Elems.thankyou.id = 'qa_thankyou';
        this.Elems.thankyou.hide();
        this.Elems.helpful.appendChild(helpful_para);
        //this.Elems.helpful.appendChild(divBreak);
        blockquote.appendChild(this.Elems.answerSpan);
        blockquote.appendChild(this.Elems.helpful);
        blockquote.appendChild(this.Elems.thankyou);
        elem.appendChild(blockquote);
    },
    constructQuestionBlock:function(elem) {
        this.Elems.errorPara = $(document.createElement('p'));
        this.Elems.errorPara.id = 'qa_error_para';
        this.Elems.questionPara = $(document.createElement('p'));
        this.Elems.questionPara.id = 'qa_question_para';
        this.Elems.questionPara.setStyle({ textAlign:'center' });
        this.Elems.question = $(document.createElement('input'));
        this.Elems.question.id = 'qa_question';
        //this.Elems.question.setStyle({ width:'500px',height:'35px',fontSize:'large',fontWeight:'bold' });
        this.Elems.question.observe('keypress',function() {
            if(window.event && window.event.keyCode == 13) {
                this.submitQuestion();
                return false;
            }
        }.bindAsEventListener(this));
        copySpan = $(document.createElement('span'));
        copySpan.id = 'qa_question_note';
        //copySpan.setStyle({ textAlign:'center',fontFamily:'Arial',fontSize:'7.5pt' });
        copySpan.appendChild(document.createTextNode('Please submit a legitimate question.'));
        copySpan.appendChild(document.createElement('br'));
        //copySpan.appendChild(document.createTextNode('It will be displayed in the Q&A session, along with your name, agency and location, for the host to address.'));
        this.Elems.questionPara.appendChild(this.Elems.question);
        this.Elems.questionPara.appendChild(document.createElement('br'));
        this.Elems.questionPara.appendChild(copySpan);
        this.Elems.submitPara = $(document.createElement('p'));
        this.Elems.submitPara.id = 'qa_submit_para';
        this.Elems.submitPara.setStyle({ textAlign:'center' });
        this.Elems.submitButton = $(document.createElement('input'));
        this.Elems.submitButton.id = 'qa_submit';
        this.Elems.submitButton.setAttribute('name','qa_submit');
        this.Elems.submitButton.setAttribute('type','button');
        this.Elems.submitButton.setAttribute('value','Ask Question');
        this.Elems.submitButton.observe('click',this.submitQuestion.bindAsEventListener(this));
        this.Elems.submitPara.appendChild(this.Elems.submitButton);
        elem.appendChild(this.Elems.errorPara);
        elem.appendChild(this.Elems.questionPara);
        elem.appendChild(this.Elems.submitPara);
    },
    constructRadioButtons:function(elem) {
        yes = $(document.createElement('input'));
        yes.id = 'qa_helpful_radio';
        yes.setAttribute('name','qa_helpful_radio');
        yes.setAttribute('type','radio');
        yes.setAttribute('value','yes');
        no = $(document.createElement('input'));
        no.id = 'qa_not_helpful_radio';
        no.setAttribute('name','qa_helpful_radio');
        no.setAttribute('type','radio');
        no.setAttribute('value','no');
        yes.observe('click', this.isHelpful.bindAsEventListener(this));
        no.observe('click', this.isNotHelpful.bindAsEventListener(this));
        elem.appendChild(yes);
        elem.appendChild(document.createTextNode(' yes '));
        elem.appendChild(no);
        elem.appendChild(document.createTextNode(' no '));
    },
    submitQuestion:function(event) {
        if (this.Elems.question.value == '')
            alert('Please submit a legitimate question');
        else
        {
            params = 'method=submitQuestion&question=' + this.Elems.question.value.replace(/&/,"and");
            new Ajax.Request(this.ajaxUrl, { postBody:params, onCreate:function() { this.Elems.progressIndicator.show(); }.bindAsEventListener(this), onComplete:function(transport) { this.Elems.progressIndicator.hide(); this.processAnswer(transport); }.bindAsEventListener(this) });
        }
    },
    isHelpful:function(event) {
        params = 'method=isHelpful';
        this.prepareRadio(params);
    },
    isNotHelpful:function(event) {
        this.ExclusionList.push(this.Answer.answer_id);
        params = 'method=isNotHelpful&question=' + this.Elems.question.value + '&answer_id=' + this.Answer.answer_id + '&exclusionList=' + encodeURIComponent(this.ExclusionList.toJSON());
        this.prepareRadio(params);
        this.Answer.clear();
        this.Elems.answerSpan.hide();
        this.Elems.answerSpan.innerHTML = '';
    },
    prepareRadio:function(params) {
        new Ajax.Request(this.ajaxUrl, { postBody:params, onCreate:function() { this.Elems.progressIndicator.show(); }.bindAsEventListener(this), onComplete:function(transport) { this.Elems.progressIndicator.hide(); this.processRadio(transport); }.bindAsEventListener(this) });
        this.Elems.helpful.hide();
        $('qa_helpful_radio').checked = false;
        $('qa_not_helpful_radio').checked = false;
    },
    processRadio:function(transport) {
        this.processJSON(transport);
    },
    processAnswer:function(transport) {
        this.processJSON(transport);
    },
    processJSON:function(transport) {
        response = transport.responseText.evalJSON();
        this.Elems.question.value = response.question;
        this.Elems.thankyou.innerHTML = response.thankyou;
        if(response.exclusionList == '' && response.answer_id != '')
            this.ExclusionList.clear();
        else
            this.parseExclusionList(response.exclusionList);
        if(response.answer_id != '') {
            this.Answer.answer_id = response.answer_id;
            this.Answer.answer = response.answer;
            this.Elems.answerSpan.innerHTML = this.Answer.answer;
            this.Elems.answerSpan.show();
            if(response.helpful == 'True')
                this.Elems.helpful.show();
            else
                this.Elems.helpful.hide();
        }
        else {
            this.Answer.clear();
            this.Elems.answerSpan.hide();
            this.Elems.answerSpan.innerHTML = '';
            this.Elems.helpful.hide();
        }
        if(this.Elems.thankyou.innerHTML == '')
            this.Elems.thankyou.hide();
        else {
            this.Elems.thankyou.show();
        }
        new Effect.Highlight(this.Elems.updateBlock.id, { endcolor:this.endColor });
    },
    parseExclusionList:function(exclusionList) {
        this.ExclusionList.clear();
        this.ExclusionList = exclusionList.toArray();
    },
    Elems: {
    },
    ExclusionList:[
    ],
    Answer: {
        clear:function() {
            this.answer_id = null;
            this.answer = null;
        }
    }
};
