var InputFile = Class.create();
InputFile.prototype = {
	initialize: function(input_file)
	{
		this.input_file = input_file;
		this.draw();
	},
	draw: function()
	{
		this.parent_all = this.input_file.parentNode
		this.my_form = document.createElement("FORM");
		this.my_form.setAttribute("method","post");
		this.my_form.setAttribute("action","/index.php?section=upload&screen=add_picture&layout=ajax&ajax=1");
		this.my_form.encoding = "multipart/form-data";
		this.parent_all.appendChild(this.my_form);

		this.max_size = document.createElement("INPUT");
		this.max_size.setAttribute("type","hidden");
		this.max_size.setAttribute("name","MAX_FILE_SIZE");
		this.max_size.setAttribute("value","2097152");
		this.my_form.appendChild(this.max_size);
		
		this.my_form.appendChild(this.input_file);
	
		this.fake_div = document.createElement("DIV");
		this.fake_div.className="fake_div";
		this.my_form.appendChild(this.fake_div);

		Event.observe(this.input_file, 'change', function(){
			if(this.fake_div.lastChild) this.fake_div.removeChild(this.fake_div.lastChild);
			this.my_text = document.createTextNode(this.input_file.value.substr(this.input_file.value.lastIndexOf("\\")+1));
			this.fake_div.appendChild(this.my_text);
			this.send_file();
			activ_inputs--;
			if(activ_inputs=="0")
			{
				this.add_input;
				activ_inputs++;
				total_inputs++;
				this.add_new_file();
			}
		}.bind(this), false);
	},
	send_file: function()
	{
		AIM.submit(this.my_form, {
			'onStart': function() {
				this.se_incarca = document.createElement("SPAN");
				this.se_incarca.style.color = "red";
				this.se_incarca.appendChild(document.createTextNode(" - se incarca"));
				this.fake_div.appendChild(this.se_incarca);
				status_incarcare = 1;
			}.bind(this),
			'onComplete': function(response) {
				json_response=response.evalJSON();
				while(this.parent_all.lastChild)
				{
					this.parent_all.removeChild(this.parent_all.lastChild);
				}
				this.resp_div = document.createElement("DIV");
				this.resp_div.className="resp";
				this.parent_all.appendChild(this.resp_div);
				if(json_response.error=="0")
				{
					this.resp_div.appendChild(document.createTextNode(json_response.name+" ("+json_response.size+")"));
					this.del_link = document.createElement("SPAN");
					this.del_link.className = "del_link";
					this.del_link.appendChild(document.createTextNode(" Sterge "));
					this.resp_div.appendChild(this.del_link);
					
					this.inp_hidden = document.createElement("INPUT");
					this.inp_hidden.setAttribute("type","hidden");
					this.inp_hidden.setAttribute("name","ul_file[]");
					this.inp_hidden.setAttribute("value",json_response.file);					
					$("my_form").appendChild(this.inp_hidden);
					
					Event.observe(this.del_link, 'click', function(){
						$("my_form").removeChild(this.inp_hidden);
						this.resp_div.removeChild(this.del_link);
						this.resp_div.className="resp strike";
					}.bind(this), false);

				}
				else this.resp_div.appendChild(document.createTextNode(json_response.error));
				status_incarcare = 0;
			}.bind(this)
		})
		this.my_form.submit();
	},
	add_new_file: function()
	{
		desp = document.createElement("DIV");
		desp.className = "desp_m";
		$("files").appendChild(desp);
		
		label_part = document.createElement("DIV");
		label_part.className = "label_part padded";
		label_part.appendChild(document.createTextNode(total_inputs+'. '));
		$("files").appendChild(label_part);
		
		input_part = document.createElement("DIV");
		input_part.className = "input_part";
		$("files").appendChild(input_part);
		
		input = document.createElement("INPUT");
		input.className = "type_file";
		input.setAttribute("type","file");
		input.setAttribute("name","poza[]");
		input_part.appendChild(input);
		
		new InputFile(input);
	}
}
var SearchInputFiles = Class.create();
SearchInputFiles.prototype = {
	initialize: function()
	{
		inputs = new Array();
		inputs = $$(".type_file");
		this.input_files = new Array();
		for(i=0;i<inputs.length;i++)
		{
			this.input_files[i] = new InputFile(inputs[i]);
		}
	}
}

