	
// --------------------------------------------------------------------------------------------
// CUSTOMIZE HOW THE SCRIPT WORKS
	
	// path to the PHP that writes the log file
		// edit this so it points to the download file.
		// If you have files in different subdirectory levels, this will need to be an absolute path, eg:
		
		// http://www.mysite.com/download-log/download.php
		// or
		// /download-log/download.php
		
		var downloadPHP		= "../download-log/download.php"	// this is a relative path, so will work in level 1 directories only!

	
	// edit the text to display on screen
		var downloadText	= " %"				// edit this string to change the text that gets displayed on screen
		
		
	// edit the settings of the automatically-created download tags
		var downloadClass	= "download-info"				// the css class (you can use the one supplied, or your own)
		var downloadTag		= "span" 						// the kind of tag created. Another option would be to use a div.


	// A note on how to display the download statistics
	
		// the script will *automatically* display the download statistics of any link 
		// with a "onmouseup="logDownload(this)" atribute next to the file.
	
		// however, if you want to display the stats in a separate html element
		// you need to create an HTML element with an "id" that matches the 
		// *FULLY-QUALIFIED URL* of the file that is being downloaded,
		// ie: http://www.mysite.com/myfiles/myfile.exe
		
		// so the tag would be: 
		// <span id="http://www.mysite.com/myfiles/myfile.exe"> </span>
	
	// debugging
		var debug			= false							// allows warning messages to be alerted
		var arrClicked		= new Array()

// --------------------------------------------------------------------------------------------
// SCRIPTS THAT RUN WHEN THE USER CLICKS SOMETHING
	
	// function that gets called when a user clicks the download 
		function logDownload(obj){
			var h	= document.getElementsByTagName("head")[0]
			var s	= document.createElement("script")
			s.src	= downloadPHP + "?file=" +obj.href + "&guid=" + Math.random()
			h.appendChild(s)
			updateDownload(obj.href)
			return false
			}
	
	// function that update the download totals on the page when the user clicks
		function updateDownload(href){
				
			if(arrClicked[href])return

			var cNode	= document.getElementById(unescape(href))
			
			if(!cNode){
				if(debug)alert('No tag found with the id of "'+ href +'"')
				return
				}
			
			var rx		= /\d+/
			rx.global	= true
			
			var str		= cNode.innerHTML
			var m		= parseInt(str.match(rx))
			cNode.innerHTML = cNode.innerHTML.replace(m, m + 1)
			
			arrClicked[href] = true
			}
	
		function createDownloadText(n){
			return downloadText.replace("%", n)
			}



// --------------------------------------------------------------------------------------------
// SCRIPTS THAT AUTOMATICALLY RUN
	
	// function that dynamically displays the download totals on the page next to the link
		function addDownloadTotals(){

			if(window.location.protocol.indexOf('http') == -1){
				return false
				}

			var arr = document.links
			for(var i = 0; i < arr.length; i++){
			
				// check if link is a download link or not
					var cNode	= arr[i]
					var handler = cNode.attributes['onmouseup']
					if(!handler || handler.value.indexOf('logDownload') == -1){
						continue
						}
					else{
						var href = unescape(cNode.href)
						}
			
				// get the download amount from the javascript array
					var n	= downloads[href]
					n		= n ? n : 0
					var str	= createDownloadText(n)
		
		
				// search for page elements with an id that matches the href
				// if found, set that element's text to the download stat
					var span = document.getElementById(href)
					if(span != undefined){
						span.innerHTML = str
						}
						
				// if not found, add the download statistics to a child node
				// of the existing link
					else{
			
						// create new html element
							var span		= document.createElement(downloadTag)
							var txt			= document.createTextNode(str)
							span.className	= downloadClass
							span.id			= href
							span.appendChild(txt)
							
						// attach new html element
							var pNode = cNode.parentNode
							for(c in pNode.childNodes){
								if(pNode.childNodes[c] == cNode){
									pNode.insertBefore(span, cNode.nextSibling)
									}
								}
							}


				}
					
			}
		
	// final code that sets up the page
		// client-side safety
			if(window.downloads == undefined){
				downloads = []
				}
		
		// window events
			if (window.addEventListener){
				window.addEventListener( "load", addDownloadTotals, false );
				}
			else if (window.attachEvent){
				window.attachEvent( "onload", addDownloadTotals );
				}
