﻿$(document).ready(function () {
    $('div.menuContainer').hover(function () {
        $("ul.menuBody", this).slideDown('medium');
    },
    function () {
        $("ul.menuBody", this).slideUp('medium');
    });

    changeBackgroundImage();
    setInterval(changeBackgroundImage, 10000);
    setUpEmailListControls();
    startWeatherScrolling();
    $(".fullHeightContainer").height($(document).height() - 88);
    if (hideFooter != "true")
        setTimeout(function () { $("#footerMain").pinFooter(); $("#footerMain").fadeIn(); }, 1000);
});
$(window).resize(function () {
    $(".fullHeightContainer").height($(document).height() - 88);
    $("#footerMain").pinFooter();
});

/**************************Background Slideshow****************************/
var startedSlideshow = false;
var currentImageIndex = -1;
var nextLoaded = true;
var switchWhenLoaded = false;
var currentImageID = "bgImage1";
function changeBackgroundImage() {
    if (!nextLoaded) {
        switchWhenLoaded = true;
        return;
    }
    var firstTime = false;
    if (currentImageIndex < 0) {
        currentImageIndex = Math.floor(Math.random() * numImages);
        var currentImage = document.getElementById(currentImageID);
        currentImage.src = getImageFilename(currentImageIndex);
        preloadNext();
        return;
    }
    currentImageIndex = (currentImageIndex + 1) % numImages;
    imageFilename = getImageFilename(currentImageIndex);

    if (currentImageID == "bgImage1") {
        $("#bgImage1").fadeOut(1000);
        $("#bgImage2").fadeIn(1000);
        currentImageID = "bgImage2";
    } else {
        $("#bgImage2").fadeOut(1000);
        $("#bgImage1").fadeIn(1000);
        currentImageID = "bgImage1";
    }
    setTimeout(preloadNext, 1000);
}

function preloadNext() {
    nextLoaded = false;
    switchWhenLoaded = false;
    startedSlideshow = true;
    var nextImage = document.getElementById(currentImageID == "bgImage1" ? "bgImage2" : "bgImage1");
    nextImage.src = getImageFilename((currentImageIndex + 1) % numImages);
}

function getImageFilename(imageIndex){
    var imageFilename = ("00" + imageIndex.toString());
    imageFilename = imageFilename.substr(imageFilename.length - 3, 3);
    return "/Images/GetImage.ashx?imageKey=background" + imageFilename + "&maxWidth=" + $(window).width() + "&maxHeight=" + $(window).height() + "&cropToFit=true";
}

function imageLoaded() {
    if (!startedSlideshow)
        return;
    nextLoaded = true;
    if (switchWhenLoaded) {
        changeBackgroundImage();
    }
}

function getSitePageContent(sitePageID) {
    $.ajax({
        type: "POST",
        url: "/Default.aspx/RenderPageContent",
        data: "{'sitePageID':'" + sitePageID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d == null)
                alert("Couldn't find page content.");
            document.getElementById("sitePageContents").innerHTML = msg.d;
        }
    });
}

/**********************Email List*************************/
function joinEmailList(emailAddress, confirmationContainer) {
    $.ajax({
        type: "POST",
        url: "/Default.aspx/JoinEmailList",
        data: "{'emailAddress':'" + emailAddress + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            $(confirmationContainer).html("You've been successfully added to our mailing list.  Thank You!");
            setTimeout(function () { $("#emailSignUpContainer").slideUp(1500); }, 2000);
            setTimeout(function () { $("#emailSignUpContainerTop").slideUp(1500); }, 2000);
            setCookie("emailListStatus", "joined", 30);
        }
    });
}

function optOutEmail() {
    $("#emailSignUpContainer").slideUp(1500);
    setCookie("emailListStatus", "optedOut", 30);
}

function setUpEmailListControls() {
    var emailListStatus = getCookie("emailListStatus");
    if (emailListStatus == "")
        emailListStatus = null;
    if (emailListStatus == null) {
        setTimeout(function () {
            $("#emailSignUpContainer").slideDown(1500);
        }, 1000);
        document.getElementById("emailSignUpContainerTop").style.display = "block";
    }
    else if (emailListStatus == "joined")
        document.getElementById("emailSignUpContainerTop").style.display = "none";
    else if (emailListStatus == "optedOut")
        document.getElementById("emailSignUpContainerTop").style.display = "block";
}

/************************Scrolling Weather*********************/
function startWeatherScrolling() {
    setInterval(scrollWeather, 5000);
}

var currentWeatherId = "weatherPhoenix";
function scrollWeather() {
    var oldDiv = document.getElementById(currentWeatherId);
    currentWeatherId = currentWeatherId == "weatherPhoenix" ? "weatherPecans" : "weatherPhoenix";
    var newDiv = document.getElementById(currentWeatherId);
    $(oldDiv).animate({ marginTop: -30 }, 1000, function () {
        oldDiv.style.display = "none";
        oldDiv.style.marginTop = "60px";
        newDiv.style.display = "block";
        $(newDiv).animate({ marginTop: 20 }, 1000);
    });
    
}

/************************Cookies************************/
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1)
                c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

function setCookie(c_name, value, expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}
