if (!document.all) {
	document.captureEvents(Event.MOUSEUP);
}


var TwickitPopup = {
	previousSelection : "",
	theme : "twickit",
		
	load: function() {
		var eventMode = "mouseup";
        var disableCreate = 0;
		var js = /twickit.js(\?.*)?$/;
		$$('head script[src]').findAll(function(s) { return s.src.match(js); }).each(function(s) {
			var themes = s.src.match(/\?.*theme=([a-z,]*)/);
			var theme = themes ? themes[1] : "twickit";
			TwickitPopup.theme = theme;
			
			var eventModes = s.src.match(/\?.*event=([a-z,]*)/);
			eventMode = eventModes ? eventModes[1] : "mouseup";

            disableCreate = s.src.search(/\?.*disableCreate=1/) == -1 ? 0 : 1;
		});
		Event.observe(window, 'load', function() {
			Event.observe(
				document, 
				'keyup',
				function(inEvent) {
					if (!inEvent) var inEvent = window.event; 
					if (inEvent.keyCode) code = inEvent.keyCode;
					if(code==27 && document.getElementById('twicktip') != null) {
						document.getElementById('twicktip').hide();
					} 
				}
			);
			
			Event.observe(
				document, 
				'mouseup',
				function(inEvent) {
					if (document.selection) {
						selection = document.selection.createRange().text;
					} else {
						selection = window.getSelection().toString();
					}
					
					if (selection != TwickitPopup.previousSelection) {
						//if (!inEvent) var inEvent = window.event; 
						if (!inEvent.altKey) {
							var twicktip = document.getElementById('twicktip');
							if (twicktip != null) {
								twicktip.hide();
							}
						} else {
							TwickitPopup.previousSelection = selection;
							
							if (selection != "") {
								var twicktip = document.getElementById('twicktip');
								if (twicktip == null) {
							    	var twicktip = new Element("div", {"id":"twicktip"});
							    	var table = new Element("table", {"cellpadding":"0", "cellspacing":"0"});
							    	var tbody = new Element("tbody");
							    	
							    	var rows = new Array("t", "u", "m", "l");
							    	for(var r=0; r<rows.length; r++) {
								    	var tr = new Element("tr");
								    	var td1 = new Element("td", {"id":"twicktip_" + rows[r] + "l"});
								    	var td2 = new Element("td", {"id":"twicktip_" + rows[r] + "m"});
								    	var td3 = new Element("td", {"id":"twicktip_" + rows[r] + "r"});
								    	tr.appendChild(td1);
								    	tr.appendChild(td2);
								    	tr.appendChild(td3);
								    	tbody.appendChild(tr);
							    	}
							    	table.appendChild(tbody);
							    	twicktip.appendChild(table);
							    	
								    $$("body")[0].appendChild(twicktip);
								    twicktip = document.getElementById('twicktip');
								}
								document.getElementById('twicktip').hide();
						    	if (false && document.all) { 
									tempX = window.event.clientX + document.body.scrollLeft;
									tempY = window.event.clientY + document.body.scrollTop;
								} else {  
									tempX = inEvent.pageX;
									tempY = inEvent.pageY;
									
								}  
						    	 
								if (tempX < 0){tempX = 0;}
								if (tempY < 0){tempY = 0;}
										
								twicktip.style.top=tempY + "px";
								twicktip.style.left=tempX + "px";
								twicktip.style.position="absolute";
								
								// CSS + JavaScript-Include
								if (document.getElementById('twickit_open_popup_js') != null) {
									document.getElementById('twickit_open_popup_js').remove();
								}
								
								if (TwickitPopup.theme != "custom") {
									if (document.getElementById('twickit_open_popup_css') != null) {
										document.getElementById('twickit_open_popup_css').remove();
									}
									
									var css = 
										new Element(
											"link",
											{
												"type" : "text/css",
												"id" : "twickit_open_popup_css",
												"rel" : "stylesheet",
												"href" :"http://twick.it/interfaces/js/popup/theme_" + TwickitPopup.theme + ".css"
											}
										);
									$$("head")[0].appendChild(css);
								}
								
								TwickitPopup.callback("<div id='twicktip_wait'>&nbsp;</div>");
								
								var script = 
									new Element(
										"script",
										{
											"type" : "text/javascript",
											"charset" : "utf-8",
											"id" : "twickit_open_popup_js",
											"src" :"http://twick.it/interfaces/js/popup/open_popup_js.php?text=" + encodeURIComponent(selection.strip()) + "&disableCreate=" + disableCreate + "&noCache=" + (new Date()).getTime()
										}
									);
								try {
									if(twickitLanguage) {
										script.src += "&lng=" + twickitLanguage;
									}
								} catch(ignored) {}
								
								$$("head")[0].appendChild(script);
							}
						}
					}
				}
			);
		});
		
	},
	
	callback: function(inText, inClose) {
		document.getElementById("twicktip").show();
		document.getElementById("twicktip_mm").update(inText);
		if (inClose) {
			document.getElementById("twicktip").fade({"duration":4});
		}
	}
}

TwickitPopup.load();