/*
 * CakePHP Ajax Chat Plugin (using jQuery);
 * Copyright (c) 2008 Matt Curry
 * www.PseudoCoder.com
 * http://github.com/mcurry/cakephp/tree/master/plugins/chat
 * http://sandbox2.pseudocoder.com/demo/chat
 *
 * @author      Matt Curry <matt@pseudocoder.com>
 * @license     MIT
 *
 */

(function(j) {
    var opts = {};

    j.fn.chat = function(options) {
        opts = j.extend({},
            j.fn.chat.defaults, options);

        return this.each(function() {
            var $this = j(this);
            update($this);
            setInterval(function() {
                update($this);
            },
            opts.interval);
            $this.find("form").bind('submit',
                function() {
                    post(j(this));
                    return false
                });
        });
    };

    function update($obj) {
        window.chatAjax = j.ajax({
            cache: false,
            url: opts.update + "/"+ $obj.attr("name"),
            success: function(ret) {
                $obj.find(".chat_window").html(ret);
                $obj.find(".chat_window").animate({
                    scrollTop: $obj.find(".chat_window").attr("scrollHeight")
                }, 3000);
            }
        });
    }

    function post($obj) {
        if(j('#generalChatMessage').attr('value')!=''){
            var $message = $obj.find("textarea[name='data[Chat][message]']");
            var $submit = $obj.find("input[type='submit']");

            var form = $obj.serialize();
            $message.attr('disabled', true);
            $submit.attr('disabled', true);
            window.chatAjax = j.ajax({
                type: "POST",
                url: $obj.attr("action"),
                data: form,
                success: function() {
                    j('#generalChatMessage').attr('value','');
                    $message.val("");
                },
                complete: function() {
                    $message.attr('disabled', false);
                    $submit.attr('disabled', false);
                    j('#generalChatMessage').attr('value','');
                }
            });
        }
    }

    //
    // plugin defaults
    //
    j.fn.chat.defaults = {
        update: '/chat/update',
        interval: 5000
    };
})(jQuery);

function chat(room){
    if(typeof(window.chatAjax)!='undefined'){
        window.chatAjax.abort();
    }
    if(typeof(window.openChats)=='undefined'||!window.openChats){
        window.openChats = [];
    }
    if(jQuery.inArray(room,window.openChats)==-1){
        window.openChats.push(room);
    }else{
        //return;
    }
    if(!$('#chat-'+room).is('div')){
        $('body').prepend('<div class="chat" id="chat-'+room+'"></div>');
    }
    window.chatAjax = $.ajax({
        url:window.webroot+'pages/chat',
        cache:false,
        type:'POST',
        data:'room='+escape(room),
        success:function(ret){
            $('#chat-'+room).dialog('destroy').empty().html(ret).dialog({
                autoOpen:false,
                title: 'Chat: '+room,
                width:405,
                height:'auto',
                left:0
            }).dialog('open');
        }
    });
}<!-- 0.0396s -->
