// ==UserScript==
// @name           Gujarati Lexicon Lookup
// @namespace      DotNet Hero
// @description    Select text and a menu pops up so that you can search for selected word in Gujarati Lexicon(http://gujaratilexicon.com)
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js
// @author Vipl Limbachiya (http://vipullimbachiya.com)
// ==/UserScript==

$(document).ready(function() {
	var BXT_open=true;
	function BXT_callmenu(e) {
		var s=window.getSelection();
		if (!BXT_open) {
			window.setTimeout(function(){
				$("#BXT_searchselector").remove(); 
				BXT_open=true;
			},100)
		}
		if (s&&BXT_open&&s!='') {
			//alert(s);
			sE=encodeURIComponent(s);
			$("<div id=\"BXT_searchselector\"><a href=\"#\" id=\"ancLookup\"><img src=\"http://www.gujaratilexicon.com/images/GL.ico\" border=0> Gujarati Lexicon Lookup</a></div>").css(
				{
				'background-color':'rgba(0,0,0,0.7)',
				'position':'absolute',
				'z-index':'99999',
				'top':(e.pageY+10)+'px',
				'left':(e.pageX+10)+'px',
				'padding':'20px',
				'border':'1px #fff solid'
				}
			).appendTo("body");
			$("#BXT_searchselector a").css(
				{
				'font-size':'15px',
				'color':'#fff',
				'text-decoration':'none',
				'boder':'0',
				'border-bottom':'1px #999999 solid'
				}
			);
			BXT_open=false;
				$("#ancLookup").click(function(){				
		var a = "";
		if (window.getSelection) {
			a = window.getSelection()
		} else if (document.getSelection) {
			a = document.getSelection()
		} else if (document.selection) {
			a = document.selection.createRange().text
		}
		if (a == "") {
			a = prompt("You have not selected any word in document. Enter any word now, or cancel");
			if (a == null) a = ""
		}
		if (a != "") {
			var b = document.getElementsByTagName("body")[0];
			var c = document.createElement("iframe");
			c.id = "frame0";
			c.setAttribute("src", "http://www.gujaratilexicon.com/Ajxindex.php?action=Ajxdictionary&mode=search&type=1&sitem=" + a);
			c.setAttribute("style", "width:99%;height:350px;border: 0px;");
			var d = document.createElement("div");
			d.id = "divnode";
			d.setAttribute("style", "width:98%;position:fixed;top:0px;left:4px;font-family:Verdana;font-size:11px;z-index:1000;border: 1px solid #000000; padding: 3px; margin-top:5px;margin-bottom: 5px;background-color: #FFFFDF;");
			var e = document.createTextNode("Close");
			var f = document.createElement("div");
			f.setAttribute("style", "cursor:pointer;text-decoration:underline;padding:10px;text-align:right;");
			f.setAttribute("onClick", "document.getElementById(\"divnode\").parentNode.removeChild(document.getElementById(\"divnode\"))");
			f.appendChild(e);
			d.appendChild(f);
			d.appendChild(c);
			var g = document.createElement("a");
			g.setAttribute("href", "http://wordpress.vipullimbachiya.com");
			g.setAttribute("target", "_blank");
			g.setAttribute("title", "Vipul Limbachiya - Personal Blogs");
			g.appendChild(document.createTextNode("Vipul Limbachiya"));
			g.setAttribute("style", "color: Maroon;text-decoration: underline;padding: 3px;float:right;");
			d.appendChild(g);
			var h = document.createElement("a");
			h.setAttribute("href", "http://gujaratilexicon.com");
			h.setAttribute("target", "_blank");
			h.setAttribute("title", "Gujarati Lexicon");
			h.appendChild(document.createTextNode("Gujarati Lexicon"));
			h.setAttribute("style", "color: Maroon;text-decoration: underline;padding: 3px;float:right;");
			d.appendChild(g);
			d.appendChild(h);
			b.appendChild(d);
			$("#divnode").append("");
			return false;
		}
			});
		}
	}
	//window.onmouseup=callmenu;
	window.addEventListener('mouseup',BXT_callmenu, true);
});
