var ref = document.referrer.toLowerCase();
if (ref.indexOf('autosurf-ru.com') !=-1 || ref.indexOf('hitstream.net') !=-1)
	window.location.href = 'http://i.ua/r.php?3';

function i_showFloat(act, url, suf)
{
	if (typeof(suf) == 'undefined')
		suf = 'Login';

	var ab = document.getElementById('Float' + suf);
	var m = document.getElementById('FloatMask' + suf);
	var dis;

	if(act) {
		m.style.display = 'block';
		if (b.opera)
			m.style.background = 'none';
		var sHeight = !b.ie ? window.innerHeight : m.offsetHeight;
		var bHeight = self.document.body.offsetHeight;
		var bWidth = self.document.body.offsetWidth;
		var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
		ab.style.overflow = 'auto';
		m.style.height = bHeight + 'px';
		ab.style.display = 'block';
		ab.style.left = (bWidth - ab.offsetWidth)/2 + 'px';
		if(!b.moz){
			ab.focus();
			ab.style.outline = 'none';
		}
		dis = 'hidden';
		if (url)
			document.lFloat._url.value = url;
		if (suf == 'Login')
			setTimeout('document.lFloat.login.focus();', 100);
	} else {
		ab.style.display = 'none';
		m.style.display = 'none';
		dis = 'visible';
	}

	toggleDisplay('SELECT', dis);
	toggleDisplay('OBJECT', dis);
	toggleDisplay('EMBED', dis);
	toggleDisplay('IFRAME', dis);

	return false;
}

function i_lForm(form)
{
	form._rand.value = Math.random();
	var pass = form.pass.value;
	if (!pass.match(/^\w+$/i) || !form.scode)
		return true;

	var crypt_pass = hex_md5(pass + form.scode.value);
	form.cpass.value = crypt_pass;
	form.pass.value = '';

	return true;
}

var i_blinkMail;
var i_blinkMailTimer;
var i_blinkMailStep;
function i_blinkM()
{
	if (!i_blinkMail) {
		i_blinkMail = document.getElementById('blinkMail');
		if (!i_blinkMail)
			return;
	}
	if (!i_blinkMailStep) {
		i_blinkMail.style.color='#778';
		i_blinkMailTimer = setTimeout(i_blinkM, 200);
	} else {
		i_blinkMail.style.color='#000';
		i_blinkMailTimer = setTimeout(i_blinkM, 1000);
	}

	i_blinkMailStep = !i_blinkMailStep;
}

var i_reqMail;
var i_chkEmlUrl = 'http://i.ua/check_mail.php';
function i_checkNewMail()
{
	if (!window.i_user)
		return;
	if (!i_blinkMail) {
		i_blinkMail = document.getElementById('blinkMail');
//		if (!i_blinkMail) {
//			clearInterval(i_checkInterval);
//			return;
//		}
	}
	if (!i_reqMail)
		i_reqMail = new Subsys_JsHttpRequest_Js();

	i_reqMail.onreadystatechange = function() {
		if (i_reqMail.readyState == 4) {
			var count = i_reqMail.responseJS.unseen;
			if (count <= 0)
				count = 0;
			var newMail = document.getElementById('newMail');
			var title = document.title.replace(/\[\d+\]$/, '');
			if (i_blinkMailTimer) {
				clearTimeout(i_blinkMailTimer);
				i_blinkMailTimer = null;
				i_blinkMail.style.color='#000';
			}
			if (count > 0) {
//				alert(count);
				document.title = title + '[' + count + ']';
				i_blinkM();
				var elem = newMail.getElementsByTagName('A')[0];
				elem.title = elem.title.replace(/: \d+$/, ': ' + count);
				elem = newMail.getElementsByTagName('IMG')[0];
				elem.title = elem.title.replace(/: \d+$/, ': ' + count);
				newMail.style.display = '';
			} else {
				document.title = title;
				newMail.style.display = 'none';
			}
			if (i_blinkMail)
				i_blinkMail.title = i_blinkMail.title.replace(/: \d+$/, ': ' + count);

//			aaa
		}
	}

	i_reqMail.caching = false;
//	i_reqMail.open('POST', 'http://chk.i.ua/?request=check_mail', true);
	i_reqMail.open('GET', i_chkEmlUrl, true);
	i_reqMail.send({ user: window.i_user });
}

var i_checkInterval = setInterval(i_checkNewMail, 300000);

var i_reqSCode;
function i_updateSCode()
{
	if (!i_reqSCode)
		i_reqSCode = new Subsys_JsHttpRequest_Js();

	i_reqSCode.onreadystatechange = function() {
		if (i_reqSCode.readyState == 4) {
			var scode = i_reqSCode.responseJS.scode;

			var form;
			for (var i = 0; i < document.forms.length; i++) {
				form = document.forms[i];

				if (form.name != 'lFloat' && form.name != 'lform')
					continue;

				var inp = form.scode;
				if (!inp)
					continue;

//				alert(form.name + ' ' + inp.value + ' ' + scode);
				inp.value = scode;
			}
		}
	}

	i_reqSCode.caching = false;
	i_reqSCode.open('GET', 'http://passport.i.ua/js/scode.js', true);
	i_reqSCode.send();
}

var i_sCodeInterval = setInterval(i_updateSCode, 3600000);

function i_removeDefVal(input)
{
	input.value = '';
	input.onfocus = null;
}

function i_keypress(e)
{
	if (!e)
		e = event;

//	DBG(e);
//	return;

	var keyCode = e.keyCode;
	if (e.which)
		keyCode = e.which;
	var tag = '';
	if (e.srcElement)
		tag = e.srcElement.tagName;
	else if (e.target)
		tag = e.target.tagName;

//	alert(keyCode);
//	alert(e.ctrlKey);
//	alert(tag);
//	return;

	switch (keyCode) {
	case 13:
		if (!e.ctrlKey)
			break;
	case 10:
		break;

		var text = null;
		if (window.getSelection) {
//			alert('window.getSelection');
			text = window.getSelection();
		} else if (document.getSelection) {
//			alert('document.getSelection');
			text = document.getSelection();
		} else if (document.selection.createRange) {
//			alert('document.selection.createRange');
			text = document.selection.createRange().text;
		}
		text = String(text);
		if (text.length > 0) {
			if (text.length > 256) {
				alert('Выделите текст меньшего размера.')
			} else {
				if (confirm('Oшибка на странице:\n"' + text + '"\n\nОтослать сообщение об ошибке службе поддержки?\nВаш броузер останется на той же странице.')) {
					var fakedImage = document.createElement('IMG');
					fakedImage.src = 'http://i.ua/page_error.php?text=' + escape(text) + '&url=' + escape(document.location.href);
//					alert(fakedImage.src);
				}
			}
		}

		break;
	case 37:
	case 39:
		if (!e.ctrlKey || tag == 'INPUT' || tag == 'TEXTAREA' || tag == 'SELECT')
			break;

		var link = document.getElementById(keyCode == 37 ? 'prevLink' : 'nextLink');
		if (!link || !link.href)
			break;

		window.location.href = link.href;

		break;
	}
}

if (!b || b.ie) {
	document.onkeydown = i_keypress;
} else {
	if (document.attachEvent)
		document.attachEvent('onkeydown', i_keypress);
	else
		document.addEventListener('keydown', i_keypress, false);
}

function i_showSections(elem)
{
//	alert('i_showSections');
	var allSec = document.getElementById('sections');
	if (!allSec)
		return;

	var cont = elem.parentNode.parentNode;
	var childs = cont.getElementsByTagName('LI');
//	alert(childs.length);
	for (var i = 1; i < childs.length; i++) {
		if (childs[i].className.match(/TopControl/))
			continue;

		childs[i].style.display = childs[i].style.display == 'none' ? 'block' : 'none';
	}
//	alert(cont.tagName);

//	alert(allSec.style.display);
	allSec.style.display = allSec.style.display == 'block' ? 'none' : 'block';
}

// Shows menu with loading friends online
poToggledObjects = new Array(); // List of blocks on screen that were toggled off when showing peopleOnline
function i_showPeopleOnline() {
	var cell = dge('cellPeopleOnline');
	if (!cell)
		return;

	// Create Div for elements if needed
	var divInsert = dge('menuPeopleOnline');
	if (!divInsert) {

		// Find where to insert it
		var divAfter = cell.parentNode;
		while (divAfter && (divAfter.tagName != 'DIV'))
			 divAfter = divAfter.parentNode;
		if (!divAfter)
			return;

		// Compose element
		var divInsert = dce('DIV');
		divInsert.className = 'TopControlExtended';
		divInsert.id = 'menuPeopleOnline';

		divAfter.nextSibling ? divAfter.parentNode.insertBefore(divInsert, divAfter.nextSibling) : divAfter.parentNode.appendChild(divInsert);

		// Make "Loading..." state of control
		i_poSetLoading(divInsert);
	}

	// Remove flash/select/iframe and other objects underneath
	i_poHideBadObjects(divInsert);

	// Show element
	divInsert.style.display = 'block';

	// Update cell and switch
	cell.className = 'calledExtended';

	var toggle = dge('togglePeopleOnline');
	if (toggle)
		toggle.onclick = i_closePeopleOnline;

	// Load people
	lmCallAfterModulesReady(['PEOPLE_ONLINE'], i_poReady);
}

// Sets 'Loading...' state to PeopleOnline control
// container is used just for optimization, can be undefined
function i_poSetLoading(container) {
	if (!container)
		container = dge('menuPeopleOnline');
	if (!container)
		return;

	// Localization
	var stLoading = 'Загрузка';
	switch (JS_LANG_ID) {
		case 2: {
			stLoading = 'Завантаження';
			break;
		}
		case 3: {
			stLoading = 'Loading';
			break;
		}
	}

	// Compose innerHTML
	var innerHTML = '<div class="l">&nbsp;</div>';
	innerHTML += '<p class="chatUsers">';
	innerHTML += '<div class="clear preload">' + stLoading + '&#133;</div>';
	innerHTML += '</p>';

	container.innerHTML = innerHTML;
}

// Hides bad objects (flash, SELECT and so on) that can interfere with listing of friends online container
function i_poHideBadObjects(container) {
	// If not visible - then toggle objects underneath and show
	var wasVisible = container.style.display == 'block';
	if (!wasVisible) {
		container.style.visibility = 'hidden';
		container.style.display = 'block';
	}

	var hiddenTags = ['SELECT','OBJECT','EMBED','IFRAME'];
	var objPos = getPosition(container);

	for (var i = 0; i < hiddenTags.length; i++)
		poToggledObjects = poToggledObjects.concat(toggleDisplay(hiddenTags[i], 'hidden', null, objPos));

	if (!wasVisible) {
		container.style.visibility = 'visible';
		container.style.display = 'none';
	}
}

// Reaction on pOnline object loaded
function i_poReady () {
	po_checkStart();
}

// Hides block of online people. Used only when managing class was not loaded still, because will be overwritten by it later.
function i_closePeopleOnline() {
	// Hide block
	var block = dge('menuPeopleOnline');
	if (block)
		block.style.display = 'none';

	// Show toggled objects
	for (var i = 0; i < poToggledObjects.length; i++)
		poToggledObjects[i].style.visibility = 'visible';
	poToggledObjects = new Array();

	// Update cell and switch
	var cell = dge('cellPeopleOnline');
	if (cell)
		cell.className = 'callExtended';

	var toggle = dge('togglePeopleOnline');
	if (toggle)
		toggle.onclick = i_showPeopleOnline;
}

function i_getBottomPos()
{
	if (window.innerWidth) {// ff & opera
		WH = window.innerHeight;
		pixelTop = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.clientWidth) {// ie
		WH = document.documentElement.clientHeight;
		pixelTop = document.documentElement.scrollTop;
	} else if (document.body) {
		WH = document.body.clientHeight;
		pixelTop = document.body.scrollTop
	}
	if (!bottomVisible && WH + pixelTop > bottomBannerHeight) {
		bottomVisible = true;
		if (typeof(viewAdsUrl)!='undefined') {
			tmp = new Image();
			tmp.src=viewAdsUrl;
		}
		window.onscroll = null;
		window.onresize = null;
	}
}