/*
	File Collector
	==============
*/

function FileCollector(_instance, _name, _upload_root, _max, _default)
{			
	// Init
	this.instance = _instance;
	this.name = _name;
	this.upload_root = _upload_root;
	this.browser = true;

	// Variables
	this.files = new Array();
	this.max_files = _max;
	this.highlight = -1;
	
	// Upload
	if(_default != undefined && _default != null && _default != "")
	{
		do
		{
			if(_default.indexOf('\n') != -1)
			{
				p = _default.substr(0, _default.indexOf('\n'));
				_default = _default.substr(_default.indexOf('\n')+1);
			}
			else
			{
				p = _default;
				_default = "";
			}
			this.addFile(p);
		}
		while(_default != "");
	}
	
	// Refresh the list
	this.refreshList();
	
	// Init the browser
	this.first = true;
	this.toggleBrowser();
}
	
FileCollector.prototype.toggleBrowser = function()
{
	this.browser = !this.browser;
	i = document.getElementById(this.name + "_browse");
	
	if(this.browser)
	{
		i.className = "iframe-visible";

		if(this.first)
		{
			i.setAttribute("src", "?page=upload&instance=" + this.name + "_upload&onclick=" + this.instance + ".addFile&root=" + this.upload_root);
			this.first = false;
		}
	}
	else
	{
		i.className = "iframe-hidden";
	}
}

FileCollector.prototype.addFile = function(_path)
{
	ta = document.getElementById("ta_" + this.name);
	
	if(ta.value.indexOf(_path) != -1)
	{
		this.highlight = this.files.indexOf(_path);
		this.refreshList();
		return;
	}

	if(this.files.length >= this.max_files)
		return;
	
	ta.value = ta.value + _path + "\n";

	this.files[this.files.length] = _path;
	this.highlight = -1;
	
	this.refreshList();
}

FileCollector.prototype.removeFile = function(_id)
{
	ta = document.getElementById("ta_" + this.name);
	var path = this.files[_id];

	var temp = new Array();
	var a = 0;
	ta.value = "";
	for(i = 0; i < this.files.length; i++)
	{
		if(i != _id)
		{
			temp[a] = this.files[i];
			ta.value += this.files[i] + "\n";
			a++;
		}
	}
	this.files = temp; 
	
	this.refreshList();
}

FileCollector.prototype.refreshList = function()
{
	var html = "";
	
	if(this.files.length > 0)
	{
		html += "<table width='100%'>";
		for(i = 0; i < this.files.length; i++)
		{
			thumb = icon_dir+"/file/unknown.png";
			if(this.files[i].match(/(jpg|jpeg|png|gif|bmp)$/))
			{
				thumb = this.files[i];
				thumb = thumb.replace(/^(.*)\/(.*)$/, '$1/_thumb_128_$2');
				thumb = thumb.replace(/\.(jpg|jpeg|png|gif|bmp)$/, '.png');
				thumb = thumb.replace(/^\.\//, root_url+"/");
			}
			else if(this.files[i].match(/(xls|xlsx)$/))
				thumb = icon_dir+"/file/spreadsheet.png";
			else if(this.files[i].match(/(doc|docx)$/))
				thumb = icon_dir+"/file/document.png";
			else if(this.files[i].match(/(ppt|pptx)$/))
				thumb = icon_dir+"/file/presentation.png";
			else if(this.files[i].match(/(txt)$/))
				thumb = icon_dir+"/file/text.png";
			else if(this.files[i].match(/(mp3)$/))
				thumb = icon_dir+"/file/audio.png";
			else if(this.files[i].match(/(flv|mp4|avi)$/))
				thumb = icon_dir+"/file/video.png";
			else if(this.files[i].match(/(pdf|ps)$/))
				thumb = icon_dir+"/file/pdf.png";
			
			if(this.highlight == i)
				html += "<tr style='font-weight: bold;'><td align='center' width='128'><a href='" + this.files[i].replace(/^\.\//, root_url+"/") + "' target='_blank'><img src='" + thumb + "'></a></td><td align='left'><a href='" + this.files[i].replace(/^\.\//, root_url+"/") + "' target='_blank'>" + this.files[i] + "</a></td><td><a href='javascript:" + this.instance + ".removeFile(" + i + ")'><img src='" + icon_dir + "/small/close.png' border='0'></a></td></tr>";
			else
				html += "<tr><td align='center' width='128'><a href='" + this.files[i].replace(/^\.\//, root_url+"/") + "' target='_blank'><img src='" + thumb + "'></a></td><td align='left'><a href='" + this.files[i].replace(/^\.\//, root_url+"/") + "' target='_blank'>" + this.files[i] + "</a></td><td><a href='javascript:" + this.instance + ".removeFile(" + i + ")'><img src='" + icon_dir + "/small/close.png' border='0'></a></td></tr>";
		}
		html += "</table>";
	}
	else
		html += "-"
	
	d = document.getElementById(this.name + "_list");
	d.innerHTML = html;
}

