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=particular&screen=add_picture");
		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()
	{
		input_item = document.createElement("DIV");
		input_item.className="file_item";
		
		$("files").appendChild(input_item);
		
		label_part = document.createElement("DIV");
		label_part.className = "label_part padded";
		label_part.appendChild(document.createTextNode("Poza "+total_inputs));
		input_item.appendChild(label_part);
		
		input_part = document.createElement("DIV");
		input_part.className = "input_part";
		input_item.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]);
		}
	}
}
AIM = {

    frame : function(c) {

        var n = 'f' + Math.floor(Math.random() * 99999);
        var d = document.createElement('DIV');
        d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
        document.body.appendChild(d);

        var i = document.getElementById(n);
        if (c && typeof(c.onComplete) == 'function') {
            i.onComplete = c.onComplete;
        }

        return n;
    },

    form : function(f, name) {
        f.setAttribute('target', name);
    },

    submit : function(f, c) {
        AIM.form(f, AIM.frame(c));
        if (c && typeof(c.onStart) == 'function') {
            return c.onStart();
        } else {
            return true;
        }
    },

    loaded : function(id) {
        var i = document.getElementById(id);
        if (i.contentDocument) {
            var d = i.contentDocument;
        } else if (i.contentWindow) {
            var d = i.contentWindow.document;
        } else {
            var d = window.frames[id].document;
        }
        if (d.location.href == "about:blank") {
            return;
        }

        if (typeof(i.onComplete) == 'function') {
            i.onComplete(d.body.innerHTML);
        }
    }
}
