wmp.editor.baseModulePi = new Class({
	Extends: wmp.editor.baseModule,
	type: "block",
	legalChilds: [],
	allowedAttributes: [],
	nodeName: "div",
	icon: "/editor/editors/misc.png",
	name: "pi",
	handleKeypress: wmp.editor.baseModule.getStatic("appendPOnEnter"),
	xml2html: function(node, editor) {
		var n = editor.c("div");
		n.addClass("pi");
		//console.log("xml2html: pi",node.target,node.data);
		n.addClass("pi" + node.target);
		n.setAttribute("_opts", node.data);
		return n;
	},
	
	exportXml: function() {
		this.pioptions = this.node.getAttribute("_opts") || "";
		var xml = "<?" + this.piName;
		if (this.pioptions && this.pioptions.length>0) 
			xml += " " + this.pioptions;
		xml += "?>";
		this.xml = xml;
		return this.xml;
	},
	
	guiactivateme: function() {
		this.guicreatemenu();
		
		this.inputs.options = new wmp.gui.url({
			onchange: this.setOpts.bind(this),
			value: this.node.getAttribute("_opts") || ""
		});
		this.menu.addInput({title: "Optionen",input: this.inputs.options});
	},
	
	setOpts: function() { 
		this.node.setAttribute("_opts", this.inputs.options.getValue());
	},
	
	fixme: function() {
		this.node.innerHTML = this.title;
		this.makeUneditable(this.node);
	},
	setNeededStyles: function() {},
	
	createPiOptions: function(content) {
		var str="",m;
		for (m in content) {
			if (content[m])
				str += " " + m + "=" + content[m];
		}
		return str.substr(1,300); 
	},
	
	parsePiOptions: function(content) {
		var retval = {}, name = value = separator = "", read = 0, i=0, ch
		// read ist 0 beim Einlesen des Namens und 1 beim Einlesen des Wertes
		// todo: erstzen durch algorithmus: http://ejohn.org/blog/search-and-dont-replace/
		while (ch = content.substr(i++,1)) {
			if (read == 0 && ch == "=") {
				read = 1;
			}
			else if (read == 0 && [' ',"\t"].contains(ch)) {
				if (name)
					retval[name] = "";
				name = "";
			}
			else if (read == 0)
				name += ch;
			else if (read == 1 && !separator && !value && ['"',"'"].contains(ch))
				separator = ch;
			else if (read == 1 && ch == separator || ([' ',"\t"].contains(ch) && !separator)) {
				retval[name] = value;
				read = 0;
				separator = name = value = "";
			}
			else
				value += ch;
		}
		if (read == 1 && separator)
			return {};
		else if (read == 1)
			retval[name] = value;
		else if (read == 0 && name)
			retval[name] = "";
		return retval;
	}
});

wmp.editor.modules.pi = new Class({
	Extends: wmp.editor.baseModulePi,
	title: "Processing Instruction",
	nodeClass: "pi",
	nodeName: "div",
	icon: "/editor/editors/misc.png",
	specificity: -2,
	fixme: function() {
		if (!this.piName) {
			this.piName =  this.node.className.match(/$| pi([^ ]+)/)[1];
			this.node.className = "pi pi" + this.piName;
		}
		this.node.innerHTML = this.title + ' "' + this.piName + '"';
		this.makeUneditable(this.node);
	}
});

wmp.editor.modules.pitoc = new Class({
	Extends: wmp.editor.baseModulePi,
	title: "Inhaltsverzeichnis",
	piName: "toc",
	specificity: 5,
	nodeClass: "pitoc",
	nodeName: "div",
	icon: "/editor/editors/misc.png",
	fixme: function() {
		this.node.className = "pi" + this.piName + " block right shaded table-of-contents";
		//this.node.innerHTML = '<h1>Inhaltsverzeichnis</h1><ul class="table-of-contents"><li>Überschrift1</li><li>Überschrift2</li></ul>';
		this.node.innerHTML = 'Inhaltsverzeichnis';
		this.makeUneditable(this.node);
	}
});

wmp.editor.modules.pinews = new Class({
	Extends: wmp.editor.baseModulePi,
	title: "News auflisten",
	piName: "news",
	nodeClass: "pinews",
	nodeName: "div",
	icon: "/editor/editors/misc.png"
});

wmp.editor.modules.piarticles = new Class({
	Extends: wmp.editor.baseModulePi,
	title: "Artikel auflisten",
	piName: "articles",
	nodeClass: "piarticles",
	nodeName: "div",
	icon: "/editor/editors/misc.png",
	//Options format: sort=title/pub_date/modified/created/-pub_date/?/-modified/created limit=1-50 tag=name tags=name1,name2,... template=list/teaser zone=portal/design
	manipulators: {
		"tags": {
			title: "Tags",
			onchange: "setproperties",
			type: "string"
		},
		"sort": {
			title: "Sortieren nach",
			onchange: "setproperties",
			type: "select",
			options: {"-pub_date":"veröffentlicht (Absteigend)", "-modified":"verändert (Absteigend)", "pub_date":"veröffentlicht", "modified":"verändert", "created":"erstellt","title":"Titel", "?":"zufällig"},
			help: "Die Tags nach denen gesucht werden soll"
		},
		"template": {
			title: "Anzeige",
			onchange: "setproperties",
			type: "select",
			options: {"list":"als Liste","teaser":"als Teaser"}
		},
		"limit": {
			title: "Maximalzahl",
			onchange: "setproperties",
			type: "select",
			options: {"5":"5", "10":"10", "20":"20", "50":"50","1":"1"},
			help: "Maxmimale Anzahl von Einträgen"
		},
		"options": {
			title: "Optionen",
			type: "string"
		}
		//"zone": {title: "Zone"}
	},
	guiactivateme: 	function() {
		this.guicreatemenu();
		var pairs = this.parsePiOptions(this.getproperty_options());
		for (m in pairs) {
			if (this.inputs[m])
				this.inputs[m].setValue(pairs[m]);
		}
	},
	setproperties: function() {
		var o={};
		for (var m in this.manipulators) {
			var v = this.inputs[m].getValue();
			if (m!="options")
				o[m] = v;
		}
		var pairs = this.parsePiOptions(this.getproperty_options());
		$extend(pairs, o);
		var str = this.createPiOptions(pairs);
		this.node.setAttribute("_opts", str);
		this.inputs.options.setValue(str); 
	},
	setproperty_options: function() {
		var val = this.inputs.options.getValue();
		var pairs = this.parsePiOptions(val);
		for (m in pairs) {
			if(this.inputs[m])
				this.inputs[m].setValue(pairs[m]);
		}
		this.node.setAttribute("_opts", val);
	},
	getproperty_options: function() {
		return this.node.getAttribute("_opts") || "";
	}
});

wmp.editor.modules.pitopics = new Class({
	Extends: wmp.editor.baseModulePi,
	title: "Foren-Themen auflisten",
	piName: "topics",
	nodeClass: "pitopics",
	nodeName: "div",
	icon: "/editor/editors/misc.png"
});

