/*
 * 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("?") > 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
							} // end if
						} // end if
					} // end for
				} // end if d
		}

	};	// 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');
