var mf_input=new mf_input_class(document.getElementById('file_list'),document.getElementById('mf_0'),5);

function mf_input_class(file_list,input_element,max) {

	this.cnt=0;
	this.max=(max?max:-1);
	this.max_id=0;
	this.file_list=file_list;
	this.input_element=input_element;

	this.input_element.mf_input=this;

	this.duplicate = function () {

		for (i=0;i<=this.max_id;i++) {

			if (document.getElementById('mf_'+i)!=null) {
				if ('mf_'+i!=this.input_element.id) {
					if (document.getElementById('mf_'+i).value==this.input_element.value) {
						return true;
					}
				}
			}
		}
		return false;
	}

	this.add_file = function() {

		if (this.cnt>=this.max) {
			var new_input=document.createElement('input');
			new_input.type='file';
			//new_input.name='mf_'+this.cnt;
//			new_input.name='mf_'+this.max_id;
			new_input.name='mf[]';
			//new_input.id='mf_'+this.cnt;
			new_input.id='mf_'+this.max_id;
			this.max_id++;
			new_input.className="mf";

			new_input.mf_input=this;

			new_input.onchange=function() {
				this.mf_input.add_file();
			}

			this.input_element.parentNode.replaceChild(new_input,this.input_element);
			alert('Na li¶cie jest już '+this.max+' plików. Żeby dodać inny, usuń który¶ z listy.');
		}
		else {
			if (!(new RegExp("\.(jpg|jpeg|gif|png|avi|mov|wmv|mp3|zip|rar|swf|pdf|txt|doc|ppt){1}$","i")).test(this.input_element.value)) {

				var new_input=document.createElement('input');
				new_input.type='file';
				//new_input.name='mf_'+this.cnt;
				new_input.name='mf[]';
				//new_input.id='mf_'+this.cnt;
				new_input.id='mf_'+this.max_id;
				this.max_id++;
				new_input.className="mf";

				new_input.mf_input=this;

				new_input.onchange=function() {
					this.mf_input.add_file();
				}

				this.input_element.parentNode.replaceChild(new_input,this.input_element);
				this.input_element=new_input;

				alert("Niepoprawny typ pliku.\nAkceptowane typy plików to: jpg, gif, png, avi, mov, wmv, mp3, zip, rar, swf, pdf, txt, doc, ppt.");
			}
			else if (this.duplicate()) {

				var new_input=document.createElement('input');
				new_input.type='file';
				//new_input.name='mf_'+this.cnt;
				new_input.name='mf[]';
				//new_input.id='mf_'+this.cnt;
				new_input.id='mf_'+this.max_id;
				this.max_id++;
				new_input.className="mf";

				new_input.mf_input=this;

				new_input.onchange=function() {
					this.mf_input.add_file();
				}

				this.input_element.parentNode.replaceChild(new_input,this.input_element);
				this.input_element=new_input;

				alert("Ten plik jest już na li¶cie.");
			}
			else {
				var new_input=document.createElement('input');
				new_input.type='file';

				++this.cnt;
				if (this.max_id==0) this.max_id++;
				//new_input.name='mf_'+(++this.cnt);
				new_input.name='mf[]';
				//new_input.id='mf_'+this.cnt;
				new_input.id='mf_'+this.max_id;
				this.max_id++;
				new_input.className="mf";

				new_input.mf_input=this;

				new_input.onchange=function() {
					this.mf_input.add_file();
				}

				this.update_file_list();

				this.input_element.style.position='absolute';
				this.input_element.style.left='-4000px';

				this.input_element_tmp=this.input_element.parentNode.insertBefore(new_input,this.input_element);
				this.input_element=this.input_element_tmp;


				if (this.cnt==0) {
					document.getElementById("comment_files_cnt").firstChild.nodeValue='Do komentarza możesz dodać max 5 plików.';
				}
				else if (this.cnt==this.max) {
					document.getElementById("comment_files_cnt").firstChild.nodeValue='Na li¶cie jest już '+this.max+' plików. Żeby dodać inny, usuń który¶ z listy.';
				}
				else {

					if (this.max-this.cnt==1) {
						file_str='plik';
					}
					else if (this.max-this.cnt>=2 && this.max-this.cnt<=4) {
						file_str='pliki';
					}
					else if (this.max-this.cnt>=5 && this.max-this.cnt<=19) {
						file_str='pliki';
					}

					document.getElementById("comment_files_cnt").firstChild.nodeValue='Możesz dodać jeszcze '+(this.max-this.cnt)+' '+file_str+'.';

				}
				if (this.cnt==this.max) {
					this.input_element.disabled=true;
				}
			}
		}
	}

	this.update_file_list = function() {

		var row=document.createElement('div');
//		var delete_button=document.createElement('input');
		var delete_button=document.createElement('a');
		var filename=document.createElement('p');

		var filename_array=new Array();

		var str_max=35;

//		delete_button.type='button';
		delete_button.href='#';
//		delete_button.value='Usuń';
		delete_button.innerHTML='[Usuń]';

		delete_button.onclick = function() {
			this.parentNode.e.parentNode.removeChild(this.parentNode.e);
			this.parentNode.parentNode.removeChild(this.parentNode);

			this.parentNode.e.mf_input.cnt--;

			if (this.parentNode.e.mf_input.cnt==0) {
				if (this.parentNode.e.mf_input.max==1) {
					file_str='plik';
				}
				else if (this.parentNode.e.mf_input.max>=2 && this.parentNode.e.mf_input.max<=4) {
					file_str='pliki';
				}
				else if (this.parentNode.e.mf_input.max>=5 && this.parentNode.e.mf_input.max<=19) {
					file_str='pliki';
				}

				document.getElementById("comment_files_cnt").firstChild.nodeValue='Do komentarza możesz dodać max 5 plików.';
			}
			else if (this.parentNode.e.mf_input.cnt==this.parentNode.e.mf_input.max) {
				document.getElementById("comment_files_cnt").firstChild.nodeValue='Na li¶cie jest już '+this.parentNode.e.mf_input.max+' plików. Żeby dodać inny, usuń który¶ z listy.';
			}
			else {
				if (this.parentNode.e.mf_input.max-this.parentNode.e.mf_input.cnt==1) {
					file_str='plik';
				}
				else if (this.parentNode.e.mf_input.max-this.parentNode.e.mf_input.cnt>=2 && this.parentNode.e.mf_input.max-this.parentNode.e.mf_input.cnt<=4) {
					file_str='pliki';
				}
				else if (this.parentNode.e.mf_input.max-this.parentNode.e.mf_input.cnt>=5 && this.parentNode.e.mf_input.max-this.parentNode.e.mf_input.cnt<=19) {
					file_str='pliki';
				}

				document.getElementById("comment_files_cnt").firstChild.nodeValue='Możesz dodać jeszcze '+(this.parentNode.e.mf_input.max-this.parentNode.e.mf_input.cnt)+' '+file_str+'.';
			}
			if (this.parentNode.e.mf_input.cnt<this.parentNode.e.mf_input.max) {
				this.parentNode.e.mf_input.input_element.disabled=false;
			}
			return false;
		}

		row.e=this.input_element;
		row.id=this.input_element.id+'_row';

		filename_array=this.input_element.value.split(new RegExp('/'));
		if (filename_array.length==1) {
			filename_array=this.input_element.value.split(new RegExp("\\\\"));
		}

		if (filename_array[filename_array.length-1].length<str_max) {
			filename.appendChild(document.createTextNode(filename_array[filename_array.length-1]+' '));
		}
		else {
			filename.appendChild(document.createTextNode('...'+
				filename_array[filename_array.length-1].
					substring(
						(filename_array[filename_array.length-1].length-str_max),
						filename_array[filename_array.length-1].length
						)+' '));
		}

		row.appendChild(filename);
		row.appendChild(delete_button);

		this.file_list.appendChild(row);
	}
}
