function RenderJobAttachments( id ) {
	var o = $("#"+id);
	var datas = o.data( "datas" );
	var files = datas["files"];
	var i = 0;
	for (i in files) {
		var file = files[i];
		AddJobAttachment( id , file , datas.bReadOnly );
	}
}

function AddJobAttachment( id , file , ro ) {
	var o = $("#"+id);
	
	var dattach = $("<div></div>").addClass( "JobAttachment" ).data( "parent" , o );
	var sid = id + "_attach_" + o.data("numoffiles" );
	dattach.attr( "id" , sid );
	
	// title
	var dtitle = $("<div></div>").addClass( "ttl" );
	dtitle.html( "#" + (Math.floor(o.data( "numoffiles" ))+1) );
	dattach.append( dtitle );

	// upload
	var dupl = $("<div></div>").addClass( "upl");
	var a = $("<a></a>").attr( "href" , file.www_path ).attr( "target" , "attachment" ).html( file.filename );
	dupl.append( a );
	dattach.append( dupl );

	// comment
	var dcomment = $("<div></div>").addClass( "comment" );
	var dcomm = $("<div></div>").html( file.comment );

	dcomment.append( dcomm );
	dattach.append( dcomment );

	if (!ro) {
		// del
		var ddel = $("<div></div>").addClass( "del" );
		var inp = $("<input></input>").attr("type","button").attr("id",id+"_del_"+o.data("numoffiles") ).val(aJSLangDefs.Del_attachment).addClass( "minismb" );
		inp.click( function() { DeleteJobAttachment( id , $(this) ); } );
		ddel.append( inp );

		dattach.append( ddel );
	}

	dattach.append( $("<div></div>").addClass("clr")  );
	
	o.append( dattach );
	o.data( "numoffiles" , o.data( "numoffiles" ) + 1 );
}

function InitJobAttachments( id , datas ) {
	var o = $("#"+id);
	if (!o.size()) return;
	o.data( "datas" , datas );
	o.data( "id" , id );
	o.data( "numoffiles" , 0 );
	RenderJobAttachments( id );
	// AddEmptyJobAttachment( id );
}

function MoveJA( id , postfix , num ) {
	var next = num + 1;
	$("#"+id+postfix+next).attr( "id" , id + postfix + num );
	if (typeof $("#"+id+postfix+next).attr("name") )
		$("#"+id+postfix+next).attr( "name" , id + postfix + num );
}

function DeleteJobAttachment( id , btn ) {
	var o = $("#"+id);
	var num = Math.floor( btn.attr("id").replace( o.data( "id" ) + "_del_" , "" ) );
	$.ajax(
		{
			type: "POST",
			cache: false,
			data: (
				{
					"inajax" : 1,
					"ajaxevent" : "delete_attachment",
					"jaid" : id,
					"num" : num
				}
			),
			success: function(ret) {
			}
		}
	);

	var f = $("#"+id+"_attach_"+num);
	f.remove();
	while (num<o.data( "numoffiles" )-1) {
		MoveJA( id , "_attach_" , num );
		MoveJA( id , "_file_" , num );
		MoveJA( id , "_comment_" , num );
		MoveJA( id , "_del_" , num );
		var dnum = id+"_attach_"+num;
		$("#"+dnum+" div.ttl").html( "#" + Math.floor(num+1) );
		num++;
	}
	o.data( "numoffiles" , o.data( "numoffiles" ) - 1 );
}

function AddEmptyJobAttachment( id ) {
	var o = $("#"+id);
	if (o.data( "numoffiles" )>=5) {
		alert( aJSLangDefs.Too_much_attachments );
		return;
	}
	var dattach = $("<div></div>").addClass( "JobAttachment" ).data( "parent" , o );
	var sid = id + "_attach_" + o.data("numoffiles" );
	dattach.attr( "id" , sid );
	dattach.data( "num" , o.data("numoffiles" ) );
	
	// title
	var dtitle = $("<div></div>").addClass( "ttl" );
	dtitle.html( "#" + (Math.floor(o.data( "numoffiles" ))+1) );
	dattach.append( dtitle );

	// upload
	var dupl = $("<div></div>").addClass( "upl");
	var inp = $("<input></input>").attr( "type" , "file");
	inp.attr( "name" , id + "_file_" + o.data("numoffiles") );
	inp.attr( "id" , id + "_file_" + o.data("numoffiles") );
	dupl.append( inp );
	dattach.append( dupl );

	// comment
	var dcomment = $("<div></div>").addClass( "comment" );
	var inp = $("<input></input>").attr( "type" , "text" );
	inp.attr( "name" , id + "_comment_" + o.data("numoffiles") );
	inp.attr( "id" , id + "_comment_" + o.data("numoffiles" ) );

	dcomment.append( inp );
	dattach.append( dcomment );

	// add
	var ddel = $("<div></div>").addClass( "del" );
	var inp = $("<input></input>").attr("type","button").attr("id",id+"_del_"+o.data("numoffiles") ).val(aJSLangDefs.Del_attachment).addClass( "minismb" );
	inp.click( function() { DeleteJobAttachment( id , $(this) ); } );
	ddel.append( inp );

	dattach.append( ddel );
	dattach.append( $("<div></div>").addClass("clr")  );

	o.append( dattach );
	o.data( "numoffiles" , o.data( "numoffiles" ) + 1 );
}
