function Jed(containerid, settings) {
	this.container = $j('#'+containerid);
	this.containerid = containerid;
	this.highestId = 0;
	this.numberOfItems = 0;
	this.settings = settings;
	
	if (!settings.unsortable) {
	
		this.container.sortable({
			axis: 'y',
			cursor: 'move',
			containment: 'parent',
			opacity: 1.0,
			cancel: ':input, button',
			placeholder: 'placeholder',
			forcePlaceholderSize: true,
			handle: '.handle, img',
			revert: true,
			tolerance: 'pointer'
		});
	
	}
	
  this.updateButtons();
	
}
Jed.prototype.updateButtons = function () {
	
	// Update state of "addPart" links
	if (this.settings.maxNumberOfItems && this.numberOfItems>=this.settings.maxNumberOfItems) 
				$j('.jed_addpart_button').addClass('disabled');
	else  $j('.jed_addpart_button').removeClass('disabled');

	if (this.numberOfItems>0) 
				$j('.jed_active').show();
	else  $j('.jed_active').hide();

}
Jed.prototype.addPart = function (kind, content) {

//	alert("jed addPart called.");

	this.numberOfItems++;
	this.updateButtons();

	if (this.settings.maxNumberOfItems && this.numberOfItems>this.settings.maxNumberOfItems) {
		
		this.numberOfItems = this.settings.maxNumberOfItems
		alert(this.settings.maxNumberOfItemsAlert);
		
	} else {

		
	
	id = (this.highestId+1);
	this.highestId = id;
	
	
	
	if (kind=='image' && content[0]) kind = 'imageb';
	if (kind=='file'  && content[0]) kind = 'fileb';
	if (kind=='userimage' && content[1]) kind = 'userimageb';

	var thishtml = html[kind];
	thishtml = thishtml.replace(/%additional%/g, content[1]);
	thishtml = thishtml.replace(/%id%/g, id);
	thishtml = thishtml.replace(/%html%/g, content[0]);

	if (content[4]) thishtml = thishtml.replace(/%handle%/g, content[4]);
	else thishtml = thishtml.replace(/%handle%/g, handle[kind]);
	
	$j('#'+this.containerid).append(thishtml);
	

	
	$j('#'+this.containerid+' #jed_'+id+'_html.autogrow').autogrow();		

	if (kind=='image' || kind=='imageb') {

		if (this.settings.noimagecontrols) {
			$j('#'+this.containerid+' #jed_div_'+id+' .imagecontrols').hide();
		}

		if (content[2]) {
			if      (content[2]=='left')   $j('#'+this.containerid+' #jed_'+id+'_float').val("left");
			else if (content[2]=='right')  $j('#'+this.containerid+' #jed_'+id+'_float').val("right");
			else if (content[2]=='inline') $j('#'+this.containerid+' #jed_'+id+'_float').val("inline");
		}
		
		var widthselector = $j('#'+this.containerid+' #jed_'+id+'_width');
		
		if (content[3]) {
			var newval = content[3];
			
			// A size has been set for the image:
			widthselector.val(newval);

			// The provided value may not be one of the choices in the menu. If so, we set the value of the last option ("Custom...") to the provided value.
			if (widthselector.val()!=newval) {
				$j('option:last', widthselector)
							.attr("value", newval)
							.text("Anden størrelse ("+newval+" px)");
				widthselector.val(newval);
			}
		}
		
		widthselector.bind('change', function(){
			//alert($j(this).val());
			if ($j("option:last", this).val()==$j(this).val()) {
				var newval = parseInt(prompt("Hvor bred ønsker du, at billedet skal være? Bredden skal angives i pixels. Bredden på hele websiden er 820 pixels bred, hvis du vil have noget at sammenligne med.", ""));
				if (newval!=null && newval>0) {
					$j('option:last', this)
								.attr("value", newval)
								.text("Anden størrelse ("+newval+" px)");
					$j(this).val(newval);
				} else {
					return false;
				}
			}
		});

	} else if (kind=='userimage' || kind=='userimageb') {
		
	} else if (kind=='userimagec') {
		
		fillUserimageSelect(id);

		$j('select#jed_select_'+id).bind('change', function(){
      changePicture(this.value, id, $j(":selected", this).attr("url"), $j(":selected", this).attr("tname"));
    });


	} else if (kind=='rich') {
		
		area1 = new nicEditor({
			fullPanel : false,
			iconsPath : 'gfx/admin/nicEditorIcons.gif',
			buttonList: ['bold', 'italic', 'link', 'unlink','left','center','right', 'ol', 'ul', 'indent', 'outdent']
			
		}).panelInstance('jed_'+id+'_html');
		
	
	
	}
	}
}
Jed.prototype.removePart = function (id) {
	this.numberOfItems--;
	$j('#'+this.containerid+' #jed_div_'+id).remove();
	this.updateButtons();
}
Jed.prototype.removeImagebPart = function (id) {
	if ($j('#'+this.containerid+' #jed_div_'+id+' #item_delete_'+id).val()=='1') {
		this.numberOfItems++;
		$j('#'+this.containerid+' #jed_div_'+id+' #item_delete_'+id).val('0');
		$j('#'+this.containerid+' #jed_div_'+id+' .data .file').css('opacity', '1.0');
		$j('#'+this.containerid+' #jed_div_'+id+' .data textarea').show();
		$j('#'+this.containerid+' #jed_div_'+id+' .data .deletemessage').hide();
	} else {
		this.numberOfItems--;
		$j('#'+this.containerid+' #jed_div_'+id+' #item_delete_'+id).val('1');
		if ($j('#'+this.containerid+' #jed_'+id+'_kind').val()=='imageb' || $j('#'+this.containerid+' #jed_'+id+'_kind').val()=='userimageb') $j('#'+this.containerid+' #jed_div_'+id+' .data .file').css('opacity', '0.1');
		else $j('#'+this.containerid+' #jed_div_'+id+' .data .file').css('opacity', '0.5');
		$j('#'+this.containerid+' #jed_div_'+id+' .data textarea').hide();
		$j('#'+this.containerid+' #jed_div_'+id+' .data .deletemessage').show();
	}
	this.updateButtons();
}





var handle = [];
var html = [];

handle['html'] = 'HTML-kode';
html['html'] = '<div class="element" id="jed_div_%id%"><div class="handle">%handle%<input type="hidden" name="jed_sort[]" id="jed_sort_%id%" value="%id%"><input type="hidden" name="jed_%id%_kind" id="jed_%id%_kind" value="html"></div><div class="data"><textarea name="jed_%id%_html" id="jed_%id%_html" class="textarea html autogrow">%html%</textarea></div><div class="remover"><a href="javascript:jed.removePart(%id%)"></a></div><div class="clearer"></div></div>';

handle['text'] = 'Tekst';
html['text'] = '<div class="element text" id="jed_div_%id%"><div class="handle">%handle%<input type="hidden" name="jed_sort[]" id="jed_sort_%id%" value="%id%"><input type="hidden" name="jed_%id%_kind" id="jed_%id%_kind" value="text"></div><div class="data"><textarea name="jed_%id%_html" id="jed_%id%_html" class="textarea autogrow">%html%</textarea></div><div class="remover"><a href="javascript:jed.removePart(%id%)"></a></div><div class="clearer"></div></div>';

handle['image'] = 'Billede';
html['image'] = '<div class="element image" id="jed_div_%id%"><div class="handle">%handle%<input type="hidden" name="jed_sort[]" id="jed_sort_%id%" value="%id%"><input type="hidden" name="jed_%id%_kind" id="jed_%id%_kind" value="image"></div><div class="data"><input type="file" name="files[]" id="files[]" class="file"><div class="imagecontrols"><div><label for="jed_%id%_width">St&oslash;rrelse:</label><select name="jed_%id%_width" id="jed_%id%_width"><option value="0">Fuld størrelse</option><option disabled>&nbsp;</option><option value="820">Hele sidens bredde (820 px)</option><option value="540">Hel kolonne (540 px)</option><option value="440">Tekst-kolonne (440 px)</option><option value="340">3/4 kolonne (340 px)</option><option value="220">Halv kolonne (220 px)</option><option value="160">Lille billede (160 px)</option><option value="100">Meget lille billede (100 px)</option><option disabled>&nbsp;</option><option value="" class="custom">Anden st&oslash;rrelse&hellip;</option></select></div><label for="jed_%id%_float">Placering:</label><select name="jed_%id%_float" id="jed_%id%_float"><option value="none">P&aring; en linje for sig selv</option><option value="left">Til venstre for teksten</option><option value="right">Til h&oslash;jre for teksten</option><option value="inline">P&aring; linje med andre billeder</option></select></div></div><div class="remover"><a href="javascript:jed.removePart(%id%)"></a></div><div class="clearer"></div></div>';

handle['imageb'] = 'Billede';
html['imageb'] = '<div class="element image imageb" id="jed_div_%id%"><div class="handle">%handle%<input type="hidden" name="jed_sort[]" id="jed_sort_%id%" value="%id%"><input type="hidden" name="jed_%id%_kind" id="jed_%id%_kind" value="imageb"></div><div class="data"><div class="deletemessage">Billedet vil blive slettet n&aring;r du gemmer dine &aelig;ndringer. (<a href="javascript:jed.removeImagebPart(%id%)">Annuller</a>)</div>%additional%<div class="imagecontrols"><div><label for="jed_%id%_width">St&oslash;rrelse:</label><select name="jed_%id%_width" id="jed_%id%_width"><option value="0">Fuld størrelse</option><option disabled>&nbsp;</option><option value="820">Hele sidens bredde (820 px)</option><option value="540">Hel kolonne (540 px)</option><option value="440">Tekst-kolonne (440 px)</option><option value="340">3/4 kolonne (340 px)</option><option value="220">Halv kolonne (220 px)</option><option value="160">Lille billede (160 px)</option><option value="100">Meget lille billede (100 px)</option><option disabled>&nbsp;</option><option value="" class="custom">Anden st&oslash;rrelse&hellip;</option></select></div><label for="jed_%id%_float">Placering:</label><select name="jed_%id%_float" id="jed_%id%_float"><option value="none">P&aring; en linje for sig selv</option><option value="left">Til venstre for teksten</option><option value="right">Til h&oslash;jre for teksten</option><option value="inline">P&aring; linje med andre billeder</option></select></div></div><div class="remover"><a href="javascript:jed.removeImagebPart(%id%)"></a></div><div class="clearer"></div></div>';

handle['userimage'] = 'Billede';
html['userimage'] = '<div class="element image userimage" id="jed_div_%id%"><div class="handle">%handle%<input type="hidden" name="jed_sort[]" id="jed_sort_%id%" value="%id%"><input type="hidden" name="jed_%id%_kind" id="jed_%id%_kind" value="userimage"></div><div class="data"><input type="file" name="files[]" id="files[]" class="file"><textarea name="jed_%id%_html" id="jed_%id%_html" class="textarea autogrow labeled billedetekst" title="Evt. billedetekst" placeholder="Evt. billedetekst">%html%</textarea></div><div class="remover"><a href="javascript:jed.removePart(%id%)"></a></div><div class="clearer"></div></div>';

handle['userimageb'] = 'Billede';
html['userimageb'] = '<div class="element image imageb userimage" id="jed_div_%id%"><div class="handle">%handle%<input type="hidden" name="jed_sort[]" id="jed_sort_%id%" value="%id%"><input type="hidden" name="jed_%id%_kind" id="jed_%id%_kind" value="userimageb"></div><div class="data"><div class="deletemessage">Billedet vil blive slettet n&aring;r du trykker "Gem &aelig;ndringer" nederst p&aring; siden. (<a href="javascript:jed.removeImagebPart(%id%)">Annuller</a>)</div><textarea name="jed_%id%_html" id="jed_%id%_html" class="textarea autogrow labeled billedetekst" title="Evt. billedetekst" placeholder="Evt. billedetekst">%html%</textarea>%additional%<div class="clearer"></div></div><div class="remover"><a href="javascript:jed.removeImagebPart(%id%)"></a></div><div class="clearer"></div></div>';

handle['userimagec'] = 'Billede';
html['userimagec'] = '<div class="element image imageb userimage" id="jed_div_%id%"><div class="handle">%handle%<input type="hidden" name="jed_sort[]" id="jed_sort_%id%" value="%id%"><input type="hidden" name="jed_%id%_kind" id="jed_%id%_kind" value="userimagec"></div><div class="data"><select name="jed_select_%id%" id="jed_select_%id%" class="list_of_user_pictures"></select><textarea name="jed_%id%_html" id="jed_%id%_html" class="textarea autogrow labeled billedetekst" title="Evt. billedetekst" placeholder="Evt. billedetekst">%html%</textarea><img class="file" id="jed_preview_%id%"></div><div class="remover"><a href="javascript:jed.removePart(%id%)"></a></div><div class="clearer"></div></div>';



handle['file'] = 'Fil';
html['file'] = '<div class="element file" id="jed_div_%id%"><div class="handle">%handle%<input type="hidden" name="jed_sort[]" id="jed_sort_%id%" value="%id%"><input type="hidden" name="jed_%id%_kind" id="jed_%id%_kind" value="file"></div><div class="data"><input type="file" name="files[]" id="files[]" class="file"><div class="description">Filen vil ikke blive vist automatisk, men du kan linke til den i et Tekst- eller HTML-element.</div></div><div class="remover"><a href="javascript:jed.removePart(%id%)"></a></div><div class="clearer"></div></div>';

handle['fileb'] = 'Fil';
html['fileb'] = '<div class="element file" id="jed_div_%id%"><div class="handle">%handle%<input type="hidden" name="jed_sort[]" id="jed_sort_%id%" value="%id%"><input type="hidden" name="jed_%id%_kind" id="jed_%id%_kind" value="fileb"></div><div class="data">%additional%<div class="deletemessage">Filen vil blive slettet n&aring;r du gemmer dine &aelig;ndringer. (<a href="javascript:jed.removeImagebPart(%id%)">Annuller</a>)</div></div><div class="remover"><a href="javascript:jed.removeImagebPart(%id%)"></a></div><div class="clearer"></div></div>';

handle['rich'] = 'Tekst';
html['rich'] = '<div class="element text" id="jed_div_%id%"><div class="handle">%handle%<input type="hidden" name="jed_sort[]" id="jed_sort_%id%" value="%id%"><input type="hidden" name="jed_%id%_kind" id="jed_%id%_kind" value="rich"></div><div class="data"><textarea name="jed_%id%_html" id="jed_%id%_html" class="textarea">%html%</textarea></div><div class="remover"><a href="javascript:jed.removePart(%id%)"></a></div><div class="clearer"></div></div>';

handle['h3'] = 'Overskrift';
html['h3'] = '<div class="element" id="jed_div_%id%"><div class="handle">%handle%<input type="hidden" name="jed_sort[]" id="jed_sort_%id%" value="%id%"><input type="hidden" name="jed_%id%_kind" id="jed_%id%_kind" value="h3"></div><div class="data"><textarea name="jed_%id%_html" id="jed_%id%_html" class="textarea autogrow h3">%html%</textarea></div><div class="remover"><a href="javascript:jed.removePart(%id%)"></a></div><div class="clearer"></div></div>';

handle['h4'] = 'Lille overskrift';
html['h4'] = '<div class="element" id="jed_div_%id%"><div class="handle">%handle%<input type="hidden" name="jed_sort[]" id="jed_sort_%id%" value="%id%"><input type="hidden" name="jed_%id%_kind" id="jed_%id%_kind" value="h4"></div><div class="data"><textarea name="jed_%id%_html" id="jed_%id%_html" class="textarea autogrow h4">%html%</textarea></div><div class="remover"><a href="javascript:jed.removePart(%id%)"></a></div><div class="clearer"></div></div>';

handle['hr'] = 'Separator';
html['hr'] = '<div class="element hr" id="jed_div_%id%"><div class="handle">Separator<input type="hidden" name="jed_sort[]" id="jed_sort_%id%" value="%id%"><input type="hidden" name="jed_%id%_kind" id="jed_%id%_kind" value="hr"></div><div class="data"><hr /></div><div class="remover"><a href="javascript:jed.removePart(%id%)"></a></div><div class="clearer"></div></div>';

