$(document).ready(function(){

	$('#onlineConsul').css('height','140px');
	tooltip();
	tooltipL();
	$("#text").focus(function () {focusInput("#text", "Текст");});
	$("#text").blur(function () {blurInput("#text", "Текст");});
	$("#email").focus(function () {focusInput("#email", "email");});
	$("#email").blur(function () {blurInput("#email", "email");});	
	$("#name").focus(function () {focusInput("#name", "введите имя");});
	$("#name").blur(function () {blurInput("#name", "введите имя");});		
	$("#text_cons").focus(function () {focusInput("#text_cons", "Вопрос");});
	$("#text_cons").blur(function () {blurInput("#text_cons", "Вопрос");});	
	
	$('#map img').hover(function(){
		$('#map img').animate({width:'362px',right:'-35px',top:'-30px'},100);
	},
	function(){
		$('#map img').animate({width:'292px',right:'0',top:'0'},100);
	});

	
	setInterval(function(){
		check_new_message();	
		}, 4000);	

$('#onlineConsul').keydown(function(event){
    if (event.which == 13 && event.ctrlKey) {
        message_send();
    };
});		
		

$('#online').keydown(function(event){
    if (event.which == 13 && event.ctrlKey) {
        feedback_send();
    };
});	

});

function move_social(h){
	var m = h + 20;
	$('#social').animate({'bottom':m},150);
}
function check_new_message(){
	$.get("/process/check_messages.php",  function (data) {
			if (data>0)
			{
				$('#forAppend').css('display','block');
				div  = $('<div id="messAdmin"> </div>')
				div.load("/process/load_messages.php",function(){
				div.appendTo('#forAppend');	
				$("#forAppend").scrollTop(10000); 
					var h = $('#onlineConsul').height();
					move_social(h);	
				})
			}
		});
}


function select_menu(){
	var attr = $('#variant').css('display');
	if (attr == 'none')
	{$('#variant').fadeIn(200);}
	else
	{$('#variant').fadeOut(350);}
}


function message_send()
{			
			var text = $("#text_cons").attr('value');
			var iden = $("#iden").attr('value');
			
			var allValid = true;
			
			if ((text != '') && (text != 'Вопрос'))
			{
				$("#text_cons").css("border-color","green");
			}
			else
			{
				allValid = false;
				$("#text_cons").css("border-color","red");
				$("#text_cons").css("color","red");
				
			}
			//allValid = false;
			if (allValid)
			{
				$("#waiting").fadeIn(200);
					$("#resultDiv").load("/process/send_massage.php", {text: text,iden:iden}, function () {
					$('#forAppend').css('display','block');
					insert = $('<div id="messUser"><div><b style="color:#fff;">вопрос</b></div>'+text+'<div>');
					insert.appendTo('#forAppend');
					$("#forAppend").scrollTop(10000); 
					$("#waiting").fadeOut(200);
					$("#text_cons").attr('value','');
					$("#text_cons").css('color','#111');
					$("#text_cons").css("border-color","");
					var h = $('#onlineConsul').height();
					move_social(h);												
					});
			}
}


function feedback_send()
{			
			var ch = $('#select a').attr('id');
			var choise  = ~~ch.substring(1);
			var name = $("#name").attr('value');
			var email = $("#email").attr('value');
			var text = $("#text").attr('value');
			var allValid = true;

			if ((name != '') && (name != 'введите имя'))
			{
				$("#name").css("border-color","green");
			}
			else
			{
				allValid = false;
				$("#name").css("border-color","red");
				$("#name").css("color","red");
				
			}

			if ((email != '') && (email != 'email'))
			{
				$("#email").css("border-color","green");
			}
			else
			{
				allValid = false;
				$("#email").css("border-color","red");
				$("#email").css("color","red");
				
			}
			
			if ((text != '') && (text != 'Текст'))
			{
				$("#text").css("border-color","green");
			}
			else
			{
				allValid = false;
				$("#text").css("border-color","red");
				$("#text").css("color","red");
				
			}
			if (allValid)
			{
					$("#resultDiv").load("/process/send_feedback.php", {text: text, name: name, email: email, choise:choise}, function () {
						$("#resultDiv").fadeIn().delay(3000).fadeOut();
						$("#text").attr('value','Текст');
						$("#text").css('color','');
						$("#text").css("border-color","");
						$("#name").attr('value','введите имя');
						$("#name").css('color','');
						$("#name").css("border-color","");
						$("#email").attr('value','email');
						$("#email").css('color','');
						$("#email").css("border-color","");						
					});
			}
}


function select_que(at){
	id = $(at).attr('id');
	text = $(at).text();
	id = ~~id.substring(1)
	$('#select a').remove();
	ap = $('<a id="c'+id+'" onclick="select_menu()" href="javascript:void(0)">'+text+'</a>');
	ap.appendTo('#select');
	$('#variant').fadeOut(350);
}

function openQuestion(id)
{			
	var attr = $('#faq .info#i'+id).css('display');
	if (attr == 'none')
	{$('#faq .info#i'+id).slideDown(200);}
	else
	{$('#faq .info#i'+id).slideUp(350);}
} 
function openFeedback()
{			
	var pos = $('#online').css('left');
	if (pos == '-258px')
	{ 
		$('#online').animate({left:'0px',height:'250px'},150);
		$('#online #buttonHide img').attr('src','/images/hide.png');
		$('#online #buttonHide img').css('height','80px');
	}
	else
	{ 
		$('#online').animate({left:'-258px'},350);
		$('#online').animate({height:'140px'},350);
		$('#online #buttonHide img').attr('src','/images/show.png');
		$('#online #buttonHide img').css('height','120px');
	}

}
function openOnline()
{			
	var pos = $('#onlineConsul').css('right');
	if (pos == '-318px')
	{
		
		$('#onlineConsul').animate({right:'0px','min-height':'240px'},200,function(){
			$('#onlineConsul').css('height','auto');
			var h = $('#onlineConsul').height();
			move_social(h);		
		});
		$('#onlineConsul #buttonHide img').attr('src','/images/hide_cons.png');
		$("#forAppend").scrollTop(10000); 
		$('#onlineConsul #buttonHide img').css('height','80px');
		
		
	}
	else
	{ 
		
		$('#onlineConsul #buttonHide img').css('height','130px');
		$('#onlineConsul').animate({right:'-318px'},350);
		$('#onlineConsul #buttonHide img').attr('src','/images/show_cons.png');
		$('#onlineConsul').animate({'height':'140px','min-height':'140px'},200,function(){
			$('#onlineConsul').css('min-height','');
			var h = $('#onlineConsul').height();
			move_social(h);
		});
	}

	

}
this.tooltip = function(){	
		xOffset = '-20';
		yOffset = '-12';		
	$("a.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX - yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX - yOffset) + "px");
	});			
};

this.tooltipL = function(){	
		xOffsetL = '30';
		yOffsetL = '170';		
	$("a.tooltipL").hover(function(e){
		//alert('asd');
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltipL'>"+ this.t +"</p>");
		$("#tooltipL")
			.css("top",(e.pageY - xOffsetL) + "px")
			.css("left",(e.pageX - yOffsetL) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltipL").remove();
    });	
	$("a.tooltipL").mousemove(function(e){
		$("#tooltipL")
			.css("top",(e.pageY - xOffsetL) + "px")
			.css("left",(e.pageX - yOffsetL) + "px");
	});			
};


function focusInput(selector, text)
{
	if ($(selector).attr("value") == text)
	{
		$(selector).attr("value","");
	}
	$(selector).css('color','#111');
		
}

function blurInput(selector, text)
{
	if ($(selector).attr("value") == '')
	{
		$(selector).attr("value",text);
		$(selector).css('color','#888');
	}
	else
	{
		$(selector).css('color','#111');
	}
}
