﻿function GoogleSearch(containerID,language,target,cssPath,showSnippet,showURL)
{
	var that = this;
	this.Container = $("#" + containerID);
	this.Language = language;
	this.Target = target;
	this.CSSPath = cssPath;
	this.ShowSnippet = showSnippet;
	this.ShowURL = showURL;
	
	if (google != undefined)
	{
		google.load('search', '1', { "language": language });
		google.setOnLoadCallback(function() { that.Load(that) });
	}
}

GoogleSearch.prototype.Container = null;
GoogleSearch.prototype.Language = "en";
GoogleSearch.prototype.Language = "blank";
GoogleSearch.prototype.CSSPath = null;
GoogleSearch.prototype.ShowSnippet = true;
GoogleSearch.prototype.ShowURL = true;

GoogleSearch.prototype.Load = function(that)
{
	var searchControl = new google.search.SearchControl();
	searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
	var options = new google.search.SearcherOptions();

	if (that.Language == "fa")
		options.setNoResultsString("نتیجه ای یافت نشد.");
	else
		options.setNoResultsString("No results found.");

	var webSearch = new google.search.WebSearch();
	webSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
	searchControl.addSearcher(webSearch, options);

	var drawOptions = new google.search.DrawOptions();
	drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);

	searchControl.draw(that.Container.find(".SearchContainer")[0], drawOptions);

	that.Container.find(".gsc-control").width("100%");
	that.Container.find(".gsc-input").width("150px");
	that.Container.find(".gsc-clear-button").width("auto");
	that.Container.find(".gsc-branding").width("110px");

	searchControl.setSearchCompleteCallback(that, function() { that.SearchComplete(that); });
	searchControl.setSearchStartingCallback(that, function() { that.SearchStart(that); });

	if (that.Language == "fa")
		that.Container.find(".gsc-search-button").val("جستجو");
	else if (that.Language == "ar")
		that.Container.find(".gsc-search-button").val("الصيد");
	else
		that.Container.find(".gsc-search-button").val("Search");

	switch (that.Target)
	{
		case "blank":
			searchControl.setLinkTarget(google.search.Search.LINK_TARGET_BLANK);
			break;
		case "self":
			searchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
			break;
		case "top":
			searchControl.setLinkTarget(google.search.Search.LINK_TARGET_TOP);
			break;
		case "parent":
			searchControl.setLinkTarget(google.search.Search.LINK_TARGET_PARENT);
			break;
	}
	that.Container.find("td.gsc-clear-button").append(that.Container.find(".gsc-loader-image"));
}

GoogleSearch.prototype.SearchStart = function(that)
{
	that.Container.find(".gsc-loader-image").show();
}

GoogleSearch.prototype.SearchComplete = function(that)
{
	that.Container.find(".gsc-trailing-more-results").hide();
	if (!that.ShowSnippet)
		that.Container.find(".gs-snippet").hide();
	if (!that.ShowURL)
		that.Container.find(".gs-visibleUrl").hide();

	that.Container.find(".gsc-loader-image").hide();
}