//$("#controlbox #minimize").live("click", function(){
//    if ($("#chat_window #window").css("display")=="block"){
//        $.post("system/modules/chat/windowajax.php", {position:"0"});
//        $("#chat_window #window").css({"display":"none"});
//        $(this).css({"background":"url('img/chat-maximize.png')"});
//    }else{
//        $.post("system/modules/chat/windowajax.php", {position:"1"});
//        $("#chat_window #window").css({"display":"block"});
//        $(this).css({"background":"url('img/chat-minimize.png')"});
//    }
//});scrollHeight
var scrollHeight = $("#messages_box").attr("scrollHeight");
$("#title_box").live("click", function(){
    if ($("#chat_window #window").css("display")=="block"){
        $.post("system/modules/chat/windowajax.php", {position:"0"});
        $("#chat_window #window").css({"display":"none"});
        $("#controlbox #minimize").css({"background":"url('img/chat-maximize.png')"});
    }else{
        $.post("system/modules/chat/windowajax.php", {position:"1"});
        $("#chat_window #window").css({"display":"block"});
        $("#controlbox #minimize").css({"background":"url('img/chat-minimize.png')"});
    }
});

$("#controlbox #close").live("click", function(){
    $.post("system/modules/chat/windowajax.php", {close:"1"});
    $("#chat_window").remove();
});



$("#show_chat_window").live("click", function(){
   show_chat_window();
    
});

function show_chat_window(){
     $.post("system/modules/chat/windowajax.php", {position:"1"});
    $.post("system/modules/chat/windowajax.php", {close:"0"});
    $.post("system/modules/chat/chatajax.php", {window:1}, function(data){
        $("body #maincontainer .wrapper  #dock_window #r_window #chat_window").remove();
        $("body #maincontainer .wrapper #dock_window #r_window").append(data);
        $("#messages_box").attr({ scrollTop: $("#messages_box").attr("scrollHeight") });
    });
    
}

function chatMessSubmit(evt){
    var charCode = (evt.which) ? evt.which : window.event.keyCode; 
    if (charCode == 13) { 
	submitChatMessage();
    }
}

function submitChatMessage(){
    text = $("#chat_window form #content").val();
    if (empty(text)){alert("Neįvesta žinutė")}
    $.post("system/modules/chat/chatajax.php", {text:text}, function(data){
        $("#chat_window #messages_box").append(data);
        $("#chat_window form #content").val("");
        if ($("#messages_box").attr("scrollHeight")==scrollHeight){
            $("#messages_box").attr({ scrollTop: $("#messages_box").attr("scrollHeight") });
        }
        scrollHeight = $("#messages_box").attr("scrollTop");
    });
    return false;
}

$(function(){
    $("#messages_box").animate({ scrollTop: $("#messages_box").attr("scrollHeight") }, 3000, function(){
        var auto_refresh = setInterval(function ()
        {
        $.post("system/modules/chat/chatajax.php", {window:0}, function(data){
            $("#chat_window #messages_box").html(data);
            if ($("#messages_box").attr("scrollHeight")==scrollHeight){
                $("#messages_box").attr({ scrollTop: $("#messages_box").attr("scrollHeight") });
            }
            scrollHeight = $("#messages_box").attr("scrollTop");
        });
        }, 3000); 
        });
        
    $("#controlbox #close").mouseenter( function(){
        $("#controlbox #close").css({"background-color":"#acc197"});
    });

    $("#controlbox #close").mouseleave( function(){
        $("#controlbox #close").css({"background-color":"transparent"});
    });

    $("#controlbox #minimize").mouseenter( function(){
        $("#controlbox #minimize").css({"background-color":"#acc197"});
    });

    $("#controlbox #minimize").mouseleave( function(){
        $("#controlbox #minimize").css({"background-color":"transparent"});
    });
})


