﻿/*
* Javascript to be invoked before syndicated content.
*/

// Make sure the CDC namespace is defined.
if (typeof CDC == "undefined") var CDC = new Object();

// Declare an object for handling syndicated content from CDC.
CDC.SyndicatedContent = function() {

    var DEFAULT_REGISTRATION_ID = "cs_000"; 		// Default registration ID for unregistered partners.
    var DEFAULT_CONTENT_DIV_ID = "cdc_syndicated"; // Default ID for container DIV holding syndicated content.

    // A variable to hold the registration ID for registered partners.
    var registrationId = DEFAULT_REGISTRATION_ID;

    // A variable to hold the ID of the syndicated content.
    var contentDivId = DEFAULT_CONTENT_DIV_ID;

    // Holds a collection of URL mappings.
    var urlMappings = new Array();

    // Holds a collection of errors that occured during the javascript processing of the synidcated content.
    var syndicationErrors = new Array();

    // A mapping class used to define a mapping from an old URL to a new URL with a specified target attribute.
    function Mapping(oldUrl, newUrl, target) {
        this.oldUrl = oldUrl;
        this.newUrl = newUrl;
        this.target = target;
    }

    return {

        //
        // The set/getCampaignCode is being kept for backward compatibility with _v2.
        //
        setCampaignCode: function(code) {
            registrationId = code;
        },

        getCampaignCode: function() {
            return registrationId;
        },
        //
        //

        setRegistrationId: function(id) {
            registrationId = id;
        },

        getRegistrationId: function() {
            return registrationId;
        },

        setContentDivId: function(id) {
            contentDivId = id;
        },

        getContentDivId: function() {
            return contentDivId;
        },

        getErrors: function() {
            return syndicationErrors;
        },

        addMapping: function(oldUrl, newUrl, target) {
            urlMappings[urlMappings.length] = new Mapping(oldUrl, newUrl, target);
        },

        hideContent: function() {
            if (typeof contentDivId == "undefined") {
                contentDivId = DEFAULT_CONTENT_DIV_ID;
            }
            var syndicatedElement = document.getElementById(contentDivId);
            if (syndicatedElement) {
                syndicatedElement.style.display = "none";
            }
        },

        showContent: function() {
            if (typeof contentDivId == "undefined") {
                contentDivId = DEFAULT_CONTENT_DIV_ID;
            }
            var syndicatedElement = document.getElementById(contentDivId);
            if (syndicatedElement) {
                syndicatedElement.style.display = "block";
            }
        },

        /*
        * A function that iterates through an array of URL mappings to replace URL and/or target attributes.
        */
        mapUrls: function() {
            if (typeof contentDivId == "undefined") {
                contentDivId = DEFAULT_CONTENT_DIV_ID;
            }
            if (urlMappings && urlMappings.length > 0) {
                var d = document.getElementById(contentDivId);
                if (d) {
                    var anchors_arr = d.getElementsByTagName('a');
                    var sourceLink;
                    var destinationLink;
                    var linkHolder;
                    for (var i in urlMappings) {
                        sourceLink = urlMappings[i].oldUrl;
                        destinationLink = urlMappings[i].newUrl;
                        destinationTarget = urlMappings[i].target;
                        for (var j in anchors_arr) {
                            if (anchors_arr[j].href == sourceLink) {
                                anchors_arr[j].href = destinationLink;
                                anchors_arr[j].target = destinationTarget;
                            } // end if
                        } // end for j
                    } // end for i
                } // end if d
            } // end if url...
        },

        /*
        * A function to add the registration ID to all inbound links
        */
        addRegistrationIds: function() {
            if (typeof contentDivId == "undefined") {
                contentDivId = DEFAULT_CONTENT_DIV_ID;
            }
            var d = document.getElementById(contentDivId);
            if (d) {
                var anchors_arr = d.getElementsByTagName('a');
                for (var j in anchors_arr) {
                    if (typeof anchors_arr[j].href != "undefined" && anchors_arr[j].href != "") {
                        if (anchors_arr[j].href.indexOf("cdc.gov") > 0) {
                            if (anchors_arr[j].href.indexOf("s_cid=") < 0) {
                                if (anchors_arr[j].href.indexOf("?") > 0) {
                                    anchors_arr[j].href = anchors_arr[j].href + "&s_cid=" + registrationId;
                                } else {
                                    anchors_arr[j].href = anchors_arr[j].href + "?s_cid=" + registrationId;
                                } // end if
                            } else {
                                anchors_arr[j].href = anchors_arr[j].href.replace(/s_cid=.*/, "s_cid=" + registrationId);
                            } // end if
                        } // end if
                    } // end if
                } // end for
            } // end if d
        },

        /*
        * Decides whether to display ingested static content or use a real-time pull from CDC
        * Assumes jquery (with ajax) is available and an array called realTimeSyndicationPages is defined
        * which contains a list of URLs.
        */
        ConditionallyRetrieveContent: function(syndicationUrl, staticContentClass) {
            //Hide the cdc_syndicated div
            $('#' + DEFAULT_CONTENT_DIV_ID).hide();
            
            //Get the URL of the current page
            var currentUrl = window.location.toString().toLowerCase();

            //Checks the realTimeSyndicationPages array to see if current page’s URL exists there.
            $.each(realTimeSyndicationPages, function(index, value) { realTimeSyndicationPages[index] = value.toLowerCase(); });

            //If the current URL exists in realTimeSyndicationPages, inject the content syndication javascript and hide the static content block
            if ($.inArray(currentUrl, realTimeSyndicationPages) > -1) {
                //Inject the content via the syndication URL
                $.getScript(syndicationUrl);

                //Remove the div containing the class name passed to this function. Also, prevent 
                //the removal of divs containing the same class name within the cdc_syndicated block.
                $(document).ready(function() {
                    $("div." + staticContentClass).not("div#" + DEFAULT_CONTENT_DIV_ID + " ." + staticContentClass).remove();
                });
            } else { //Remove the syndication block and show the static content div
                $(document).ready(function() {
                    $('#' + DEFAULT_CONTENT_DIV_ID).remove();
                    $('.' + staticContentClass).show();
                });
            }
        }

    }; // end return

} ();

// Set some default URL mappings here.  The partner can also add mappings in their local javascript using the same syntaxt.
CDC.SyndicatedContent.addMapping('http://emergency.cdc.gov/disasters/floods/cleanupwater.asp', 'http://www.odh.ohio.gov/alerts/floodclean.aspx', '_self');
CDC.SyndicatedContent.addMapping('http://emergency.cdc.gov/disasters/floods/', 'http://www.odh.ohio.gov/alerts/floods.aspx', '_self');

