// --------------- obsługa czatu

var czat_lastid = 0;
var czat_timeout = 0;
var czat_user_timeout = 0;
var czat_drag = false;

function czat() {
  if ($('#czat').length!=0) {
    return;
  }
  $.post("services/czat.srv.php",
    {action: 'loadwindow'},
    function(dane) {
      $('.main-content').append(dane);
      $('#czat').center().draggable({ handle: '.header' });
      $('#czat-minmax').toggle(
        function(){
          $('#czat').css("height","30px");
          $('#czat-list').hide();
          $('#czat-user-list').hide();
          $('#czat-minmax').attr("src", "images/down.png");
        },
        function(){
          $('#czat').css("height","").children().show();
          $('#czat-list').show();
          $('#czat-user-list').show();
          $('#czat-minmax').attr("src", "images/up.png");
      });
      $('#czat input[name=zamknij]').click(czat_close);
      $('#czat input[name=wyslij]').click(czat_send);
      $('#czat-info').keydown(function(event){
        if (event.keyCode==13) czat_send();
      });
      if (czat_timeout==0)
        czat_getmessages();
      if (czat_user_timeout==0)
        czat_getuser();    
    });
}

function czat_close() {
  if (czat_timeout!=0) clearInterval(czat_timeout);
  czat_timeout = 0;
  if (czat_user_timeout!=0) clearInterval(czat_user_timeout);
  czat_user_timeout = 0;
  $('#czat').remove();
  czat_lastid = 0;
}


function czat_update_posts(data) {
  if ($('#czat').length==0) {
    czat_close();
    return;
  }  
  if (data.lastid>0) 
    czat_lastid = data.lastid;
  
  if (data.count>0) 
  { 
    for (i=0; i<data.count; i++)
    {
      $('#czat-list').append(
      '<div class="czat-item">'+
      '<img width="50" height="50" src="'+data.items[i].awatar+'">'+
      data.items[i].data_czas+' - <span class="user-info">'+
      data.items[i].nazwa_uz+'</span><div class="chat-text">'+data.items[i].tresc+'</div></div>');
    }
    
    if ($('.czat-item').length>30) {
      $('.czat-item').slice(0,$('.czat-item').length-30).remove();
    }
    $('#czat-list').scrollTop($('#czat-list').get(0).scrollHeight-$('#czat-list').height());      
  }  
    
  czat_timeout = setTimeout("czat_getmessages();", 1000);  
}

function czat_send() {
  $.post("services/czat.srv.php",
    {action: 'send', info: $('#czat-info').val()});
  $('#czat-info').val('');  
  return false;
}

function czat_getmessages() {
  $.post("services/czat.srv.php",
    {action: 'get', last_id: czat_lastid},
    czat_update_posts,
    "json");
}

function czat_user_update(data) {
  $("#czat-user-list").replaceWith(data);
   if ($('#czat').length==0) {
    czat_close();
    return;
  }
  czat_user_timeout = setTimeout("czat_getuser();", 5000);
}

function czat_getuser() {
  $.post("services/czat.srv.php",
    {action: 'getuser'},
    czat_user_update);
}

// -------------- koniec CZATU
function click_clicked() {
  window.location.href = 
    $("a[clicked]").attr("href");
}

function close_userinfo() {
  $("#user-info-panel").remove();
}

function show_userinfo() {
  $("#user-info-panel").loadmask(false).center().show();
  $("#user-info-panel a").click(function(){
    close_userinfo();
    return false;
  });
}

function load_userinfo(nazwa){
  if ($("#user-info-panel").length==0) {
    $('body').append('<div id="user-info-panel"></div>');
    $("#user-info-panel").center().hide();
  }  
  $("#user-info-panel").loadmask().load(
    "services/user.srv.php",
    {akcja: 'user-info', nazwa: nazwa},
    show_userinfo);      
  return false;
};

function change_status(change, nazwa) {
  $.post(
    "services/user.srv.php",
    {akcja: 'change-status', change: change, nazwa: nazwa}, 
    function(){load_userinfo(nazwa)});
}

$(document).ready(function(){ 
  //$('#main-menu-holder a').corner('bl br 20px');
  
  $('a[lang]').live("click", function(){
    var jezyk = $(this).attr("lang");
    $(this).attr("clicked", 1).removeAttr("lang");  
    $.post("services/languages.srv.php", 
      {lang: jezyk}, click_clicked);  
    return false;
  });
  
  $('span.user-info').live("click", function(){
    load_userinfo($(this).html());
  });  
});

function ChangeBase(lang) {
  $.post("services/languages.srv.php",
    {action:'base', lang:lang}, function(){
      document.location.href = document.location.href;
    });  
}

function ChangeSystem(lang) {
  $.post("services/languages.srv.php",
    {lang:lang}, function(){
      document.location.href = document.location.href;
    });  
}

function confirmation_send(txt) {
  if (txt=='')
    alert($('#panel-uzytkownika').attr('alert_send_ok'));
  else 
    alert($('#panel-uzytkownika').attr('alert_send_failed'));  
}

// ponownie wysyła maila z potwierdzeniem
function send_confirmation() {
  $.post("services/mailing.srv.php",
    {akcja: 'send-confirmation'}, confirmation_send
  );

}