
/*

	Alpha Down - File Upload and Masking Service
	Programmed and Designed by Colin Wood (http://methanecandy.com)
	
	Copyright Colin Wood 2009

*/

var isEven = true; // used to determine the alternating backgounds of the file slots (in the middle collumn)
var totalFiles = 1; // the number of files the user is uploading
var MAX_FILES = 10; // the max number of files a user can upload

function init() { // called opon load of body
	
	// this following bit makes the middle block the same height as the biggest outer block
	// this acheive the effect of having the blocks al the same height
	
	// store these DOM elements in handy-dandy variables
	var lb = document.getElementById("left_block"); // left collumn
	var mb = document.getElementById("middle_block"); // middle collumn
	var rb = document.getElementById("right_block"); // right collumn
	
	var biggest = (lb.offsetHeight>rb.offsetHeight)?lb.offsetHeight:rb.offsetHeight; // find the tallest div of the two outer collumns
	mb.style.minHeight = biggest+"px"; // set the middle collum's height to the tallest outer div
}

function addFile() { // called when user clicks "add a file"; adds a file slot to the middle div
	totalFiles++; // keep track of how manny files are going to be uploaded
	
	isEven = !isEven; // toggle backgound color
	
	var newFields = document.getElementById("file_1").cloneNode(true); // clone the first file slot's DOM representation
	newFields.id = "file_"+totalFiles; // give the clone a new ID
	newFields.style.display = "block"; // make sure it gets displayed, lol
	document.getElementById("file_container").insertBefore(newFields, null); // insert the cloned file slot
	
	// and populate the newly cloned object with the HTML necessary for operation, with dynamic attributes
		
	document.getElementById("file_"+totalFiles).innerHTML = "<div id=\"file_"+totalFiles+"\" class=\"file_block"+((isEven)?"":" odd_file")+"\"><div class=\"file_item\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"2px\"><tr><td align=\"right\" style=\"padding-right: 1em;\"><label for=\"file_input"+totalFiles+"\">File "+totalFiles+" of <span class=\"totalFileCount\">"+totalFiles+"</span>: </label></td><td><input type=\"file\" name=\"file_input"+totalFiles+"\" id=\"file_input"+totalFiles+"\"  onchange=\"processSelection("+totalFiles+");\" /></td></tr><tr><td align=\"right\" style=\"padding-right: 1em;\"><label for=\"name"+totalFiles+"\">New Name</label></td><td><input type=\"text\" name=\"name"+totalFiles+"\" id=\"name"+totalFiles+"\" class=\"red_border\" /></td></tr><tr><td align=\"right\" style=\"padding-right: 1em;\"><label for=\"tags"+totalFiles+"\">Optional Tags</label></td><td><input type=\"text\" name=\"tags"+totalFiles+"\" id=\"tags"+totalFiles+"\" class=\"red_border\" onfocus=\"document.getElementById('ss"+totalFiles+"').style.visibility = 'visible';\" onblur=\"document.getElementById('ss"+totalFiles+"').style.visibility = 'hidden';\" /><span style=\"visibility: hidden; color: #"+((isEven)?"888888":"666666")+"; font-size: 10px; margin-left: 5px;\" id=\"ss"+totalFiles+"\">(space separated)</span></td></tr><tr><td></td><td><input type=\"checkbox\" name=\"private"+totalFiles+"\" id=\"private"+totalFiles+"\" /><label for=\"private"+totalFiles+"\">Keep this upload </label><a onclick=\"privateAlert();\" >private</a>.</td></tr></table></div></div>";
	
	// this loop finds all <span>s with the class name totalFileCount and updates their contents to reflect the new number of files. Spiffy!!
	var totals = document.getElementsByTagName("span");
	for(var i=0; i<totals.length; i++) {
		if(totals[i].className == "totalFileCount") {
			totals[i].innerHTML = totalFiles;
		}
	}
	
	// take away the "Add File" button if the limit has been reached
	if(totalFiles > MAX_FILES-1) {
		document.getElementById("fileAdder").innerHTML = "";
	}
	
	document.getElementById("next_file").htmlFor = "file_input"+totalFiles; // Auto-select the new file's browser dialog - nifty! :D
}

function processSelection(id) { // called when the user selects the file he wants to upload
	// this part takes the file name and breaks it up into words and auto-populates the tag field with these words
	var tagString = document.getElementById("file_input"+id).value; // get the file path
	
	if(tagString.match(/[a-z]{1}:\\/i)) {
		// if the file path is of the pattern "C:\\", then this is being uploaded from a Windows machine
		var tagArray = tagString.split("\\"); // so, explode with respect to backslashes
		tagString = tagArray[tagArray.length-1]; // and the file name will be the last element in the resulting array
		// these lines remove the file extension
		tagArray = tagString.split(".");
		tagString = tagArray[0];
	} else {
		// otherwise, explode with respect to forward slashes
		var tagArray = tagString.split("/");
		tagString = tagArray[tagArray.length-1];
		// these lines remove the file extension
		tagArray = tagString.split(".");
		tagString = tagArray[0];
	}
	
	// replace dashes and underscores with spaces
	tagString = tagString.replace(/\-/g, " ");
	tagString = tagString.replace(/_/g, " ");
	
	// and populate the tag field with the result
	document.getElementById("tags"+id).value = tagString.toLowerCase();
	
	// this part gives focus to the name feild and auto-fills it with the filename and selects it for easy deleting
	document.forms['files'].elements['name'+id].focus();
	document.forms['files'].elements['name'+id].value = tagString;
	document.forms['files'].elements['name'+id].select();
}

function privateAlert() { // a friendly message to the user who clicked the "keep private" link
	alert("Keeping an upload \"private\" means that it will be hidden from the public eye. That means that no one can search for it and it will not appear in the \"Last Ten Uploads\" sections.");
}
