function CUploadImage( write_id, json ) {
	//}
	var thisO = this;
	this._wi = "ui_"+write_id;
	eval("DDS."+this._wi+" = this;");
	this._json = json;
	
	this._name = write_id;
	this._id = write_id;
	this._oid = write_id;
	
	this._a = 'none'; // action
	this._i = 0; // image id
	this._s = ''; // image src
	this._c = null; // callback
	this._preselect;
	
	this._o = (arguments.length > 2) ? arguments[2] : {}; // options
	if( typeof(this._o) == "object" ) {
		for( var opt in this._o ) {
			v = this._o[opt];
			if( opt == 'id' && typeof(v) == "string" ) this._id = v;
			if( opt == 'name' && typeof(v) == "string" ) this._name = v;
			if( opt == 'callback' && typeof(v) == "function" ) this._c = v;
			if( opt == 'preselect' && typeof(v) == "number" ) this._preselect = v;
		}
	}
	
	this._ai = []; //arr_image
	this._ai[0] = {id:0,name:"",src:"",category:"",width:0,height:0};
	this._ac = []; //arr_category
	this._aci = []; //arr_category_image
	this._oc; // currently Open Category
	this._original_c; // category of upload;
	
	this._to; // timeout
	
	this.click = function( what ) {
		switch( what ) {
			case 'img':
				this._a = 'img';
				this.ifrCls();this.hide();
				this.sel(arguments[1]);
				break;
			case 'cat':
				this._a = 'none';
				this.sel(0);this.ifrCls();
				this.openCat(arguments[1]);
				break;
			case 'new':
				this._a = 'new';
				this.sel(0);this.hide();
				this.ifrOpn();
				break;
			case 'keep':
				this._a = 'none';
				this.sel('keep');this.hide();this.ifrCls();
				this.disp('Bild nicht &auml;ndern');
				break;
			case 'delete':
				this._a = 'none';
				this.sel('delete');this.hide();this.ifrCls();
				this.disp('Bild l&ouml;schen');
				break;
			case 'show':
				window.clearTimeout(thisO._to);
				$("#"+this._wi+" .ui_menu").show();
				break;
		}
	}
	
	this.over = function() {
		window.clearTimeout(this._to);
		$("#"+this._wi+" .ui_arrow").show();
	}
	
	this.out = function() {
		this._to = window.setTimeout(function() {thisO.hide();}, 800);
	}
	
	this.hide = function() {
		window.clearTimeout(this._to);
		$("#"+this._wi+" .ui_menu, #"+this._wi+" .ui_arrow").hide("fast");
	}
	
	this.sel = function(id) {
		if( this._i == id ) return;
		this._i = id;
		
		if( id == "keep" || id == "delete" ) {
			$("#"+this._wi+" input").val(this._i);
			return;
		}
		
		if( typeof(this._c) == "function" ) this._c(this._ai[this._i]);
		if( !this._i ) return;
		$("#"+this._wi+" input").val(this._i);
		this.disp(this._ai[this._i].name);
		this.prev(this._i);
	}
	
	this.disp = function( str ) {
		$("#"+this._wi+" .ui_text").html(str);
	}
	
	this.prev = function(id) {
		if( !isNaN(parseInt(id, 10)) ) $('#'+this._wi+' .ui_prev').html('<a href="prev.php?i='+id+'&w=500" target="_blank"><img src="prev.php?i='+id+'&w=50" width="50" alt="Loading..." /></a>');
	}
	
	this.up = function(img) {
		this.ifrCls();
		this._ai[img.id] = img;
		var cat = false;
		for( var c in this._ac ) if( this._ac[c].name == img.category ) cat = c;
		if( cat === false ) this._ac.push({name:img.category,num:1});
		else this._ac[cat].num++;
		for( var c in this._ac ) if( this._ac[c].name == img.category ) cat = c;
		if( typeof(this._aci[img.category]) == 'undefined' ) this._aci[img.category] = [];
		this._aci[img.category].push(img.id);
		this._oc = cat;
		this.write('#'+this._wi+'_rep');
		this.openCat(img.category);
		this.sel(img.id);
	}
	
	this.openCat = function(id) {
		if( this._oc == id ) return;
		if( typeof(this._aci[id]) == 'undefined' ) this.reqCat(id);
		else {
			$("#"+this._wi+"_catc_"+this._oc).hide();
			$("#"+this._wi+"_catc_"+id).html(this.lImg(id)).show();
			this._oc = id;
			if( this._aci[this._oc].length ) for( var i in this._aci[this._oc] ) this.addClick('img', this._aci[this._oc][i]);
		}
	}
	
	this.reqCat = function(category) {
		$.post("ajax.php",{c:'UploadImage',m:'getCategoryImage',category:category}, function(data) { thisO.resCat(data); }, 'json' );
	}
	
	this.resCat = function(data) {
		var id = data.option.category;
		var img = data.image;
		for( var i in img ) {
			this._ai[img[i].id] = img[i];
			if( typeof(this._aci[img[i].category]) == 'undefined' ) this._aci[img[i].category] = [];
			this._aci[img[i].category].push(img[i].id);
		}
		this.openCat(id);
	}
	
	this.lImg = function( cat ) {
		var r='';
		for( var i in this._aci[cat] ) {
			var id = this._aci[cat][i];
			var img = this._ai[id];
			var cnt = parseInt(i, 10) + 1;
			var name = img.name;
			if( name.length > 24 ) name = name.substring(0, 21)+"...";
			//r += '<a href="javascript:void(0);" id="'+this._wi+'_'+id+'" title="('+img.width+'x'+img.height+') '+img.name+'"><span>('+img.width+'x'+img.height+')</span>'+cnt+'. '+name+(name.length)+'</a>';
			r += '<a href="javascript:void(0);" id="'+this._wi+'_'+id+'" title="('+img.width+'x'+img.height+') '+img.name+'">'+cnt+'. '+name+'</a>';
		}
		return r;
	}
	
	this.ifrCls = function() {
		$("#"+this._wi+" .ui_upload_iframe").hide("fast");
	}
	
	this.ifrOpn = function() {
		$("#"+this._wi+" .ui_upload_iframe").show("fast");
	}
	
	this.write = function(rep) {
		var cat = this._ac;
		var str = '<div id="'+this._wi+'_rep"><div class="ui_prev"></div><div class="ui"><div class="ui_menu_rel"><div class="ui_menu">Option<a href="javascript:void(0);" class="ui_a_keep">Bild nicht &auml;ndern</a><a href="javascript:void(0);" class="ui_a_delete">Bild l&ouml;schen</a><a href="javascript:void(0);" class="ui_a_upload_iframe" >Neues Bild hochladen</a><hr />';
		for( var c in cat ) str += "<div id=\""+this._wi+"_cat_"+cat[c].name+"\" class=\"ui_cat\">Category '"+cat[c].name+"' ("+cat[c].num+")</div><div id=\""+this._wi+"_catc_"+cat[c].name+"\">"+(cat[c].name == this._oc ? this.lImg(cat[c].name) : "")+"</div>";
		str += '</div></div><div class="ui_arrow_rel"><div class="ui_arrow"></div></div><div class="ui_text">Bild w&auml;hlen</div></div><div class="ui_upload_iframe"><iframe style="width: 98%; height: 120px; border-width: 0px;" frameborder="0" src="upload_image_iframe.php?write_id='+this._wi+'&category='+this._original_c+'" ></iframe></div>';
		$(rep).replaceWith("<div id=\""+this._wi+"\"><input type=\"hidden\" value=\"keep\" name=\""+this._name+"\" id=\""+this._id+"\" />"+str+"</div></div>");
		$("#"+this._wi+" .ui").mouseover(function(){thisO.over();}).mouseout(function(){thisO.out();}).click(function(){thisO.click('show');});
		for( var i in this._ac ) this.addClick('cat', this._ac[i].name);
		if( this._aci[this._oc] && this._aci[this._oc].length ) for( var i in this._aci[this._oc] ) this.addClick('img', this._aci[this._oc][i]);
		$("#"+this._wi+" .ui_a_upload_iframe").click(function(){thisO.click('new');});
		$("#"+this._wi+" .ui_a_keep").click(function(){thisO.click('keep');});
		$("#"+this._wi+" .ui_a_delete").click(function(){thisO.click('delete');});
		if( typeof(this._c) == "function" ) $("#"+this._wi+" input").change(function() {alert(this.value);/*thisO._c(thisO._ai[this.value]);*/});
	}
	
	this.addClick = function(what, id) {
		if(what == 'img') $("#"+this._wi+"_"+id).click(function(){thisO.click('img', id);});
		if(what == 'cat') $("#"+this._wi+"_cat_"+id).click(function(){thisO.click('cat', id);});
	}
	
	this.parse = function() {
		//this._json = typeof(json) == 'string' ? eval("("+json+")") : json;
		this._oc = this._json.option.category;
		this._original_c = this._json.option.category;
		var img = this._json.image.image;
		for( var i in img ) {
			this._ai[img[i].id] = img[i];
			if( typeof(this._aci[img[i].category]) == 'undefined' ) this._aci[img[i].category] = [];
			this._aci[img[i].category].push(img[i].id);
		}
		var cat = this._json.category.category;
		for( var i in cat ) {
			this._ac.push({name:cat[i].name,num:cat[i].num});
		}
	}
	
	this.ajaxResponse = function( json ) {
		this._json = json;
		this.init();
	}
	
	this.init = function() {
		if( typeof(this._json) == 'string' ) {
			$.post(
				"ajax.php",
				{c:"UploadImage",m:"getJson",category:this._json},
				function(data){thisO.ajaxResponse(data);}, 
				'json'
			);
		} else {
			this.parse();
			this.write("#"+this._oid);
			if( this._preselect ) this.sel(this._preselect);
		}
	}
	
	this.init();
}
