
if (typeof AITIA_POSITION == 'undefined') {
	document.write("<script src='" + mainURL + "/../js/webra3_gui.js' type='text/javascript' charset='utf-8'></script>");
}

var search_lastSearchString = '';
var _timedCall = 0;
var searchTimer = null;
var automaticFocus = false;

var searchControl;
var siteSearch;
var GOOGLE_SEARCH_API_KEY;
var RESULT_DIV = "googleResults";
var RESULTS_NUM = 5;
var STRING_MAX_LENGTH = 42;
var ALL_RESULTS_STRING = LANG['SEARCH_ALL_GOOGLE_RESULTS'];
var NO_RESULTS_STRING = LANG['SEARCH_NO_GOOGLE_RESULTS'];
var RESULT_TITLE = "Google";


function search_fastSearchManage(webraInstanceId, automatic, resultPosition) {
	if (automaticFocus) {
		automaticFocus = false;
		return;
	}

	if (window.event && window.event.keyCode == 13) {
		if (searchTimer) {
			clearTimeout(searchTimer);
			searchTimer = null;
			document.getElementById('searchInput').style.backgroundPosition = "-1000px";
		}
		search_sendSearchRequest(webraInstanceId, resultPosition);
	}
	else if (searchTimer == null) {
		_timedCall = 1;
		searchTimer = setTimeout("search_fastSearchManageSend('" + webraInstanceId + "', '" + resultPosition + "');", 1500);
	}
}

function search_fastSearchManageSend(webraInstanceId, resultPosition) {
	searchTimer = null;
	if (document.getElementById('searchInput') && document.getElementById('searchInput').value.length > 1 && (search_lastSearchString != document.getElementById('searchInput').value || _timedCall)) {
		search_lastSearchString = document.getElementById('searchInput').value;
		search_sendSearchRequest(webraInstanceId, resultPosition);
		search_fastSearchManage(webraInstanceId, false, resultPosition);
	}
	_timedCall = 0;
}

function search_sendSearchRequest(webraInstanceId, resultPosition) {
	if (document.getElementById('searchInput').value.length > 2) {
		var callbackParams = new Array();
		callbackParams.push(Array('resultPosition', resultPosition));
		document.getElementById('searchInput').style.backgroundPosition = "right";
		addRequest('/main.php?webraInstance=' + webraInstanceId + '&webra_action=search_xmlhttprequest&searchStr=' + encodeURIComponent(document.getElementById('searchInput').value), 'search_refreshSearchResult', Array(), callbackParams);
	}
}

function search_refreshSearchResult(answerXML, callbackParams) {
	var resultPosition;
	if (callbackParams) {
		for (var i = 0; i < callbackParams.length; i++) {
			if (callbackParams[i][0] == "resultPosition") {
				resultPosition = callbackParams[i][1];
			}
		}
	}
	if (!resultPosition) {
		resultPosition = 'left';
	}

	var answer = unescape(answerXML);

	var searchInput = document.getElementById('searchInput');
	var newDiv = document.getElementById('searchresultfixed');
	searchInput.style.backgroundPosition = '-1000px';

	if (newDiv) {
		newDiv.parentNode.removeChild(newDiv);
		newDiv.setAttribute('id', '');
		newDiv.innerHTML = '';
		newDiv = null;
	}

	if (!newDiv) {
		//tartalmazó elem létrehozása, ha még nincs
		var newDiv = document.createElement('div');
		document.body.appendChild(newDiv);
		newDiv.setAttribute('id', 'searchresultfixed');
		newDiv.setAttribute('class', 'select-free');
		newDiv.innerHTML = '<div class="fastsearchResultContainer" id="searchresult"></div><iframe frameborder="no" src="/mods/blank.html" style="display: none;"></iframe>';
	}
	newDiv.style.position = 'absolute';

	document.getElementById('searchresult').style.position = 'static';
	newDiv.style.display = 'block';

	var searchresultElement = document.getElementById('searchresult');
	if (searchresultElement) {

		if (GOOGLE_SEARCH_API_KEY != '') {
			try {
				loadGoogleJS();
			} catch (err) {
				console.info(err.message);
			}
		}

		searchresultElement.innerHTML = answer;
		newDiv.style.visibility = 'visible';

		var searchInputX = findPosX(searchInput);
		var searchInputY = findPosY(searchInput);

		if (resultPosition == 'left') {
			if (searchInputX + newDiv.offsetWidth < Aitia.Position.getInnerWidth()) {
				// a felugró popup nem lógna ki jobb oldalon
				newDiv.style.top = (searchInputY + searchInput.offsetHeight) + 'px';
				newDiv.style.left = (searchInputX) + 'px';
			}
			else {
				// a felugró popup kilógna jobb oldalon
				newDiv.style.top = (searchInputY + searchInput.offsetHeight) + 'px';
				newDiv.style.left = (Aitia.Position.getInnerWidth() - newDiv.offsetWidth) + 'px';
			}
		}
		else if (resultPosition == 'right') {
			if (0 < searchInputX + searchInput.offsetWidth - newDiv.offsetWidth) {
				// a felugró popup nem lógna ki bal oldalon
				newDiv.style.top = (searchInputY + searchInput.offsetHeight) + 'px';
				newDiv.style.left = (searchInputX + searchInput.offsetWidth - newDiv.offsetWidth) + 'px';
				//newDiv.style.backgroundColor = 'red';
			}
			else {
				// a felugró popup kilógna bal oldalon
				newDiv.style.top = (searchInputY + searchInput.offsetHeight) + 'px';
				newDiv.style.left = (Aitia.Position.getInnerWidth() - newDiv.offsetWidth) + 'px';
				//newDiv.style.backgroundColor = 'green';
			}
		}

		if (document.getElementById('searchresult_close')) {
			document.getElementById('searchresult_close').style.visibility = 'visible';
			if (document.all) {
				document.getElementById('searchresult_close').style.left = searchresultElement.clientWidth - 5 + 'px';
			}
			else {
				document.getElementById('searchresult_close').style.left = searchresultElement.offsetWidth - 5 + 'px';
			}
		}
		
		alphaBackgrounds();
	}

}

function search_closeResultbox() {
	hideElementTimer('searchresultfixed', 1000, 1);
	hideElementTimer('searchresult_close', 1, 1);
}


// Google keresés függvényei ---------------------

// Google kereső JS betöltő fv
function loadGoogleJS() {
	if (typeof google == 'undefined' || (typeof google == 'object' && typeof google.load != 'function')) {
		// 1: még nem volt betöltve a google jsapi, ezért nincs google class
		// 2: be van töltve a google simplemap api, ezért van google class, csak nincs load metódusa
		var script = document.createElement("script");
		script.src = document.location.protocol + "//www.google.com/jsapi?callback=loadGoogleApi";
		script.type = "text/javascript";
		document.getElementsByTagName("head")[0].appendChild(script);
	}
	else {
		loadGoogleApi();
	}
}

// Google kereső api betöltő fv
function loadGoogleApi() {
	if (typeof siteSearch == 'undefined') {
		// be kell tölteni a search api-t
		// A googleMain elindítja az első keresést is
		google.load('search', '1', {"nocss" : true, "callback": googleMain});
	}
	else {
		// Már be van töltve a search api, indítható a keresés
		googleStartSearch();
	}
}

// Google kereső inicializáló fv
function googleMain() {
	siteSearch = new google.search.WebSearch();
	siteSearch.setUserDefinedLabel(RESULT_TITLE);
	siteSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
	siteSearch.setNoHtmlGeneration();
	siteSearch.setUserDefinedClassSuffix("siteSearch");
	siteSearch.setSiteRestriction(GOOGLE_SEARCH_API_KEY);
	siteSearch.setSearchCompleteCallback(null, googleSearchComplete, [null]);

	googleStartSearch();
}

// Google keresés indítása
function googleStartSearch() {
	var searchInput = document.getElementById('searchInput');
	if (!searchInput) {
		return false;
	}
	siteSearch.execute(searchInput.value);
}

// Google keresés eredményének feldolgozása
function googleSearchComplete() {
	var divText;
	var input = document.getElementById("searchInput");
	if (siteSearch.results && siteSearch.results.length) {
		var i;
		divText = "<table class='fastsearch_center' cellspacing='0' cellpadding='0' border='0'><tbody><tr class='fastsearch_row'><td class='fastsearch_left'>" + RESULT_TITLE + " (" + siteSearch.cursor.estimatedResultCount + ")</td><td class='fastsearch_right' >";

		for (i = 0; i < siteSearch.results.length; i++) {
			if (RESULTS_NUM > i) {
				var result = siteSearch.results[i];
				if (result.titleNoFormatting.length > STRING_MAX_LENGTH) {
					result.titleNoFormatting = result.titleNoFormatting.substring(0, STRING_MAX_LENGTH - 3) + "...";
				}
				divText += "<div class='fastsearch_item'><a title='" + result.titleNoFormatting + "' href='" + result.unescapedUrl + "'>" + result.titleNoFormatting + "</a></div>";
			}
		}

		divText += "<div class='fastsearch_resultDetailLink'><a class='' title='' href='/kereses?engine=google&amp;searchStr=" + encodeURI(document.getElementById('searchInput').value) + "'>" + ALL_RESULTS_STRING + " (" + siteSearch.cursor.estimatedResultCount + ")</a></div></td></tr></tbody></table>";
		if (document.getElementById(RESULT_DIV) != null) {
			document.getElementById(RESULT_DIV).innerHTML = divText;
		}
	}
	else {
		if (document.getElementById(RESULT_DIV) != null) {
			document.getElementById(RESULT_DIV).innerHTML = NO_RESULTS_STRING;
		}
	}
	//setCloseDivPosition();
}

// Google keresés függvényeinek vége -------------

