var lang;
var tid;

addMask = function(){
	var mask = $('<div></div>');
	mask.css({'width': $(document).width(),'height':$(document).height()});  
	mask.addClass('mask');
	$('body').append(mask);
	$('.mask').show();
}
	
delMask = function(obj) {
	$(obj).fadeOut(500, 
		function(){ $(this).remove();});
	$('.mask').remove();
}

setMailFormPos = function(obj){
	$(obj).css({left:($(window).width()-$(obj).width())/2});
	$(obj).css({top:( $('body').scrollTop()+($(window).height()-$(obj).height())/2)});
}

showError = function(str) {
	$('#error').html(str);
	clearTimeout(tid);
	tid = setTimeout(function() { $('#error').html(''); }, 3000); 	
}

sendForm = function() {
	$('#doform').attr('disabled', 'disabled');
	$('#doform').after('<span class="myLoader"></span>');
	
	$.ajax({url:'/feedback.php', type: 'POST', cache: true, timeout: 2000, dataType: "json", data: 'lang='+lang+'&m=2&n='+$('#name').val()+'&c='+$('#country').val()+'&e='+$('#email').val()+'&p='+$('#phone').val()+'&com='+$('#comment').val()+'&ur='+window.location,

	    error: function() {
			alert('Sorry! Request Error.');
			$('#doform').attr('disabled', '');
			$('.myLoader').remove();
    	},
		
		success: function(res) {
			$('#doform').attr('disabled', '');
			$('.myLoader').remove();
			
			if(res[0]!=0) {
				$('#doform').attr('disabled', '');
				return showError(res[1]);
			}
			else {
				$('#error').html(res[1]);
				clearTimeout(tid);
				tid = setTimeout(function() { delMask('.fbForm') }, 2000); 	
			}
		}
	});
}

sendSubscribe = function() {

	$('#doSubscr').attr('disabled', 'disabled');
	$('#doSubscr').after('<span class="myLoader"></span>');
	
	$.ajax({url:'/feedback.php', type: 'POST', cache: true, timeout: 2000, dataType: "json", data: 'lang='+lang+'&m=4&e='+$('#myemail').val(),

	    error: function() {
			alert('Server Request Error!');
			$('#doSubscr').attr('disabled', '');
			$('.myLoader').remove();
    	},
		
		success: function(res) {
			$('#doSubscr').attr('disabled', '');
			$('.myLoader').remove();
			
			if(res[0]!=0) {
				$('#doSubscr').attr('disabled', '');
				return showError(res[1]);
			}
			else {
				$('#error').html(res[1]);
				clearTimeout(tid);
				tid = setTimeout(function() { delMask('.subscrForm') }, 2000); 	
			}
		}
	});
}





$(function() {

$('.feedback').click(function() {
	addMask();
	$.ajax({url:'/feedback.php', type: 'POST', cache: true, timeout: 2000, dataType: "json", data: 'm=1',
	
	    error: function() {
			alert('Sorry! Request Error.');
    	},
    	success: function(res) {
		
			if(res) {
				var mform = $('<div></div>');
				mform.addClass('fbForm');
					
				$('body').append(mform);
				setMailFormPos('.fbForm');
				
				mform.append(res);
				$('.fbForm').fadeIn(500);
					
				$('#close').click(function() {
					delMask('.fbForm');
				});		
				
				$('#doform').click(function() {
					sendForm();
				});
			}
		}
	});
	
});



$('.subscribe').click(function() {
	addMask();
	$.ajax({url:'/feedback.php', type: 'POST', cache: true, timeout: 2000, dataType: "json", data: 'm=3',
	
	    error: function() {
			alert('Server Request Error!');
    	},
    	success: function(res) {
		
			if(res) {
				var mform = $('<div></div>');
				mform.addClass('subscrForm');
					
				$('body').append(mform);
				setMailFormPos('.subscrForm');
				
				mform.append(res);
				$('.subscrForm').fadeIn(500);
					
				$('#close').click(function() {
					delMask('.subscrForm');
				});
					
				$('#doSubscr').click(function() {
					sendSubscribe();
				});
			}
		}
	});
	
});



});