﻿/// <reference path="jquery-1.3.2-vsdoc2.js"/>

//base url
var baseURL = "/";
function SetBaseURL(URL) {
    baseURL = URL;
}

//rotating headers

function SetHeadersArray(elements) {
    var rotatingHeadersArray = [];
    $(elements).each(function(index) {
        rotatingHeadersArray.push("#" + this.id);
    });
    return rotatingHeadersArray;
}

//generic function to rotate stuff in an array
var calloutDelay = 4000;
var calloutFadeSpeed = 2000;
var calloutTimeout;
function RotateCallout(i, calloutArray) {
    var arrayLength = calloutArray.length;
    var maxIndex = arrayLength - 1;
    var previousIndex = i - 1;
    if (previousIndex < 0)
        previousIndex = maxIndex;
    var currentIndex = i;
    var nextIndex = currentIndex + 1;
    if (nextIndex > maxIndex)
        nextIndex = 0;
    $(calloutArray[previousIndex]).css("z-index", "0");
    $(calloutArray[currentIndex]).css("z-index", "100");
    $(calloutArray[currentIndex]).fadeIn(calloutFadeSpeed, function() {
        $(calloutArray[previousIndex]).hide();
        var timeoutFunction = function() { RotateCallout(nextIndex, calloutArray); }
        calloutTimeout = setTimeout(timeoutFunction, calloutDelay);
    });
}

//BMI Related functions--------------------------------------------------------

function cal_bmi(lbs, ins) {
    h2 = ins * ins;
    bmi = lbs / h2 * 703
    f_bmi = Math.floor(bmi);
    diff = bmi - f_bmi;
    diff = diff * 10;
    diff = Math.round(diff);
    if (diff == 10) {
        // Need to bump up the whole thing instead
        f_bmi += 1;
        diff = 0;
    }
    bmi = f_bmi + "." + diff;
    return bmi;
}

function CheckInt(w) {
    if (isNaN(parseInt(w))) {
        return false;
    } else if (w < 0) {
        return false;
    } else {
        return true;
    }
}

function ValidateBMIInput() {
    var returnValue = true;
    if (!CheckInt($("#bmiHeightFeet").val()))
        returnValue = false;
    if (!CheckInt($("#bmiHeightInches").val()))
        returnValue = false;
    if (!CheckInt($("#bmiWeight").val()))
        returnValue = false;
    return returnValue;
}

function CalculateBMI() {
    if (ValidateBMIInput()) {
        var heightFeet = parseInt($("#bmiHeightFeet").val());
        var heightInches = parseInt($("#bmiHeightInches").val());
        var weight = parseInt($("#bmiWeight").val());
        var inches = (heightFeet * 12) + heightInches;
        var BMI = cal_bmi(weight, inches);
        $("#BMIFieldsHolder").slideUp("fast", function() {
            $("#BMIResult").html(BMI);
            $("#BMIResultHolder").slideDown("fast");
        });
    }
    else {
        alert("Please enter your height and weight.");
    }
}

function ResetBMI() {
    $("#BMIResultHolder").slideUp("fast", function() {
        $("#BMIResult").html("");
        $("#bmiHeightFeet").val("feet");
        $("#bmiHeightInches").val("inches");
        $("#bmiWeight").val("lbs");
        document.getElementById("bmiHeightFeet").style.color = "gray";
        document.getElementById("bmiHeightInches").style.color = "gray";
        document.getElementById("bmiWeight").style.color = "gray";
        $("#BMIFieldsHolder").slideDown("fast");
    });
}

function ClearField(object, text) {
    if (document.getElementById(object.id).value == text) {
        document.getElementById(object.id).style.color = "black";
        document.getElementById(object.id).value = "";
    }
}

function RestoreField(object, text) {
    if (document.getElementById(object.id).value == "") {
        document.getElementById(object.id).style.color = "gray";
        document.getElementById(object.id).value = text;
    }
}

//movie popup related------------------------------------------------
function SetPositionToCenter(objectId, objectWidth, objectHeight) {
    var topPosition = ($(window).scrollTop() + ($(window).height() / 2) | 0) - objectHeight;
    var leftPosition = ($(window).scrollLeft() + ($(window).width() / 2) | 0) - objectWidth;
    document.getElementById(objectId).style.top = topPosition + "px";
    document.getElementById(objectId).style.left = leftPosition + "px";
}

function ShowMovie(movieName, movieTitle) {
    SetPositionToCenter("movieOuterHolder", 250, 250);
    $("#movieTitleHolder").html(movieTitle);
    $("#flashHolder").fadeOut("fast", function() {
        $("#movieOuterHolder").fadeIn("slow", function() {
            $("#movieHolder").show();
            flowplayer("movieHolder", baseURL + "/images/swf/flowplayer-3.1.0.swf", movieName);
        });    
    });
}

function CloseMovie() {
    $("#movieHolder").hide();
    $("#movieOuterHolder").fadeOut("slow", function() {
        $("#flashHolder").fadeIn("fast");
    });
}

//intro movie------------------------------------------------------

function ShowIntroMovie() {
    $("#videoPlaceHolder").fadeOut("fast", function() {
        RenderIntroMovie();
        $("#introMovie").fadeIn("slow");
    });
}

function RenderIntroMovie() {
    flowplayer("introMovie", baseURL + "/images/swf/flowplayer-3.1.0.swf", {
        clip: {
            url: baseURL + '/images/swf/intro.flv'
        },
        plugins: {
            controls: {
                scrubber: true,
                fullscreen: false,
                time: false
            }
        }
    });
}

//form related-------------------------------------------------------

function ShowFormThankyou() {
    //phone
    //disclaimer
    $("#fieldsHolder").slideUp("slow", function() {
        $("#formThankyou").slideDown("slow");
    });
    $("#phone").fadeOut("fast");
    $("#disclaimer").fadeOut("fast");

}

//marketingOther
function ToggleMarketingOther(value) {
    if (value.indexOf("Other") > -1) {
        $("#marketingOther").slideDown("fast");
    }
    else {
        $("#marketingOther").slideUp("fast");
    }
}

//flash banner and commercial popup----------------------------------

function ShowCommercial() {
    alert('would show commercial');
}

//advanced BMI routines

function ValidateADVBMIInput() {
    var returnValue = true;
    if (!CheckInt($("#advBMIWeight").val()))
        returnValue = false;
    if (!CheckInt($("#advBMIHeightFeet").val()))
        returnValue = false;
    if (!CheckInt($("#advBMIHeightInches").val()))
        returnValue = false;
    if ($("#advBMISex").val() == "")
        returnValue = false;
    return returnValue;
}

function CalculateADVBMIValues() {
    var weight = $("#advBMIWeight").val();
    var heightFeet = $("#advBMIHeightFeet").val();
    var heightInches = $("#advBMIHeightInches").val();
    var totalHeightInches = heightInches - -(heightFeet * 12);
    var sex = $("#advBMISex").val();
    var isMale = true;
    if (sex == "Female")
        isMale = false;
    
    $("#enteredLBS").text(weight);
    $("#enteredFeet").text(heightFeet);
    $("#enteredInches").text(heightInches);
    $("#enteredSex").text(sex);

    var BMIValue = CalculateADVBMI(weight, totalHeightInches);
    var idealWeight = CalculateIdealWeight(totalHeightInches, isMale);
    var proteinUSRDA = CalculateProtein(idealWeight, true);
    var proteinBariatric = CalculateProtein(idealWeight, false);
    var bandWeightLoss = CalculateBand(weight, idealWeight);
    var bypassWeightLoss = CalculateBypass(weight, idealWeight);
    var sleeveWeightLoss = CalculateSleeve(weight, idealWeight);

    $("#sleeveWeightLoss").text(sleeveWeightLoss);
    $("#sleeveFinalWeight").text(weight - sleeveWeightLoss);

    $("#bypassWeightLoss").text(bypassWeightLoss);
    $("#bypassFinalWeight").text(weight - bypassWeightLoss);
    $("#bandWeightLoss").text(bandWeightLoss);
    $("#bandFinalWeight").text(weight - bandWeightLoss);
    $("#proteinUSRDA").text(proteinUSRDA);
    $("#proteinBariatric").text(proteinBariatric);
    $("#idealBodyWeight").text(idealWeight);
    $("#advBMICurrent").text(BMIValue);
}

function CalculateADVBMI(lbs, totalInches) {
    var value = ((lbs / totalInches) / totalInches) * 703;
    return (Round(value));
}

function CalculateIdealWeight(totalHeight, isMale) {
    if (isMale) {
        return Round(106 + (6 * (totalHeight - 60)));
    }
    else {
        return Round(100 + (5 * (totalHeight - 60)));
    }
}

function CalculateProtein(idealWeight, isUSRDA) {
    if (isUSRDA) {
        return Round((idealWeight / 2.2) * 0.8);
    }
    else {
        return Round((idealWeight / 2.2));
    }
}

function CalculateBand(currentWeight, idealWeight) {
    return Round((currentWeight-idealWeight)*0.4);
}

function CalculateBypass(currentWeight, idealWeight) {
    return Round((currentWeight - idealWeight) * 0.8);
}

function CalculateSleeve(currentWeight, idealWeight) {
    return Round((currentWeight - idealWeight) * 0.63);
}

function Round(original) {
    var result = Math.round(original * 100) / 100;
    return result;
}

var speed = "slow";
function ShowADVBMIResults() {
    if (ValidateADVBMIInput()) {
        CalculateADVBMIValues();
        $("#startHolder").hide();
        $.scrollTo("#advBMITop", speed, function() {
            $("#enteredHolder").slideDown(speed, function() {
                $("#proteinHeader").slideDown(speed);
                $("#proteinContent").slideDown(speed, function() {
                    $("#weightlossHeader").slideDown(speed);
                    $("#bandResults").slideDown(speed);
                    $("#bypassResults").slideDown(speed);
                    $("#sleeveResults").slideDown(speed);
                    $("#advBMIDisclaimer").slideDown(speed);
                });
            });
        });
    }
    else {
        alert("Please enter whole numbers for weight and height and indicate whether you are male or female to begin the calculation.");
    }
}

function ResetADVBMIResults() {
    ResetADVBMIFields();
    $("#enteredHolder").hide();
    $("#startHolder").fadeIn(speed);
    $("#advBMIDisclaimer").fadeOut(speed);
    $("#sleeveResults").fadeOut(speed);
    $("#bypassResults").fadeOut(speed);
    $("#bandResults").fadeOut(speed);
    $("#weightlossHeader").fadeOut(speed);
    $("#proteinContent").fadeOut(speed);
    $("#proteinHeader").fadeOut(speed);
}

function ResetADVBMIFields() {
    $("#enteredLBS").text("");
    $("#enteredFeet").text("");
    $("#enteredInches").text("");
    $("#enteredSex").text("");
    $("#advBMICurrent").text("");
    $("#idealBodyWeight").text("");
    $("#proteinUSRDA").text("");
    $("#proteinBariatric").text("");
    $("#bandWeightLoss").text("");
    $("#bandFinalWeight").text("");
    $("#bypassWeightLoss").text("");
    $("#bypassFinalWeight").text("");
    $("#sleeveWeightLoss").text("");
    $("#sleeveFinalWeight").text("");    

    $("#advBMIWeight").attr("value", "");
    $("#advBMIHeightFeet").attr("value", "");
    $("#advBMIHeightInches").attr("value", "");
    $("#advBMISex").attr("value", "");
}


//News page routines
function ShowNewsStory(newsHolder, newsLink) {
    $("#" + newsLink).fadeOut("fast", function() {
        $("#" + newsHolder).slideDown("slow");
    });
}

function HideNewsStory(newsHolder, newsLink, newsItem) {
    $("#" + newsHolder).slideUp("slow", function() {
        $("#" + newsLink).fadeIn("fast");
    });
}

/*patient stories*/

function ShowPatientDetail(object, firstName, weightLost, surgeryName, surgeryDate, afterPic) {
    var p = $(object);
    var position = p.position();
    if (position.left >= 507) {
        ShowPatientDetailRight(object, firstName, weightLost, surgeryName, surgeryDate, afterPic);
    }
    else {
        ShowPatientDetailLeft(object, firstName, weightLost, surgeryName, surgeryDate, afterPic);
    }
}

function ShowPatientDetailLeft(object, firstName, weightLost, surgeryName, surgeryDate, afterPic) {
    var p = $(object);
    var position = p.position();
    var left = position.left + 120;
    var top = position.top - 45;
    var speed = "fast";
    if ($.browser.msie) {
        speed = 0;
    }
    $("#patientDetailsRight").fadeOut(speed, function() {
        $("#patientDetailsLeft").fadeOut(speed, function() {
            $("#patientDetailsLeft").css("top", top);
            $("#patientDetailsLeft").css("left", left);
            $("#patientImageLeft").attr("src", "../images/pages/PatientStories/BeforeAfter/" + afterPic);
            $("#patientNameLeft").html(firstName);
            $("#patientWeightLeft").html(weightLost);
            $("#patientSurgeryLeft").html(surgeryName);
            $("#patientDateLeft").html(surgeryDate);
            $("#patientDetailsLeft").fadeIn(speed);
        });
    });
}

function ShowPatientDetailRight(object, firstName, weightLost, surgeryName, surgeryDate, afterPic) {
    var p = $(object);
    var position = p.position();
    var left = position.left - 607;
    var top = position.top - 45;
    var speed = "fast";
    if ($.browser.msie) {
        speed = 0;
    }
    $("#patientDetailsLeft").fadeOut(speed, function() {
        $("#patientDetailsRight").fadeOut(speed, function() {
            $("#patientDetailsRight").css("top", top);
            $("#patientDetailsRight").css("left", left);
            $("#patientImageRight").attr("src", "../images/pages/PatientStories/BeforeAfter/" + afterPic);
            $("#patientNameRight").html(firstName);
            $("#patientWeightRight").html(weightLost);
            $("#patientSurgeryRight").html(surgeryName);
            $("#patientDateRight").html(surgeryDate);
            $("#patientDetailsRight").fadeIn(speed);
        });
    });
}

function HideRightPatientDetail() {
    var speed = "fast";
    if ($.browser.msie) {
        speed = 0;
    }
    $("#patientDetailsRight").fadeOut(speed);
}

function HideLeftPatientDetail() {
    var speed = "fast";
    if ($.browser.msie) {
        speed = 0;
    }
    $("#patientDetailsLeft").fadeOut(speed);
}

function HidePatientDetail() {
    HideLeftPatientDetail();
    HideRightPatientDetail();
}

//-------------------------------------------------------------------
$(document).ready(function() {
    $.localScroll();
});