
var logintimer=null;
var emailtimer=null;
var loginfocus=false;

var errorstr=new Array();

var my_ajax = new dle_ajax();

errorstr[0]='Вы ввели недопустимый символ';
errorstr[1]='Вы ввели недостаточно символов';
errorstr[2]='Вы ввели больше символов';
errorstr[3]='Вы ввели недопустимый email';
errorstr[4]='Пароль слишком короткий';
errorstr[5]='Пароли не совпадают';
errorstr[6]='Неверное число символов';
errorstr[7]='Введите не менее 4 символов';
errorstr['login']='Пользователь с таким именем уже существует';
errorstr['login2']='Пользователь с таким именем не существует';
errorstr['email']='Такой E-Mail уже зарегистрирован';
errorstr['server']='Ошибка сервера. Попробуйте позже.';



function checkpost(type, id)
{
	
	function onAjaxSuccess(data)
	{
//		alert(data);
		if (data=="ok")
		{
			$("#"+type+"_error").addClass('ok');
			$("#"+type+"_error").html('V');
		} else if (data=="error") {
			$("#"+type+"_error").removeClass('ok');
			$("#"+type+"_error").html(errorstr[type]);
		} else {
			$("#"+type+"_error").removeClass('ok');
			$("#"+type+"_error").html(errorstr['server']);
		}
	}
	
	$.post(
		'/engine/ajax/checkform.php',{type: type, id: id},onAjaxSuccess
	);
	
}


function checke(field)
{

	
		$("#email_error").removeClass('ok');
		
		if(field.match(/[^a-z0-9\_\.\-\@]+/i))
		{
			
			$("#email_error").html(errorstr[0]);

		} else if (!field.match(/^[a-z0-9][a-z0-9_.-]*@[a-z0-9_.-]+\.[a-z]{2,7}$/i)) {
			
			$("#email_error").html(errorstr[3]);

		} else {
			$("#email_error").html('');
			
			clearTimeout(emailtimer);
			emailtimer = setTimeout(function(){
				checkpost("email", field);
			}, 500);
		}
		

	
	return ;
	
}



$(document).bind("ready", function() {

	$("#search-tips").click(function() {
		$("#search-query").attr("value",$(this).text());
	});
	
	
	
	$("input[name='password1']").bind("change keyup", function(){
		field=$("input[name='password1']").attr("value");
		
		if (field.length < 6) {
			
			$("#password1_error").removeClass('ok');
			$("#password1_error").html(errorstr[4]);
			
		} else {
			
			$("#password1_error").addClass('ok');
			$("#password1_error").html('V');
	
		}
	});	
	
	
	$("input[name='password2']").bind("change keyup", function(){
		field=$("input[name='password2']").attr("value");
		
		if (field.length < 6 || field!=$("input[name='password1']").attr("value")) {
			
			$("#password2_error").removeClass('ok');
			$("#password2_error").html(errorstr[5]);
			
		} else {
			
			$("#password2_error").addClass('ok');
			$("#password2_error").html('V');
	
		}
	});	
	
	
	$("input[name='story']").bind("change keyup", function(){
		field=$("input[name='story']").attr("value");
		
		if (field.length < 4 ) {
			
			$("#story_error").removeClass('ok');
			$("#story_error").html(errorstr[7]);
			
		} else {
			
			$("#story_error").addClass('ok');
			$("#story_error").html('V');
	
		}
	});	
	
	
	
	$("#close").bind("click", function(){
		
		$(".b-alert").css({"display": "none"});
		
		$.post(
			'/engine/ajax/hint.php',{type: 1},function(data) {	}
		);
		
		return false;
	});
	
	
//	$("body.screenshots table td a img").load(function()
//	{

		var screen_run=false;
		var table_width=parseInt($("div.screenshots table").width());
		var div_width=parseInt($("div.screenshots").width());
		var eshe=table_width-div_width;
		var width_scroll=150;
		var time_scroll=600;

//		alert(table_width+"-"+div_width);
		
		$("div.b-film-info-screenshots a.preview").bind("click",function() {

			if(screen_run)
				return false;

			table_width2=parseInt($("div.screenshots table").width());
			if (table_width<table_width2)
			{
				table_width=table_width2;
				div_width=parseInt($("div.screenshots").width());
				eshe=table_width-div_width;
			}

			$("div.b-film-info-screenshots a.next").removeClass( "disabled" );

			contLeft=parseInt($("div.screenshots table").css("marginLeft"));

			ostatok=eshe+contLeft;

			if (contLeft<-width_scroll)
			{
				screen_run=true;
				$("div.screenshots table").animate({"marginLeft":"+="+width_scroll+"px"},time_scroll, function(){screen_run=false;});
			}
			else if (contLeft>=-width_scroll)
			{
				screen_run=true;
				$("div.screenshots table").animate({"marginLeft":"0px"},time_scroll, function(){screen_run=false;} );
			}

			if (contLeft>=-width_scroll)
			{
				$("div.b-film-info-screenshots a.preview").addClass( "disabled" );
			}

			return false;
		});



		$("div.b-film-info-screenshots a.next").bind("click",function() {

			if(screen_run)
				return false;

			table_width2=parseInt($("div.screenshots table").width());
			if (table_width<table_width2)
			{
				table_width=table_width2;
				div_width=parseInt($("div.screenshots").width());
				eshe=table_width-div_width;
			}

			$("div.b-film-info-screenshots a.preview").removeClass( "disabled" );

			contLeft=parseInt($("div.screenshots table").css("marginLeft"));

			ostatok=eshe+contLeft;

			//		$("p.column1").html($("p.column1").html()+'<br>o-'+$("div.screenshots table").width());

			if (ostatok>=width_scroll)
			{
				screen_run=true;
				$("div.screenshots table").animate({"marginLeft":"-="+width_scroll+"px"},time_scroll, function(){screen_run=false;});
			}
			else if (ostatok>0)
			{
				screen_run=true;
				$("div.screenshots table").animate({"marginLeft":"-="+ostatok+"px"},time_scroll, function(){screen_run=false;});
			}

			if (ostatok<=width_scroll)
			{
				$("div.b-film-info-screenshots a.next").addClass( "disabled" );
			}

			return false;
		});

		
	var search_input = false;
	var search_result = false;
	var search_timer = null;
	
	function search_result_check()
	{
		$('div#s-result').css( 'display' , 'none' );
		
		clearTimeout(search_timer);
	}
	
		
		
	$('input.search-field').bind("keyup", function(){
		
		var s = $(this).val();

		if (s.length < 3)
		{
			$('div#s-result').css( 'display' , 'none' );
			
			return;
		}

		function onAjaxSuccess(data)
		{			

			if (data.length == 0)
			{
				$('div#s-result').css( 'display' , 'none' );
				
			} else {
				
				$('div#s-result').html( data );
				$('div#s-result').css( 'display' , 'block' );
			}
		}
		
		$.post(
			'/engine/ajax/search.php', {
				s : s
			}, onAjaxSuccess
		);

	});
	
	$('input.search-field').bind("blur", function() { 
		
		search_input = false;
		
		if (!search_result)
		{
			search_timer = setTimeout(function() {
				search_result_check();
			}, 500);
		}
		
	});
	
	$('input.search-field').bind("focus", function() { 
		
		search_input = true;
		
		clearTimeout(search_timer);

	});
		
	
	$('div#s-result').bind("mouseenter", function() {

		search_result = true;
		
		clearTimeout(search_timer);
		
	});
	
		
	$('div#s-result').bind("mouseleave", function() { 
		
		search_result = false;
		
		if (!search_input)
		{
		
			search_timer = setTimeout(function() {
				search_result_check();
			}, 500);
		}
	});
		
	
});

