﻿/********************************************** 
 * TiExplorer plugin.
 * This plugin allow you to explore your drive.
 * Require : Mootools 1.2.2 & Titanium PR3
 * Author : Albin POIGNOT
 * Version : 1.0
 * Licence : Creative Commons BY-NC-SA
 **********************************************/

// Set, for the first time, the userDirectory for the path to explore
var directory = Titanium.Filesystem.getUserDirectory();


/* **************************************************************
 * - Function : createExplorer()								*
 * - Description : Create the UI for the explorer				*
 * - Arguments : None											*
 * - Returns : None												*
 ****************************************************************/
function createExplorer() {
	
	// A div and a table to host the explorer
	var explorerDiv = new Element('div', {'id':'explorer'});
	var explorerTable = new Element('table', {'id':'explorerTable'});
	
	// We add the div in the container and we set its position
	$$('body').grab(explorerDiv);
	$('explorer').setStyles({
								'float':'right',
								'position':'absolute',
								'top':'240px',
								'left':'650px'
							});

	// We add the path containers and the current path
	$('explorer').grab(new Element('p',{'id':'pathPara', 'html':'Path : '}));
	$('pathPara').grab(new Element('span', {'id':'path', 'html':directory}));
	$('path').set('html', $('path').get('html').replace('\\\\','\\'));
	
	// We add the table who host data
	$('explorer').grab(explorerTable);
	
	// Finally, we launch the exploration
	explore();
	
}
 
 
/* **************************************************************
 * - Function : explore()										*
 * - Description : Explorer the 'directory' path variable		*
 * - Arguments : None											*
 * - Returns : None												*
 ****************************************************************/
 function explore() {

	// List the file and directories in the current directory
	currentDirectory = directory.getDirectoryListing();
	
	// We add the link to the "parent directory"
	$('explorerTable').grab(new Element('tr'));
	$('explorerTable').getLast('tr').grab(new Element('td', {'id':'parent','html': '...'}));
	
	// Exploration of the objects array
	for(var i = 0; i < currentDirectory.length; i++) {

		var currentFileObject = currentDirectory[i];
		
		$('explorerTable').grab(new Element('tr'));
		
		var currentRow = $('explorerTable').getLast('tr');
		currentRow.grab(new Element('td'));
		
		var currentLine = currentRow.getLast('td');
		
		currentLine.appendText(currentFileObject.name());
		currentLine.set('id', currentFileObject.name());
		
		// The difference between directory and file
		if (currentDirectory[i].isDirectory() == true) {
			
			currentLine.set('class', 'directory');
			
		} else {
		
			currentLine.set('class', 'file');
		
		}	
	}
	
	setEvents();
	
}

/* **************************************************************
 * - Function : setEvents()										*
 * - Description : Set the events for each file object returned *
 * 				   by the explore() function					*
 * - Arguments : None											*
 * - Returns : None												*
 ****************************************************************/
function setEvents() {

	// If we click on a directory, we'll update the directory to explore and explore it
	$$('.directory').addEvent('click', function() {
		
		directory = Titanium.Filesystem.getFile(directory + '\\' + this.get('id'));
		$('explorerTable').empty();
		
		$('path').set('html', directory);
		$('path').set('html', $('path').get('html').replace('\\\\','\\'));
		
		explore();
		
		/* 
		 * **********************
		 * ADD SOME STUFF HERE !!! 
		 * **********************
		 */
		
	});
	
	// We get the parent directory and explore it
	$('parent').addEvent('click', function() {
	
		directory = directory.parent();
		$('explorerTable').empty();
		
		$('path').set('html', directory);
		$('path').set('html', $('path').get('html').replace('\\\\','\\'));
		
		explore();
		
		/* 
		 * **********************
		 * ADD SOME STUFF HERE !!! 
		 * **********************
		 */
		
	});
	
	// For a click in a file, we update the "selectedFile" field
	$$('.file').addEvent('click', function() {
		
		/* 
		 * **********************
		 * ADD SOME STUFF HERE !!! 
		 * **********************
		 */
		 
	});

}