﻿
// Page is ready. Call all other functions from here.
$(document).ready(function () {
    $.ajaxSetup({
        timeout: 90000
    });
    getMenu();

    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);


    if (sPage.toLowerCase() == "index.aspx") {
        $("#ad-main-holder").empty();
        $(".pocketshadow").hide();
    }
    else {
        $("#ad-main-holder").cycle({
            fx: "fade",
            timeout: 12000,
            delay: -600
        });
    }
}
);
// Use ajax to get the menu items.
function getMenu() {
    var xml = $.ajax({
        type: "POST",
        url: "Master_Pages/MenuItems.asmx/GetPublicMenuItems",
        dataType: "xml",
        success: parseXmlMenuItems,
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
}

// Called from GetMenu() on success.  Reads the menu items and creates an object for 
// each menu item with the following attributes:
// normal: Unselected list item.
// selected: Selected / current page. Same as normal, but adds a css class.
// footer: Footer link item.
// page: The file name of the page.
// theme: One of the backround color choices: yellow, pink, green, default - (blue).
function parseXmlMenuItems(xml) {
    var menuItems = new Array();
    $(xml).find("MenuItem").each(function () {
        var url = $(this).find("Url").text();
        var displayText = $(this).find("DisplayText").text();
        var pageName = $(this).find("PageName").text();
        var theme = $(this).find("Theme").text();

        menuItems.push({ normal: "<li><a href='" + url + "'>" + displayText + "</a><span>&nbsp;</span></li>",
            selected: "<li class='my-page' ><a href='" + url + "'>" + displayText + "</a><span>&nbsp;</span></li>",
            footer: "<li><a href='" + url + "'>" + displayText.toLowerCase() + "</a></li>",
            page: pageName,
            theme: theme
        });

    });

    // Get the current page attributes.
    var href = jQuery(location).attr('href');
    var title = jQuery(this).attr('title');
    $("#menu").empty();

    // Add in the menu links.
    // Determine if this is the current selected page and set the menu items style to 'my-page'.
    // Change the background color of the page.
    $.each(menuItems, function (index, value) {
        // Page header links and page background theme.
        if (href.toLowerCase().indexOf(value.page.toLowerCase()) >= 0) {
            $("#menu").append(value.selected);
            if (value.theme.toLowerCase().indexOf('green') >= 0)
                themeGreen();
            else if (value.theme.toLowerCase().indexOf('yellow') >= 0)
                themeYellow();
            else if (value.theme.toLowerCase().indexOf('pink') >= 0)
                themePink();
        }
        else {
            $("#menu").append(value.normal);
        }
        // Page footer links.
        $("#footer-nav").append(value.footer);
    });
}

// Changes the background to green.
function themeGreen() {
    $("body").attr("id", "forhim").add;
}

// Changes the background to pink.
function themePink() {
    $("body").attr("id", "forher").add;
}

// Changes the background to yellow.
function themeYellow() {
    $("body").attr("id", "forkids").add;
}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-17676476-1']);
_gaq.push(['_trackPageview']);

(function () {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
