var LinkDivActive=0;
var onDiv;
var timeOnDiv;
var layerRef;
var styleRef;
var showVar;
var hideVar;
var ua=navigator.userAgent.toLowerCase();
var dom=(document.getElementById) ? 1 : 0;
var nn4=(document.layers) ? 1 : 0;
var ie=(((ua.indexOf('msie')!=-1) && (ua.indexOf('opera')==-1))) ? 1 : 0;
var opera=(ua.indexOf('opera')!=-1) ? 1 : 0;
var pos=new Array();
var XMenuOffset=0;
var YMenuOffset=10;

var nochange;
var cla;

if (dom) {
	layerRef="document.getElementById(divId)";
	styleRef=".style";
	showVar="'visible'";
	hideVar="'hidden'";
} else if (ie) {
	layerRef="document.all[divId]";
	styleRef=".style";
	showVar="'visible'";
	hideVar="'hidden'";
} else if (nn4) {
	layerRef="document.layers[divId]";
	styleRef="";
	showVar="'show'";
	hideVar="'hide'";
}

function hideDiv(divId) {
	$(divId).style.display='none';
	if(!nochange && cla)	{
		$('link_more').className = "more";
		cla = false;
	}
}

function showDiv(divId) {
	if (eval(layerRef))	eval(layerRef+styleRef+'.visibility='+showVar);
}

function m_show(divId, cl) {
	if (eval(layerRef)) {
		if (timeOnDiv != null) {
			clearTimeout(timeOnDiv);
			hideDiv(onDiv);
		}
		$(divId).style.display='block';
		onDiv=divId;
		cla = cl;
		if(cla) $('link_more').className = "more more_active";
	}
}

function m_timer() {
	timeOnDiv=setTimeout("divOut()",1000)
}

function divOut() {
	if (LinkDivActive==0) {
		hideDiv(onDiv);
	}
}

function m_over() {
	LinkDivActive=1;
	clearTimeout(timeOnDiv);
}

function m_out() {
	LinkDivActive=0;
	timeOnDiv=setTimeout("divOut()",500)
}

function outAll() {
	if (onDiv && LinkDivActive==0) {
		hideDiv(onDiv);
	}
}



/**
 *	Prototype Template
 */
var tpl_userInfo = new Template('<div class="userInfo"><a href="/account/" class="account_link" title="Ваш аккаунт">#{name}</a><br /><a href="/account/logout/" class="exit" onclick="logout(this); return false; ">Выход</a></div>');
/**
 *	Just string
 */
var tpl_userLogin = '<a href="/account/" title="Ваш аккаунт" onmouseover="m_show(\'form_login\', false);" onmouseout="m_timer()"><img src="/img/account.gif" alt="Ваш аккаунт" id="account" /></a>';

var tpl_Loading = '<img src="/img/loading.gif" />';

var div_account;
var div_loginForm;
var form_Login;

function login(form)
{
	field_login = $('login_email').value;
	field_password = $('login_password').value;
	div_loginForm.style.display='none';
	var URL = form.action + "?ajax";
	if(!empty(field_login)&&!empty(field_password)){
		loading_account();
		new Ajax.Request(URL, {
		  parameters: { ajax: true, email: field_login, password: field_password },
		  method: 'post',
		  encoding: 'windows-1251',
		  onSuccess: function(transport){
		  	result = transport.responseText.evalJSON();
		  	if(result['error']){
		  		alert(result['error']);
				var html = tpl_userLogin;
		  		div_account.innerHTML = html;
		  	} else {
		  		var html = tpl_userInfo.evaluate({name: result['data']});
		  		div_account.innerHTML = html;
				//div_loginForm.style.display='none';
		  	}
		  }
		});
	} else {
		alert('Укажите email и пароль');
	}
}

function logout(link)
{
	var URL = link.href;
	loading_account();
	new Ajax.Request(URL, {
		parameters: {ajax: true},
		method: 'post',
		onSuccess: function(transport){
			result = transport.responseText.evalJSON();
			if(result.status == 1){
				var html = tpl_userLogin;
		  		div_account.innerHTML = html;
				//div_loginForm.style.display='block';
			} else {
			}
		}
	});
}

function loading_account()
{
	div_account.innerHTML = tpl_Loading;	
}

window.onload = function()
{
	div_account = $('account');
	div_loginForm = $('form_login');
	form_Login = $('f_login');
}
