$(function(){
    $(document.body).append($('<div id="sys_dialog"></div>'));
    $('#sys_dialog').dialog({
        autoOpen:false
    });
    /*cuSel({
		changedEl: ".selects",
		visRows: 4,
		scrollArrows: true
	});*/
});

var cfg = {
    'aspeed':'fast' // animation speed
};

var msg = {
    id:'',
    timeout:10000,
    current: null,
    init:function(id){
        
        msg.id = id;
        msg.wait();
    },
    wait:function(){
        $.post('/ru/ajax/msg/get_msg',function(d){
            d = eval('('+d+')');
            if(!d['content'] || msg.current == d.id){
                setTimeout(function(){msg.wait();},msg.timeout);
            }else{
                msg.user_msg(d);
            }
        });
    },

      close_user_msg: function(msg_id,id){
            $('#new_msg_block').hide();
            msg.wait();
            return true;
      },

      reply_user_msg: function(){
          $('#new_msg_reply_container').show();
          $('#new_msg_reply').hide();
          $('#new_msg_send').show();
      },

      user_msg: function(d){
        msg.current = d.id;
        $('#new_msg_reply_txt').val('');
        $('#new_msg_reply_container').hide();
        $('#new_msg_send').hide();
        $('#new_msg_reply').show();
        $('#new_msg_close').unbind('click').bind('click',function(){
                $.post('/ru/ajax/msg/read/'+d['id']);
                msg.close_user_msg();
        });
        $('#new_msg_reply').unbind('click').bind('click',function(){msg.reply_user_msg()});
          $('#new_msg_send').unbind('click').bind('click',function(){
              if(!$('#new_msg_reply_txt').val()) return false;
              $.post('/ru/ajax/msg/send/'+d['id'],{
                    msg:$('#new_msg_reply_txt').val(),
                    to:d['from']
                });
              msg.close_user_msg();
          });
        $('#new_msg_avatar').attr('src',d['avatar']);
        $('#new_msg_nick').html(d['nick']);
        $('#new_msg_online').hide();
        $('#new_msg_offline').show();
        if(d['online']){
            $('#new_msg_online').show();
            $('#new_msg_offline').hide();
        }
        $('#new_msg_time').html(d['humantime']);
        $('#new_msg_txt').html(d['msg']);
        $('#new_msg_block').show('drop',cfg['aspeed']);
      }
};
comments = {
    obj:'',
    dir:'',
    lng:'',
    init: function(obj,dir,lng){
        lng = lng?lng:'ru';
        this.obj = obj;
        this.dir = dir;
        this.lng = lng;
    },
    get: function(page){
        $('#comments').html('<img src="/images/ajax_loading.gif">');
        $.post('/'+this.lng+'/ajax/comments/get/'+this.obj+'/'+this.dir+'/'+page,function(d){
            $('#comments').html(d);
            document.location = '#comments';
        });
    },
    add: function(){
        txt = $('#new_comment').val();
        $('#new_comment_container').html('<img src="/images/ajax_loading.gif">');
        $.post('/'+this.lng+'/ajax/comments/add/'+this.obj+'/'+this.dir+'/',{txt:txt},function(d){
            $('#comments').html(d);
            document.location = '#comments';
        });
    }
};
