﻿var menuclosetimer = [];


(function ($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function () {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)


function setPageContainerPosition() {
    var top = (($(window).height() / 2) - (($("#PageContainer").height() + 20) / 2));
    if (top < 0) top = 0;
    $("#PageContainer").css("top", top + 'px');
}

function validateEmail(email) {
    var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (!filter.test(email)) {
        return false;
    }
    return true;
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

var cform_message = "Message:";

$(document).ready(function () {
    if (window.location.pathname.indexOf("BeInvolved") > 0)
        $("#Menu-BeInvolved").addClass("ActiveMenu")
    else if (window.location.pathname.indexOf("BeInformed") > 0)
        $("#Menu-BeInformed").addClass("ActiveMenu")
    else if (window.location.pathname.indexOf("Give") > 0)
        $("#Menu-Give").addClass("ActiveMenu")

    setPageContainerPosition();
    $(window).resize(setPageContainerPosition);

    $(".ShowContactForm").click(function () {
        cform_message = "Message:";
        $("#ContactForm label[for=message]").text("Message:");
        $("#ContactForm label[for=name]").text("Name:");
        $("#ContactForm label[for=email]").text("Email:");
        $("#ContactForm label[for=subject]").text("Subject:");
        $("#ContactForm label[for=message]").text(cform_message);
        $("#ContactForm #FormError").text("");
        $("#ContactForm #askQuestionImage").hide();
        $("#ContactForm #contactUsImage").show();


        $("#ContactForm #subject").parent().parent().show();
        $("#ContactForm #ContactFormTable").show();
        $("#ContactForm #ContactForm_Send").show();
        $("#ContactForm #ContactForm_Cancel").text('CANCEL');
        $("#ContactForm #name").val('');
        $("#ContactForm #email").val('');
        $("#ContactForm #subject").val('');
        $("#ContactForm #message").val('');
        $("#ContactForm").css({ height: 15, opacity: 0 }).show().animate({ height: 340, opacity: 1 }, 750);
    });

    $("#ContactForm_Cancel").click(function (e) {
        e.preventDefault();
        $("#ContactForm").animate({ height: 15, opacity: 0 }, 750, function () { $(this).hide(); });
    });

    $("#ContactForm_Send").click(function (e) {
        e.preventDefault();

        var name = $("#ContactForm #name").val();
        var email = $("#ContactForm #email").val();
        var subject = $("#ContactForm #subject").val();
        var message = $("#ContactForm #message").val();
        var cont = true;

        $("#ContactForm label[for=name]").text("Name:");
        $("#ContactForm label[for=email]").text("Email:");
        $("#ContactForm label[for=subject]").text("Subject:");
        $("#ContactForm label[for=message]").text(cform_message);
        $("#ContactForm #FormError").text("");

        if (name.length === 0) {
            $("#ContactForm label[for=name]").text("* Name:");
            $("#ContactForm #FormError").text("Required Field Missing");
            cont = false;
        }
        if (email.length === 0) {
            $("#ContactForm label[for=email]").text("* Email:");
            $("#ContactForm #FormError").text("Required Field Missing");
            cont = false;
        }
        if (subject.length === 0) {
            $("#ContactForm label[for=subject]").text("* Subject:");
            $("#ContactForm #FormError").text("Required Field Missing");
            cont = false;
        }
        if (message.length === 0) {
            $("#ContactForm label[for=message]").text("* " + cform_message);
            $("#ContactForm #FormError").text("Required Field Missing");
            cont = false;
        }
        if (cont & !validateEmail(email)) {
            $("#ContactForm label[for=email]").text("* Email:");
            $("#ContactForm #FormError").text("Invalid Email Address");
            cont = false;
        }

        if (cont) {
            $.ajax({
                url: "/Services/Utility.aspx",
                data: {
                    command: "email",
                    name: $("#ContactForm #name").val(),
                    email: $("#ContactForm #email").val(),
                    subject: $("#ContactForm #subject").val(),
                    message: $("#ContactForm #message").val()
                },
                type: "post",
                success: function (data) {
                    var result = data.split("|")[0];
                    if (result === "ERROR") {
                        var message = data.split("|")[1];
                        $("#ContactForm #FormError").text(message);
                    } else if (result === "SUCCESS") {
                        $("#ContactForm #ContactFormTable").hide();
                        $("#ContactForm .validateTips").html('Your Message Has Been Sent');
                        $("#ContactForm #ContactForm_Send").hide();
                        $("#ContactForm #ContactForm_Cancel").text('CLOSE');
                    } else {
                    }
                }
            });
        }
    });

    $(".DonateNow").hover(function () {
        $(this).find("img").attr('src', '../images/donate-over.jpg');
    }, function () {
        $(this).find("img").attr('src', '../images/donate.jpg');
    });

    //    $(".Question div.Answer").each(function () {
    //        $(this).attr('size', $(this).height());
    //        $(this).css({ height: 0 }).show().css('visible', 'hidden');
    //    });

    //    $(".Question a.Answer").click(function (e) {
    //        e.preventDefault();
    //        $(".Question a.Answer img").attr('src', '../../images/aarowdown.png');
    //        $(".Question div.Answer.Selected").each(function () {
    //            var toheight = $(this).height();
    //            $(this).animate({ height: 0, opacity: 0 }, 500, function () {
    //                $(this).css('visible', 'hidden');
    //            });
    //        });

    //        if ($(this).hasClass("Selected")) {
    //            $(".Question a.Answer").removeClass("Selected");
    //            $(".Question div.Answer").removeClass("Selected");
    //        }
    //        else {
    //            $(".Question a.Answer").removeClass("Selected");
    //            $(".Question div.Answer").removeClass("Selected");
    //            $(".Question a.Answer img").attr('src', '../../images/aarowdown.png');
    //            $(this).addClass("Selected");
    //            $(this).parent().find("div.Answer").addClass("Selected");
    //            var toheight = $(this).parent().find("div.Answer").attr('size');
    //            $(this).find('img').attr('src', '../../images/aarowup.png');
    //            $(this).parent().find("div.Answer").css({ height: 0 }).show().animate({ height: toheight, opacity: 1 }, 500);
    //        }
    //    });

    $("#AskExpertButton>img").click(function () {
        if ($("#AskExpertButton>img").attr('src') == '../../images/askachef-open.png') {
            $("#AskForm").animate({ height: 0 }, 500, function () { $(this).hide(); $("#AskExpertButton>img").attr('src', '../../images/askachef.png'); $("#AskExpertButton>img").css({ top: 10 }); });
        }
        else {
            $("#AskExpertButton>img").attr('src', '../../images/askachef-open.png');
            $("#AskExpertButton>img").css({ top: 2 });
            $("#AskForm").css({ height: 0 }).show().animate({ height: 210 }, 500);
        }
    });

    $("#Ask_Cancel").click(function (e) {
        e.preventDefault();
        $("#AskForm").animate({ height: 0 }, 500, function () { $(this).hide(); $("#AskExpertButton>img").attr('src', '../../images/askachef.png'); $("#AskExpertButton>img").css({ top: 10 }); });
    });

    $("#Ask_Send").click(function (e) {
        e.preventDefault();

        var name = $("#AskForm #name").val();
        var email = $("#AskForm #email").val();
        var subject = "Ask an Expert: " + expert;
        var message = $("#AskForm #message").val();
        var form = "#AskForm";
        var cont = true;

        $(form + " label[for=name]").text("Name:");
        $(form + " label[for=email]").text("Email:");
        $(form + " label[for=subject]").text("Subject:");
        $(form + " label[for=message]").text("Message:");
        $(form + " #FormError").text("");

        if (name.length === 0) {
            $(form + " label[for=name]").text("* Name:");
            $(form + " #FormError").text("Required Field Missing");
            cont = false;
        }
        if (email.length === 0) {
            $(form + " label[for=email]").text("* Email:");
            $(form + " #FormError").text("Required Field Missing");
            cont = false;
        }
        if (message.length === 0) {
            $(form + " label[for=message]").text("* Question:");
            $(form + " #FormError").text("Required Field Missing");
            cont = false;
        }
        if (cont & !validateEmail(email)) {
            $(form + " label[for=email]").text("* Email:");
            $(form + " #FormError").text("Invalid Email Address");
            cont = false;
        }

        if (cont) {
            $.ajax({
                url: "/Services/Utility.aspx",
                data: {
                    command: "email",
                    name: name,
                    email: email,
                    subject: subject,
                    message: message
                },
                type: "post",
                success: function (data) {
                    var result = data.split("|")[0];
                    if (result === "ERROR") {
                        var message = data.split("|")[1];
                        $(form + " #FormError").text(message);
                    } else if (result === "SUCCESS") {
                        $(form + " #ContactFormTable").hide();
                        $(form + " .validateTips").html('Your Message Has Been Sent');
                        $(form + " #Ask_Send").hide();
                        $(form + " #Ask_Cancel").text('CLOSE');
                    } else {
                    }
                }
            });
        }
    });

    $("a.Rollover").click(function (e) { e.preventDefault(); });
    $("a.Rollover").each(function () {
        $(this).SetBubblePopup({
            imageFolder: '/Images/bp_images',
            innerHtml: $('#' + $(this).attr('rel')).html(),
            width: '500px',
            contentStyle: 'font-size: 12px;',
            bubbleAlign: 'left',
            tailAlign: 'left'
        });
    });
    $("a.RightRollover").click(function (e) { e.preventDefault(); });
    $("a.RightRollover").each(function () {
        $(this).SetBubblePopup({
            imageFolder: '/Images/bp_images',
            innerHtml: $('#' + $(this).attr('rel')).html(),
            width: '500px',
            contentStyle: 'font-size: 12px;',
            bubbleAlign: 'right',
            tailAlign: 'right'
        });
    });
});

function askQuestionForm() {
    $("#ContactForm label[for=name]").text("Name:");
    $("#ContactForm label[for=email]").text("Email:");
    $("#ContactForm label[for=subject]").text("Subject:");
    $("#ContactForm label[for=message]").text("Question:");
    $("#ContactForm #FormError").text("");
    cform_message = "Question:";
    $("#ContactForm #askQuestionImage").show();
    $("#ContactForm #contactUsImage").hide();
    $("#ContactForm #ContactFormTable").show();
    $("#ContactForm #ContactForm_Send").show();
    $("#ContactForm #ContactForm_Cancel").text('CANCEL');
    $("#ContactForm #name").val('');
    $("#ContactForm #email").val('');
    $("#ContactForm #subject").val('Choose2Choose Question Submission');
    $("#ContactForm #subject").parent().parent().hide();
    $("#ContactForm #message").val('');
    $("#ContactForm").css({ height: 15, opacity: 0 }).show().animate({ height: 310, opacity: 1 }, 750);
}
