$(document).ready(function() {

	//select all the a tag with name equal to modal
	$('a[name=modal], input#add_car').click(function(e) {
	    //анимация
         $("#reg").click(function(){
            $('#dialog').animate({left: "0"}, "slow");

                $("#regger").show();
          		//Get the window height and width
		          var winH = $(window).height();
		          var winW = $(window).width();
            	$("#regger").css('left', winW);
                  $("#regger").animate({left: winW/2-$('#regger').width()/2},"slow");


            });




		//фокус.........
            $('#log_log').focus(function() {

			if ( $('#log_log').val()=="логин"){
				$('#log_log').val("")
				};

            $(this).css('color','#000000');
        });



            $('#log_pass').focus(function() {

			if ( $('#log_pass').val()=="пароль"){
				$('#log_pass').val("")
				};

            $(this).css('color','#000000');
        });



            $('#reg_email,#reg_log,#reg_pass,#reg_region,#log_pass_remember').focus(function() {

            $(this).css('color','#000000');
        });














		//блюр...........
	            $('#log_log').blur(function() {

			if ( $('#log_log').val()==""){
				$('#log_log').val("логин")
				$(this).css('color','silver');
				}else{
				$(this).css('color','#000000');
			   	};
        });


	            $('#log_pass').blur(function() {

			if ( $('#log_pass').val()==""){
				$('#log_pass').val("пароль")
				$(this).css('color','silver');
				}else{
				$(this).css('color','#000000');
			   	};
        });

		//................







		//Cancel the link behavior
		e.preventDefault();

		//Get the A tag
		var id = $(this).attr('href');


        login_mask(id);
        resize_lm(id);
        resize_lw(id);
        resize_lw('#regger');
	});

//login-маска
function login_mask(id){
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();

		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});

		//transition effect
		$('#mask').fadeIn(100);
		$('#mask').fadeTo("slow",0.8);

		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();

		//Set the popup window to center
		//$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);

		//transition effect
		$(id).fadeIn(2000);
        }
//коорректировка маски при ресайзе
function resize_lm(id){
        $(window).resize(function() {
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	   $('#mask').css({'width':maskWidth,'height':maskHeight});


});
}


//коорректировка окна при ресайзе
function resize_lw(id){
       		//Корректировка окна
         		//Get the window height and width
           $(window).resize(function() {
           var winW = $(window).width();
           if($(id).position().left=="0"){$(id).css('left', "0") }
           else{ $(id).css('left', winW/2-$(id).width()/2);
            }



});
}

function login_box_show(id){

        login_mask(id);
        resize_lm(id);
        resize_lw(id);
        $(id).show();

}


	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();

		$('#mask').hide();
		$('.window').hide();
        $(window).unbind('resize');
	});

	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});

// Клик по кнопке 'вход'
                jQuery('#login_button').click(function(){




				 $.ajax({
                    type: "POST",
                    url: "/auth/log_in_ajax/",
                    data: "action=login&log_log="+$('#log_log').val()+"&log_pass="+$('#log_pass').val(),
                    success: function(html){

						if(html!="" && html.slice(0,3)!='sid') {
						$('#err_place').html(html)
                        return false;

						} else {
						var login = true;
				        $('#login_sub_button').click();

                        if (todo_request!='')
                        {
                            window.location.href=""+todo_request+"?sid="+html.slice(3);
                        }
                        else
                        {
                           // window.location.reload();
                           window.location.href="/auth/visitorsinfo/garaj/";
                        }

					//при логине перезагрузка страниц, не работало автозаполнение формы браузером                    window.location.href="";
						}

                    }
                });



			    });
// enter в passworde
$('#loginus').submit(function() {
  jQuery('#login_button').click();
  return false;
});

//
// Клик по кнопке 'выход'
                jQuery('#logout_button').click(function(){
					$.ajax({
                    type: "POST",
                    url: "/auth/log_out_ajax/",
                    success: function(html){

					window.location.href="/";

                    }
              			  });
				});
//

		$('#remember_content').hide();
// Клик по кнопке 'вспомнить пароль переход на форму'
                jQuery('#rem_link').click(function(){
		$('#remember_content').show();
		$('#dialog_content').hide();
        return false;
				});
////////


// Клик по кнопке 'возврат(от вспомнить пароль)'
				jQuery('#to_login_link').click(function(){
						$('#remember_content').hide();
						$('#dialog_content').show();
                        return false;

				});
////////

// Клик по кнопке 'вспомнить пароль'
                jQuery('#rem_pass_button').click(function(){
					$.ajax({
                    type: "POST",
                    url: "/auth/log_lostpsw_ajax/",
					data: "usernameoremail="+$('#log_pass_remember').val(),
                    success: function(html){

							if(html!="") {

							 $('#err_rem_place').css({'color':'#AA2222'}).html(html);
						//	alert(obj.err);
							} else {
							 $('#err_rem_place').html('Новый пароль выслан вам на почу').css({'color':'green'});
							 $('#log_pass_remember').val("");
							}

						 }
              			});
				});
////////


// Форма регистрации

                jQuery('#regis_button').click(function(){
                    if($('#soglashenie').attr('checked')=='checked')
                    {
    					$.ajax({
                        type: "POST",
                        url: "/auth/log_reg_ajax/",
    					data: "username="+$('#reg_log').val()+"&email="+$('#reg_email').val()+"&pass="+$('#reg_pass').val()+"&region="+$('#reg_region').val(),
                        success: function(html){

    							if(html!="") {

    							 $('#err_reg_place').css({'color':'#AA2222'}).html(html);
    						//	alert(obj.err);
    							} else {
    							 $('#err_reg_place').html('Спасибо за регистрацию, новый пароль выслан вам на почту').css({'color':'green'});
    							 $('#reg_log').val("");
    							 $('#reg_email').val("");
    							 $('#reg_pass').val("");
    							}



    							}
                  			});
                    }
                    else
                    {
                        $('#err_reg_place').css({'color':'#AA2222'}).html('Вы обязаны согласиться с пользовательским соглашением');
                    }
				});


////////


if (todo_frame!='')
{

    switch(todo_frame)
    {
    case 'register':
     $("a[name=modal]").click();
     $("#reg").click();
      break;
    case 'sendpassword':
    $("a[name=modal]").click();
    $('#rem_link').click();
      break;
    case 'changepass':
        if(todo_request=="")
        {
            $('#ln_err_rem_place').addClass('green').html('Ваш пароль изменен успешно!');
        }
        else
        {
            $('#ln_err_rem_place').addClass('red').html(todo_request);
        }
        login_box_show('#login_notice');
          break;
    }



}



});

