/**
 * Choice game: Card
 * Serge Dolgov (C) 2002
 * studio.quazzle.com
 */

CHECK = new QButtonRes(QButtonRes.SIGNAL, 27, 26, "images/check1.gif", "images/check2.gif", "images/check3.gif");
CLUE1 = new QButtonRes(QButtonRes.BUTTON, 42, 46, "images/clue11.gif", "images/clue12.gif", "images/clue10.gif", "Show The First Clue! (Minus 3 Points)");
CLUE2 = new QButtonRes(QButtonRes.BUTTON, 42, 46, "images/clue21.gif", "images/clue22.gif", "images/clue20.gif", "Show The Second Clue! (Minus 3 Points)");
NEXT  = new QButtonRes(QButtonRes.BUTTON, 48, 36, "images/next1.gif", "images/next2.gif", "images/next0.gif", "Answer Next Question!");
OK    = new QButtonRes(QButtonRes.BUTTON, 47, 36, "images/ok1.gif", "images/ok2.gif");
CARD  = new QBoxRes(398, 29, 30, "images/cardtop.gif", "images/cardbtm.gif", "images/cardbody.gif", null, QLayer.FAST);
BOX   = new QBoxRes(202, 14, 15, "images/boxtop.gif", "images/boxbtm.gif", "images/boxbody.gif");

function QChoiceCard_content() {
    var args = this.parent.arguments;
    with (document) {
        write('<table width="398" border="0" cellspacing="0" cellpadding="0"><tr valign="top"><td rowspan="2" width="266" align="left">');
        write('<table width="266" border="0" cellspacing="0" cellpadding="0">');
        write('<tr><td width="266" colspan="2"><div class="question">' + args[2] + '</div></td></tr>');
        this.checks = new Array();
        this.choices = new Array();
        var count = args.length - 7;
        for (var j=0; j<count; j++) {
            write ('<tr valign="top"><td width="36" align="right">');
            var btn = new QButton(this, "checks[" + j + "]", CHECK);
            btn.tag = j;
            btn.onClick = this.parent.handleAnswer;
            this.checks[j] = btn;
            write ('</td><td width="230" align="left">');
            var anc = new QAnchor(this, "choices[" + j + "]", args[j + 7]);
            anc.tag = j;
            anc.onClick = this.parent.handleAnswer;
            this.choices[j] = anc;
            write ('</td></tr>');
        }
        write('</table>');
        write('</td><td width="132">');
        write('<table width="132" height="153" border="0" cellspacing="0" cellpadding="0">');
        write('<tr><td colspan="5" width="132" height="107" background="images/cardscr.gif" align="left" valign="top">');
        write('<img src="' + args[3] + '" border="0" width="100" height="80" hspace="13"></td></tr>');
        write('<tr><td width="9" height="46"><img src="images/z.gif" border="0" width="1" height="1"></td>');
        write('<td width="42" height="46">')
        this.clue1 = new QButton(this, "clue1", CLUE1);
        this.clue1.tag = args[4];
        this.clue1.onClick = QChoiceCard_showClue;
        write('</td><td width="17" height="46"><img src="images/z.gif" border="0" width="1" height="1"></td>')
        write('<td width="42" height="46">')
        this.clue2 = new QButton(this, "clue2", CLUE2);
        this.clue2.tag = args[5];
        this.clue2.onClick = QChoiceCard_showClue;
        write('</td><td width="22" height="46"><img src="images/z.gif" border="0" width="1" height="1"></td></tr>')
        write('</table>');
        write('</td></tr><tr><td width="132" valign="bottom" align="right" nowrap>');
        this.next = new QButton(this, "next", NEXT);
        this.next.onClick = QChoiceCard_nextCard;
        write('<img src="images/z.gif" border="0" width="17" height="50"></td></tr></table>');
    }
}

function QChoiceCard_show(flag) {
    with (this.card) {
        if (flag && complete && !visible) { // reset the card
            this.card.complete = false;
            next.enable(false);
            clue1.enable(true);
            clue2.enable(true);
            for (var j=0; j<choices.length; j++) {
                choices[j].set(this.arguments[j + 7]);
                checks[j].enable(true);
                checks[j].set(0);
            }
        }
        show(flag);
    }
}

function QChoiceCard_showClue(stub, cluetext) {
    with (this.parent.parent) {
        if (clue.visible) clue.show(false);
        if (this.parent.parent.onClue && onClue()) {
            clue.set(cluetext);
            this.enable(false);
        }
        clue.show(true);
    }
}

function QChoiceCard_handleAnswer(stub, id) {
    with (this.parent.parent) {
        if (!card.complete) {
            if (clue.visible) clue.show(false);
            card.checks[answer].enable(false);
            card.checks[id].set(1);
            for (var j=0; j<card.choices.length; j++) {
                if ((j != id) && (j != answer)) card.choices[j].set(null);
            }
            card.complete = true;
            card.clue1.enable(false);
            card.clue2.enable(false);
            card.next.enable(true);
            card.next.tag = id; // pass user answer as a tag
        }
    }
}

function QChoiceCard_nextCard(stub, user_ans) {
    with (this.parent.parent) {
        card.tag = user_ans;
        show(false);
    }
}

function QChoiceCard_handleClose(show, user_ans) {
    if (!show) {
        with (this.parent) {
            if (this.parent.onAnswer) onAnswer(answer == user_ans);
        }
    }
}

function QChoiceCard(parent, name, question, pic, clue1, clue2, answer) {
    this.init(parent, name);
    this.answer = answer - 1;
    this.arguments = QChoiceCard.arguments;
    this.show = QChoiceCard_show;
    this.showClue = QChoiceCard_showClue;
    this.handleAnswer = QChoiceCard_handleAnswer;
    this.card = new QBox(this, "card", 120, 70, CARD, QChoiceCard_content);
    this.card.complete = false;
    this.card.next.enable(false);
    this.card.onShow = QChoiceCard_handleClose;
    this.clue = new QMessageBox(this, "clue", 370, 90, BOX, OK, "You don't have enough points to get a clue!");
}
QChoiceCard.prototype = new QControl();
