﻿// main.js

var MMPA = {
    domain: null,
    fadeInDuration: 500,
    fadeOutDuration: 300,

    displayFlash: function() {
        // HomePage.aspx
        if ($("#the-power-of-your-hometown").length > 0) {
            var flashvars = {};
            flashvars.width = 693;
            flashvars.height = 414;
            flashvars.backcolor = '0xFFFFFF';
            flashvars.bufferlength = 1;
            flashvars.backcolor = '0xFFFFFF';
            flashvars.frontcolor = '0xF26722';
            flashvars.lightcolor = '0xFBE9A1';
            flashvars.screencolor = '0xFFFFFF';
            flashvars.volume = 100;
            flashvars.showdigits = false;
            flashvars.usefullscreen = false;
            var params = {
                play: "true",
                loop: "true",
                menu: "true",
                quality: "high",
                scale: "showall",
                salign: "",
                wmode: "opaque",
                bgcolor: "#ffffff",
                devicefont: "false",
                allowFullScreen: "false",
                allowScriptAccess: "sameDomain"
            };
            var attributes = {};
            swfobject.embedSWF("/CMSTemplates/mmpa/flash/mmpa_home_animation.swf", "the-power-of-your-hometown", "693", "414", "8", false, flashvars, params, attributes);
        }

        // OurCommunities.aspx
        if ($("#our-communities").length > 0) {
            var flashvars = {};
            flashvars.width = 693;
            flashvars.height = 412;
            flashvars.backcolor = '0xFFFFFF';
            flashvars.bufferlength = 1;
            flashvars.backcolor = '0xFFFFFF';
            flashvars.frontcolor = '0xF26722';
            flashvars.lightcolor = '0xFBE9A1';
            flashvars.screencolor = '0xFFFFFF';
            flashvars.volume = 100;
            flashvars.showdigits = false;
            flashvars.usefullscreen = false;
            var params = {
                play: "true",
                loop: "true",
                menu: "true",
                quality: "high",
                scale: "showall",
                salign: "",
                wmode: "opaque",
                bgcolor: "#ffffff",
                devicefont: "false",
                allowFullScreen: "false",
                allowScriptAccess: "sameDomain"
            };
            var attributes = {};
            swfobject.embedSWF("/CMSTemplates/mmpa/flash/OurCommunities.swf", "our-communities", "693", "412", "8", false, flashvars, params, attributes)
        }

        // PowerSupply.aspx
        if ($("#power-supplies").length > 0) {
            var flashvars = {};
            flashvars.width = 693;
            flashvars.height = 412;
            flashvars.backcolor = '0xFFFFFF';
            flashvars.bufferlength = 1;
            flashvars.backcolor = '0xFFFFFF';
            flashvars.frontcolor = '0xF26722';
            flashvars.lightcolor = '0xFBE9A1';
            flashvars.screencolor = '0xFFFFFF';
            flashvars.volume = 100;
            flashvars.showdigits = false;
            flashvars.usefullscreen = false;
            var params = {
                play: "true",
                loop: "true",
                menu: "true",
                quality: "high",
                scale: "showall",
                salign: "",
                wmode: "opaque",
                bgcolor: "#ffffff",
                devicefont: "false",
                allowFullScreen: "false",
                allowScriptAccess: "sameDomain"
            };
            var attributes = {};
            swfobject.embedSWF("/CMSTemplates/mmpa/flash/PowerSupply.swf", "power-supplies", "693", "412", "8", false, flashvars, params, attributes)
        }

        // RenewableEnergy.aspx
        if ($("#sustainable-energy").length > 0) {
            var flashvars = {};
            flashvars.width = 693;
            flashvars.height = 412;
            flashvars.backcolor = '0xFFFFFF';
            flashvars.bufferlength = 1;
            flashvars.backcolor = '0xFFFFFF';
            flashvars.frontcolor = '0xF26722';
            flashvars.lightcolor = '0xFBE9A1';
            flashvars.screencolor = '0xFFFFFF';
            flashvars.volume = 100;
            flashvars.showdigits = false;
            flashvars.usefullscreen = false;
            var params = {
                play: "true",
                loop: "true",
                menu: "true",
                quality: "high",
                scale: "showall",
                salign: "",
                wmode: "opaque",
                bgcolor: "#ffffff",
                devicefont: "false",
                allowFullScreen: "false",
                allowScriptAccess: "sameDomain"
            };
            var attributes = {};
            swfobject.embedSWF("/CMSTemplates/mmpa/flash/SustainableEnergy.swf", "sustainable-energy", "693", "412", "8", false, flashvars, params, attributes)
        }

        // LiveTurbineInfoPage.aspx
        if ($("#live-turbine-information").length > 0) {
            var flashvars = {};
            flashvars.width = 693;
            flashvars.height = 512;
            flashvars.backcolor = '0xFFFFFF';
            flashvars.bufferlength = 1;
            flashvars.backcolor = '0xFFFFFF';
            flashvars.frontcolor = '0xF26722';
            flashvars.lightcolor = '0xFBE9A1';
            flashvars.screencolor = '0xFFFFFF';
            flashvars.volume = 100;
            flashvars.showdigits = false;
            flashvars.usefullscreen = false;
            var params = {
                play: "true",
                loop: "true",
                menu: "true",
                quality: "high",
                scale: "showall",
                salign: "",
                wmode: "opaque",
                bgcolor: "#ffffff",
                devicefont: "false",
                allowFullScreen: "false",
                allowScriptAccess: "sameDomain"
            };
            var attributes = {};
            swfobject.embedSWF("/CMSTemplates/mmpa/flash/LiveTurbineInformation.swf", "live-turbine-information", "693", "512", "8", false, flashvars, params, attributes);
        }
    },

    fadeIn: function() {
        MMPA.displayFlash();
        $("#ajax").fadeIn(MMPA.fadeInDuration);
        MMPA.setTabs();
        MMPA.initPage();
    },

    getDefaultPage: function() {
        var location = document.location.toString();
        var index = location.lastIndexOf("#");
        var url;
        if (index > 0) {
            url = location.substring(index + 1, location.length);
            MMPA.requestPage(url);
            $("#ajax").html("Loading");
            $("#ajax").load(url + " #content", null, null);
        }
    },

    init: function() {
        MMPA.initLeftNav();
        MMPA.getDefaultPage();
        MMPA.displayFlash();
        MMPA.setTabs();
        MMPA.setAdditionalLinks();
    },

    initLeftNav: function() {
        //set ID for main nav anchors
        var main_nav = $("#left-nav h3.main-nav a");

        for (x = 0; x < main_nav.length; x++) {
            // get the main nav
            $(main_nav[x]).attr("id", "n" + x);
            if ($(main_nav[x]).hasClass("selected_main")) {
                $(main_nav[x]).html('<img src="/CMSTemplates/mmpa/images/left_nav/nav_' + x + '_open.gif" />');
            }
            else {
                $(main_nav[x]).html('<img src="/CMSTemplates/mmpa/images/left_nav/nav_' + x + '_closed.gif" />');
            }
            // var sec_nav = $(main_nav[x]).find(".secondary-nav");
        }

        var secondary_nav = $("#left-nav div.navitem");

        for (x = 0; x < secondary_nav.length; x++) {
            var sec_nav = $(secondary_nav[x]).find(".secondary-nav");
            for (y = 0; y < sec_nav.length; y++) {
                $(sec_nav[y]).attr("id", "s" + x + "-" + y);
            }
        }

        // reset all anchor links
        /*  tagged out by ef july 31, 2008
        we are seeing odd behavior in left nav 
        FogBugz 1436

        $("a").bind("click", function(e)
        {
        if($(this).hasClass("no-ajax"))
        {
        // Use to prevent link behavior from being rewritten
        // Set this in CMS > [content item] > Properties > Menu > Menu item CSS class
        // Do nothing
        }
        else if($(this).hasClass("primary-nav"))
        {
        MMPA.updateLeftNav($(this).attr("id"));
        MMPA.requestPage(this.href);
        return false;
        }
        else if($(this).hasClass("secondary-nav"))
        {
        MMPA.updateSecondaryNav($(this).attr("id"));
        MMPA.requestPage(this.href);
        return false;
        }
        else
        {
        MMPA.requestPage(this.href);
        return false;
        }
        });
        */

        // sets open or closed to menus...
        var menus = $("#left-nav div.navitem");
        for (x = 0; x < menus.length; x++) {
            $(menus[x]).attr("id", "d" + x);
            if (!$(menus[x]).hasClass("selected_main_nav")) {
                $(menus[x]).css("display", "none");
            }
        }
    },

    initLinks: function() {
        // Override the default "click" event of anchors.
        $("a").bind("click", function(e) {
            MMPA.requestPage(this.href);
            //Bswing.updatePageTitle(this.id);
            return false;
        });
    },

    setAdditionalLinks: function() {
        $("a.external").click(function() {
            window.open(this.href);
            return false;
        });
    },

    /**
    * Handles page/template specific initialization.
    * @return void
    */
    initPage: function() {
        // ShareYourTipsForm.aspx
        if ($("#ShareYourTipsFormAspx").length > 0) {
            MMPA.ShareYourTipsForm.init();
        }
        // NewsletterSignupForm.aspx
        if ($("#NewsletterSignupFormAspx").length > 0) {
            MMPA.NewsletterSignupForm.init();
        }
    },

    navArr: null,

    requestPage: function(url) {
        if (url.indexOf(MMPA.domain) > 0) {
            url = url.substring(url.indexOf(MMPA.domain) + MMPA.domain.length, url.length);
        }

        if (url == "/default.aspx") {
            MMPA.updateLeftNav();
        }

        unFocus.History.addHistory(url);
    },

    retrievePage: function(historyHash) {
        $("#ajax").fadeOut(MMPA.fadeOutDuration, function() { $("#ajax").load(historyHash + " #content", null, MMPA.fadeIn); });
    },

    setPanel: function(i) {
        if (i == null || i < 0) {
            i = 0;
        }

        var tabset = $("div.tab-set");
        var panels = $(tabset).find("div.panel");

        for (x = 0; x < panels.length; x++) {
            if (x == i) {
                $(panels[x]).css("display", "block");
            }
            else {
                $(panels[x]).css("display", "none");
            }
        }
    },

    setTabs: function() {
        var tabset = $("div.tab-set");
        var tabs = $(tabset).find("div.tab");
        for (x = 0; x < tabs.length; x++) {
            $(tabs[x]).attr("id", "t" + x);

            $(tabs[x]).bind("click", function(e) {
                var c = $(this).attr("id");

                for (y = 0; y < tabs.length; y++) {
                    var key = "t" + y;
                    var img_id;

                    if ($(tabs[y]).hasClass("t0")) {
                        img_id = 0;
                    }
                    else if ($(tabs[y]).hasClass("t1")) {
                        img_id = 1;
                    }
                    else if ($(tabs[y]).hasClass("t2")) {
                        img_id = 2;
                    }
                    else if ($(tabs[y]).hasClass("t3")) {
                        img_id = 3;
                    }
                    if (key == c) {
                        $(tabs[y]).html('<img src="/CMSTemplates/mmpa/images/communities/t_' + img_id + '_on.gif" />');
                        MMPA.setPanel(y);
                    }
                    else {
                        $(tabs[y]).html('<img src="/CMSTemplates/mmpa/images/communities/t_' + img_id + '_off.gif" />');
                    }
                }
            });
        }
        MMPA.setPanel(0);
    },

    updateLeftNav: function(selected_obj) {
        MMPA.updateSecondaryNav();

        var close_id = null;
        var open_id = null;

        var main_nav = $("#left-nav h3.main-nav a");
        for (x = 0; x < main_nav.length; x++) {
            var test_id = "n" + x;
            var target_div = "d" + x;

            if (selected_obj == test_id) {
                open_id = "#" + target_div;
                $(main_nav[x]).html('<img src="/CMSTemplates/mmpa/images/left_nav/nav_' + x + '_open.gif" />');
            }
            else {
                $(main_nav[x]).html('<img src="/CMSTemplates/mmpa/images/left_nav/nav_' + x + '_closed.gif" />');
                if ($("#" + target_div).css("display") == "block") {
                    close_id = "#" + target_div;
                }
            }
        }

        if (close_id == null) {
            $(open_id).slideDown(400)
        }
        else {
            $(open_id).slideDown(400)
            $(close_id).slideUp(400);
        }
    },

    updateSecondaryNav: function(selected_item) {
        var navgroup = $("#left-nav .navitem");
        for (x = 0; x < navgroup.length; x++) {
            var sec_nav = $(navgroup[x]).find(".secondary-nav");
            for (y = 0; y < sec_nav.length; y++) {
                var key = "s" + x + "-" + y;
                if (selected_item == key) {
                    $(sec_nav[y]).css("font-weight", "bold");
                }
                else {
                    $(sec_nav[y]).css("font-weight", "normal");
                }
            }
        }
    }
};

/**
 * Handles behavior specific to the Newsletter Sign-up form.
 */
MMPA.NewsletterSignupForm = {
    init: function() {
        // Hide the utility SELECT.
        $("#UtilityInputView").css("display", "none");
        
        // Show the utility SELECT if "IsCustomerInput1" radio button is selected.
        if ($("input[@name='ctl00$plcMain$IsCustomerInput']:checked").val() == "yes") {
            MMPA.NewsletterSignupForm.showUtilityInputView();
        }

        // Set event handlers.
        MMPA.NewsletterSignupForm.setIsCustomerInput1_Click();
        MMPA.NewsletterSignupForm.setIsCustomerInput2_Click();
        MMPA.NewsletterSignupForm.setIsCustomerInput3_Click();
        MMPA.NewsletterSignupForm.setSubmitButton_Click();
    },
    
    /**
     * Shows the utility SELECT.
     */
    showUtilityInputView: function() {
        $("#UtilityInputView").fadeIn("slow");
    },
    
    /**
     * Hides the utility SELECT.
     */
    hideUtilityInputView: function() {
        $("#UtilityInputView").fadeOut("slow");
    },
    
    /**
     * Sets "click" event handler on "IsCustomerInput1" radio button.
     */
    setIsCustomerInput1_Click: function() {
        $("#ctl00_plcMain_IsCustomerInput1").click(function() {
            MMPA.NewsletterSignupForm.showUtilityInputView();
        });
    },
    
    /**
     * Sets "click" event handler on "IsCustomerInput2" radio button.
     */
    setIsCustomerInput2_Click: function() {
        $("#ctl00_plcMain_IsCustomerInput2").click(function() {
            MMPA.NewsletterSignupForm.hideUtilityInputView();
        });
    },
    
    /**
     * Sets "click" event handler on "IsCustomerInput3" radio button.
     */
    setIsCustomerInput3_Click: function() {
        $("#ctl00_plcMain_IsCustomerInput3").click(function() {
            MMPA.NewsletterSignupForm.hideUtilityInputView();
        });
    },
    
    /**
     * Sets "click" even handler on "SubmitButton".
     */
    setSubmitButton_Click: function() {
        $("#ctl00_plcMain_SubmitButton").click(function() {
            // Collect data from inputs
            var inputEmail = $("#ctl00_plcMain_EmailInput").val();
            var inputName = $("#ctl00_plcMain_NameInput").val();
            var inputCity = $("#ctl00_plcMain_CityInput").val();
            var inputRegion = $("#ctl00_plcMain_RegionInput").val();
            var inputPostalCode = $("#ctl00_plcMain_PostalCodeInput").val();
            var inputIsCustomer = "";
            if ($("input[@name='ctl00$plcMain$IsCustomerInput']:checked").val() == "yes") {
                inputIsCustomer = "yes";
            }
            if ($("input[@name='ctl00$plcMain$IsCustomerInput']:checked").val() == "no") {
                inputIsCustomer = "no";
            }
            if ($("input[@name='ctl00$plcMain$IsCustomerInput']:checked").val() == "ns") {
                inputIsCustomer = "ns";
            }
            var inputUtility = $("#ctl00_plcMain_UtilityInput").val();

            // Ajax post back
            $.post("/News/E-mail-Newsletter.aspx", {postback: true, email: inputEmail, name: inputName, city: inputCity, region: inputRegion, postalcode: inputPostalCode, isCustomer: inputIsCustomer, utility: inputUtility }, function(data) {
                var start = data.indexOf('<div id="content">');
                var stop = data.indexOf('<div id="end">');
                var c = data.substring(start, stop)
                $("#ajax").fadeOut(MMPA.fadeOutDuration, function() { $("#ajax #content").replaceWith(c); MMPA.fadeIn(); });
            }, "html");
            
            // Prevent submission via the submit button.
            return false;
        });
    }
};



/**
 * Handles behavior specific to the Share Your Tips form.
 */
MMPA.ShareYourTipsForm = {
    init: function() {
        // Hide the utility SELECT.
        //MMPA.ShareYourTipsForm.hideUtilityInputView();
        $("#UtilityInputView").css("display", "none");
        
        // Show the utility SELECT if "IsCustomerInput1" radio button is selected.
        if ($("input[@name='ctl00$plcMain$IsCustomerInput']:checked").val() == "yes") {
            MMPA.ShareYourTipsForm.showUtilityInputView();
        }
        
        // Set event handlers.
        MMPA.ShareYourTipsForm.setIsCustomerInput1_Click();
        MMPA.ShareYourTipsForm.setIsCustomerInput2_Click();
        MMPA.ShareYourTipsForm.setIsCustomerInput3_Click();
        MMPA.ShareYourTipsForm.setSubmitButton_Click();
    },
    
    /**
     * Shows the utility SELECT.
     */
    showUtilityInputView: function() {
        $("#UtilityInputView").fadeIn("slow");
    },
    
    /**
     * Hides the utility SELECT.
     */
    hideUtilityInputView: function() {
        $("#UtilityInputView").fadeOut("slow");
    },
    
    /**
     * Sets "click" event handler on "IsCustomerInput1" radio button.
     */
    setIsCustomerInput1_Click: function() {
        $("#ctl00_plcMain_IsCustomerInput1").click(function() {
            MMPA.ShareYourTipsForm.showUtilityInputView();
        });
    },
    
    /**
     * Sets "click" event handler on "IsCustomerInput2" radio button.
     */
    setIsCustomerInput2_Click: function() {
        $("#ctl00_plcMain_IsCustomerInput2").click(function() {
            MMPA.ShareYourTipsForm.hideUtilityInputView();
        });
    },
    
    /**
     * Sets "click" event handler on "IsCustomerInput3" radio button.
     */
    setIsCustomerInput3_Click: function() {
        $("#ctl00_plcMain_IsCustomerInput3").click(function() {
            MMPA.ShareYourTipsForm.hideUtilityInputView();
        });
    },
    
    /**
     * Sets "click" even handler on "SubmitButton".
     */
    setSubmitButton_Click: function() {    
        $("#ctl00_plcMain_SubmitButton").click(function() {
            // Collect data from inputs
            var inputEmail = $("#ctl00_plcMain_EmailInput").val();
            var inputName = $("#ctl00_plcMain_NameInput").val();
            var inputTip = $("#ctl00_plcMain_TipInput").val();
            var inputTipType = "";
            if ($("input[@name='ctl00$plcMain$TipTypeInput']:checked").val() == "Home") {
                inputTipType = "Home";
            }
            if ($("input[@name='ctl00$plcMain$TipTypeInput']:checked").val() == "Work") {
                inputTipType = "Work";
            }
            var inputCity = $("#ctl00_plcMain_CityInput").val();
            var inputRegion = $("#ctl00_plcMain_RegionInput").val();
            var inputPostalCode = $("#ctl00_plcMain_PostalCodeInput").val();
            var inputIsCustomer = "";
            if ($("input[@name='ctl00$plcMain$IsCustomerInput']:checked").val() == "yes") {
                inputIsCustomer = "yes";
            }
            if ($("input[@name='ctl00$plcMain$IsCustomerInput']:checked").val() == "no") {
                inputIsCustomer = "no";
            }
            var inputUtility = $("#ctl00_plcMain_UtilityInput").val();
            
            // Ajax post back
            $.post("/Conservation/Share-Your-Tips.aspx", {postback: true, email: inputEmail, name: inputName, tip: inputTip, tiptype: inputTipType, city: inputCity, region: inputRegion, postalcode: inputPostalCode, isCustomer: inputIsCustomer, utility: inputUtility }, function(data) {
                var start = data.indexOf('<div id="content">');
                var stop = data.indexOf('<div id="end">');
                var c = data.substring(start, stop)
                $("#ajax").fadeOut(MMPA.fadeOutDuration, function() { $("#ajax #content").replaceWith(c); MMPA.fadeIn(); });
            }, "html");
            
            // Prevent submission via the submit button.
            return false;
        });
    }
};

$(document).ready(function () {
    MMPA.domain = "mmpa.dev.b-swing.com";
    MMPA.init();
    unFocus.History.addEventListener('historyChange', MMPA.retrievePage);
});
