
// Live
function live() {
	jQuery(".live").bind('click', function(event) {
		if( jQuery(".live-block").css("right") == "-262px" ) {
			live_openLiveBox();
		} else {
			jQuery(".live-block").animate({right: "-262px"}, 1000 );
			jQuery(".live img").attr({src: "i/title/ru/live.png" });
		}
		event.preventDefault();
		event.stopPropagation();
	});
	/*jQuery(".live-chat-button").bind('click', function(event) {
		if( jQuery(".live-chat").css("display") == "none" ) {
			live_showChat();
		}
		event.preventDefault();
		event.stopPropagation();
	});*/
	jQuery(".live-mail-button").bind('click', function(event) {
		if( jQuery(".live-mail").css("display") == "none" ) {
			live_showMessageForm();
		}
		event.preventDefault();
		event.stopPropagation();
	});
	
	jQuery("#live_emailInput").bind('focus', function(event) {
		if( jQuery(this).attr("value") == "Your e-mail" ) {
			jQuery(this).attr({value: "" });
			jQuery(this).css("color","#333");
		}
	});
	jQuery("#live_emailInput").bind('blur', function(event) {
		if( jQuery(this).attr("value") == "" ) {
			jQuery(this).attr({value: "Your e-mail" });
			jQuery(this).css("color","#a0a0a0");
		}
	});
	jQuery("#live_messageInput").bind('focus', function(event) {
		if( jQuery(this).attr("value") == "Your question or suggestion" ) {
			jQuery(this).attr({value: "" });
			jQuery(this).css("color","#333");
		}
	});
	jQuery("#live_messageInput").bind('blur', function(event) {
		if( jQuery(this).attr("value") == "" ) {
			jQuery(this).attr({value: "Your question or suggestion" });
			jQuery(this).css("color","#a0a0a0");
		}
	});
	
}

function live_submitHandler(){
	var email=jQuery('#live_emailInput').val();
	var message=jQuery('#live_messageInput').val();
	
	if(message==''){
		return false;
	}
	
	jQuery.ajax({
	      type: "POST",
	      url: "/live/message",
	      data: '&redirect=false&fEmail='+email+'&fMessage='+message,
	      error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert("Message wasn't sent");
			},
	      success: function(data, textStatus) {
				if(!data.match(/Ok/i)){
					alert("Message wasn't sent");
				}else{
					live_showMessageOk();
				}
	      }
    });
	
	jQuery('#live_emailInput').val('');
	jQuery('#live_messageInput').val('');
	
	return false;
}

function live_openLiveBox(){
	if( jQuery(".live-block").css("right") == "-262px" ) {
		jQuery(".live-block").animate({right: "0"}, 1000 );
		jQuery(".live img").attr({src: "i/title/ru/live-hide.png" });
	}
}


function live_showMessageOk(){
	jQuery(".live-mail").show().fadeOut(200,function(){
		jQuery("#live_messageOk").show();
	});
}

function live_showChat(){
	jQuery(".live-mail-button").removeClass("live-button-current");
	jQuery(this).addClass("live-button-current");
	jQuery(".live-mail").hide();
	jQuery("#live_messageOk").hide();
	
	jQuery(".live-chat").show();
}

function live_showMessageForm(){
	//jQuery(".live-chat-button").removeClass("live-button-current");
	jQuery(".live-mail-button").addClass("live-button-current");
	
	//jQuery(".live-chat").hide();
	jQuery("#live_messageOk").hide();
	
	jQuery(".live-mail").show();
}


function openLiveBox(){
	try{
		jQuery(".live-block").animate({right: "0"}, 1000 );
		jQuery(".live img").attr({src: "i/title/ru/live-hide.png" });
	}catch(e){
		
	}
}


runOnLoad(function(){
	live();
	
	try{
		jQuery("#live_messageForm").submit(function(){
			try{
				live_submitHandler();
			}catch(e){
				alert(e);
			}finally{
				return false;
			}
		});
	}catch(e){}
});