﻿var enabled = false;
var firstGoalSelected = false;

function enableFlyOut() {
    if (enabled == false) {
        enabled = true;
        resetSelections();
        
        var containerOffset = $("#get-started-wrapper").offset();
        var enabledButtonOffset = -5;
        var top = 35;
        var right = $("body").outerWidth() - containerOffset.left + enabledButtonOffset;

        $("div#get-started-flyout").appendTo("form");
        $("div#get-started-flyout").css({ 'top': top + 'px', 'right': right + 'px', 'left': 'auto', 'z-index': '1000' });

        if ($.browser.msie && $.browser.version <= 6) 
        {
            $("div#get-started-flyout").addClass("ie6");
            right = right - 5;
            $("div#get-started-flyout").css({ 'top': top + 'px', 'right': right + 'px', 'left': 'auto', 'z-index': '1000' });       
        }
        
        $('div#get-started-flyout').css('display', 'block');
        $('#get-started-wrapper').removeClass('off').addClass('on');
        $('#get-started-button').attr('src', '/images/which-is-right/button_get_started_selected.png');
    }
    else {
        enabled = false;
        $('div#get-started-flyout').css('display', 'none');
        $('#get-started-wrapper').removeClass('on').addClass('off');
        $('#get-started-button').attr('src', '/images/which-is-right/button_get_started.png');
    }
    
    $("img").ifixpng();
    $("div").ifixpng();
}

function processButtonClick() {
    if (firstGoalSelected != true) {
        _gaq.push(['_trackEvent', "WhichEnsure", "Picked First Available Selection"]);
        firstGoalSelected = true;
        $('#health-goals').attr('disabled', 'true');
        var selectionValue = $('#health-goals').val();
        var selectionObject = $('#' + selectionValue);
        if (selectionValue == 'support-bone-health') {
            _gaq.push(['_trackEvent', "WhichEnsure", "Ensure Bone Health"]);
            $('.first-selection').hide();
            $('#flyout-button').hide();
            $('#bone-health-results').show();
            var footnote = $('.results-product-footnotes', '#bone-health-results').html();
            $('#flyout-footnotes').html(footnote);
        }
        else if (selectionValue == 'high-protein-results') {
            _gaq.push(['_trackEvent', "WhichEnsure", "Ensure High Protein"]);
            $('.first-selection').hide();
            $('#flyout-button').hide();
            $('#high-protein-results').show();
            var footnote = $('.results-product-footnotes', '#high-protein-results').html();
            $('#flyout-footnotes').html(footnote);
        }
        else {
            selectionObject.show();
            $('select.second-question', selectionObject).addClass('current');
        }
        //if (selectionValue == 'support-bone-health') window.location = "/products/ensure-bone-health-shakes";

    }
    else {
        firstGoalSelected = false;
        var selection = $('select.current').val();
        var node = $('#' + selection);
        $(node).show();
        $('.first-selection').hide();
        $('.second-selection').hide();
        $('#flyout-button').hide();
        var footnote = $('.results-product-footnotes', node).html();
        $('#flyout-footnotes').html(footnote);
        if (selection == 'ensure-results') {
            _gaq.push(['_trackEvent', "WhichEnsure", "Ensure Nutrition Shake"]);
        }
        else if (selection == 'muscle-health-results') {
            _gaq.push(['_trackEvent', "WhichEnsure", "Ensure Muscle Health"]);
        }
        else if (selection == 'immune-health-results') {
            _gaq.push(['_trackEvent', "WhichEnsure", "Ensure Immune Health"]);
        }
        else if (selection == 'plus-results') {
            _gaq.push(['_trackEvent', "WhichEnsure", "Ensure Plus"]);
        }
        else if (selection == 'bone-health-results') {
            _gaq.push(['_trackEvent', "WhichEnsure", "Ensure Bone Health"]);
        }
        else if (selection == 'clinical-strength-results') {
            _gaq.push(['_trackEvent', "WhichEnsure", "Ensure Clinical Strength"]);
        }
        else if (selection == 'high-protein-results') {
            _gaq.push(['_trackEvent', "WhichEnsure", "Ensure High Protein"]);
        }
    }
    $("img").ifixpng();
    $("div").ifixpng();
}

function resetSelections() {
    $('#health-goals').attr('disabled', '');
    $('#health-goals').each(function() {
        this.selectedIndex = 0;
    });
    $('.second-question').each(function() {
        this.selectedIndex = 0;
    });
    $('#flyout-footnotes').html('');
    $('.first-selection').show();
    $('.second-selection').hide();
    $('#flyout-button').show();
    $('#health-goals').attr('disabled', '');
    $('.results').hide();
    $('select.second-question').removeClass('current');
    firstGoalSelected = false;
}

$(document).ready(function() {
    $('#get-started-button').click(function() { enableFlyOut(); _gaq.push(['_trackEvent', "WhichEnsure", "Open Quick Select Tool", document.title]); });
    $('#flyout-close').click(function() { enableFlyOut(); });
    $('div#flyout-button img').click(function() { processButtonClick(); });
    $('#flyout-content-reset').click(function() { resetSelections(); });
});



