﻿

/********************************************************
 NOTE : THIS REQUIRES IMAGE-PRELOADER TO BE LOADED FIRST
********************************************************/


var battle =
{
    //PROPERTIES
    quiz_id: "",
    seconds_left: 10,
    timer_id: "lblTimer",
    interval_id: "",
    submit_button_id: "btn-submit",
    next_button_id: "btn-next",
    question_container_id: "quiz_icon",
    correct: true,
    incorrect: false,
    path_prefix: "",
    score_id: "",


    //METHODS
    Start_Timer: function() {
        if (this.interval_id == "") {
            this.seconds_left = 10;
            this.interval_id = window.setInterval("battle.Timer_Interval();", 1000);
        }
    },


    Stop_Timer: function() {
        window.clearInterval(this.interval_id);
        this.interval_id = "";
    },


    Timer_Interval: function() {
        if (this.seconds_left > 0)
            $("#" + this.timer_id).text(this.seconds_left--);
        else {
            this.Stop_Timer();
            $("#" + this.timer_id).text("0");
            this.Submit_Answer();
        }
    },


    Next_Question: function() {
        var xx = this;

        img_teaser.Hide_Tease();

        $.get(
        this.path_prefix+"QA.ashx",
        "action=next_question",
        function(data, textStatus) {
            if (textStatus == "success") {
                var quiz_data = xx.Json_To_Object(data);
                if (quiz_data == null)
                    return;
                xx.Display_Question(quiz_data.question_package);
            }
            else {
                //TODO: HANDLE ERROR HERE
            }
        });
    },


    Submit_Answer: function() {
        this.Stop_Timer();
        this.Disable_Submit_Button();

        //get selected answer
        var xx = this;
        var selected = $('input[name=answers]:checked').val();
        var ans = -1

        if (selected)
            ans = selected;

        $.get(
        this.path_prefix+"QA.ashx",
        "action=submit_answer&ans=" + ans,
        function(data, textStatus) {
            if (textStatus == "success") {
                var quiz_data = xx.Json_To_Object(data);
                if (quiz_data == null)
                    return;
                xx.Display_Answer(quiz_data.question_package);
            }
            else {
                //TODO: HANDLE ERROR HERE
            }
        });
    },

    Mark_Answer: function(divid) {
        $("#" + divid).attr("checked", true);
    },

    Start_Battle: function() {
        var xx = this;
        $.get(
        this.path_prefix + "QA.ashx",
        "action=start_battle",
        function(data, textStatus) {

            if (textStatus == "success") {
                //did server send back a redirect instead
                if ((/^redirectto:/gi).test(data)) {
                    window.location = data.replace("redirectto:", '')
                    return;
                }

                var quiz_data = xx.Json_To_Object(data);
                if (quiz_data == null)
                    return;
                xx.Display_Question(quiz_data.question_package);
            }
            else {
                alert("error: " + data);
                //TODO: HANDLE ERROR HERE
            }
        });
    },

    Display_Question: function(question_package) {
        //reset classes
        $("#divAnswer0").attr("class", "potential-answer");
        $("#divAnswer1").attr("class", "potential-answer");
        $("#divAnswer2").attr("class", "potential-answer");
        $("#divAnswer3").attr("class", "potential-answer");

        $("#radAnswer0").attr("checked", false);
        $("#radAnswer1").attr("checked", false);
        $("#radAnswer2").attr("checked", false);
        $("#radAnswer3").attr("checked", false);


        //reset text
        $("#lblQuestion").text(this.Fix(question_package.question));
        $("#lblAnswer0").text(this.Fix(question_package.answer_0));
        $("#lblAnswer1").text(this.Fix(question_package.answer_1));
        $("#lblAnswer2").text(this.Fix(question_package.answer_2));
        $("#lblAnswer3").text(this.Fix(question_package.answer_3));
        $("#question-number").text(question_package.question_number);
        $("#" + this.question_container_id).attr("class", "answr_question");

        //show hide
        $("#question-container").css("display", "inline");
        $("#start-container").css("display", "none");
        this.Enable_Submit_Button();
        this.Disable_Next_Button();
        $("#btn-container-results").css("display", "none");

        //timer
        battle.Start_Timer();
    },

    Display_Answer: function(question_package) {
        //show answers
        $("#divAnswer" + question_package.marked_answer).attr("class", "marked-answer");
        $("#divAnswer" + question_package.correct_answer).attr("class", "correct-answer");
        var myscore = document.getElementById(this.score_id);
        myscore.replaceText('' + question_package.current_score + '');



        //show hide
        if (question_package.question_number == "11") {
            $("#btn-container-submit").css("display", "none");
            $("#btn-container-next").css("display", "none");
            $("#btn-container-results").css("display", "inline");
        }
        else {
            this.Disable_Submit_Button();
            this.Enable_Next_Button();
            $("#btn-container-results").css("display", "none");
        }

        if (question_package.marked_answer != question_package.correct_answer) {
            //wrong answer
            $("#" + battle.question_container_id).attr("class", "answr_wrong");
            img_teaser.Show_Tease(this.incorrect, question_package.is_product_question);
        }
        else {
            //correct answer
            $("#" + battle.question_container_id).attr("class", "answr_correct");
            img_teaser.Show_Tease(this.correct, question_package.is_product_question);
        }
    },

    Display_Results: function() {
        window.location = "results.aspx";
    },

    Disable_Submit_Button: function() {
        $("#" + this.submit_button_id).unbind('click');
        $("#" + this.submit_button_id).addClass("disabled");
    },

    Disable_Next_Button: function() {
        $("#" + this.next_button_id).unbind('click');
        $("#" + this.next_button_id).addClass("disabled");
    },

    Enable_Submit_Button: function() {
        $("#" + this.submit_button_id).unbind('click');
        $("#" + this.submit_button_id).click(function() { battle.Submit_Answer(); });
        $("#" + this.submit_button_id).removeClass("disabled");
    },

    Enable_Next_Button: function() {
        $("#" + this.next_button_id).unbind('click');
        $("#" + this.next_button_id).click(function() { battle.Next_Question(); });
        $("#" + this.next_button_id).removeClass("disabled");
    },

    Json_To_Object: function(JsonString) {
        var JsonObj = unescape(JsonString);
        if (JsonObj.length < 1)
            return null;
        JsonObj = eval('(' + JsonObj + ')');
        return JsonObj;
    },

    Fix: function(text) {
        text = text.replace(/\&#34;/gi, "\x22");
        text = text.replace(/\&#39;/gi, "\x27");
        text = text.replace(/\&#34;/gi, "\x22");
        return text;
    }
};












var img_teaser =
{
    male_correct_images:
    [
         "../../../common/img/battle/popups/men_correct_001.png"
        , "../../../common/img/battle/popups/men_correct_002.png"
        , "../../../common/img/battle/popups/men_correct_003.png"
        , "../../../common/img/battle/popups/men_correct_004.png"
        , "../../../common/img/battle/popups/men_correct_005.png"
    ],

    male_wrong_images:
    [
         "../../../common/img/battle/popups/men_wrong_001.png"
        , "../../../common/img/battle/popups/men_wrong_002.png"
        , "../../../common/img/battle/popups/men_wrong_003.png"
        , "../../../common/img/battle/popups/men_wrong_004.png"
        , "../../../common/img/battle/popups/men_wrong_005.png"
    ],

    female_correct_images:
    [
         "../../../common/img/battle/popups/women_correct_001.png"
        , "../../../common/img/battle/popups/women_correct_002.png"
        , "../../../common/img/battle/popups/women_correct_003.png"
        , "../../../common/img/battle/popups/women_correct_004.png"
        , "../../../common/img/battle/popups/women_correct_005.png"
    ],

    female_wrong_images:
    [
         "../../../common/img/battle/popups/women_wrong_001.png"
        , "../../../common/img/battle/popups/women_wrong_002.png"
        , "../../../common/img/battle/popups/women_wrong_003.png"
        , "../../../common/img/battle/popups/women_wrong_004.png"
        , "../../../common/img/battle/popups/women_wrong_005.png"
    ],

    male_product_images:
    [
         "../../../common/img/battle/popups/male_product_1.png"
        , "../../../common/img/battle/popups/male_product_2.png"
    ],

    female_product_images:
    [
         "../../../common/img/battle/popups/female_product_1.png"
        , "../../../common/img/battle/popups/female_product_2.png"
    ],

    status_id: "preloader_status",
    start_button_id: "btn-start-battle",
    teaser_image_id: "img-teaser",
    teaser_img: new Image(),
    teaser_img_dock_pos: 0,
    player_gender: "M",
    total_images: 0,
    all_images: [],
    preload_count: 0,


    Preload_Images: function() {
        //init preloader
        var loader = new image_preloader();
        loader.Init
        (
            function() { img_teaser.Image_Loaded(); }, //run after each image loads
            function() { img_teaser.Done_Loading(); } //run when done loading all images
        );

        if (this.player_gender == 'M') {
            this.all_images = this.male_correct_images.concat(this.female_wrong_images);
            this.all_images = this.all_images.concat(this.male_product_images);
            this.total_images = this.all_images.length;
            loader.Preload_Images(this.all_images);
        }
        else {

            this.all_images = this.female_correct_images.concat(this.male_wrong_images);
            this.all_images = this.all_images.concat(this.female_product_images);
            this.total_images = this.all_images.length;
            loader.Preload_Images(this.all_images);
        }
    },

    Load_Image: function(image_path) {
        var i = $("<IMG>");
        $(i).load(function() { img_teaser.Image_Loaded(); });
        $(i).attr("src", image_path);
    },

    Update_Status: function(newtext) {
        $("#" + this.status_id).text(newtext);
    },

    Done_Loading: function() {
        $("#" + this.start_button_id).unbind('click');
        $("#" + this.start_button_id).click(function() { battle.Start_Battle(); });
        $("#imgLoading").css("display", "none");
    },

    Image_Loaded: function() {
        this.preload_count++;
        this.Update_Status(this.preload_count + " of " + this.total_images);
        //        if (this.preload_count == this.total_images)
        //            this.Done_Loading();
    },

    Show_Tease: function(isCorrect, isProduct) {
        if (isProduct.toLowerCase() == "true") {
            //product
            if (this.player_gender == 'M') {
                var x = Math.floor(Math.random() * this.male_product_images.length);
                $(this.teaser_img).attr("src", this.male_product_images[x]);
            }
            else {
                var x = Math.floor(Math.random() * this.female_product_images.length);
                $(this.teaser_img).attr("src", this.female_product_images[x]);
            }
        }
        else if (isCorrect) {
            //correct
            if (this.player_gender == 'M') {
                var x = Math.floor(Math.random() * this.male_correct_images.length);
                $(this.teaser_img).attr("src", this.male_correct_images[x]);
            }
            else {
                var x = Math.floor(Math.random() * this.female_correct_images.length);
                $(this.teaser_img).attr("src", this.female_correct_images[x]);
            }
        }
        else {
            //incorrect
            if (this.player_gender == 'F') {
                var x = Math.floor(Math.random() * this.female_wrong_images.length);
                $(this.teaser_img).attr("src", this.female_wrong_images[x]);
            }
            else {
                var x = Math.floor(Math.random() * this.male_wrong_images.length);
                $(this.teaser_img).attr("src", this.male_wrong_images[x]);
            }
        }

        $(this.teaser_img).ifixpng();


        //animate
        $(this.teaser_img).attr("display", "block").animate({
            bottom: '0'
        }
            , 200
            , function() {
                // Animation complete.
            }
        );
        return false;
    },

    Hide_Tease: function() {
        $(this.teaser_img).animate({
            bottom: this.teaser_img_dock_pos
        }
            , 200
            , function() {
                // Animation complete.
            }
        );
        return false;
    },

    Init: function(gender) {
        this.player_gender = gender;
        this.Preload_Images();
        this.teaser_img = $("#" + this.teaser_image_id);
        this.teaser_img_dock_pos = document.getElementById(this.teaser_image_id).style.bottom;
        this.Hide_Tease();
    }
};

