	var rsc = null;
	var searchplaces = new Array();
	var q;
	var t;
	var start_count = 0;
	
	searchplaces[0] = {value: 'site', title: 'по сайту', searcher: 'web', additional: 'site:rzn.info'};
	searchplaces[1] = {value: 'news', title: 'в новостях', searcher: 'web', additional: 'site:rzn.info/news'};
	searchplaces[2] = {value: 'company', title: 'в предприятиях', searcher: 'web', additional: 'site:rzn.info/company'};
	searchplaces[3] = {value: 'forum', title: 'на форуме', searcher: 'web', additional: 'site:forum.rzn.info'};
	searchplaces[4] = {value: 'ryazan', title: 'в Рязани', searcher: 'web', additional: 'Рязань'};
	searchplaces[5] = {value: 'web', title: 'в интернете', searcher: 'web', additional: ''};
	
	//searchplaces[6] = {value: 'local', title: 'Рязань на карте', searcher: 'local', additional: 'Россия, Рязанская область, Рязань'};
	
	google.load('search', '1.0');
	
	function OnLoad() {
		if ($('#goosearch').length == 0) return;
		rsc = new RawSearchControl();
	}
	
	function RawSearchControl() {
		this.searcherform = document.getElementById("goosearch");
		this.results = document.getElementById("goosearch-results");
		this.cursor = document.getElementById("goosearch-cursor");
		this.searchform = document.getElementById("goosearch-form");
		this.activeSearcher = "site";
		this.searchers = new Array();
	
		var searcher = null;
		var data = '';
	
		var cur = 0;
	
		for (i in searchplaces)
		{
			data += '<li><label for="gooi'+i+'"><input name="searcherType" id="gooi'+i+'" value="'+searchplaces[i].value+'" type="radio" '+(!data || searchplaces[i].value == t ? 'checked="checked" ' : '')+'/>'+searchplaces[i].title+'</label></li>';
	
			if (searchplaces[i].searcher == 'local')
			{
				searcher = new google.search.LocalSearch();
				searcher.setCenterPoint(searchplaces[i].additional);
			}
			else
			{
				searcher = new google.search.WebSearch();
			}
			searcher.setResultSetSize(google.search.Search.LARGE_RESULTSET);
			searcher.setNoHtmlGeneration();
			searcher.setSearchCompleteCallback(this, RawSearchControl.prototype.searchComplete, [searcher]);
			this.searchers[searchplaces[i].value] = searcher;
	
			if (searchplaces[i].value == t)
			{
				cur = i;
			}
		}
	
		this.searchForm = new google.search.SearchForm(true, this.searchform);
		this.searchForm.setOnSubmitCallback(this, RawSearchControl.prototype.onSubmit);
		this.searchForm.setOnClearCallback(this, RawSearchControl.prototype.onClear);

		$('body').click(function(){
			if (opened_goo)
			{
				$('#goosearch .gsc-options').hide();
				opened_goo = false;
			}
		});

		$('input.gsc-input').val(q);
	
		$('td.gsc-input').after('<td class="gsc-places"><span class="gsc-current">'+searchplaces[cur].title+'</span></td>');
		$('td.gsc-search-button').html($('td.gsc-search-button').html()+'<ul class="gsc-options">'+data+'<li class="gsc-copy"></li></ul>');
	
		$('.gsc-branding').appendTo('.gsc-copy');
	
		$('.gsc-options label').bind('click', function(){
			$('.gsc-current').text($(this).text());
		});
	
		$('#goosearch .gsc-current, #goosearch .gsc-places').click(function(){
			$('#goosearch .gsc-options').show();
			setTimeout('opened_goo=true', 100);
		});

		$('form.gsc-search-box').attr('action', 'http://www.rzn.info/searchresult/');
		$('form.gsc-search-box').attr('accept-charset', 'Windows-1251');

		if (q)
		{
			$('#goosearch-loader').show();
			this.find();
		}
	}
	
	RawSearchControl.prototype.computeActiveSearcher = function() {
		$('input[name="searcherType"]').each(function(){
			if ($(this).attr('checked'))
			{
				rsc.activeSearcher = $(this).val();
				return;
			}
		});
	}
	
	RawSearchControl.prototype.find = function() {
		var form = document.getElementById("goosearch");
		var a = document.location.href;
		if (a.indexOf('/searchresult') >= 0)
		{
			if (q) {
				var val = q;
	
				for (i in searchplaces)
				{
					if (searchplaces[i].value == t)
					{
						val += ' ' + searchplaces[i].additional;
						break;
					}
				}
	
				this.searchers[t].execute(val);
			}
	
			return false;
		}
	}
	
	RawSearchControl.prototype.onSubmit = function(form) {
		$('#goosearch-loader').show();
		var a = document.location.href;
		start_count = 0;

		document.title = 'Результаты поиска по запросу: '+form.input.value+' — город Рязань на городском сайте RZN.info';

		if (a.indexOf('/searchresult') >= 0)
		{
			this.computeActiveSearcher();
			if (form.input.value) {
				var val = form.input.value;
	
				for (i in searchplaces)
				{
					if (searchplaces[i].value == this.activeSearcher)
					{
						val += ' ' + searchplaces[i].additional;
						break;
					}
				}
	
				this.searchers[this.activeSearcher].execute(val);
			}
	
			return false;
		}
		form.submit();
	}
	
	RawSearchControl.prototype.onClear = function(form) {
		this.clearResults();
	}
	
	RawSearchControl.prototype.searchComplete = function(searcher) {
		this.clearResults();

		if (searcher.results && searcher.results.length > 0) {
	
			rsc.res = '';
	
			$.each(searcher.results,
				function(i, val) {
					if (!val) return;
	
					rsc.res += '<li style="padding-bottom: 10px"><a href="'+val.unescapedUrl+'"  target="_blank">'+val.title+"</a><br />"+val.content+'<br /><a href="'+val.unescapedUrl+'" target="_blank" style="color: #008000;">'+val.unescapedUrl+'</a>'; 
	
					if (val.cacheUrl)
					{
						rsc.res += ' &nbsp; <a href="'+val.cacheUrl+'" style="color: #999999" target="_blank">Сохраненная версия</a>';
					}
	
					rsc.res += '</li>';
				}
			);
	
			$('#goosearch-results').html('<p style="display: none" id="goosearch-loader"><img src="/img/loading.gif" alt="" />Подождите, идет поиск...</p>'+$('#goosearch-results').html()+'<ol start="'+(start_count*8+1)+'">'+rsc.res+'</ol>');
	
			if (searcher.cursor) {
				var cursorNode = createDiv(null, "gsc-cursor");
				for (var i=0; i<searcher.cursor.pages.length; i++) {
					var className = "gsc-cursor-page";
					if (i == searcher.cursor.currentPageIndex) {
						className = className + " gsc-cursor-current-page";
					}
					var pageNode = createDiv(searcher.cursor.pages[i].label, className);
					pageNode.onclick = methodClosure(this, this.gotoPage, [searcher, i]); 
					cursorNode.appendChild(pageNode);
				}
				var div = createDiv("Страницы", "header");
				this.cursor.appendChild(div);
				this.cursor.appendChild(cursorNode);
				var more = createLink(searcher.cursor.moreResultsUrl,
									GSearch.strings["more-results"] + "&nbsp;&raquo;",
									GSearch.LINK_TARGET_SELF,
									"gsc-trailing-more-results");
				this.cursor.appendChild(more);
			}
		}
		else
		{
			$('#goosearch-results').html('<p style="display: none" id="goosearch-loader"><img src="/img/loading.gif" alt="" />Подождите, идет поиск...</p><p>Во вашему запросу ничего не найдено</p>');
			$('#goosearch-cursor').html('');
		}
	}
	
	RawSearchControl.prototype.gotoPage = function(searcher, page) {
		start_count = page;
		searcher.gotoPage(page);
	}
	
	RawSearchControl.prototype.clearResults = function() {
		removeChildren(this.results);
		removeChildren(this.cursor);
	}
	
	function removeChildren(parent) {
		while (parent.firstChild) {
			parent.removeChild(parent.firstChild);
		}
	}
	function createDiv(opt_text, opt_className) {
		var el = document.createElement("div");
		if (opt_text) {
			el.innerHTML = opt_text;
		}
		if (opt_className) { el.className = opt_className; }
		return el;
	}
	
	function methodClosure(object, method, opt_argArray) {
		return function() {
			return method.apply(object, opt_argArray);
		}
	}
	
	function createLink(href, opt_text, opt_target, opt_className, opt_divwrap) {
		var el = document.createElement("a");
		el.href = href;
		if (opt_text) {
			el.innerHTML = opt_text;
		}
		if (opt_className) {
			el.className = opt_className;
		}
		if (opt_target) {
			el.target = opt_target;
		}
		if (opt_divwrap) {
			var div = this.createDiv(null, opt_className);
			div.appendChild(el);
			el = div;
		}
		return el;
	}
	
	google.setOnLoadCallback(OnLoad, true);
