var last_form = false
var check_close = false
var inform_f = false
var inform_c = false

$j(document).click(function(){
	//alert(inform_f+' '+inform_c+' '+check_close)
	if (!inform_f && !inform_c && check_close) {
		auth('close')
	}
	if(inform_f && inform_c){
		inform_c = false
		inform_f = false
	}
})

$j(window).keydown(function(event){
  //alert(event.keyCode);
  if(event.keyCode == 27){
	auth('close')
  }
});

function auth(v) {
	//alert(1)
	var popup = document.getElementById('auth_popup')
	var f = document.getElementById('auth-popup-form')
	var fs = document.getElementById('auth-popup-form-shadow')
	var fc = document.getElementById('auth-popup-form-close')
	if (!popup) return
	popup.style.height = document.body.offsetHeight + 5 + 'px'
	document.getElementById('auth-popup-full-shadow').style.height = document.body.offsetHeight + 5 + 'px'
	if (v == 'close') {
		if (last_form) {
			document.getElementById('auth-popup-'+last_form).style.display = 'none'
		}
		popup.style.display = 'none'
		check_close = false
	} else {
		if (last_form) {
			document.getElementById('auth-popup-'+last_form).style.display = 'none'
		}
		last_form = v
		document.getElementById('auth-popup-'+last_form).style.display = 'block'
		popup.style.display = 'block'
		document.getElementById('auth-popup-shadow-middle').style.height = document.getElementById('auth-popup-content').offsetHeight + 64 + 'px'
		check_close = true
	}
	if (v == 'login') {
		f.style.top = '170px';fs.style.top = '197px';fc.style.top = '176px'
	} else if (v == 'sclerosis') {
		f.style.top = '185px';fs.style.top = '213px';fc.style.top = '191px'
	} else if (v == 'registr') {
		f.style.top = '15px';fs.style.top = '43px';fc.style.top = '21px'		
	} else {
		f.style.top = '';fs.style.top = '';fc.style.top = ''
	}
	
	inform_c = true
	inform_f = true
	$j("#auth-popup-form").mouseout(function(){
		inform_f = false
	})
	$j("#auth-popup-form").mouseover(function(){
		inform_f = true
	})
	$j("#auth-popup-form-close").mouseout(function(){
		inform_c = false
	})
	$j("#auth-popup-form-close").mouseover(function(){
		inform_c = true
	})
	//alert(2)
}

function auth_check (obj) {
	if (obj.getElementsByTagName('input')[0].value == 'N') {
		obj.style.backgroundPosition = '0px -13px'
		obj.getElementsByTagName('input')[0].value = 'Y'
	} else {
		obj.style.backgroundPosition = '0px 0px'
		obj.getElementsByTagName('input')[0].value = 'N'
	}
}

function input_focus (id, text)
{
	qs_input = document.getElementById(id);
	if(qs_input){
		if (qs_input.value == text)  {
			qs_input.value = '';
		}
	}	
}

function input_onblur (id, text)
{
	qs_input = document.getElementById(id);
	if(qs_input){
		if (qs_input.value == ''){
			qs_input.value = text;
		}
	}
}

$j(document).ready(function(){
	var form;
	$j("#ajax-login").click(function(){
		if(form) {
			auth('login');
		}
		else {
			$j("#auth-popup-content").load('/includes/form.php',{'action':'login'});
			form=true;
		}
	})
	$j("#ajax-reg").click(function(){
		if(form) {
			auth('registr');
		}
		else {
			$j("#auth-popup-content").load('/includes/form.php',{'action':'registr'});		
			form=true;
		}
	})
})