// adding exists function to jQuery core.
jQuery.fn.exists = function(){ return jQuery(this).length>0; }

$(function(){
	Download.init( 'download-player' );
	Download.run();	
});


Download = function(){
	
	var download_section;
	var hasDownload = false;
	
	var introMarkup = '<div id="download-player-list"><p>File Assistance: Free downloads for viewing files</p>{{tmpl "downloadTemplate"}}</div>';	
	var downloadMarkup = '<ul>{{each doctype}}<li><a href="${url}" class="file-link" target="_blank">${link_text}</a><span class="${icon}" title="${link_title}">&nbsp;</span><span class="exit" title="exit NIEHS">&nbsp;</span></li>{{/each}}</ul>';
	
	var data = {
		'pdf' : {
			fileext : [ 'pdf' ]
			, url : 'http://get.adobe.com/reader/'
			, link_text : 'Adobe Reader'
			, icon : 'pdf-logo'
			, link_title : 'Download Adobe Reader'	
		}
		,
		'word' : {
			fileext : [ 'doc', 'docx' ]
			, url : 'http://www.microsoft.com/downloads/en/details.aspx?FamilyID=3657ce88-7cfa-457a-9aec-f4f827f20cac'
			, link_text : 'Microsoft Word Viewer'
			, icon : 'doc-logo'
			, link_title : 'Download MS Word Viewer'
		}
		,
		'flash' : {
			fileext : [ 'swf' ]
			, url : 'http://get.adobe.com/flashplayer/'
			, link_text : 'Flash Player'
			, icon : 'swf-logo'
			, link_title : 'Download Flash Player'
		}	
		,
		'excel' : {
			fileext : [ 'xls', 'xlsx' ]
			, url : 'http://www.microsoft.com/downloads/en/details.aspx?FamilyID=1cd6acf9-ce06-4e1c-8dcf-f33f669dbc3a'
			, link_text : 'Microsoft Excel Viewer'
			, icon : 'xls-logo'
			, link_title : 'Download Microsoft Excel Viewer'
		}	
		,
		'powerpoint' : {
			fileext : [ 'ppt', 'pptx' ]
			, url : 'http://www.microsoft.com/downloads/en/details.aspx?FamilyID=cb9bf144-1076-4615-9951-294eeb832823'
			, link_text : 'Microsoft PowerPoint Viewer'
			, icon : 'ppt-logo'
			, link_title : 'Download Microsoft PowerPoint Viewer'
		}	
		,
		'wmp' : {
			fileext : [ 'wmv' ]
			, url : 'http://windows.microsoft.com/en-US/windows/downloads/windows-media-player'
			, link_text : 'Microsoft Windows Media Player'
			, icon : 'wmv-logo'
			, link_title : 'Download Microsoft Windows Media Player'
		}
		,
		'quicktime' : {
			fileext : [ 'mov' ]
			, url : 'http://www.apple.com/quicktime/download/'
			, link_text : 'QuickTime'
			, icon : 'mov-logo'
			, link_title : 'Download QuickTime'
		}
		,
		'mp3' : {
			fileext : [ 'mp3' ]
			, url : 'http://www.apple.com/quicktime/download/'
			, link_text : 'QuickTime'
			, icon : 'mp3-logo'
			, link_title : 'Download QuickTime'
		}
		,
		'mp4' : {
			fileext : [ 'mp4' ]
			, url : 'http://www.apple.com/quicktime/download/'
			, link_text : 'QuickTime'
			, icon : 'mp4-logo'
			, link_title : 'Download QuickTime'
		}
		,
		'real' : {
			fileext : [ 'rm', 'ram', 'ra' ]
			, url : 'http://www.real.com/'
			, link_text : 'Real Player'
			, icon : 'ram-logo'
			, link_title : 'Download Real Player'
		}
		,
		'zip' : {
			fileext : [ 'zip' ]
			, url : 'http://www.hud.gov/offices/adm/grants/zipassist.cfm'
			, link_text : 'Zip'
			, icon : 'zip-logo'
			, link_title : 'Information About ZIP Files'
		}
		,
		'wpd' : {
			fileext : [ 'wpd' ]
			, url : 'http://www.corel.com/servlet/Satellite/us/en/Product/1171405162003#tabview=tab5'
			, link_text : 'WordPerfect'
			, icon : 'wpd-logo'
			, link_title : 'Information About WordPerfect Files'
		}
	}
	
	var isDocTypePresent = function(type){
		var fileext = data[ type ][ 'fileext' ];
		var found = false;
		for (var i = 0; i < fileext.length; i++ ){
			found = $( 'a[href$=' + fileext[ i ] + ']').exists();
			if (found)
				break;
		}	
		
		if (!hasDownload){
			hasDownload = (found ) ? true : false;
		}	
		return found;
	}
	
	var getDataForType = function(type){
		return data[ type ];
	}
	
	var getDocTypes = function(){
		var docTypes = [];
		$.each( data, function( key, value ){
			docTypes[ docTypes.length ] = key;
			
		});
		return docTypes;
	}	
	
	return {
		init: function(player){
			download_section = $( '#' + player  );
			$.template( "introTemplate", introMarkup );
			$.template( "downloadTemplate", downloadMarkup );			
			
		}
		,
		run : function(){
			if (download_section.exists()){
				var docTypes = getDocTypes();
	
				var cnt = 0;
				var myData = [
				   { 'doctype' : [] }            
				];
				
				for (var i = 0; i < docTypes.length; i++ ){
					if ( isDocTypePresent( docTypes[ i ] ) ){
						myData[ 0 ][ 'doctype' ][ cnt ] = getDataForType( docTypes[ i ] );
						cnt++;
					}
				}	
				
				if (hasDownload){
					//run the named template
					$.tmpl( 'introTemplate', myData ).appendTo( download_section );
				}	
			}
		}	
	};
}();
