﻿var _selectedType = 0;
var _oldValue = '';
var _cookie = 'f3.by';
var text = document.getElementById('text');
var inner = document.getElementById('inner');
var login = document.getElementById('login');
var profile = document.getElementById('profile');
var addBusiness = document.getElementById('add-business');

if (text != null)
	text.setAttribute("autocomplete", "off");
	
String.prototype.startsWith = function(String)
{
	return (String.toLowerCase() == this.substring(0, String.length).toLowerCase());
}

String.prototype.endsWith = function(String)
{
	return (String.toLowerCase() == this.substring(this.length - String.length).toLowerCase());
}

function LoadSearch(Type, SetFocus)
{
	if (!text)
		return;
		
	var _searchText = GetUrlParam('q');	
	text.value = UrlDecode(_searchText);
	
	var _temp = GetUrlParam('t');
	if (_temp != "")
		Type = _temp;
	
	if (window.location.pathname.startsWith('/video/'))
		Type = '4';
	else if (window.location.pathname.startsWith('/catalog/'))
		Type = '0';
	
	if (Type == '1')
		SetSearchType(GetSearchTypeItem('belarus'), Type, SetFocus);
	else if (Type == '2')
		SetSearchType(GetSearchTypeItem('catalog'), Type, SetFocus);
	else if (Type == '3')
		SetSearchType(GetSearchTypeItem('images'), Type, SetFocus);
	else if (Type == '4')
		SetSearchType(GetSearchTypeItem('videos'), Type, SetFocus);
	else
		SetSearchType(GetSearchTypeItem('internet'), Type, SetFocus);
}

LoadSearch(null, (GetUrlParam('q') == null || GetUrlParam('q') == "") ? true : false);

if (login != null && GetCookie('F3.BY-Login') != "")
{
	login.href = "javascript:void(0)"
	login.innerHTML = "Выйти";
	profile.style.display = 'block';
	addBusiness.style.display = 'none';
	login.onclick = function Logout()
	{
		if (!confirm('Вы уверены, что хотите выйти?')) 
			return false;
		
		SetCookie('F3.BY-Login', '');
		document.location.reload();
	}
}
else if (profile != null)
{
	profile.style.display = 'none';
	addBusiness.style.display = 'block';
}

function FillKeywords(Link)
{
	text.value = Link.innerHTML;
	text.focus();
}

function Search()
{
	SearchSite(GetUrlParam('site'));
}

function SearchSite(Site)
{
	if (Trim(text.value).length < 1)
	{
		text.focus();
		return;
	}

	if (!_selectedType)
		_selectedType = 2;

	var _site = Site;
	document.location.href = '/search?q=' + UrlEncode(text.value) + '&t=' + _selectedType + ((_site != null && _site != "" && inner != null && inner.checked) ? '&site=' + _site : ((_oldValue && _oldValue != "") ? '&old=' + UrlEncode(_oldValue) : ''));
}

function CheckKeywords(CheckBox, Text)
{
	if (CheckBox.checked)
	{
		var _old = GetUrlParam('old');
		var _site = GetUrlParam('site');
		
		if (_site == null || _site == "")
			_oldValue = _old + ((_old != null && _old != "") ? ' :: ' : '') + text.value;
		else
			_oldValue = 'site:' + _site;
			
		if (text.value == UrlDecode(GetUrlParam('q')))
			text.value = '';
	}
	else
		text.value = UrlDecode(GetUrlParam('q'));

	text.focus();
}

var _showSeo = document.getElementById('show-seo');
if (_showSeo != null)
	if (GetCookie('f3.by-seo') == 'true')
		_showSeo.checked = true;
		
$(document).ready(function() {
	$('a[href^=http]:not("[href*=://' + document.domain + ']")').click(function() {
		outgoingPageTracker._trackPageview('/outgoing/'+ $(this).attr('href'));
	});
});
