
var callbackForm = function(){
	
	this.$btn = null;
	this.$form = null;
	this.$el = null;
	var self = this;
	this.callback = null;
	
	this.init = function(btn, callback){
		
		this.callback = callback || null;

		if(typeof btn == 'string'){
			this.$btn = $(btn);
			
			if(this.$btn.attr('data-href')) this.$btn.attr('href', this.$btn.attr('data-href') );
			
			this.$btn.fancybox({
					width:600,maxWidth	: 800,fitToView	: false, 	
				autoSize:true
			});
			this.$el = $( this.$btn.attr('href') );

			this.$form = this.$el.find('form');
		} else{
			this.$el = btn;
			this.$form = this.$el.find('form')
		}
		this.$form.submit(function(){ self.submit(); return false;});
		this.$form.find('.callback-submit').attr('disabled', false);
		this.$form.find("input[name=phone]").inputmask({"mask": "+7 (999) 999-9999"});
	}
	
	this.submit = function(){
		var post = this.validate();
		if(!post) return false;
		
	//	var phone = this.$form.find.("input[name=phone]").inputmask('unmaskedvalue') || '';
	//	if(phone.length < 10){this.$form.find.('.phone-error').show();return;}
		
		this.$form.find('.callback-submit').attr('disabled', true).html('<i>Отправка...</i>');
		$.post(post._url, post, function(){
			self.$form.hide();
			if(self.$btn) $( self.$btn.attr('href') ).find('.callback-success').show();
			else self.$el.find('.callback-success').show();
			if(self.callback) self.callback();
		});
	}
	
	this.validate = function(){
		var post = this.getForm(), min, flag = true;
		self.$form.find('.err').hide();
		$.each(post, function(name, val){
			min = self.$form.find('input[name='+name+'], textarea[name='+name+']').attr('data-min');
			if(min === undefined) return;
			
			if(name == 'phone') val = self.$form.find('input[name=phone]').inputmask({"mask":"+7 (999) 999-9999"});
			if(val === null) val = '';
			
			
			min = parseInt(min);
			if(val.length >= min) return;
			if(name == 'phone' && val.length === 1) return;
			
			self.$form.find('.'+name+'-error').show();
			flag = false;
			self.$form.find('input[name='+name+'], textarea[name='+name+']').keyup(function(){
				self.$form.find('.'+name+'-error').hide();
			});
			return false;
		});
		return flag ? post : false;
	}
	
	this.getForm = function(){
		var form = this.$form.serializeArray();
		var post = {}, i;
		for(i in form) post[form[i].name] = form[i].value;
		return post;
	}
	
}

jQuery(function(){
	$('input[name=page_link]').val(document.location.pathname);
	$('input[name=page_name]').val($('h1').text());
	
	var str = '<a href="'+document.location.pathname+'">'+$('h1').text()+'</a>';
	$('.service-form-link-header').html(str);
	
	new callbackForm().init('.ask-a-question', function(){ yandexReachGoal('vopros');});	
	new callbackForm().init('.ask-a-zakaz', function(){ 
		if((location.pathname.indexOf('/products/')) == -1) yandexReachGoal('zakazprimeraraboty');
		else yandexReachGoal('zakazsustanovkoi'); 
	});
	new callbackForm().init('.callme_viewform', function(){ yandexReachGoal('zvonok'); });
	new callbackForm().init( $('#bottomForm'), function(){ yandexReachGoal('skvoznayaforma'); } );
	
	if(document.location.search.indexOf('showform=1') != -1) $('.ask-a-zakaz').click();
	
});