// ==UserScript==
// @name           Gujarati Lexicon Lookup + Urban Dictionary
// @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), and now from Urban dictionary as well!
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js
// @require        http://stilbuero.de/jquery/cookie/jquery.cookie.js
// @author Vipl Limbachiya (http://vipullimbachiya.com)
// @exclude http://www.gujaratilexicon.com/Ajxindex.php?*
// @exclude http://www.urbandictionary.com/tooltip.php?*
// ==/UserScript==

$(document).ready(function() {
	var BXT_open=true;
	if(!isGLLDisabled())
	{
			function BXT_callmenu(e) {
				var s=getSelectedText();
				if (!BXT_open) {
					window.setTimeout(function(){
						$("#BXT_searchselector").remove(); 
						BXT_open=true;
					},100)
				}
				if (s&&BXT_open&&s!='' && s.toString().indexOf(" ")<0&&!isGLLDisabled() && $("#iframeGLL").length==0) {
					//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><br /><br /><a href=\"#\" id=\"ancLookupUD\"><img src=\"http://static3.urbandictionary.com/favicon.ico\" border=0 /> Urban Dictionary Lookup</a><br /> <div style=\"float:right;margin-top:5px;\"><span id=\"ancDisableGLL\" style=\"cursor:pointer;font-size:10px;color:yellow;margin:10px;\">Disable on this website</span></div></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,#ancLookupUD").click(function(){				
						var clickedElm = $(this);
				var a = getSelectedText();
				if (a == "") {
					a = prompt("You have not selected any word in document. Enter any word now, or cancel");
					if (a == null) a = ""
				}
				if (a != "") {
				
					if(a.indexOf(" ")>=0)
					{
						$("#BXT_searchselector").remove(); 			
					}
					{
					var b = document.getElementsByTagName("body")[0];
					var c = document.createElement("iframe");
					c.id = "iframeGLL";
					if($(clickedElm).attr("id")==="ancLookupUD")
					{
						c.setAttribute("src", "http://www.urbandictionary.com/tooltip.php?term=" + a);
						c.setAttribute("style", "width:98%;height:250px;border: 0px;padding:0px;margin:0px;");
					}
					else
					{
					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:99999;border: 1px solid #000000; padding: 3px; margin-top:5px;margin-bottom: 5px;background-color: #fff;");
					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://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");
					if($(clickedElm).attr("id")==="ancLookupUD")
					{
						h.setAttribute("href", "http://urbandictionary.com");
						h.setAttribute("target", "_blank");
						h.setAttribute("title", "Urban Dictionary");
						h.appendChild(document.createTextNode("Urban Dictionary"));					
					}
					else
					{
						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;
				}}
					});
					
					$("#ancDisableGLL").click(function(){
						disableGLL();
						alert("GujaratiLexicon lookup disabled, you can enable it if required from link shown at bottom of the page");
						addGLLEnableLink();
						return false;
					});
				}
			}
			function getSelectedText()
			{
				var a = "";
				if (window.getSelection) {
					a = window.getSelection()
				} else if (document.getSelection) {
					a = document.getSelection()
				} else if (document.selection) {
					a = document.selection.createRange().text
				}	
				return a.toString().replace(/^\s\s*/, '').replace(/\s\s*$/, '');;
			}
			//window.onmouseup=callmenu;
			window.addEventListener('mouseup',BXT_callmenu, true);
	}
	else
	{
		addGLLEnableLink();
	}
});

function isGLLDisabled()
{
	return $.cookie("disabledGLL")=="true";
};

function disableGLL()
{
	$.cookie("disabledGLL", 'true', { expires: 365 });
	return false;
};

function enableGLL()
{
	$.cookie("disabledGLL", 'false',{expires: 365});
	return false;
};

function addGLLEnableLink()
{
	$("body").append("<div style=\"float:right;\"><span id=\"ancEnableGLL\" style=\"cursor:pointer;font-size:9px;color:green;margin:10px;margin-right:20px;\">Enable GLLookup</a></div>");
			$("#ancEnableGLL").click(function(){
						enableGLL();
						alert("GujaratiLexicon lookup enabled, reload page to apply changes");
						$("#ancEnableGLL").parent().remove();						
						return false;
					});
}

