﻿var CPGA = {};

CPGA.splash = {
    init: function () {
        // videos (splash)
        $("#videos div.video a").click(function () {
            embedYouTube("ytplayer", $(this).attr("id"), 318, 245);
            $.scrollTo("#splash-video", 1000);
            return false;
        });

        // rss (splash)	
        if ($("#rss").length && $("#rss li").length > 10) {
            $(".read-all-rss").click(function () {
                $(this).hide();
                $("#rss li:hidden").show("fast");
                return false;
            });
        }

    }
};

CPGA.Banners = {
    $bannersContainer: null,
    bannerTimerId: null,
    currentBanner: 1,
    numberOfBanners: 1,
    animating: true,
    init: function () {
        $bannersContainer = $("#featured-stories");
        this.currentBanner = $(".show", $bannersContainer).index() + 1;
        this.numberOfBanners = $("#featured-stories li").length;
        this.startBannerAnimation();

        $("#banner-buttons a").click(this.onBannerButtonClick);

        //stop animation if click on youtube video
        $('.story-media').mousedown(this.stopAnimation);

        //stop animation if mouse over article
        $('#featured-stories').mouseover(function () {
            CPGA.Banners.stopAnimation();
        });

    },
    startBannerAnimation: function () {
        this.animating = true;
        this.bannerTimerId = setInterval(function () {
            CPGA.Banners.currentBanner++;
            if (CPGA.Banners.currentBanner > CPGA.Banners.numberOfBanners) {
                CPGA.Banners.currentBanner = 1;
            }
            CPGA.Banners.loadBanner();
        }, 4000);
    },
    loadBanner: function () {
        // fade current banner out, fade supplied banner in
        $(".show", $bannersContainer).removeClass("show").fadeTo(700, 0.0, function () {
            $(this).addClass("hidden");



            $("li:eq(" + (CPGA.Banners.currentBanner - 1) + ")", $bannersContainer).addClass("show").css("opacity", "0.0").removeClass("hidden").fadeTo(700, 1.0);

            // change button
            $("#banner-buttons a:eq(" + (CPGA.Banners.currentBanner - 1) + ")").addClass("active-banner-button").siblings().removeClass("active-banner-button");
        });

    },
    onBannerButtonClick: function (e) {
        e.preventDefault();
        clearInterval(CPGA.Banners.bannerTimerId);
        CPGA.Banners.currentBanner = $(this).index();
        CPGA.Banners.loadBanner(false);

    },
    stopAnimation: function () {
        clearInterval(CPGA.Banners.bannerTimerId);
    }
};


CPGA.common = {
    init: function () {
        this.setTop10();
        this.setTopNav();
        this.newsSubNavDropDowns();
        this.findAProDropDowns();
        this.emailSignupBox();
        this.searchBox();
    },
    emailSignupBox: function () {
        var emailTxt = $("#ctl00_ctl00_footer_ft_sub_mailing_Email").val();
        $("#ctl00_ctl00_footer_ft_sub_mailing_Email").css("color", "#8b8b8b");
        $("#ctl00_ctl00_footer_ft_sub_mailing_Email").click(function () {
            $(this).val("");
        });

        $("#ctl00_ctl00_footer_ft_sub_mailing_Email").blur(function () {
            if ($(this).val() == "") {
                $(this).val(emailTxt);
            }
        });
    },
    searchBox: function () {
        var searchTxt = $("#ctl00_ctl00_topNav_search_site").val();
        $("#ctl00_ctl00_topNav_search_site").css("color", "#8b8b8b");
        $("#ctl00_ctl00_topNav_search_site").click(function () {
            $(this).val("");
        });

        $("#ctl00_ctl00_topNav_search_site").blur(function () {
            if ($(this).val() == "") {
                $(this).val(searchTxt);
            }
        });
    },
    setTop10: function () {
        // top ten (splash)
        var $topten = $("#topten div");
        if ($topten.length) {
            $topten.find(".topten-previous, .topten-facility").addClass("hidden");
            $topten.find(">div")
            .click(function () {
                $topten.find(">div").removeClass('show').each(function () {
                    $(this).find(".topten-previous, .topten-facility").addClass("hidden");
                });
                $(this).addClass("show").find(".topten-previous, .topten-facility").removeClass("hidden");
            });
            //.mouseout(function () {
            //    $(this).removeClass("show").find(".topten-previous, .topten-facility").addClass("hidden");
            //});
        }

        $("div#topten").mouseenter(function () {
            $('.find-a-pro-flyout').css("visibility", "hidden");
            $("#find-a-pro").children("img#btarrow").attr("src", "/images/hd/btn_up.gif");

            $("#nav-right  ul#nav li").find('div').css("visibility", "hidden");
            $("#nav-right ul#nav li a").removeClass("top-nav-active");
        });
    },
    setTopNav: function () {
        // top nav sub nav
        $("#nav-right ul#nav >li").mouseenter(function () {

            $('.find-a-pro-flyout').css("visibility", "hidden");
            $("#nav-right ul#nav li div").css("visibility", "hidden");
            $("#nav-right ul#nav li a").removeClass("top-nav-active");

            $("#find-a-pro").children("img#btarrow").attr("src", "/images/hd/btn_up.gif");
            $(this).find('a').addClass("top-nav-active");
            $(this).find('div').css("visibility", "visible");
        });

        $("a.last-item").mouseover(function () {
            $(this).parent().parent().parent().css("background", "#ffffff");
        });
        $("a.last-item").mouseout(function () {
            $(this).parent().parent().parent().css("background", "#cfcfcf");
        });

        $(".content").mouseenter(function () {
            $('.find-a-pro-flyout').css("visibility", "hidden");

            $("#nav-right  ul#nav li").find('div').css("visibility", "hidden");
            $("#nav-right ul#nav li a").removeClass("top-nav-active");
            $("#find-a-pro").children("img#btarrow").attr("src", "/images/hd/btn_up.gif");

        });

        $("#header-top").mouseenter(function () {
            $('.find-a-pro-flyout').css("visibility", "hidden");

            $("#nav-right  ul#nav li").find('div').css("visibility", "hidden");
            $("#nav-right ul#nav li a").removeClass("top-nav-active");
            $("#find-a-pro").children("img#btarrow").attr("src", "/images/hd/btn_up.gif");

        });

        $(".content-with-subnav").mouseenter(function () {
            $('.find-a-pro-flyout').css("visibility", "hidden");
            $("#find-a-pro").children("img#btarrow").attr("src", "/images/hd/btn_up.gif");

            $("#nav-right  ul#nav li").find('div').css("visibility", "hidden");
            $("#nav-right ul#nav li a").removeClass("top-nav-active");
        });

        // top nav rollover
        $("ul.topnav-subnav li a").mouseover(function () {
            $(this).find('.sub-nav-selector').attr("src", "/images/rd_arrow_collapsed.gif");
        });

        $("ul.topnav-subnav li a").mouseout(function () {
            $(this).find('.sub-nav-selector').attr("src", "/images/rd_arrow_greyBG.gif");
        });


        // find a pro flyout
        $("#find-a-pro").mouseenter(function () {
            $(this).parent().find('.find-a-pro-flyout').css("visibility", "visible");
            $(this).children("img#btarrow").attr("src", "/images/hd/btn_down.gif");
            $(".flyout-text-box").show();
        });

        $("#nav-left").mouseenter(function () {
            $("#nav-right  ul#nav li").find('div').css("visibility", "hidden");
            $("#nav-right ul#nav li a").removeClass("top-nav-active");

        });
    },
    newsSubNavDropDowns: function () {
        var height = 0;
        var $newsContainer = $(".news-subnav");

        $(".news-subnav :input").focus(function () {
            $newsContainer.height(122);
        });

        $(".news-subnav .month-dropdown").focus(function () {
            $newsContainer.height(122);
            height = $newsContainer.height();
            var newHeight = ($(this).children().length * $(this).height()) + 15;

            $newsContainer.height(newHeight);
        });

        $(".news-subnav .year-dropdown").focus(function () {
            $newsContainer.height(122);
            height = $newsContainer.height();
            var newHeight = ($(this).children().length * $(this).height()) + 35;

            $newsContainer.height(newHeight);

        });

    },
    findAProDropDowns: function () {
        var height = 0;
        var $findAProContainer = $(".find-a-pro-flyout");

        $(".find-a-pro-flyout :input").focus(function () {
            height = 227;
            $findAProContainer.height(height);
        });

        $(".certification-dropdown").focus(function () {
            height = 227;
            height = $findAProContainer.height();
            var newHeight = ($(this).children().length * $(this).height()) + 55;

            $findAProContainer.height(newHeight);
        });


    }
};

CPGA.content = {
    init: function () {

        // search widget
        $("#nav-search-button").click(function () {
            $(this).toggleClass("nav-search-selected");
            $("#search").toggle();
        });

        // mailing
        $("input.join-mailing-input").focus(function () {
            if (this.value === $(this).attr("title")) {
                this.value = '';
            }
        });

        // colorbox
        if ($("#gallery").length) {
            $("#gallery a[rel='gallery']").colorbox({ width: 550, height: 550, current: "{current} / {total}" });
        }

        /* toggle side nav expand collapse*/
        $('ul.subnav-tier2 li a img.sub-nav-selector').toggle(function (e) {
            e.preventDefault();

            $(this).parent().parent().children("ul").removeClass("hidden").addClass("show");
            $(this).parent().addClass("mn-highlight");

            $(this).attr('src', '/images/tier2_expanded_arrow.gif');
        }, function (e) {
            e.preventDefault();

            $(this).parent().parent().children("ul").removeClass("show").addClass("hidden");
            $(this).parent().removeClass("mn-highlight");


            $(this).attr('src', '/images/tier2_collapsed_arrow.gif');

        });

        $("ul.subnav-tier2 a.tier2").mouseover(function () {
            $(this).hasClass("mn-highlight");

            if (!$(this).hasClass("mn-active") && !$(this).hasClass("mn-highlight")) {
                $(this).find('.sub-nav-selector').attr('src', '/images/tier2_collapsed_arrow.gif');
            }
        });

        $("ul.subnav-tier2 a.tier2").mouseout(function () {
            if (!$(this).hasClass("mn-active") && !$(this).hasClass("mn-highlight")) {
                $(this).find('.sub-nav-selector').attr("src", "/images/rd_arrow_collapsed.gif");
            }
        });

        //rd_arrow_greyBG

        $('ul.subnav-tier3 li a img.sub-nav-selector').toggle(function (e) {
            e.preventDefault();

            $(this).parent().parent().children("ul").removeClass("hidden").addClass("show");
            $(this).parent().addClass("mn-active");

            $(this).attr('src', '/images/tier3_expanded_arrow_dark.gif');
        }, function (e) {
            e.preventDefault();

            $(this).parent().parent().children("ul").removeClass("show").addClass("hidden");
            $(this).parent().removeClass("mn-active");


            $(this).attr('src', '/images/tier3_collapsed_arrow_dark.gif');

        });


        $("ul.subnav-tier3 a.tier3").mouseover(function () {
            $(this).hasClass("mn-highlight");

            if (!$(this).hasClass("mn-active") && !$(this).hasClass("mn-highlight")) {
                $(this).find('.sub-nav-selector').attr('src', '/images/tier3_collapsed_arrow_dark.gif');
            }
        });

        $("ul.subnav-tier3 a.tier3").mouseout(function () {
            if (!$(this).hasClass("mn-active") && !$(this).hasClass("mn-highlight")) {
                $(this).find('.sub-nav-selector').attr("src", "/images/tier3_collapsed_arrow.gif");
            }
        });

    }
};

$(function () {
    CPGA.common.init();
});

function embedYouTube(containerId, videoId, width, height) {
    var params = { allowScriptAccess: "always", wmode: "transparent" },
		atts = { id: "ytplayer" };
    swfobject.embedSWF("http://www.youtube.com/v/" + videoId + "?hl=en_US&fs=1", containerId, width, height, "8", null, null, params, atts);
}

function PollDisplay() {
    $("#poll-bd").toggle();
    $("#poll-ft").toggle();
    return false;
} 


