$(document).ready(function() {	

	$(".open_zv").click(function () {
		$(".form_zvonok").toggle();
		$("#commentarea").html('<textarea name="comment" id="comment" cols="" rows="3"></textarea>');
	})
	
	$("#call_button").click(function(){
            var process = $(this).attr('process');
            
            if(process == 0){
                
                if($('#callb').valid()){
                    $(this).attr('process','1');
                    var fio = $('#fio').val();
                    var tel = $('#tel').val();
                    var comment = $('#comment').val();

                    $.get("/kontakty", { act: 'callback', fio: fio,  tel:tel, comment:comment},
                                   function(data){
                                   
                                     if(data=='ok'){
                                         $('#callback').html('Сбасибо, Ваше сообщение отправлено. В ближайшее время с Вами свяжется один из наших менежеров');
                                         $('#call_button').css('display','none');
                                     } else{
                                         $('#error_message').html('Произошла ошибка. Попробуйте позже');
                                         $(this).attr('process','0');
                                     }
                     });
                 }
            }
        });


        $(document).keyboard('ctrl+enter', sendmsg);

    $(".open_er").click(function(){
        $(".send_error_message").css('display','none');
    })

    function sendmsg(){
        if(window.getSelection)
            txt = window.getSelection().toString();
        else if(document.getSelection)
             txt = document.getSelection();
         else if(document.selection)
             txt = document.selection.createRange().text;


        var popupY = $(document).scrollTop() + Math.round($(window).height()/2) - Math.round($(".send_error_message").height()/2);

        $(".send_error_message").css('top', popupY);
        $(".send_error_message").css('display','block');
            locat = document.location;
        $("#selected_text").text('<!!!>'+txt+'<!!!>');
        $("#selected_url").text('url: '+locat);
        $("#textarea").html('<textarea rows="5" cols="" id="comment_selected" name="comment_selected" style="border: 1px solid #C5B895; width: 220px;"></textarea>');

        //alert(document.location);
    }
     $("#er_button").click(function(){
        var error = $("#selected_text").text();
        var url = document.location+'';
        var comment = $('#comment_selected').val();
//alert(error);
//alert(url);
//alert(comment);
        $.get("/kontakty", { act: 'error', error: error, url: url, comment:comment},
               function(data){

                 if(data=='ok'){
                     $(".send_error_message").css('display','none');
                 }
        });

     })

})
