﻿///SCRIPT 1 //////

    var detectVideoSupport = function () {
        var detect = document.createElement('video') || false;
        this.html5 = detect && typeof detect.canPlayType !== "undefined";
        this.mp4 = this.html5 && (detect.canPlayType("video/mp4") === "maybe" || detect.canPlayType("video/mp4") === "probably");
        this.ogg = this.html5 && (detect.canPlayType("video/ogg") === "maybe" || detect.canPlayType("video/ogg") === "probably");
        return this;
    };

    var replaceVideoWithObject = function (video_id) {
        if (!video_id) { return false; }
        var video = document.getElementById(video_id);
        if (video) {
            var obj = video.getElementsByTagName("object")[0];
            if (obj) {
                var obj_copy = obj.cloneNode(true);
                video.parentNode.insertBefore(obj_copy, video);
                document.getElementById("vidDiv").removeChild(video);
            }
        }
    };

    var loadVideo = function () {
        var video = detectVideoSupport();
        //Both Opera and firefox support ogg but lack MP4 support
        if (video.ogg && !video.mp4) {
            replaceVideoWithObject("player");
        }
    };

    window.onload = function () {
        var video = detectVideoSupport();
        //Both Opera and firefox support ogg but lack MP4 support
        if (video.ogg && !video.mp4) {
            replaceVideoWithObject("player");
        }
    };

///////

    function setCookie(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
    }

    function getCookie(c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return null;
    }

    ////// with jquery.homepagejq.js

    function UpdateScore() {
        // var CurrentPosition = parseInt(getCookie('Quiz_Position')) + 1;

        // if (parseInt(getCookie('Quiz_Position')) > 0) {
        setCookie('Q' + parseInt(getCookie('Quiz_Position')), getCookie('LastAnswer'), 1);
        //}
    }
    function GetNextQuestion() {


        var CurrentPosition = parseInt(getCookie('Quiz_Position')) + 1;

        //
        if (CurrentPosition == 7 || CurrentPosition == 8) {
            $("#quiz_question").css("font-size", "22pt");
        }

        if (CurrentPosition > 1) {
            $(".quiz-back").show();
        }

        setCookie('Quiz_Position', CurrentPosition, 1);

        if (CurrentPosition == 12) {


            var i = 1;
            var FinalResults = 0;
            for (i = 1; i <= 11; i++) {
                FinalResults = parseInt(getCookie('Q' + i)) + FinalResults;

            }

            setCookie('TEST_Results', FinalResults, 1);

            if (FinalResults < 14) {
                $("#quiz-results").addClass('spring');
                var Result = 'spring';
            }
            else if (FinalResults < 20) {
                $("#quiz-results").addClass('summer');
                var Result = 'summer';
            }
            else if (FinalResults < 27) {
                $("#quiz-results").addClass('fall');
                var Result = 'fall';
            }
            else if (FinalResults > 26) {
                $("#quiz-results").addClass('winter');
                var Result = 'winter';
            }

            $.ajax({
                url: "RecordResults.aspx?result=" + Result,
                type: "POST"

            });
            //$("#quiz-results").show();							
            $("#quiz-seasons").show();
            $("#quiz").hide();
        }
        else {
            ProcessQuiz()

        }

        var question_currentposition = 'Q' + CurrentPosition;
        $("a[name=" + getCookie(question_currentposition) + "]").css('background-position', '0px -114px');

        if (getCookie(question_currentposition)) {
            $("#quiz-next").show();
        }

    }

    function GetPreviousQuestion() {

        var CurrentPosition = parseInt(getCookie('Quiz_Position')) - 1;

        if (CurrentPosition < 2) {
            $(".quiz-back").hide();
        }
        setCookie('Quiz_Position', CurrentPosition, 1);

        ProcessQuiz()
        var question_currentposition = 'Q' + CurrentPosition;

        $("a[name=" + getCookie(question_currentposition) + "]").css('background-position', '0px -114px');

        $("#quiz-next").show();

    }

    function ResetQuiz() {
        setCookie('Quiz_Position', 0, 1);

        $("#progress-fg").css("width", '9%');
        $(".quiz-back").hide();

        ClearQuizCookies()
        GetNextQuestion()

    }

    function ClearQuizCookies() {

        var i = 1;
        for (i = 1; i <= 11; i++) {
            setCookie('Q' + i, '', -1);
        }
    }

    function ProcessQuiz() {

        var CurrentPosition = parseInt(getCookie('Quiz_Position'));
        var question_currentposition = 'Q' + CurrentPosition;

        switch (CurrentPosition) {
            case 1:
                var question = "1. How do you take your coffee or tea?";
                var QuizAnswers = [
        { name: "I don't drink coffee or tea", id: 0 },
        { name: "A lot of milk or cream", id: 1 },
        { name: "A little milk or cream", id: 2 },
         { name: "Black/Nothing added", id: 3 }
    ];
                break;
            case 2:
                var question = "2. How much sugar do you add to your coffee or tea?";
                var QuizAnswers = [
        { name: "I don't drink coffee or tea", id: 1 },
        { name: "Two or more teaspoons", id: 2 },
        { name: "A teaspoon", id: 3 },
         { name: "None", id: 4 }
    ];
                break;
            case 3:
                var question = "3. What type of chocolate do you prefer?";
                var QuizAnswers = [
        { name: "I don't eat or like chocolate", id: 0 },
        { name: "White Chocolate", id: 1 },
        { name: "Milk Chocolate", id: 2 },
         { name: "Dark Chocolate", id: 3 }
    ];

                break;
            case 4:
                var question = "4. How often do you put lemon on your fish?";
                var QuizAnswers = [
        { name: "I don't eat fish", id: 0 },
        { name: "Always", id: 1 },
        { name: "Sometimes", id: 2 },
         { name: "Never", id: 3 }
    ];
                break;
            case 5:
                var question = "5. Which is your favorite juice?";
                var QuizAnswers = [
        { name: "None of these/I don't drink juice.", id: 0 },
        { name: "Lemonade", id: 1 },
        { name: "Orange", id: 2 },
         { name: "Apple", id: 3 }
    ];
                break;
            case 6:
                var question = "6. How spicy do you like your food?";
                var QuizAnswers = [
        { name: "Mild/None", id: 1 },
        { name: "Medium", id: 2 },
        { name: "Extremely Hot", id: 3 }
    ];
                break;
            case 7:
                var question = "7. If you compare the body of a white wine to the body of skim milk, whole milk, or heavy cream, which would you prefer for the body of your white wines?";
                var QuizAnswers = [
        { name: "Skim Milk", id: 1 },
        { name: "Whole Milk", id: 2 },
        { name: "Heavy Cream", id: 3 }
    ];
                break;
            case 8:
                var question = "8. If you compare the body of a red wine to the body of skim milk, whole milk, or heavy cream, which would you prefer for the body of your red wines?";
                var QuizAnswers = [
        { name: "Skim Milk", id: 1 },
        { name: "Whole Milk", id: 2 },
        { name: "Heavy Cream", id: 3 }
    ];
                break;
            case 9:
                var question = "9. What type perfume or cologne do you like?";
                var QuizAnswers = [
        { name: "I don't like perfume or cologne", id: 0 },
        { name: "Floral/Fresh", id: 1 },
        { name: "Sweet/Candied", id: 2 },
         { name: "Spicy/Intense", id: 3 }
    ];
                break;
            case 10:
                var question = "10. Which type of a wine sounds most appealing to you?";
                var QuizAnswers = [
        { name: "None of these", id: 0 },
        { name: "A Refreshing and refined wine", id: 1 },
        { name: "A jammy and fruity wine", id: 2 },
         { name: "A Bold and intense wine", id: 3 }
    ];
                break;
            case 11:
                var question = "11. What is your preferred snack?";
                var QuizAnswers = [
        { name: "None of these", id: 0 },
        { name: "Something light like a piece of fruit or carrot sticks", id: 1 },
        { name: "Something savory like chips or crackers", id: 2 },
         { name: "Something rich like chocolate or a candy bar", id: 3 }
    ];
                break;
            default:
                var question = "1. How do you take your coffee or tea?";
                var QuizAnswers = [
        { name: "I don't drink coffee or tea", id: 0 },
        { name: "A lot of milk or cream", id: 1 },
        { name: "A little milk or cream", id: 2 },
         { name: "Black/Nothing added", id: 3 }
    ];
        }

        $("#quiz_question").empty();
        $("#quiz-options").empty();
        $("#clientTemplate").tmpl(QuizAnswers).appendTo("#quiz-options");

        $("#quiz_question").append(question);

        if (CurrentPosition > 1) {
            var Progress_width = (parseInt(getCookie('Quiz_Position')) / 11) * 100;
            $("#progress-fg").css("width", Progress_width + '%');

        }
        else {
            $("#progress-fg").css("width", '0%');
        }


        $("#quiz-next").hide();

    }

    //////////// end ///////////////////
