$.fn.blink = function(message) {
  $(this).animate({opacity: 0.01}, 200, function() {
    $(this).html(message).show();
    $(this).animate({opacity:1}, 200, function() {
      $(this).animate({opacity: 0.1}, 200, function() {
        $(this).animate({opacity: 1}, 200, function(){
               $(this).delay(4000).fadeOut(1000);   
        })
      })
    })
  });
}

function pyet(){

var pyetja = $("#pyetja").val();
if(pyetja != ""){
$.post("php/ac.php",{ action: "p", p: pyetja },
 function(data) {
     if(data.success){
      $("#chat").append('<div><b>user: </b><i>' + data.msg + '</i><div class="hr"><hr /></div></div>');
    if($("#chat>div").size()>11){
       $("#chat div:first").remove();
    }
    $("#chat").scrollTop(10000);

      $('#pyetja').val("");
     }else{
      $("#chat").append("Error");      
     }
 }, 'json');
}
}

var timestamp = 0;
  var url = 'php/backend1.php';
  var noerror = true;
  var response = "";
  var new_n1 = "";

var comet = {
  printTest: function (whare, what) {
    //$(what).append(what);

    $(whare).append( '<div><b>n: </b><i>' + what + '</i><div class="hr"><hr /></div></div>');
    if($("#chat>div").size()>11){
       $("#chat div:first").remove();
    }
    $("#chat").scrollTop(10000);

      $("#chat > div").mouseenter(function(){
         $(this).addClass('word-over');
      }).mouseleave(function(){
         $(this).removeClass('word-over');
      });
      

    $('#comet_iframe').contents().find('body').html("");

  }  
}


  function connect (){
    var ajax = $.ajax({
      method: 'GET',
      url: url,
      timeout: 25000, 
      data: { 'timestamp' : timestamp },
      dataType: "json",
      success: function(transport) {
         response = transport;
         timestamp = response.timestamp;
         handleResponse(response);
         noerror = true;
      },
      complete: function(transport) {
        if (!noerror)
          setTimeout(function(){ connect() }, 5000); 
        else
          connect();
          noerror = false;
      }


    });

  }

  function doRequest (request){
    $.ajax({
      type: 'GET',
      url: url,
      data: { 'msg' : request }
    });
  }

  function handleResponse(response){
    $("#chat").append( '<div><b>user</b>' + response.msg + '<div class="hr"><hr /></div</div>');
    if($("#chat>div").size()>11){
       $("#chat div:first").remove();
    }
    $("#chat").scrollTop(10000);
  }



$(document).ready(function(){

    $.ajaxSetup ({
      // Disable caching of AJAX responses */
    cache: false

    });

  connect()

 $("#pyetja").focus();
 
 $("#euml").click(function(){ 
   $("#pyetja").val($("#pyetja").val() + $("#euml").val().trim()); 
   $("#pyetja").focus();
 });

 $("#ccedil").click(function(){ 
   $("#pyetja").val($("#pyetja").val() + $("#ccedil").val().trim()); 
   $("#pyetja").focus();
 });


$('#pyetja').keydown(function(event) {
  if (event.shiftKey) {
  $("#euml").val("Ë");
  $("#ccedil").val("Ç");
  }
}).keyup(function(event) {

  $("#euml").val("ë");
  $("#ccedil").val("ç");

});


  $("#submit_button").click( function() {
     //pyet();

    doRequest($('#pyetja').val());
    $('#pyetja').val('');
    $('#pyetja').focus();
    return false;

  });


   $('#pyetja').keyup(function(e) {
    if(e.keyCode == 13) {
      //pyet();
    doRequest($('#pyetja').val());
    $('#pyetja').val('');
    $('#pyetja').focus();
    return false;
    }
   });



});
