﻿var Hyaets = function()
{
    this.lightBoxSettings = { overlayOpacity: '0.7' };

    this.animating = false;

    this.quotes = new Array();
    
    this.quoteIdx = 0;

    this.quoteSpeed = 5000;

    this.headHtml = '<div class="container">'
    +   '<ul class="nav">'
    +       '<li><a href="index.html">home</a></li>'
    +       '<li><a href="about.html">about</a></li>'
    +       '<li><a href="branches.html">branches</a></li>'
    +       '<li><a href="media.html">media</a></li>'
    +       '<li><a href="stories.html">stories</a></li>'
	+		'<li><a href="updates.html">updates</a></li>'
    +       '<li><a href="contact.html">contact</a></li>'
    +   '</ul>'
    +   '<a class="logo" href="http://www.hyaets.com"></a>'
    +   '</div>';

    this.footHtml = '<div class="container">' + '<ul class="nav">' + '<li><a href="index.html">home</a></li>' + '<li><a href="about.html">about</a></li>' + '<li><a href="branches.html">branches</a></li>' + '<li><a href="media.html">media</a></li>' + '<li><a href="stories.html">stories</a></li>' + '<li><a href="contact.html">contact</a></li>' + '<li><a href="https://npo.networkforgood.org/Donate/Donate.aspx?npoSubscriptionId=1002111">donate</a></li>' + '</ul>' + '<p>' + 'Hyaets is a 501c3 non-profit, faith-based organization.' + '<br />' + 'All donations are tax deductible and can be sent to or dropped off at 2904 Tuckaseegee Rd., Charlotte, NC 28208.' + '</p>' + '</div>';

};

var hyaets = new Hyaets();

$(document).ready(function ()
{
    $("#lnkIcon").attr("href", "http://www.southtunnel.com/favicon.ico");
    
    $("#head").html(hyaets.headHtml);
    $("#foot").html(hyaets.footHtml);
        
    var body = $("body");

    if(body.attr("id") === "homepage") initHomePage();
    else if(body.attr("id") === "aboutpage") initAboutPage();
    else if(body.attr("id") === "branchpage") initBranchPage();
    else if(body.attr("id") === "contactpage") initContactPage();
    else if(body.attr("id") === "updatespage") initUpdatesPage();

});

/// Home Page ///

function initHomePage()
{
    $(".pagegallery").lightBox(hyaets.lightBoxSettings);

    $(".tree").find(".quote").each(function ()
    {
        hyaets.quotes.push($(this));
    });
        
    if (hyaets.quotes.length > 0)
    {
        setTimeout("hyaets.quotes[hyaets.quoteIdx].fadeIn(800)", 500);
        setTimeout("nextQuote()", hyaets.quoteSpeed + 1500);
    }
}

function nextQuote()
{
    $(".tree .quote:visible").fadeOut(2000, function ()
    {
        hyaets.quoteIdx++;
        
        if (hyaets.quoteIdx >= hyaets.quotes.length)
        {
            hyaets.quoteIdx = 0;
        }

        hyaets.quotes[hyaets.quoteIdx].fadeIn(800);

        setTimeout("nextQuote()", hyaets.quoteSpeed);
    });
}

/// About Page ///

function initAboutPage()
{
    $(".leftcol:not(#about)").hide();

    $(".lnkSection").click(function ()
    {
    	var section = $($(this).attr("href"));

    	if (section.not(":visible") && !hyaets.animating)
    	{
    		hyaets.animating = true;

    		var finishAnimate = function (sect)
    		{
    			$(".leftcol:visible").fadeOut(function ()
    			{
    				sect.fadeIn(function ()
    				{
    					hyaets.animating = false;
    				});
    			});
    		}

    		var page = $.browser.msie ? $("html") : $("body");

    		if (page.scrollTop() > 165)
    		{
    			page.animate({ scrollTop: 140 }, 500, function ()
    			{
    				finishAnimate(section);
    			});
    		}
    		else
    		{
    			finishAnimate(section);
    		}
    	}

    	return false;
    });
}

/// Updates Pages ///

function initUpdatesPage()
{
    $("#needslist a").click(function()
    {
        var need = $($(this).attr("href"));
        
        if (!need.is(":visible") && !hyaets.animating)
    	{
    		hyaets.animating = true;

            $(".need:visible").fadeOut(function ()
    		{
                need.fadeIn(function()
                {
                    hyaets.animating = false;
                });    
            });
        }

        return false;
    });

    $("#regular").show();
}

/// Branch Pages ///

function initBranchPage()
{
    var sections = $(".section");

    sections.hide();

    $(".lnkList li a:not(.link), .lnkJump").click(function(e)
    {
        var id = $(this).attr("href");
        var section = $(".section:visible");

        if (("#" + section.attr("id")) !== id)
        {
            if (section.length > 0)
            {
                var finishAnimate = function(sect)
                {
                    sect.fadeOut(function()
                    {
                        $(id).fadeIn();
                    });
                }
            
                if($("body").scrollTop() > 0)
                {
                    $("html,body").animate({scrollTop: 0}, 500, function()
                    {
                        finishAnimate(section);
                    });
                }
                else finishAnimate(section);
            }
            else $(id).fadeIn();    
        }
        e.preventDefault();
        return false;
    });

    var startSection = $("#" + document.URL.substring(document.URL.indexOf("#branch") + 7));

    if(startSection.length > 0)
    {
        startSection.show();
    }
    else
    {
        $(sections[0]).show();
    }
}


/// Contact Pages ///

function initContactPage()
{
    var sendto = getUrlVars()["sendto"];

    $("#ddlSendTo").val(sendto);

    $("#btnSend").click(function() { sendMail(); } );
}

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function sendMail()
{    
    var form = $("form#sendmail");    
    var sendto = form.find("#ddlSendTo");
    var name = form.find("#txtName");
    var email = form.find("#txtEmail");
    var phone = form.find("#txtPhone");
    var message = form.find("#txtMessage");
    var button = form.find("#btnSend");
    var error = form.find(".error").text("");
        
    var nstr = name.val();
    var estr = email.val();
    var pstr = phone.val();
    var mstr = message.val();

    var errormessage = "";
    var gotname = ($.trim(nstr) !== "");
    var gotmessage = ($.trim(mstr) !== "");
    var gotcontact = ($.trim(estr) !== "" || $.trim(pstr) !== "");

    if (!gotname || !gotmessage || !gotcontact)
    {
        errormessage = "You must enter your";

        if (!gotname) errormessage += " name";
        
        if (!gotname && !gotmessage) errormessage += ", message";
        else if (!gotmessage) errormessage += " message";

        if (!gotcontact && (!gotname || !gotmessage)) errormessage += " and a phone number or email address";
        else if (!gotcontact) errormessage += " phone number or email address";

        errormessage += ".";
    }
    else
    {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if ($.trim(estr) !== "" && !filter.test(estr))
        {
            errormessage = "You entered an invalid email.";
        }        
    }

    if(errormessage !== "")
    {
        error.text(errormessage);
        return false;
    }       

    error.text("Sending...");

    sendto.attr('disabled', 'disabled').addClass("disabled");
    name.attr('disabled', 'disabled').addClass("disabled");
    email.attr('disabled', 'disabled').addClass("disabled");
    phone.attr('disabled', 'disabled').addClass("disabled");
    message.attr('disabled', 'disabled').addClass("disabled");
    button.attr('disabled', 'disabled').addClass("disabled");

    $.post(
        "sendmail.php",
        {
            sendto: sendto.val(),
            name: nstr,
            email: estr,
            phone: pstr,
            message: mstr
        },
        function (data)
        {
            sendto.removeAttr('disabled').removeClass("disabled");
            name.removeAttr('disabled').removeClass("disabled");
            email.removeAttr('disabled').removeClass("disabled");
            phone.removeAttr('disabled').removeClass("disabled");
            message.removeAttr('disabled').removeClass("disabled");
            button.removeAttr('disabled').removeClass("disabled");

            if (data.returnValue === "success")
            {
                error.text("Your message was sent successfully");

                name.val("");
                email.val("");
                phone.val("");
                message.val("");
            }
            else
            {
                error.text("Sorry. We are unable to send at this time.");
            }
        },
        "json"
    );
    
}
