var lastActiveId 	= 'web';
var lastActiveName 	= 'Web';
var activeWeatherItem = 0;
var activeWeather 	  = 0;
var weatherLimit	  = 25;
var weatherLast 	  = 0;
var fixedQuery		  = '';

function getLink(type)
{
	return type+'.tut.ua';
}

function SetActive(id) // Функция для закладок в шапке. Единая для всех страниц
{
	hideWeatherHelp();
	if (id != lastActiveId) {
		if (id=='web')
		{
			document.getElementById('this_page_mark').style.left = '65px';
			document.getElementById(id).innerHTML = '<a href="http://'+getLink('search')+'/">Web</a>';
			document.getElementById('search_form').action = 'http://'+getLink('search')+'/';
			document.getElementById('checkboxes_web').style.display = 'block';
			document.getElementById('checkboxes_images').style.display = 'none';
			document.getElementById('checkboxes_weather').style.display = 'none';
			document.getElementById('checkboxes_board').style.display = 'none';
			document.getElementById('checkboxes_tv_tab').style.display = 'none';

			document.getElementById('all_search_q').style.display = 'block';
			document.getElementById('weather_search_q').style.display = 'none';
		}
		if (id=='weather')
		{
			document.getElementById('this_page_mark').style.left = '353px';
			document.getElementById(id).innerHTML = '<a href="http://'+getLink('weather')+'/search/">Погода</a>';
			document.getElementById('search_form').action = 'http://'+getLink('weather')+'/search/';
			document.getElementById('checkboxes_web').style.display = 'none';
			document.getElementById('checkboxes_images').style.display = 'none';
			document.getElementById('checkboxes_weather').style.display = 'block';
			document.getElementById('checkboxes_board').style.display = 'none';
			document.getElementById('checkboxes_tv_tab').style.display = 'none';

			document.getElementById('all_search_q').style.display = 'none';
			document.getElementById('weather_search_q').style.display = 'block';
		}
		if (id=='tv_tab')
		{
			document.getElementById('this_page_mark').style.left = '208px';
			document.getElementById(id).innerHTML = '<a href="http://'+getLink('tv')+'/search/tv/">Телепрограмма</a>';
			document.getElementById('search_form').action = 'http://'+getLink('tv')+'/search/tv/';
			document.getElementById('checkboxes_web').style.display = 'none';
			document.getElementById('checkboxes_images').style.display = 'none';
			document.getElementById('checkboxes_weather').style.display = 'none';
			document.getElementById('checkboxes_board').style.display = 'none';
			document.getElementById('checkboxes_tv_tab').style.display = 'block';

			document.getElementById('all_search_q').style.display = 'block';
			document.getElementById('weather_search_q').style.display = 'none';
		}

			document.getElementById(lastActiveId).className = 'menu_point_tv_tab';
			document.getElementById(id).className = 'this_page_tv_tab';


		var i = 2;
		switch (lastActiveId)
		{
			case 'tv_tab': i = 1;
			case 'web': i = 0;
		}
		//document.getElementById(lastActiveId).innerHTML = '<a href="'+link[i]+'">'+lastActiveName+'</a>';
		lastActiveId = id;
		lastActiveName = document.getElementById(id).innerHTML;
//		document.getElementById('q').focus();
	}
	return false;
}

function changeQuery(key)
{
	if (key == 13)
	{
		hideWeatherHelp();
		return true;
	}
	if (lastActiveId == 'weather') {
		weatherHelp();
	}
}

function weatherHelp()
{
	q = document.getElementById('weather_search_q');
	if (q.value.length==fixedQuery.length) {
		return true;
	}
	if (q.value=='') {
		hideWeatherHelp();
		return true;
	}
	var out = loadXMLDoc("/xml/weather/"+enBASE64(enBASE64(q.value))+"/");
	items = out.getElementsByTagName('item');

	var rows = '';
	var place = document.getElementById('weather_search');

	for (var i=0; i<items.length; i++)
	{
		title 	= getElementTextNS('title', items[i], 0);
		type 	= getElementTextNS('type', items[i], 0);

		title = '<div id="weather_value_'+i+'">'+title+'</div>';
		if (type=='country') {
			title = '<b>'+title+'</b>';
		}

		rows = rows + '<a href="javascript:setWeatherQuery();">';
		rows = rows + '<div class="item" id="weather_item_'+i+'" onmouseover="setActiveWeatherHelp('+i+')">';
		rows = rows + title;
		rows = rows + '</div></a>';
	}

	weatherLast = i;

	if (i==weatherLimit) {
		rows = rows + '<div class="item">...</div>';
	}

	if (i>0) {

		html = '<div>';
		html = html + rows;
		html = html + '</div>';

		place.innerHTML = html;
		place.style.display = 'block';
		activeWeather = 1;
		setActiveWeatherHelp(0);

	} else {
		hideWeatherHelp();
	}
}

function hideWeatherHelp()
{
	activeWeather = 0;
	document.getElementById('weather_search').style.display = 'none';
}

function setActiveWeatherHelp(num)
{
//	document.getElementById('weather_item_'+activeWeatherItem).setAttribute("className","item");
	document.getElementById('weather_item_'+activeWeatherItem).className="item";
//	document.getElementById('weather_item_'+activeWeatherItem).setAttribute("class","item");
//	document.getElementById('weather_item_'+num).setAttribute("className","item_over");
	document.getElementById('weather_item_'+num).className="item_over";
//	document.getElementById('weather_item_'+num).setAttribute("class","item_over");
	activeWeatherItem = num;
}

function setWeatherQuery()
{
	document.getElementById('weather_search_q').value = document.getElementById('weather_value_'+activeWeatherItem).innerHTML;
	activeWeatherItem = 0;
	hideWeatherHelp();
	document.getElementById('search_form').submit();
}

function fixQuery()
{
	fixedQuery = document.getElementById('weather_search_q').value;
}
