//
// This object is used to modify links that point to non-HTML content so that they are rendered with an appropriate image. 
// It also displays the "legend" for each image/file type used on the page.
//

// Make sure the CDC and CDC.Policy namespaces are defined.
if (typeof CDC == "undefined") var CDC = new Object();
if (typeof CDC.Policy == "undefined") CDC.Policy = new Object();

// Declare an object for handling non-HTML page links.
CDC.Policy.Plugins = function() {

	// A class used to define the image and attributes associated with a particular file extension.
	function pluginDefinition(fileExtension, imageSrc, imageAlt, imageTitle, legendClass) {
		this.fileExtension = fileExtension;
		this.imageSrc = imageSrc;
		this.imageAlt = imageAlt;
		this.imageTitle = imageTitle;
		this.legendClass = legendClass;
	};

	var pluginDefinitions = new Array(
		new pluginDefinition(".pdf", "/TemplatePackage/images/icon_pdf.gif", "Adobe PDF file", "Adobe PDF file", "plugin-pdf"),
		new pluginDefinition(".doc", "/TemplatePackage/images/icon_word.gif", "Microsoft Word file", "Microsoft Word file", "plugin-word"),
		new pluginDefinition(".docx", "/TemplatePackage/images/icon_word.gif", "Microsoft Word file", "Microsoft Word file", "plugin-word"),
		new pluginDefinition(".xls", "/TemplatePackage/images/icon_excel.gif", "Microsoft Excel file", "Microsoft Excel file", "plugin-excel"),
		new pluginDefinition(".xlsx", "/TemplatePackage/images/icon_excel.gif", "Microsoft Excel file", "Microsoft Excel file", "plugin-excel"),
		new pluginDefinition(".ppt", "/TemplatePackage/images/icon_ppt.gif", "Microsoft PowerPoint file", "Microsoft PowerPoint file", "plugin-ppt"),
		new pluginDefinition(".pptx", "/TemplatePackage/images/icon_ppt.gif", "Microsoft PowerPoint file", "Microsoft PowerPoint file", "plugin-pptx"),
		new pluginDefinition(".avi", "/TemplatePackage/images/icon_wmv.gif", "Audio/Video file", "Audio/Video file", "plugin-wmv"),
		new pluginDefinition(".mp3", "/TemplatePackage/images/icon_wmv.gif", "Audio/Video file", "Audio/Video file", "plugin-wmv"),
		new pluginDefinition(".mp4", "/TemplatePackage/images/icon_wmv.gif", "Audio/Video file", "Audio/Video file", "plugin-wmv"),
		new pluginDefinition(".mpg", "/TemplatePackage/images/icon_wmv.gif", "Audio/Video file", "Audio/Video file", "plugin-wmv"),
		new pluginDefinition(".mpeg", "/TemplatePackage/images/icon_wmv.gif", "Audio/Video file", "Audio/Video file", "plugin-wmv"),
		new pluginDefinition(".wmv", "/TemplatePackage/images/icon_wmv.gif", "Audio/Video file", "Audio/Video file", "plugin-wmv"),
		new pluginDefinition(".wav", "/TemplatePackage/images/icon_wmv.gif", "Audio/Video file", "Audio/Video file", "plugin-wmv"),
		new pluginDefinition(".wma", "/TemplatePackage/images/icon_wmv.gif", "Audio/Video file", "Audio/Video file", "plugin-wmv"),
		new pluginDefinition(".zip", "/TemplatePackage/images/icon_zip.gif", "Zip Archive file", "ZIP Archive file", "plugin-zip"),
		new pluginDefinition(".ram", "/TemplatePackage/images/icon_real.gif", "RealPlayer file", "RealPlayer file", "plugin-real"),
		new pluginDefinition(".rmm", "/TemplatePackage/images/icon_real.gif", "RealPlayer file", "RealPlayer file", "plugin-real"),
		new pluginDefinition(".ra", "/TemplatePackage/images/icon_real.gif", "RealPlayer file", "RealPlayer file", "plugin-real"),
		new pluginDefinition(".rax", "/TemplatePackage/images/icon_real.gif", "RealPlayer file", "RealPlayer file", "plugin-real"),
		new pluginDefinition(".rv", "/TemplatePackage/images/icon_real.gif", "RealPlayer file", "RealPlayer file", "plugin-real"),
		new pluginDefinition(".rvx", "/TemplatePackage/images/icon_real.gif", "RealPlayer file", "RealPlayer file", "plugin-real"),
		new pluginDefinition(".rm", "/TemplatePackage/images/icon_real.gif", "RealPlayer file", "RealPlayer file", "plugin-real"),
		new pluginDefinition(".rms", "/TemplatePackage/images/icon_real.gif", "RealPlayer file", "RealPlayer file", "plugin-real"),
		new pluginDefinition(".mov", "/TemplatePackage/images/icon_qt.gif", "Apple QuickTime file", "Apple QuickTime file", "plugin-qt"),
		new pluginDefinition(".qt", "/TemplatePackage/images/icon_qt.gif", "Apple QuickTime file", "Apple QuickTime file", "plugin-qt")
	);

	return {

		//
		// This function modifies content links to documents (e.g., Word, Powerpoint, etc.) so that they are 
		// rendered with the appropriate image.  It also displays the document legend on the page if any document 
		// links are on the page.
		//
		fixLinks: function() {

			// Find all anchors within #content-main and see if they are non-HTML links (a.k.a plugins).
			var node = $("#content-main");
			if (node.length > 0 && $("#plugin-legend").length > 0) {
				
				
				node.find("a").each(function() {
					var anchor = $(this);
					var href = anchor.attr("href");
					if (href) {
						href = href.toLowerCase();
						// Get the text of the anchor.
						var anchorContent = anchor.html();
						// Find out where the location of the "[" is.  If not found the image will be placed at the end.
						var bracketPos = anchorContent.indexOf("[");
						if (bracketPos == -1) {
							bracketPos = anchorContent.length;
						}
						// Now determine what image should be displayed based on the href attribute.
						for (var i = 0; i < pluginDefinitions.length; i++) {
							if (href.endsWith(pluginDefinitions[i].fileExtension.toLowerCase())) {							
								// Replace the inner HTML with the image included.
								if (anchor.parents("li").parents("ul").parents("div.rounders").length > 0 ||
									anchor.parents("li").parents("ul").parents("div.module").length > 0) {
									anchor.html(anchorContent.substring(0, bracketPos) + 
									"<br /><span class=\"plugIns\"><img src=\"" + pluginDefinitions[i].imageSrc + "\" alt=\"" + pluginDefinitions[i].imageAlt + "\" title=\"" + pluginDefinitions[i].imageTitle + "\" class=\"plugin\" />" +
									anchorContent.substring(bracketPos) +
									"</span></a>");
								} else {
									anchor.html(anchorContent.substring(0, bracketPos) + 
									"<span class=\"plugIns\"><img src=\"" + pluginDefinitions[i].imageSrc + "\" alt=\"" + pluginDefinitions[i].imageAlt + "\" title=\"" + pluginDefinitions[i].imageTitle + "\" class=\"plugin\" />" +
									anchorContent.substring(bracketPos) +
									"</span></a>");
								}
								
								// Turn on the corresponding list item in the legend.
								$("#plugin-legend li." + pluginDefinitions[i].legendClass).addClass("pluginOn");
								
								//need to set the remaining li s to regain their margin
								
								break;
							}
						}
					}
				});
				
				// Now display the plugin legend if any of the anchors have had images added.
				if ($(".plugin").length > 0) {
					$("#plugin-legend").css("display", "block");
				} else {
					$("#plugin-legend").css("display", "none");
				}
			
			}
		
		}
	
	}; // end return

}();
	
// Associate the Plugins.fixLinks() function to the window.onload event.
$(document).ready(function(){
	CDC.Policy.Plugins.fixLinks();
});
