var Quiz = new Class({
    Implements:[Options], 
    
    options:
    {
    },
    
    element:null,
    navigation:null,
    buttons:
    {
        next:null, 
        prev:null, 
        check:null, 
        reset:null
    },
    label:null,
    questions:[],
    answers:[],
    results:[],
    index:0,
    fx:null,
    shown:false,
    
    initialize:function(element, options)
    {
        this.element = $(element);
        if(!$chk(this.element)){ return; }
        
        this.fx = new Fx.Tween(this.element, {
            onComplete:function()
            {
                if(this.shown)
                {
                    this.close_complete();
                }
                else
                {
                    this.open_complete();
                }
            }.bind(this)
        });
        
        var open_button = $("quiz_open_button");
        if($chk(open_button))
        {
            open_button.addEvent("click", function(e){ e.stop(); this.open(); }.bind(this));
        }
        var close_button = $("quiz_close_button");
        if($chk(close_button))
        {
            close_button.addEvent("click", function(e){ e.stop(); this.close(); }.bind(this));
        }
        
        this.navigation = this.element.getElement(".navigation");
        
        this.buttons.prev = this.navigation.getElement(".prev");
        this.buttons.next = this.navigation.getElement(".next");
        this.buttons.check = this.navigation.getElement(".check");
        this.buttons.reset = this.navigation.getElement(".reset");
        this.label = this.navigation.getElement(".label .num");
        
        this.buttons.prev.addEvent("click", function(e){ e.stop(); this.prev_question(); }.bind(this));
        this.buttons.next.addEvent("click", function(e){ e.stop(); this.next_question(); }.bind(this));
        this.buttons.check.addEvent("click", function(e){ e.stop(); this.check(); }.bind(this));
        this.buttons.reset.addEvent("click", function(e){ e.stop(); this.reset(); }.bind(this));
        
        if(options != undefined && options != null)
            this.setOptions(options);
            
        this.questions = this.element.getElements(".question");
        
        this.questions.each(function(question)
        {
            question.addClass("hidden");
            
            var answer = question.getElement("li input[value='true']");
            this.answers.push(answer);
            //this.answers.push(answers); //[this.answers.length] = answers;
        }.bind(this));
        
        this.results = this.element.getElements(".result");
        
        this.results.each(function(result)
        {
            result.addClass("hidden");
        });
        
        this.update();
    },
    
    next_question:function()
    {
        if(this.index >= this.questions.length){ return; }
        
        if(!this.check_question())
        {
            this.display_error("You must choose an answer before moving to the next question.");
            return;
        }
        
        this.questions[this.index].addClass("hidden");
        
        this.index++;
        
        this.update();
    },
    
    prev_question:function()
    {
        if(this.index <= 0){ return; }

        this.questions[this.index].addClass("hidden");
        
        this.index--;
        
        this.update();
    },
    
    check_question:function()
    {
        this.index = Math.min(this.questions.length - 1, this.index);
        this.index = Math.max(0, this.index);
        
        var selected = this.questions[this.index].getElement("li input[checked]");
        
        return selected != null;
    },
    
    update:function()
    {
        this.index = Math.min(this.questions.length - 1, this.index);
        this.index = Math.max(0, this.index);
        
        this.questions[this.index].removeClass("hidden");
        
        this.label.set("html", this.index + 1);
        
        this.buttons.prev.removeClass("hidden");
        this.buttons.next.removeClass("hidden");
        this.buttons.reset.addClass("hidden");
        
        if(this.index == 0 || this.index == this.questions.length)
        {
            this.buttons.prev.addClass("hidden");
        }

        if(this.index == this.questions.length - 1)
        {
            this.buttons.next.addClass("hidden");
            this.buttons.check.removeClass("hidden");
        }
        
        if(this.index < this.questions.length - 1)
        {
            this.buttons.check.addClass("hidden");
        }
    },
    
    reset:function()
    {
        this.questions.each(function(question)
        {
            question.getElements("li input[checked]").each(function(el)
            {
                el.checked = false;
            });
        });
        
        this.index = 0;
        
        this.label.getParent().removeClass("hidden");
        this.element.getElement(".questions").removeClass("hidden");
        this.questions.each(function(q){ q.addClass("hidden"); });
        this.element.getElement(".results").addClass("hidden");
        this.update();
    }, 
    
    check:function()
    {
        var correct = 0;
        
        if(!this.check_question())
        {
            this.display_error("You must choose an answer before checking your score.");
            return;
        }
        
        this.answers.each(function(answer)
        {
            if(answer.checked)
                correct++;
        });
        
        this.display_results(correct);
    },
    
    display_error:function(message)
    {
        alert(message);
    },
    
    display_results:function(level)
    {
        var questions = this.element.getElement(".questions");
        questions.addClass("hidden");
        
        this.label.getParent().addClass("hidden");
        this.buttons.prev.addClass("hidden");
        this.buttons.next.addClass("hidden");
        this.buttons.check.addClass("hidden");
        this.buttons.reset.removeClass("hidden");
        
        if(level >= this.results.length){ return; }
        
        this.results.each(function(result){ result.addClass("hidden"); });
        
        this.element.getElement(".questions").addClass("hidden");
        this.element.getElement(".results").removeClass("hidden");
        this.results[level].removeClass("hidden");
        
        this.formulate_answers(this.results[level]);
    },
    
    formulate_answers:function(result)
    {
        var i = 0;
        var answers = result.getElement(".answers");
        if(!$chk(answers)){ return; }
        
        answers.empty();
        
        this.questions.each(function(question)
        {
            var selected = question.getElement("input[checked]");
            var correct  = question.getElement("input[value='true']");
            
            this.inject_answer(answers, i, question, selected, correct);
            
            i++;
        }.bind(this));
    },
    
    inject_answer:function(answers, index, question, selected, correct)
    {
        var is_correct = selected == correct;
                
        var container = new Element("div", {"class":"answer"});
        
        var h2a = new Element("h2");
        
        var q = new Element("span");
        var num = index + 1;
        var ql = "QUESTION " + num.toString() + ": ";
        
        if(is_correct){ ql += " Correct!"; }
        q.set("html", ql);
        
        var status = new Element("span", {"class":"incorrect"});
        status.set("html", "Incorrect!");

        h2a.grab(q);
        if(!is_correct)
        {
            h2a.grab(status);
        }
        container.grab(h2a);
        
        var p = new Element("p");
        var label = question.getElement("p .label");
        p.set("html", label.get("html"));
        container.grab(p);
        
        var h2b = new Element("h2");
        h2b.set("html", "ANSWER:");
        container.grab(h2b);
        
        var p2 = new Element("p");
        
        var selected_text = selected.getParent().get("text");
        var correct_text = correct.getParent().get("text");
        
        var text = "";
        if(!is_correct){ text += "You said " + selected_text + ". "; }
        text += "The correct answer is " + correct_text + ".";
        if(is_correct){ text += " You got it right!"; }
        
        p2.set("html", text);
        container.grab(p2);
        
        answers.grab(container);
    },
    
    open:function()
    {
        if(this.shown){ return; }
        
        var docbod = $(document.body);
        var parent = this.element.getParent();
        
        if(parent != docbod)
        {
            docbod.grab(this.element, "top");
        }
        
        //var viewport = Window.getViewport();
        /*
        var viewport = this.element.getParent().getCoordinates();
        var coords = this.element.getCoordinates();

        this.element.setStyles({
            top:viewport.height / 2 - coords.height / 2,
            left:viewport.width / 2 - coords.width / 2
        });
        */
        
        var open_button_coords = $("quiz_open_button").getCoordinates();
        this.element.setStyles({
            top:open_button_coords.top - 200,
            left:open_button_coords.left + 200
        });
        
        
        this.fx.set("opacity", 0);
        this.element.removeClass("hidden");
        this.fx.start("opacity", 0, 1);
    },
    
    open_complete:function()
    {
        this.shown = true;
    },
    
    close:function()
    {
        if(!this.shown){ return; }
        
        this.fx.start("opacity", 1, 0);
    },
    
    close_complete:function()
    {
        this.element.addClass("hidden");
        this.shown = false;
    }
});
