$(function () {
	var is_logon = $.cookie('logon')=="yes"?true:false;
	var sLang = $("html").attr("lang");
	var jLoginFormContainer = $("#login-form");
	var jSalesFormContainer = $("#increase-sales");
	var jLoginInput = $("#auth_name");
	var jPasswdInput = $("#auth_passwd");
	var jSubmit = $(":submit",jLoginFormContainer);
	var sUserName = $.cookie('auth.name');
	var jElementToFucus = jLoginInput;
	var jErrors = jLoginFormContainer.find(".errors");
		jErrors.addClass("hidden");
	var jLoginForm = jLoginFormContainer.find("form");
	var oFormData = {};
	var isAuthErrors = false;
	if(jLoginInput.get(0) && sUserName){
		jLoginInput.get(0).value = sUserName;
		jLoginInput.change();
		jElementToFucus = jPasswdInput;
	}
if(document.getElementById("form_authorisation")){
	ZForms.getFormById("form_authorisation").enableSubmit();
}

	/* ajax-submit формы авторизации */
	jLoginForm.submit(function(){

		var result = true;
		jErrors.addClass("hidden");
		// собираем данные формы
		oFormData = {"is_ajax": "true", lang: sLang, region: sRegionAbbr};
		jLoginForm.find("input").each(function () {
			oFormData[this.name] = this.value;
		});
		$.getJSON(
			'/_ajax/auth/',
			oFormData,
			function(data){
				if (data.js.status === "error") {
					jErrors.html(data.js.message).removeClass("hidden");
				} else {
					if(data.js.redirect_url){
						document.location = data.js.redirect_url;
					}else{
						document.location.reload();
					}
				}
			},
			"json"
		);
		return false;
	});


	var oPopupRegistration = popup_block({
		container: $("#registration-form"),
		link: $("#registaration-link"),
		fader: $("#fader"),
		close: $("#registration-form span.btn-close"),
		onBlockShow: function(){
			oPopupLogin.hide();
			jElementToFucus.focus();
			jErrors.addClass("hidden");
			/*
			window.setTimeout(function(){
				var zF = ZForms.getFormById("form_authorisation");
				//var zW = zF.getWidgetById("auth_passwd");
				if(jPasswdInput.get(0).value){ zF.enableSubmit() }
			}, 1000);
			dfsdjflkj*/
		}
	});

	var oPopupSales  = popup_block({
		container: $("#increase-sales"),
		link: $("#sale-link-main"),
		fader: $("#fader"),
		close: $("#increase-sales span.btn-close"),
		onBlockShow: function(){
			oPopupInteriorphoto.hide();
			oPopupPriemi.hide();
			oPopupArciphoto.hide();
			$("#priemi-link").focus();
			jErrors.addClass("hidden");
		}
	});
	
	var oPopupSales_back  = popup_block({
		container: $("#increase-sales"),
		link: $("#sale-link"),
		fader: $("#fader"),
		close: $("#increase-sales span.btn-close"),
		onBlockShow: function(){
			oPopupPriemi.hide();
			oPopupInteriorphoto.hide();
			oPopupArciphoto.hide();
			jElementToFucus.focus();
			jErrors.addClass("hidden");
		}
	});
	
	var oPopupSales_back1  = popup_block({
		container: $("#increase-sales"),
		link: $("#sale1-link"),
		fader: $("#fader"),
		close: $("#increase_sales span.btn-close"),
		onBlockShow: function(){
			oPopupPriemi.hide();
			oPopupInteriorphoto.hide();
			oPopupArciphoto.hide();
			jElementToFucus.focus();
			jErrors.addClass("hidden");
		}
	});
	
	var oPopupSales_back2  = popup_block({
		container: $("#increase-sales"),
		link: $("#sale2-link"),
		fader: $("#fader"),
		close: $("#increase-sales span.btn-close"),
		onBlockShow: function(){
			oPopupPriemi.hide();
			oPopupInteriorphoto.hide();
			oPopupArciphoto.hide();
			jElementToFucus.focus();
			jErrors.addClass("hidden");
		}
	});
	
		var oPopupSales_back  = popup_block({
		container: $("#increase-sales"),
		link: $("#sale-linkh"),
		fader: $("#fader"),
		close: $("#increase-sales span.btn-close"),
		onBlockShow: function(){
			oPopupPriemi.hide();
			oPopupInteriorphoto.hide();
			oPopupArciphoto.hide();
			jElementToFucus.focus();
			jErrors.addClass("hidden");
		}
	});
	
	var oPopupSales_back1h  = popup_block({
		container: $("#increase-sales"),
		link: $("#sale1-linkh"),
		fader: $("#fader"),
		close: $("#increase_sales span.btn-close"),
		onBlockShow: function(){
			oPopupPriemi.hide();
			oPopupInteriorphoto.hide();
			oPopupArciphoto.hide();
			jElementToFucus.focus();
			jErrors.addClass("hidden");
		}
	});
	
	var oPopupSales_back2h  = popup_block({
		container: $("#increase-sales"),
		link: $("#sale2-linkh"),
		fader: $("#fader"),
		close: $("#increase-sales span.btn-close"),
		onBlockShow: function(){
			oPopupPriemi.hide();
			oPopupInteriorphoto.hide();
			oPopupArciphoto.hide();
			jElementToFucus.focus();
			jErrors.addClass("hidden");
		}
	});
	
	var oPopupArciphoto = popup_block({
		container: $("#archiphoto-form"),
		link: $("#archiphoto-link"),
		fader: $("#fader"),
		close: $("#archiphoto-form span.btn-close"),
		onBlockShow: function(){
			oPopupSales.hide();
			oPopupInteriorphoto.hide();
			oPopupPriemi.hide();
			jElementToFucus.focus();
			jErrors.addClass("hidden");
		}
	});

	var oPopupArciphotoDetail = popup_block({
		container: $("#archiphoto-form"),
		link: $("#archiphoto-link-detail"),
		fader: $("#fader"),
		close: $("#archiphoto-form span.btn-close1"),
		onBlockShow: function(){
			oPopupSales.hide();
			oPopupInteriorphoto.hide();
			oPopupPriemi.hide();
			jElementToFucus.focus();
			jErrors.addClass("hidden");
		}
	});
	
	var oPopupInteriorphoto = popup_block({
		container: $("#interiorphoto-form"),
		link: $("#interiorphoto-link"),
		fader: $("#fader"),
		close: $("#interiorphoto-form span.btn-close"),
		onBlockShow: function(){
			oPopupSales.hide();
			oPopupArciphoto.hide();
			oPopupPriemi.hide();
			jElementToFucus.focus();
			jErrors.addClass("hidden");
		}
	});
	
	var oPopupInteriorphotoDetail = popup_block({
		container: $("#interiorphoto-form"),
		link: $("#interiorphoto-link-detail"),
		fader: $("#fader"),
		close: $("#interiorphoto-form span.btn-close1"),
		onBlockShow: function(){
			oPopupSales.hide();
			oPopupArciphoto.hide();
			oPopupPriemi.hide();
			jElementToFucus.focus();
			jErrors.addClass("hidden");
		}
	});
	
	var oPopupPriemi = popup_block({
		container: $("#priemi-form"),
		link: $("#priemi-link"),
		fader: $("#fader"),
		close: $("#priemi-form span.btn-close"),
		onBlockShow: function(){
			oPopupSales.hide();
			oPopupArciphoto.hide();
			oPopupInteriorphoto.hide();
			jElementToFucus.focus();
			jErrors.addClass("hidden");
		}
	});

	var oPopupPriemiDetail = popup_block({
		container: $("#priemi-form"),
		link: $("#priemi-link-detail"),
		fader: $("#fader"),
		close: $("#priemi-form span.btn-close1"),
		onBlockShow: function(){
			oPopupSales.hide();
			oPopupArciphoto.hide();
			oPopupInteriorphoto.hide();
			jElementToFucus.focus();
			jErrors.addClass("hidden");
		}
	}); 
	if(is_logon){
		// нужно загрузить пользовательские данные (визитку и ссылки на заявки)
		$.post(
			"/_ajax/auth/data.html",{
				lang: sLang,
				region: sRegionAbbr
			},
			function(data){
				$("#site_sections div.profile").addClass("profile-enabled")
				 .html(data.js.personal_block)
				 .removeClass("hidden");
				$("#main_requests_links").html(data.js.app_links);
			},
			'json'
		);
	} else {
		$("#main_requests_links").hide();
		$("#site_sections div.profile").removeClass("hidden");
		oPopupLogin = popup_block({
			container: jLoginFormContainer,
			link: $("#site_sections div.profile a"),
			fader: $("#fader"),
			close: $("#login-form span.btn-close"),
			onBlockShow: function(){
				oPopupRegistration.hide();
				jElementToFucus.focus();
				jErrors.addClass("hidden");
			}
		});
	}



	/* проверка занятости емейла или логина */
	CheckField = function(jContainer, sType){
		if(!jContainer.length) return;
		var that = this;
		that.container = jContainer;
		that.input = $("input",this.container);
		that.message = $("ins.example",this.container);
		var oForm = ZForms.getFormById(that.input.parents("form:first").attr("id"));
		var oWidget = ZForms.getFormById(that.input.parents("form:first").attr("id")).getWidgetById(that.input.attr("id"));

		var hData = {};
		hData.lang = sLang;

		that.input.bind("change, keyup",function(){
			if( !that.container.hasClass("invalid") ){
				hData[sType] = $(this).attr("value");
				$.get("/_ajax/auth/check.html", hData,
					function(data){
						if(data && data.type == 'occupied'){
							oWidget.setInvalid();
							oForm.disableSubmit();
							that.message.html(data.message);
						}
						if(data && data.type == 'available'){
							that.message.html(data.message);
						}
					},
					'json'
				);
			} else {
				that.message.html('');
			}
		});
	};
	/* * * * */

	/* для формы регистрации проверяем занятость емейла и логина */
	oCheckEmailField = new CheckField($("dl#r_auth\\.email"), "email");
	oCheckLoginField = new CheckField($("dl#r_auth\\.login"), "login");

	/* Блок "Гляди, что здесь Est!" */
	LookHere = function(jContainer){
		if(!jContainer.length) return;
		var that = this;
		that.container = jContainer;
		that.data = $("div.data",jContainer);
		that.content = $("div.content",jContainer);
		that.reloader = $(".reloader:first",jContainer);
		var iBlockAmount = that.data.length;
		var iCurrentBlock = Math.round(Math.random()*(iBlockAmount-1));

		that.refreshContent = function(){
			that.content.html(that.data.eq(iCurrentBlock).html());
		};

		that.reloader.click(function(){
			if((iCurrentBlock+1) == iBlockAmount){
				iCurrentBlock = 0;
			} else {
				iCurrentBlock += 1;
			}
			that.refreshContent();
			return false;
		});

//		that.refreshContent();
	};

	LookHere( $("#info_box") );
});
