var AVERY = {};

AVERY.matrix = function(id,QAs) {
    if (id && QAs) {
        this.init(id,QAs); 
    }
};
AVERY.matrix.prototype = {	
	
	init : function(id,QAs) {
	    this.stage = YAHOO.util.Dom.get(id);
        YAHOO.util.Dom.addClass(this.stage,"qaStage");
	    this.QAs = QAs;
	    // start at the root question, and clear out the loading
	    this.stage.innerHTML = '';
	    this.nextQuestion("0");
	},
	
    nextQuestion : function(pathway,parent) { 
        
        // use the pathway to find out what question should be shown
        var currentQA = this.QAs[0];
        var pieces = pathway.split('-');
        for (var i=1; i<pieces.length; i++) {
            currentQA = currentQA.answers[pieces[i]].qa;
        }
                  
        // create the question
        var question = document.createElement("h3");
        YAHOO.util.Dom.addClass(question,"question");
        YAHOO.util.Dom.setStyle(question,"marginBottom","5px");
        question.innerHTML = currentQA.question;
        this.stage.appendChild(question);
        
        // create the answers
        var answers = document.createElement("select");
        YAHOO.util.Dom.addClass(answers,"answers");
        YAHOO.util.Event.on(answers,'change',this.choseAnswer,this,true);
        var answer = new Array();
        answer[0] = document.createElement("option");
        YAHOO.util.Dom.addClass(answer[0],"answer");
        answer[0].innerHTML = "Pick an answer:";
        answer[0].value = '';
        answers.appendChild(answer[0]);
        for (var i=0; i<currentQA.answers.length; i++) {
            answer[i+1] = document.createElement("option");
            YAHOO.util.Dom.addClass(answer[i+1],"answer");
            answer[i+1].innerHTML = currentQA.answers[i].answer;            
            if (currentQA.answers[i].href) {
                answer[i+1].value = "HREF="+currentQA.answers[i].href;
            }
            else { 
                answer[i+1].value = "NEWQA="+pathway+"-"+i;
            }
            answers.appendChild(answer[i+1]);
        }
        this.stage.appendChild(answers);
    },
    
    choseAnswer : function(e,clickObj) {
        var answers = YAHOO.util.Event.getTarget(e);
        var tempArray = answers.value.split("=");
        var answerType = tempArray[0];
        tempArray.splice(0,1);
        var answer = tempArray.join("=");
        answers.firstChild.disabled = "disabled";
        if (answerType == "HREF") {
            location.href = answer;
        }
        else if (answerType == "NEWQA") {
            // remove QAs that exist after this drop down
            while (answers.nextSibling) {
                var removeMe = answers.nextSibling;
                while (removeMe.hasChildNodes()) {
                    removeMe.removeChild(removeMe.childNodes[0]);
                }
                removeMe.parentNode.removeChild(removeMe);
            }
            this.nextQuestion(answer);
        }
    }
    
};