/*
	Fájlgyűjtő
	==========
*/

function FileCollector(_instance, _name, _upload_root, _max, _default)
{			
	// Init
	this.instance = _instance;
	this.name = _name;
	this.upload_root = _upload_root;
	this.browser = true;

	// Változók
	this.files = new Array();
	this.max_files = _max;
	this.highlight = -1;
	
	// Feltöltés
	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 != "");
	}
	
	// Lista frissítése
	this.refreshList();
	
	// Tallózó inicializálása
	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(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(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 += "<ul>";
		for(i = 0; i < this.files.length; i++)
		{
			if(this.highlight == i)
				html += "<li><b><a href='" + this.files[i] + "' target='_blank'>" + this.files[i] + "</a></b> <a href='javascript:" + this.instance + ".removeFile(" + i + ")'><img src='./themes/" + theme_name + "/images/icons/small/close.png' border='0'></a></li>";
			else
				html += "<li><a href='" + this.files[i] + "' target='_blank'>" + this.files[i] + "</a> <a href='javascript:" + this.instance + ".removeFile(" + i + ")'><img src='./themes/" + theme_name + "/images/icons/small/close.png' border='0'></a></li>";
		}
		html += "</ul>";
	}
	else
		html += "-"
	
	d = document.getElementById(this.name + "_list");
	d.innerHTML = html;
}

