NUM_QUESTIONS = 3;
NUM_PRESCRIPTIONS = 5;
CURRENT_HELP = "rx_welcome";
var rx = 0;

function bind_actions()
{
    UNDER26=$("span[class=under26]");
    FAMILY=$("span[class=family]");
    EMPLOYER=$("span[class=employer]");

    // Questions
    var radioClasses = [ "under26", "family", "employer" ];
    for (c in radioClasses) {
        (function () {
        var n = c;
        var elements = $("span[class=" + radioClasses[n] + "]");
        elements.click(function(index) {
            elements.removeClass("checked");
            $(this).addClass("checked");
            update_quiz(Number(n)+1);
        });
        })();
    }

    // Other buttons
    var email = $("#email");
    email.focusin(function(event) {
        // First time only: Clear the initial help text
        email.attr("value", "");
        email.css("color", "#000000");
        email.unbind(event);
    });
    email.keydown(function(event) {
        if (event.keyCode == 13) {
            //send_email();
        }
    });
    $(".start-over-quiz").click(function(event) {
        show_question(1);
        show_help("rx_welcome");
    });
    $("#send").click(function(event) {
        //send_email();
    });
    $(".btn-display").click(function(event) {
        show_result();
    });
    $("#d-cov-prev").click(function(event) {
        $('#d-cov-explained-2').hide(); 
        $('#d-cov-explained-1').show();
    });
    $("#d-cov-next").click(function(event) {
        $('#d-cov-explained-1').hide(); 
        $('#d-cov-explained-2').show();
    });
    $("#send-self-button-area").click(function(event) {
        isParent = $("#parents-quiz").val();
        sendSelfSimplePrescription(isParent);
        return false;
    });
    $("#back-to-quiz").click(function(event) {
        hide_result();
    });
    $(".go-to-quiz").click(function(event) {
        $("#quiz-body").show();
        $(".info-page").hide();
    });

    // Info pages
    $(".why-get-covered-link").click(function(event) {
        show_info("why-get-covered");
    });
    $(".dependent-cov-link").click(function(event) {
        show_info("dependent-cov");
    });
    $(".faq-link").click(function(event) {
        show_info("faq");
    });
    $(".know-your-rights-link").click(function(event) {
        show_info("know-your-rights");
    });
    $(".what-comes-next-link").click(function(event) {
        show_info("what-comes-next");
    });
    $("#reason-1-link").click(function(event) { show_reason(1); });
    $("#reason-2-link").click(function(event) { show_reason(2); });
    $("#reason-3-link").click(function(event) { show_reason(3); });
    $("#reason-4-link").click(function(event) { show_reason(4); });
    $("#reason-5-link").click(function(event) { show_reason(5); });
    show_reason(1);
    $("#faq-link-1").click(function(event) { show_faq("1"); });
    $("#faq-link-2").click(function(event) { show_faq("2"); });
    $("#faq-link-3").click(function(event) { show_faq("3a"); });
    $("#faq-link-4").click(function(event) { show_faq("4"); });
    $("#faq-link-5").click(function(event) { show_faq("5"); });
    $("#faq-3-next").click(function(event) { show_faq("3b"); });
    $("#faq-3-prev").click(function(event) { show_faq("3a"); });
    show_faq("1");
    $("#know-link-next").click(function(event) {
        $("#know-first-3").hide();
        $("#know-second-3").show();
    });
    $("#know-link-prev").click(function(event) {
        $("#know-first-3").show();
        $("#know-second-3").hide();
    });
    $("#next-right-now-link").click(function(event) {show_next("right-now");});
    $("#next-sep23-link").click(function(event) {show_next("sep23");});
    $("#next-2011-link").click(function(event) {show_next("2011");});
    $("#next-2014-1-link").click(function(event) {show_next("2014-1");});
    show_next("right-now");
    $("#next-link-next").click(function(event) {
        $("#next-2014-1").hide();
        $("#next-2014-2").show();
    });
    $("#next-link-prev").click(function(event) {
        $("#next-2014-1").show();
        $("#next-2014-2").hide();
    });

    var visible = true
    setInterval(function() {
        if (visible) {
            $("#red-arrow").css("opacity", 0.0);
            visible = false;
        } else {
            $("#red-arrow").css("opacity", 1.0);
            visible = true;
        }
    }, 800);
}

function sendSelfSimplePrescription(isParent)
{
    $('#send-self-button-area').hide();
    $('#send-self-spinner').show();

    $.ajax({
        type: "POST",
        url: "../email_self_prescription/",
        data: { 
            email:      $("#emailresults").val(), 
            accept:     $("#list-accept").attr("checked"), 
            page_url: window.location.href,
            is_parent:  isParent,
            rx:         rx 
        },
        success: function(data) {
            alert("Successfully emailed your prescription!");
        },
        error: function(msg) {
            alert("Sorry, there was a problem emailing your prescription.  "+
                "Please make sure all email addresses are formatted correctly.");
        },
        complete: function(data) {
            $('#send-self-spinner').hide();
            $('#send-self-button-area').show();
        }
    });
}

function sendOthersPrescription(isParent)
{
    $('#rx-send').hide();
    $('#send-pretty-spinner').show();

    $.ajax({
        type: "POST",
        url: "../email_others_prescription/",
        data: { 
            sender_name:  $("#rx-name").val(),
            sender_email: $("#rx-email").val(),
            accept:       $("#list-accept-share-prescription").attr("checked"), 
            msg:          $("#rx-msg").val(),
            email:        $("#rx-sendto").val(),
            page_url:     window.location.href,
            is_parent:    isParent,
            rx: rx 
        },
        success: function(data) {
            alert("Successfully emailed your prescription!");
        },
        error: function(msg) {
            alert("Sorry, there was a problem emailing your prescription.  "+
                "Please make sure all email addresses are formatted correctly.");
        },
        complete: function(data) {
            $('#send-pretty-spinner').hide();
            $('#rx-send').show();
        }
    });
}

function shareQuiz(isParent)
{
    $('#send').hide();
    $('#share-spinner').show();

    $.ajax({
        type: "POST",
        url: "../share_quiz/",
        data: { 
            sender_email: $("#your_email_address").val(),
            msg:          $("#message").val(),
            email:        $("#recipients").val(),
            accept:       $("#list-accept-share-quiz").attr("checked"), 
            page_url:     window.location.href,
            is_parent:    isParent
        },
        success: function(data) {
            alert("Successfully shared the quiz!");
        },
        error: function(msg) {
            alert("Sorry, there was a problem sharing the quiz.  "+
                "Please make sure all email addresses are formatted correctly.");
        },
        complete: function(data) {
            $('#share-spinner').hide();
            $('#send').show();
        }
    });
}

function shareYAPledge(currentPage)
{
    $('#send').hide();
    $('#share-spinner').show();

    $.ajax({
        type: "POST",
        url: "/share_ya_pledge/",
        data: { 
            sender_email: $("#email").val(),
            sender_name:  $("#name").val(),
            msg:          $("#body").val(),
            page:         currentPage,
            recipients:   $("#recipients").val(),
        },
        success: function(data) {
            alert("Successfully shared!");
        },
        error: function(msg) {
            alert("Sorry, there was a problem sharing.  "+
                "Please make sure all email addresses are formatted correctly.");
        },
        complete: function(data) {
            $('#share-spinner').hide();
            $('#send').show();
        }
    });
}

function shareStory(share_url, story_id)
{
    $('#send').hide();
    $('#share-story-spinner').show();
    $.ajax({
        type: "POST",
        url: share_url,
        data: { 
            sender_email: $("#your_email_address").val(),
            msg:          $("#message").val(),
            email:        $("#recipients").val(),
            page_url:     window.location.href,
            story_id:     story_id,
            accept:       $("#list-accept-share-story").attr("checked") 
        },
        success: function(data) {
            alert("Successfully shared the story!");
        },
        error: function(msg) {
            alert("Sorry, there was a problem sharing the story.  "+
                "Please make sure all fields are formatted correctly.");
        },
        complete: function(data) {
            $('#share-story-spinner').hide();
            $('#send').show();
        }
    });
}

function openTwitterLink(text)
{
    window.open("http://twitter.com/home?status=" + escape(text));
}

function twitterShare(isParent)
{
    if (isParent) {
        if(rx == 2) {
            text = "My kid can get covered on my health insurance until age 26. Take the quiz at http://bit.ly/aevKoE and find out if yours can get covered too.";
        } else {
            text = "I just took a quiz at http://bit.ly/aevKoE. It gave the best options for my kids to get insurance. Try it out!";
        }
    } else {
        if(rx == 2) {
            text = "Just found out I can get covered on my parent's plan until age 26. Find out in 4 questions if you can get covered too http://bit.ly/b0xx90";
        } else {
            text = "I just took a quiz at http://bit.ly/b0xx90. It gave my best options for getting insurance. Check it out and get your prescription.";
        }
    }
    openTwitterLink(text);
}

function twitterStoryShare()
{
    text = "I just read this great story about health care reform: " + document.URL;
    openTwitterLink(text);
}

function twitterPledgeShare()
{
    openTwitterLink($("#twitter-pledge-text").val());
}

function openFacebookLink(url, title)
{
    window.open("http://www.facebook.com/sharer.php?u=" + escape(url) + "&t=" + escape(title));
}

function facebookShare(isParent)
{
    if (isParent) {
        url = "http://gettingcovered.org/parents";
        title = "Getting Covered - Parents";
    } else {
        url = "http://gettingcovered.org/young-adults";
        title = "Getting Covered - Young Adults";
    }
    openFacebookLink(url, title);    
}

function facebookStoryShare()
{
    openFacebookLink(document.URL, document.title);    
}

function facebookPledgeShare()
{
    openFacebookLink("http://gettingcovered.org/pledge", "Take the Pledge:  Defend Health Care Reform");
}

function facebookToolkitShare()
{
    openFacebookLink("http://gettingcovered.org/toolkit", "Graduating? Visit  http://www.YoungInvincibles.org/Toolkit to learn your health care options.");
}

// Show the named block on the right side of the quiz
function show_help(name)
{
    var names = [ "rx_welcome", "rx_ns2", "rx_ns3", "rx_ready" ];
    var toShow = null;

    if (name != CURRENT_HELP) {
        if (name == "rx_ready") {
            $("p[class=title]").hide();
        } else {
            $("p[class=title]").show();
        }
        for (n in names) {
            var element = $("#" + names[n]);
            element.hide();
            if (name == names[n]) {
                toShow = element;
            }
        }
        toShow.slideDown("fast");
        CURRENT_HELP = name;
    }
}

function show_question(n)
{
    // Clear any previous response to the question
    switch (n) {
        case 1:
            UNDER26.removeClass("checked");
            break;
        case 2:
            FAMILY.removeClass("checked");
            break;
        case 3:
            EMPLOYER.removeClass("checked");
            break;
    }
    // Hide upcoming questions
    for (i = n; i <= NUM_QUESTIONS; i++) {
        $("#question" + i).hide();
    }
    $("#question" + n).slideDown("slow");
}

function show_info(page)
{
    hide_result();
    $("ul.nav li").removeClass("current");
    $("li[class=li-" + page + "]").addClass("current");
    $(".info-page").hide();
    $("#quiz-body").hide();
    $("#" + page).show();
}

function show_reason(n)
{
    $("li[id^=li-reason-]").removeClass("current");
    $("div[id^=why-reason-]").hide();
    $("#li-reason-" + n).addClass("current");
    $("#why-reason-" + n).show();
}

function show_faq(n)
{
    $("li[id^=faq-li-]").removeClass("current-faq");
    $("div[id^=faq-answer-]").hide();
    $("#faq-li-" + n[0]).addClass("current-faq");
    $("#faq-answer-" + n).show();
}

function show_next(n)
{
    $("li[id^=li-next-]").removeClass("current-next");
    $("div[id^=next-]").hide();
    $("#li-next-" + n).addClass("current-next");
    $("#next-" + n).show();
}

function hide_result()
{
    $("#back-to-quiz").hide();
    $("#rx-young-adults").show();
    $("#quiz").animate({ left: 0 }, 1000, "swing", function () {
        $(".box-precription").hide();
    });
}

function set_rx()
{
    if ($("#under26_y").hasClass("checked")) {
        if ($("#family_y").hasClass("checked")) {
            if ($("#employer_y").hasClass("checked")) {
                rx = 1;
            } else {
                rx = 2;
            }
        } else {
            if ($("#employer_y").hasClass("checked")) {
                rx = 3;
            } else {
                rx = 0;
            }
        }
    } else {
        if ($("#employer_y").hasClass("checked")) {
            rx = 3;
        } else {
            rx = 4;
        }
    }
}

function show_result()
{
    // TODO: Hide everything rx_* w/ wildcard, then just show what you want.  no loop.
    for (i = 0; i < NUM_PRESCRIPTIONS; ++i) {
        if (i == rx) {
            $("#rx_" + i).show();
        } else {
            $("#rx_" + i).hide();
        }
    }
    $("#back-to-quiz").show();
    $("#rx-young-adults").hide();
    $(".box-precription").show();
    $("#quiz").animate({ left: -828 }, 1000);
}

// Renumber question 3 (sometimes it appears to be question 2)
function renumber(n)
{
    if (n == 2) {
        $(".third-question-a").show();
        $(".third-question").hide();
    } else {
        $(".third-question-a").hide();
        $(".third-question").show();
    }
}

function start_quiz()
{
    bind_actions();
    show_question(1);
}

// Update the state of the quiz after the user answers question #q
function update_quiz(q)
{
    switch (q) {
        case 1:
            $("#question2").hide();
            $("#question3").hide();
            if ($("#under26_y").hasClass("checked")) {
                renumber(3);
                show_question(2);
            } else {
                renumber(2);
                show_question(3);
            }
            show_help("rx_welcome");
            break;
        case 2:
            $("#question3").hide();
            if ($("#family_ns").hasClass("checked")) {
                show_help("rx_ns2");
            } else {
                show_help("rx_welcome");
            }
            show_question(3);
            break;
        case 3:
            if ($("#employer_ns").hasClass("checked")) {
                show_help("rx_ns3");
            } else {
                set_rx();
                show_help("rx_ready");
            }
            break;
    }
}

// starts at zero
function showStoryQuote(index)
{
    var quotes = $("#stories-quotes blockquote p");
    if (quotes.length) {
        $(quotes[index]).fadeIn('slow');
        
        var nextIndex = index + 1;
        nextIndex = (nextIndex >= quotes.length) ? 0 : nextIndex // go back to first quote

        setTimeout(function(){
            $(quotes[index]).fadeOut('slow', function() {
                showStoryQuote(nextIndex);
            }); 
        }, 10000 );
    }
}


