﻿
//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;
var popupStatus = 0;


//END GENERAL POPUP FUNCTIONS..............................................................
function loadPopup(name) {
    if (popupStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("fast");
        $(name).fadeIn("fast");
        popupStatus = 1;
    }
}

function disablePopup(name) {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#backgroundPopup").fadeOut("fast");
        $(name).fadeOut("fast");
        popupStatus = 0;
    }
}

function centerPopup(name) {
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $(name).height();
    var popupWidth = $(name).width();
    //centering
    $(name).css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6
    $("#backgroundPopup").css({//backgroundPopup
        "height": windowHeight
    });
}

function scrollWinDown(name) {
    $('html, body').animate({ scrollTop: $(name).offset().top }, 100);
}

function scrollWinUp(location) {
    $('html, body').animate({
        scrollTop: $(location).offset().top
    }, 100);
}

//END GENERAL POPUP FUNCTIONS..............................................................





$(document).ready(function() {

    $('a.group').fancybox({
        'hideOnContentClick': false,
        'frameWidth': 500,
        'frameHeight': 500
    });


    //ORDER WIDGET.........................................................................
    $('div.CategoriesWidget> div').hide();
    $('div.ProductsWidget> div').hide();

    //THIS IS TO EXPAND AND COLAPSE CATEGORIES
    $('div.CategoriesWidget> h5.Category').click(function() {
        var $nextDiv = $(this).next();
        var $visibleSiblings = $nextDiv.siblings('div:visible');
        if ($visibleSiblings.length) {
            $visibleSiblings.slideUp('fast', function() { $nextDiv.slideToggle('fast'); });
        }
        else {
            $nextDiv.slideToggle('fast');
        }
    });

    //THIS IS TO EXPAND AND COLAPSE PLANS
    $('div.ProductsWidget> h6.Product').click(function() {
        var $nextDiv = $(this).next();
        var $visibleSiblings = $nextDiv.siblings('div:visible');
        if ($visibleSiblings.length) {
            $visibleSiblings.slideUp('fast', function() { $nextDiv.slideToggle('fast'); });
        } else {
            $nextDiv.slideToggle('fast');
            $("a").toggleClass("Product-title");
        }
    });
    //END ORDER WIDGET.......................................................................






    //CALCULATOR STUFF.........................................................................
    //when key is pressed in the textbox
    $("#calc_age").keypress(function(e) {
        //if the letter is not digit then don't type anything
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            return false;
        }
    });

    $("#calc_heightfeet").keypress(function(e) {
        //if the letter is not digit then don't type anything
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            return false;
        }
    });

    $("#calc_heightinch").keypress(function(e) {
        //if the letter is not digit then don't type anything
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            return false;
        }
    });

    $("#calc_pounds").keypress(function(e) {
        //if the letter is not digit then don't type anything
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            return false;
        }
    });

    $("#btnCalc").click(function() {
        // used http://www.freedieting.com/tools/calorie_calculator.htm# for ref
        $('.error').hide();
        var _bmr
        var _sex = "";
        if ($("input#calc_sexM").is(":checked")) {
            _sex = "M";
        }
        if ($("input#calc_sexF").is(":checked")) {
            _sex = "F";
        }
        var _age = $("input#calc_age").val();
        var _heightfeet = $("input#calc_heightfeet").val();
        var _heightinch = $("input#calc_heightinch").val();
        var _pounds = $("input#calc_pounds").val();
        if (_sex == "") {
            $("label#gender_error").show();
        }
        if (_age == "") {
            $("label#age_error").show();
        }
        if (_heightfeet == "") {
            $("label#heightfeet_error").show();
        }
        if (_pounds == "") {
            $("label#pounds_error").show();
        }
        if (_sex == "" || _age == "" || _heightfeet == "" || _pounds == "") {
            return;
        }
        var _kgs
        _kgs = _pounds * 0.45359; //B * C * C / 4535.92;

        var _cms
        _cms = (_heightfeet * 30.48) + (_heightinch * 2.54);
        //calc
        if (_sex == "F") {
            //     10 x (kg) + 6.25 x (cm) - 5 x age  - 161    Female
            _bmr = 10 * _kgs + 6.25 * _cms - 5 * _age - 161;
        } else {
            //     10 x (kg) + 6.25 x (cm) - 5 x age  + 5      Male
            _bmr = 10 * _kgs + 6.25 * _cms - 5 * _age + 5;
        }
        var _activityMultiplier = $("select#calc_activity").val();
        var _maintain = _bmr
        var _suffix = " Calories/day";
        var _fatLoss = "-";
        if (_activityMultiplier > 1) {
            _maintain = _bmr * _activityMultiplier;
            _fatLoss = _maintain - (_maintain * 0.20);
            $('#resultFatLoss').html(_fatLoss.toFixed(0) + _suffix)
        } else {
            $('#resultFatLoss').html(_fatLoss)
        }
        $('#resultMaintain').html(_maintain.toFixed(0) + _suffix)

    });
    //END CALCULATOR STUFF.........................................................................





    //DISPLAY WIN 1 WEEK WIDGET......................................................................
    $("#Win").click(function() {
        centerPopup("#popupWin");
        loadPopup("#popupWin");
        scrollWinUp("body");
    });
    //DISPLAY WIN 1 WEEK WIDGET......................................................................





    //DISPLAY ORDER WIDGET......................................................................
    $("#order-today-arrow").click(function() {
        centerPopup("#popupOrder");
        loadPopup("#popupOrder");
        scrollWinUp("body");
    });

    $("#order-today-Menu").click(function() {
        centerPopup("#popupOrder");
        loadPopup("#popupOrder");
        scrollWinUp("body");
    });
    //END DISPLAY ORDER WIDGET......................................................................





    //SHOPPING CART CONT SHOPPING DISPLAY ORDER WIDGET......................................................................
    $("#contShop").click(function() {
        centerPopup("#popupOrder");
        loadPopup("#popupOrder");
        scrollWinUp("body");
    });
    //END SHOPPING CART CONT SHOPPING DISPLAY ORDER WIDGET......................................................................






    //DISPLAY ABOUT CALC......................................................................
    $("#button").click(function() {
        centerPopup("#popupCalc");
        loadPopup("#popupCalc");
        scrollWinUp("body");
    });
    //END DISPLAY ABOUT CALC......................................................................




    //CLOSING POPUPs...........................................................
    //Close About Calc
    $("#popupCalcClose").click(function() {
        disablePopup("#popupCalc");
        scrollWinDown("#button");
    });

    //Close Order Widget
    $("#popupOrderClose").click(function() {
        disablePopup("#popupOrder");
    });

    //Close WIN 1 WEEK
    $("#popupWinClose").click(function() {

        //this method worked but you were able to see changes while it was fading
        //disablePopup("#popupWin");
        //$('#thanks').hide();s
        //$('#WinArea').show();

        //this method waits for the fade to finish before it continues on
        if (popupStatus == 1) {
            $("#backgroundPopup").fadeOut("fast");
            $("#popupWin").fadeOut("fast", function() {
                //clear out after submit
                $('#fname').attr('value', '');
                $('#lname').attr('value', '');
                $('#street').attr('value', '');
                $('#apt').attr('value', '');
                $('#city').attr('value', '');
                $('#state').attr('value', '');
                $('#zip').attr('value', '');
                $('#telephone').attr('value', '');
                $('#email').attr('value', '');
                //reset divs
                $('#thanks').hide();
                $('#WinArea').show();
            });
            popupStatus = 0;
        }


    });
    //END CLOSING POPUPs...........................................................


});





//WIN 1 WEEK ......................................................................
$(function() {

    $('.errorSimple').hide();

    $('input.text-input').css({ backgroundColor: "#FFFFFF" });

    $('input.text-input').focus(function() {
        $(this).css({ backgroundColor: "#FFDDAA" });
    });

    $('input.text-input').blur(function() {
        $(this).css({ backgroundColor: "#FFFFFF" });
    });


    //Click Enter to win button on pop up window
    $("#btnEnterToWin").click(function() {

        // validate and process form
        // first hide any error messages
        $('.errorSimple').hide();

        //start from last text box to first because of set focus
        var email = $("input#email").val();
        if (email == "") {
            $("label#email_error").show();
            $("input#email").focus();
        }

        var zip = $("input#zip").val();
        if (zip == "") {
            $("label#zip_error").show();
            $("input#zip").focus();
        }

        var state = $('#state :selected').text();
        if (state == "") {
            $("label#state_error").show();
            $("input#state").focus();
        }

        var city = $("input#city").val();
        if (city == "") {
            $("label#city_error").show();
            $("input#city").focus();
        }

        var street = $("input#street").val();
        if (street == "") {
            $("label#street_error").show();
            $("input#street").focus();
        }

        var lname = $("input#lname").val();
        if (lname == "") {
            $("label#lname_error").show();
            $("input#lname").focus();
        }

        var fname = $("input#fname").val();
        if (fname == "") {
            $("label#fname_error").show();
            $("input#fname").focus();
        }


        if (fname == "" || lname == "" || street == "" || city == "" | state == "" || zip == "" || email == "") {
            return false;
        }

        //hide entry fields
        $('#WinArea').hide();

        //Display Thank YOU
        $('#thanks').show();

        //congrats you've entered to win
        $('#thanks').html("<div id='message'></div>");
        $('#message').html("<h4 id='Look'>Entry Form Submitted!</h4>")
                .append("<p>Winner will be contacted via email.</p>")



    });

});
//END WIN 1 WEEK ......................................................................




