/**
 * переворачивает строку
 * @return {String}
 */
String.prototype.reverse = function(){
	splitext = this.split("");
	revertext = splitext.reverse();
	reversed = revertext.join("");
	return reversed;
}

function SwitchLangSelector()
{
	var ul = $("#langz");

	if (!ul.hasClass("visible"))
	{
		ul.addClass("visible");
		ul.show('fast');
	}
	else
	{
		ul.removeClass("visible");
		ul.hide('fast');
	}
}

function LoadLanguage(lang)
{
	var p = String(location.pathname);
	if (0 == p.indexOf("/"))
	{
		p = p.substring(1, p.length);
	}
	if (2 == p.indexOf("/"))
	{
		p = p.substring(3,p.length);
	}
	if ('ru' != lang)
	{
		lang = '/'+lang;
	}
	else
	{
		lang = '';
	}
	location.replace(lang+'/'+p+location.search+location.hash);

}

$(document).ready(function(){
//расставлем активные ссылки
	var lp = new String(document.location.pathname);
	lp = lp.reverse();

	var ars = $("ul.main-menu a");
	var i = 0;
	for(i=0; i < ars.length; i++)
	{
		var a = $(ars[i]);
		var h = new String(a.attr("href"));
		if (-1 != h.indexOf("?"))
		{
			h = h.substring(0, h.indexOf("?"));
		}
		if (-1 != h.indexOf("#"))
		{
			h = h.substring(0, h.indexOf("#"));
		}
		h = h.reverse();
		if (-1 != h.indexOf("/"))
		{
			h = h.substring(0, h.lastIndexOf("/"));
		}
		if (-1 != lp.indexOf(h))
		{
			a.parent().addClass("active");
			break;
		}
	}
//подвешиваемся на вызов обменника
	var mnu = $("ul.main-menu");
	$("a.exchange", mnu).attr("href", "exchange-holder.html");
	$("a.exchange", mnu).bind("click", function (/*Event*/event) {
		$.facebox({
					ajax: $(this).attr("href")
				});
		event.stopPropagation();
		return false;
	});
	$("a.history", mnu).attr("href", "history-holder.html");
	$("a.history", mnu).bind('click', function (/*Event*/event) {
		$.facebox({
					ajax: $(this).attr("href")
				});
		event.stopPropagation();
		return false;
	});
	//подвешиваемся на закрытие фейсбокса, чтобы
	//закрывать вместе с фейсбоксом и окно "попапа" касссы или истории
	$(document).bind('close.facebox', function() {
		if (_allowGameRefresh)
		{
			if ('undefined' != typeof(window.opener) && null !== window.opener)
			{
				window.opener.location.reload();
			}
//			if ('undefined' != typeof(_lastOpenGameWindow) && null !== _lastOpenGameWindow)
//			{
//				window.opener.location.reload();
//			}
		}
		if (_allowSelfClose)
		{
			try
			{
				if ('undefined' != typeof(window.opener) && null !== window.opener)
				{
					window.opener.focus();
				}
//				if ('undefined' != typeof(_lastOpenGameWindow) && null !== _lastOpenGameWindow)
//				{
//					_lastOpenGameWindow.focus();
//				}
				self.close();
				window.close();
			}catch(Exception){}
		}
    })

	//немножко правим форму регистрации!
	var td = $('div.reg form table input[type=checkbox]');
	if (td)
	{
		td = $(td).parent();
		if ( td && td.get(0) && 'TD' == td.get(0).tagName)
		{
			td.addClass("p20");
			$("a", td).each(function(){
				$(this).css("color","");
			})
		}
		$('#pN0').css("display","none");
	}


	//переключение языков
	var html = '';
	html+='<div id="langz">';
	html+='<ul>';
	html+='<li><a href="javascript:LoadLanguage(\'ua\');" class="ua">Українська</a></li>';
	html+='<li><a href="javascript:LoadLanguage(\'ru\');" class="ru">Русский</a></li>';
	html+='</ul>';
	html+='</div>';

	var box = $("#languageSwitcher");
	if (box.length > 0)
	{
		box.after(html);
	}

	$("#languageSwitcher").click(function(){
		SwitchLangSelector();
		return false;
	});
	$("#languageSwitcher").mouseover(function(){
		$(this).addClass("hover");
	});
	$("#languageSwitcher").mouseout(function(){
		$(this).removeClass("hover");
	});

	checkHashes();


});
var _allowSelfClose = false;
var _allowGameRefresh = false;
function checkHashes()
{
	try
	{

		if ('#exchange' == document.location.hash)
		{
			_allowSelfClose = true;
			_allowGameRefresh = true;
			document.location.hash = '';
			$('ul.main-menu a.exchange').focus();
			$('ul.main-menu a.exchange').click();
			window.document.body.focus();
			$('body').focus();
		}
		if ('#history' == document.location.hash)
		{
			_allowSelfClose = true;
			document.location.hash = '';
			$('ul.main-menu a.history').focus();
			$('ul.main-menu a.history').click();
			window.document.body.focus();
			$('body').focus();
		}
	}
	catch(exception){}
	setTimeout('checkHashes()', 500);
}