ReadSpeaker = function() {	
	
	this.customerid = "1003740";
	this.actionURL = "http://asp.readspeaker.net/cgi-bin/nsrsone";
	this.language = "nl";

	var self = this;
	
	this.selection = "";
	if(!document.getElementById("rs_form")) this.createForm(); // create a form if none exists (just for the REL links)
	this.form = document.getElementById("rs_form");
		
	this.exceptions = new RegExp("^(tr|option)$", "i");
				
	if(this.form.url.value == "") {
		this.form.url.value = document.location.href;
	}

	EventListener.addEvent(this.form, "submit", this.onSubmit, this);
	EventListener.addEvent(this.form.getElementsByTagName("a")[0], "click", this.click, this);	
	EventListener.addEvent(document, "click", this.clickElement, this);		
	
	EventListener.addEvent(document, "keyup", function(){ setTimeout(function(){self.doSelection()}, 50) }); 
	EventListener.addEvent(document, "mouseup", function(){ setTimeout(function(){self.doSelection()}, 50) }); 
}

ReadSpeaker.prototype = {
	click:function(e) {
		this.onSubmit();
		this.createPopup();		
		this.form.submit();
		EventListener.preventDefault(e);
	},

	onSubmit:function(id) {
		var dom = document.getElementById(id || "content");
		if(this.selection != "") var selection = this.selection; 
		else var selection = this.parseDom(dom).innerHTML;
		this.form.rshtml.value = selection;
		if(document.location.href.match("rsdebug")) {
			// open extra debug window
			var rsdebug = window.open();
			rsdebug.document.write("<textarea style='width:100%;height:100%;'>"+selection+"</textarea><br><br>"+selection);
		}
		return true;
	},
	
	doSelection:function() {
		if (document.getSelection) this.selection = document.getSelection();
		else if (document.all) this.selection = document.selection.createRange().text;
		else if (window.getSelection) this.selection = window.getSelection();
	},

	parseDom:function(dom) {
		var buffer = document.createElement("div");
		this.parseNode(dom, buffer);
		return buffer;
	},

	parseNode:function(node, buffer) {
		var l = node.childNodes.length, child, clone;
		for(var i=0; i<l; i++) {
			child = node.childNodes.item(i);
			if(child.nodeType == 1 && !child.offsetHeight && !child.offsetWidth && !this.exceptions.test(child.nodeName)) {
				continue;
			}

			clone = buffer.appendChild(child.cloneNode(false));
			this.parseNode(child, clone);
		}
	},
	
	clickElement:function(e) {
		var target = EventListener.getTarget(e);
		if(!target.rel || target.rel != "readspeaker") return;
		var id = target.hash.substring(1);
		this.sayElement(id);
		EventListener.preventDefault(e);		
	},
	
	sayElement:function(id) {
		this.onSubmit(id);
		this.createPopup();
		this.form.submit();
	},
	
	createPopup:function() {
		var rs = window.open("about:blank", "rs", "width=400, height=220");
		rs.focus();
	},
	
	createForm:function() {
		var form = document.createElement("form");
		form.id = form.name = "rs_form";
		form.action = this.actionURL;
		form.method = "post";
		form.target = "rs";
		// nasty that appended INPUTs don't get seen in some browsers
		form.innerHTML = "<input type=\"hidden\" name=\"rshtml\" value=\"\" /><input type=\"hidden\" name=\"url\" value=\"\" /><input type=\"hidden\" name=\"customerid\" value=\"" + this.customerid + "\" /><input type=\"hidden\" name=\"lang\" value=\"" + this.language + "\" />";
		document.getElementsByTagName("body")[0].appendChild(form);
	}
}

EventListener.addEvent(window, "load", function() {
	window.readSpeaker = new ReadSpeaker();
});