var swSearch = null;

function swPositionDiv(swCursor) {
	var swInnerWidth = 0
	var swInnerHeight = 0;

	if (typeof(window.innerWidth) == "number") {
		//Non-IE
		swInnerWidth = window.innerWidth;
		swInnerHeight = window.innerHeight;
	}
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		swInnerWidth = document.documentElement.clientWidth;
		swInnerHeight = document.documentElement.clientHeight;
	}
	else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		swInnerWidth = document.body.clientWidth;
		swInnerHeight = document.body.clientHeight;
	}

	var swDiv = document.getElementById("swSearchWidget");
	var swLeft = swCursor.x;
	var swTop = swCursor.y;
	swDiv.style.left = swLeft + "px";
	swDiv.style.top = swTop + "px";

	var swDiv = document.getElementById("swSearchResults");
	swDiv.scrollTop = "0";
}

function swCloseDiv() {
	var swDiv = document.getElementById("swSearchWidget");
	swDiv.style.height = "0";
	swDiv.style.visibility = "hidden";
	swDiv.style.display = "none";
	swSearch.clearAllResults();
}

function swSearchOnLoad() {
	// create search controls
	swSearch = new GSearchControl();

	// all searchers will run in small mode
	swSearch.setResultSetSize(GSearch.SMALL_RESULTSET);
	swSearch.addSearcher(new GwebSearch());

	// site restricted web search with custom label and class suffix
	var swSiteSearch = new GwebSearch();
	swSiteSearch.setUserDefinedLabel("Dictionary");
	swSiteSearch.setUserDefinedClassSuffix("swSiteSearch");
	swSiteSearch.setSiteRestriction("dictionary.reference.com/browse/");
	swSearch.addSearcher(swSiteSearch);

	// site restricted web search with custom label and class suffix
	var swSiteSearch = new GwebSearch();
	swSiteSearch.setUserDefinedLabel("WikiPedia");
	swSiteSearch.setUserDefinedClassSuffix("swSiteSearch");
	swSiteSearch.setSiteRestriction("en.wikipedia.org");
	swSearch.addSearcher(swSiteSearch);

	// site restricted web search with custom label and class suffix
	var swSiteSearch = new GwebSearch();
	swSiteSearch.setUserDefinedLabel("Answers");
	swSiteSearch.setUserDefinedClassSuffix("swSiteSearch");
	swSiteSearch.setSiteRestriction("answers.com");
	swSearch.addSearcher(swSiteSearch);

	var swDrawOptions = new GdrawOptions();
	swDrawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
	swSearch.draw(document.getElementById("swSearchResults"), swDrawOptions);

	// no need for default search
	//swSearch.execute("default");
}

//GSearch.setOnLoadCallback(swSearchOnLoad);

var swObject = null;
var swCursor = {x:0, y:0};

function swRegisterEvent(swE) {
	if (!swE) swE = window.event;

	var swTarget;
	var swTargetClass = "";
	if (swE.target) swTarget = swE.target;
	else if (swE.srcElement) swTarget = swE.srcElement;
	if (swTarget.nodeType == 3) swTarget = swTarget.parentNode; // defeat Safari bug
	swTargetClass = swTarget.className;
	swTargetClass = swTargetClass.replace(/^\s+/, "").substr(0,2); // strip leading spaces and substr first 2 letters

	if (swE.keyCode != 81 && (swTargetClass != "sw" && swTargetClass != "gs")) { swCloseDiv(); }

	if (swE.type == "click") {
		if (swE.pageX > 0 && swE.pageY > 0) {
			swCursor.x = swE.pageX;
			swCursor.y = swE.pageY;
		}
		else {
			var swDE = document.documentElement;
			var swB = document.body;
			var swX = swE.clientX + (swDE.scrollLeft || swB.scrollLeft) - (swDE.clientLeft || 0);
			var swY = swE.clientY + (swDE.scrollTop || swB.scrollTop) - (swDE.clientTop || 0);
			if (swX > 0 && swY > 0) {
				swCursor.x = swX;
				swCursor.y = swY;
			}
		}
	}

	if (swE.keyCode == 81) {
		swGetText(swCursor);
	}

	return true;
}

function swGetText(swCursor) {
	var swTxt = "";

	if (window.getSelection) {
		swTxt = window.getSelection();
	}
	else if (document.getSelection) {
		swTxt = document.getSelection();
	}
	else if (document.selection) {
		swTxt = document.selection.createRange().text;
	}
	else return;

	if (swTxt != "") {
		swPositionDiv(swCursor);

		swTxt = swTxt + ""; // convert to string for ease of use
		swTxt = swTxt.replace(/^\s+/, "").replace(/\s+$/, ""); // strip leading and trailing spaces

		swSearch.execute(swTxt);

		var swDiv = document.getElementById("swSearchWidget");
		swDiv.style.height = "300px";
		swDiv.style.visibility = "visible";
		swDiv.style.display = "block";
	}
}

function swInit() {
	document.onclick = swRegisterEvent;
	document.onkeydown = swRegisterEvent;

	swSearchOnLoad();
}

swInit();
