/*
 
 Copyright (c) 2009 Anant Garg (anantgarg.com | inscripts.com)
 
 This script may be used for non-commercial purposes only. For any
 commercial purposes, please contact the author at
 anant.garg@inscripts.com
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 
 */

var windowFocus 		= true;
var username;
var chatHeartbeatCount 	= 0;
var minChatHeartbeat 	= 1000;
var maxChatHeartbeat 	= 33000;
var chatHeartbeatTime 	= minChatHeartbeat;
var originalTitle;
var blinkOrder 			= 0;

var chatboxFocus 		= new Array();
var newMessages 		= new Array();
var newMessagesWin 		= new Array();
var chatBoxes 			= new Array();



/*
 jQuery.noConflict();
 
 jQuery(document).ready(function(){
 originalTitle = document.title;
 startChatSession();
 
 jQuery([window, document]).blur(function(){
 windowFocus = false;
 }).focus(function(){
 windowFocus = true;
 document.title = originalTitle;
 });
 });
 */


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

//**********************************************************************************************************
// restructureChatBoxes
//**********************************************************************************************************

function restructureChatBoxes() {
    align = 0;
    for (x in chatBoxes) {
        chatboxtitle = chatBoxes[x];
        if (jQuery("#chatbox_"+chatboxtitle).css('display') != 'none') {
            align++;
        }
    }
    if (align > 0)
    {
        width = (align)*(325+7)+20;
    }
    align = 0;
    for (x in chatBoxes) {
        chatboxtitle = chatBoxes[x];
        
        if (jQuery("#chatbox_"+chatboxtitle).css('display') != 'none') {
            if (align == 0) {
                jQuery("#chatbox_"+chatboxtitle).css('left', '20px');
            } else {
                width = (align)*(325+7)+20;
                jQuery("#chatbox_"+chatboxtitle).css('left', width+'px');
            }
            align++;
        }
    }
}



//**********************************************************************************************************
// chatWith
//**********************************************************************************************************

function chatWith(chatuserId, chatuser) {
    chatuser = chatuser.replace(/\./g, '');
    chatuser = chatuser.split(' ').join('_');
    createChatBox(chatuserId, chatuser, 0, 1, -1);
    jQuery("#chatbox_"+chatuser+" .chatboxtextarea").focus();
}


//**********************************************************************************************************
// createChatBox
//**********************************************************************************************************




function createChatBox(chatuserId, chatboxtitle,minimizeChatBox,cargarChat,asesorId) {
    if (jQuery("#chatbox_"+chatboxtitle).length > 0) {
        if (jQuery("#chatbox_"+chatboxtitle).css('display') == 'none') {
            jQuery("#chatbox_"+chatboxtitle).css('display','block');
            restructureChatBoxes();
        }
        jQuery("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
        
        //Limpiar el chat.
        //alert ('se limpiara el chat');
        jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").empty();
        
        if (cargarChat == 1)
        {
            $ulrChat =  "http://default.haztuapp.x10.mx/ChatAJAX?action=abreChat&usrId=" + chatuserId + "&asesorId=" + asesorId;
            jQuery.ajax({
                        url: $ulrChat,
                        cache: false,
                        dataType: "json",
                        success: function(data) {
                        
                        //alert('Cargando chat!' + data.items.length + $ulrChat);
                        
                        jQuery.each(data.items, function(i,item)
                                    {
                                    if (item)
                                    { // fix strange ie bug
                                    chatboxID = item.f;
                                    
                                    if (item.cn == "")
                                    {
                                        $color = "<font color=red>";
                                    }
                                    else
                                    {
                                        $color = "<font>";
                                    }
                                    
                                    jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+$color+item.fn+'<br>'+item.d+':</font><br></span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
                                    //jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop(jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
                                    }
                                    });
                        }});
        }
        return;
    }
    
    tituloChat = chatboxtitle;
    tituloChat = tituloChat.substring(tituloChat.indexOf('_')+1, tituloChat.length);
    Asesor = tituloChat.substring(tituloChat.indexOf('_para_') + 6, tituloChat.length);
    AsesorId = Asesor.substring(0, Asesor.indexOf('_'));
    Asesor = Asesor.substring(Asesor.indexOf('_') + 1, Asesor.length);
    Asesor = Asesor.replace(/_/g, ' ');
    tituloChat = tituloChat.substring(0, tituloChat.indexOf('_para_'));
    tituloChat = tituloChat.replace(/_/g, ' ');
    nombreUsuario = tituloChat;
    tituloChat = tituloChat + '/' + Asesor;
    
    jQuery(" <div />" ).attr("id","chatbox_"+chatboxtitle)
    .addClass("chatbox")
    .html('<div class="chatboxhead"><div class="chatboxtitle">'+tituloChat+'</div><div class="chatboxoptions"><a href="javascript:void(0)" onclick="javascript:toggleChatBoxGrowth(\''+chatboxtitle+'\')">-</a> <a href="javascript:void(0)" onclick="javascript:closeChatBox(\''+chatboxtitle+'\','+chatuserId+')">X</a></div><br clear="all"/></div><div class="chatboxcontent"></div><div class="chatboxinput"><textarea class="chatboxtextarea" onkeydown="javascript:return checkChatBoxInputKey(event,this,'+chatuserId+',\''+chatboxtitle+'\',\'-'+AsesorId+'\');"></textarea></div>')
    //.appendTo(jQuery( "body" ));
    .appendTo(jQuery( "#chatContainer" ));
    
    //jQuery("#chatbox_"+chatboxtitle).css('bottom', '0px');
    jQuery("#chatbox_"+chatboxtitle).css('top', '0px');
    
    chatBoxes.unshift(chatboxtitle);
    restructureChatBoxes();
    
    if (minimizeChatBox == 1) {
        minimizedChatBoxes = new Array();
        
        if (jQuery.cookie('chatbox_minimized')) {
            minimizedChatBoxes = jQuery.cookie('chatbox_minimized').split(/\|/);
        }
        minimize = 0;
        for (j=0;j<minimizedChatBoxes.length;j++) {
            if (minimizedChatBoxes[j] == chatboxtitle) {
                minimize = 1;
            }
        }
        
        if (minimize == 1) {
            jQuery('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
            jQuery('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
        }
    }
    
    chatboxFocus[chatboxtitle] = false;
    
    jQuery("#chatbox_"+chatboxtitle+" .chatboxtextarea").blur(function(){
                                                              chatboxFocus[chatboxtitle] = false;
                                                              jQuery("#chatbox_"+chatboxtitle+" .chatboxtextarea").removeClass('chatboxtextareaselected');
                                                              }).focus(function(){
                                                                       chatboxFocus[chatboxtitle] = true;
                                                                       newMessages[chatboxtitle] = false;
                                                                       jQuery('#chatbox_'+chatboxtitle+' .chatboxhead').removeClass('chatboxblink');
                                                                       jQuery("#chatbox_"+chatboxtitle+" .chatboxtextarea").addClass('chatboxtextareaselected');
                                                                       });
    
    jQuery("#chatbox_"+chatboxtitle).click(function() {
                                           if (jQuery('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
                                           jQuery("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
                                           }
                                           });
    
    jQuery("#chatbox_"+chatboxtitle).show();
    ///////////////
    //$("#chatbox_"+chatboxtitle).draggable();
    //$(".chatbox_").draggable();
    ///////////
    
    
    //Cargar el historial de conversación del Chat.
    
    
    //jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").empty();
    if (cargarChat == 1)
    {
        //Limpiar el chat.
        jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").empty();
        
        $ulrChat =  "http://default.haztuapp.x10.mx/ChatAJAX?action=abreChat&usrId=" + chatuserId + "&asesorId=" + asesorId;
        jQuery.ajax({
                    //url: "http://appyuda.myh.x10host.com/application/views/chat/chat.php?action=chatheartbeat",
                    url: $ulrChat,
                    cache: false,
                    dataType: "json",
                    success: function(data) {
                    //alert('Cargando chat!' + data.items.length + $ulrChat);
                    
                    jQuery.each(data.items, function(i,item)
                                {
                                if (item)
                                { // fix strange ie bug
                                chatboxID = item.f;
                                
                                tituloChat = chatboxtitle;
                                tituloChat = tituloChat.substring(tituloChat.indexOf('_')+1, tituloChat.length);
                                Asesor = tituloChat.substring(tituloChat.indexOf('_para_') + 6, tituloChat.length);
                                AsesorId = Asesor.substring(0, Asesor.indexOf('_'));
                                Asesor = Asesor.substring(Asesor.indexOf('_') + 1, Asesor.length);
                                Asesor = Asesor.replace(/_/g, ' ');
                                tituloChat = tituloChat.substring(0, tituloChat.indexOf('_para_'));
                                tituloChat = tituloChat.replace(/_/g, ' ');
                                nombreUsuario = tituloChat;
                                tituloChat = tituloChat + '/' + Asesor;

                                $nombreChat = tituloChat;
                                
                                if (item.cn == "")
                                {
                                    $color = "<font color=red>";
                                    $nombreChat = nombreUsuario;
                                }
                                else
                                {
                                    $color = "<font>";
                                    $nombreChat = Asesor;
                                }
                            
                                jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+$color+$nombreChat+'<br>'+item.d+':</font><br></span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
                                //jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop(jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
                                }
                                });
                    jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop(jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
                    }});
    }
}




//**********************************************************************************************************
// chatHeartbeat
//**********************************************************************************************************


function chatHeartbeat(){
    
    var itemsfound = 0;
    
    if (windowFocus == false) {
        
        var blinkNumber = 0;
        var titleChanged = 0;
        for (x in newMessagesWin) {
            if (newMessagesWin[x] == true) {
                ++blinkNumber;
                if (blinkNumber >= blinkOrder) {
                    document.title = x+' says...';
                    titleChanged = 1;
                    break;
                }
            }
        }
        
        if (titleChanged == 0) {
            document.title = originalTitle;
            blinkOrder = 0;
        } else {
            ++blinkOrder;
        }
        
    } else {
        for (x in newMessagesWin) {
            newMessagesWin[x] = false;
        }
    }
    
    for (x in newMessages) {
        if (newMessages[x] == true) {
            if (chatboxFocus[x] == false) {
                //FIXME: add toggle all or none policy, otherwise it looks funny
                jQuery('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink');
            }
        }
    }
    
    jQuery.ajax({
                //url: "http://appyuda.myh.x10host.com/application/views/chat/chat.php?action=chatheartbeat",
                url: "http://default.haztuapp.x10.mx/ChatAJAX?action=chatheartbeat",
                cache: false,
                dataType: "json",
                success: function(data) {
                
                var nuevosCargados = new Array();

                jQuery.each(data.items, function(i,item){
                            if (item)	{ // fix strange ie bug
                            
                            
                            if (item.cn == "")
                            {
                                chatboxtitle = item.fn;
                                $color = "<font color=red>";
                            }
                            else
                            {
                                chatboxtitle = item.cn;
                                $color = "<font>";
                            }
                            chatboxtitle = chatboxtitle.replace(/\./g, '');
                            chatboxtitle = chatboxtitle.split(' ').join('_');
                            chatboxID = item.f;
                            
                            tituloChat = chatboxtitle;
                            tituloChat = tituloChat.substring(tituloChat.indexOf('_')+1, tituloChat.length);
                            Asesor = tituloChat.substring(tituloChat.indexOf('_para_') + 6, tituloChat.length);
                            AsesorId = Asesor.substring(0, Asesor.indexOf('_'));
                            Asesor = Asesor.substring(Asesor.indexOf('_') + 1, Asesor.length);
                            Asesor = Asesor.replace(/_/g, ' ');
                            tituloChat = tituloChat.substring(0, tituloChat.indexOf('_para_'));
                            tituloChat = tituloChat.replace(/_/g, ' ');
                            nombreUsuario = tituloChat;
                            tituloChat = tituloChat + '/' + Asesor;
                            
                            $nombreChat = tituloChat;
                            
                            if (item.cn == "")
                            {
                                $nombreChat = nombreUsuario;
                            }
                            else
                            {
                                $nombreChat = Asesor;
                            }
                            
                            
                            //alert(chatboxtitle);
                            
                            
                            //alert (chatboxtitle);
                            
                            if ((jQuery("#chatbox_"+chatboxtitle).length <= 0) && (nuevosCargados.indexOf(chatboxtitle) < 0))
                            {
                                //alert(chatboxtitle);
                                nuevosCargados.push(chatboxtitle);
                                createChatBox(chatboxID, chatboxtitle, 0, 1, item.t);
                            
                            }
                            else
                            {
                                if (nuevosCargados.indexOf(chatboxtitle) < 0)
                                {
                                    if (jQuery("#chatbox_"+chatboxtitle).css('display') == 'none')
                                    {
                                        jQuery("#chatbox_"+chatboxtitle).css('display','block');
                                        restructureChatBoxes();
                                    }
                            
                                    newMessages[chatboxtitle] = true;
                                    newMessagesWin[chatboxtitle] = true;
                                    jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+$color+$nombreChat+'<br>'+item.d+':</font><br></span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
                            
                                    jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop(jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
                                }
                            }
                            itemsfound += 1;
                            }
                            });
                
                chatHeartbeatCount++;
                
                if (itemsfound > 0)
                {
                    chatHeartbeatTime = minChatHeartbeat;
                    chatHeartbeatCount = 1;
                }
                else if (chatHeartbeatCount >= 10)
                {
                    chatHeartbeatTime *= 2;
                    chatHeartbeatCount = 1;
                    if (chatHeartbeatTime > maxChatHeartbeat)
                    {
                        chatHeartbeatTime = maxChatHeartbeat;
                    }
                }
                setTimeout('chatHeartbeat();',chatHeartbeatTime);
                }});
}


//**********************************************************************************************************
// closeChatBox
//**********************************************************************************************************

function closeChatBox(chatboxtitle, id) {
    jQuery('#chatbox_'+chatboxtitle).css('display','none');
    restructureChatBoxes();
    
    jQuery.post(
                //"http://appyuda.myh.x10host.com/application/views/chat/chat.php?action=closechat",
                "http://default.haztuapp.x10.mx/ChatAJAX?action=closechat",
                { chatbox: id} , function(data){
                });
    
}


//**********************************************************************************************************
// toggleChatBoxGrowth
//**********************************************************************************************************

function toggleChatBoxGrowth(chatboxtitle) {
    if (jQuery('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') == 'none') {
        
        var minimizedChatBoxes = new Array();
        
        if (jQuery.cookie('chatbox_minimized')) {
            minimizedChatBoxes = jQuery.cookie('chatbox_minimized').split(/\|/);
        }
        
        var newCookie = '';
        
        for (i=0;i<minimizedChatBoxes.length;i++) {
            if (minimizedChatBoxes[i] != chatboxtitle) {
                newCookie += chatboxtitle+'|';
            }
        }
        
        newCookie = newCookie.slice(0, -1)
        
        
        jQuery.cookie('chatbox_minimized', newCookie);
        jQuery('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','block');
        jQuery('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','block');
        jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop(jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
    } else {
        
        var newCookie = chatboxtitle;
        
        if (jQuery.cookie('chatbox_minimized')) {
            newCookie += '|'+jQuery.cookie('chatbox_minimized');
        }
        
        
        jQuery.cookie('chatbox_minimized',newCookie);
        jQuery('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
        jQuery('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
    }
    
}




//**********************************************************************************************************
// checkChatBoxInputKey
//**********************************************************************************************************

function checkChatBoxInputKey(event,chatboxtextarea,chatuserId,chatboxtitle,fromId) {
    
    if(event.keyCode == 13 && event.shiftKey == 0)
    {
        message = jQuery(chatboxtextarea).val();
        message = message.replace(/^\s+|\s+$/g,"");
        
        //$message = htmlentities($message);
        
        jQuery(chatboxtextarea).val('');
        jQuery(chatboxtextarea).focus();
        jQuery(chatboxtextarea).css('height','44px');
        if (message != '')
        {
            jQuery.post(
                        //"http://appyuda.myh.x10host.com/application/views/chat/chat.php?action=sendchat",
                        "http://default.haztuapp.x10.mx/ChatAJAX?action=sendchat",
                        {to: chatboxtitle, message: message, toID:chatuserId, fromId:fromId} ,
                        function(data)
                        {
                        //alert (data);
                        message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
                             
                        $color = "<font>";
                        tituloChat = chatboxtitle;
                        tituloChat = tituloChat.substring(tituloChat.indexOf('_')+1, tituloChat.length);
                        Asesor = tituloChat.substring(tituloChat.indexOf('_para_') + 6, tituloChat.length);
                        AsesorId = Asesor.substring(0, Asesor.indexOf('_'));
                        Asesor = Asesor.substring(Asesor.indexOf('_') + 1, Asesor.length);
                        Asesor = Asesor.replace(/_/g, ' ');
                        tituloChat = tituloChat.substring(0, tituloChat.indexOf('_para_'));
                        tituloChat = tituloChat.replace(/_/g, ' ');
                        nombreUsuario = tituloChat;
                        tituloChat = tituloChat + '/' + Asesor;
                                                                                            
                        //jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+$color+Asesor+'<br>'+''+':</font><br></span><span class="chatboxmessagecontent">'+message+'</span></div>');
                                                                                            
                        //jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+username+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+message+'</span></div>');
                        //jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop(jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
                        }
                        );
        }
        chatHeartbeatTime = minChatHeartbeat;
        chatHeartbeatCount = 1;
        
        return false;
    }
    
    var adjustedHeight = chatboxtextarea.clientHeight;
    var maxHeight = 94;
    
    if (maxHeight > adjustedHeight)
    {
        adjustedHeight = Math.max(chatboxtextarea.scrollHeight, adjustedHeight);
        if (maxHeight)
            adjustedHeight = Math.min(maxHeight, adjustedHeight);
        if (adjustedHeight > chatboxtextarea.clientHeight)
            jQuery(chatboxtextarea).css('height',adjustedHeight+8 +'px');
    }
    else
    {
        jQuery(chatboxtextarea).css('overflow','auto');
    }
}



//**********************************************************************************************************
// startChatSession
//**********************************************************************************************************

function startChatSession(){
    //chatHeartbeat();
    //return;
                        
    jQuery.ajax({
                //url: "http://appyuda.myh.x10host.com/application/views/chat/chat.php?action=startchatsession",
                url: "http://default.haztuapp.x10.mx/ChatAJAX?action=startchatsession",
                cache: false,
                dataType: "json",
                success: function(data) {
                
                username = data.username;
                
                jQuery.each(data.items, function(i,item)
                        {
                            if (item)
                            {
                                // fix strange ie bug
                                if (item.cn == "")
                                {
                                    michatboxtitle = item.fn;
                                    $color = "<font color=red>";
                                }
                                else
                                {
                                    michatboxtitle = item.cn;
                                    $color = "<font>";
                                }
                            
                                michatboxtitle = michatboxtitle.replace(/\./g, '');
                                michatboxtitle = michatboxtitle.split(' ').join('_');
                        
                                chatboxID = item.f;

                                tituloChat = michatboxtitle;
                                tituloChat = tituloChat.substring(tituloChat.indexOf('_')+1, tituloChat.length);
                                Asesor = tituloChat.substring(tituloChat.indexOf('_para_') + 6, tituloChat.length);
                                AsesorId = Asesor.substring(0, Asesor.indexOf('_'));
                                Asesor = Asesor.substring(Asesor.indexOf('_') + 1, Asesor.length);
                                Asesor = Asesor.replace(/_/g, ' ');
                                tituloChat = tituloChat.substring(0, tituloChat.indexOf('_para_'));
                                tituloChat = tituloChat.replace(/_/g, ' ');
                                nombreUsuario = tituloChat;
                                tituloChat = tituloChat + '/' + Asesor;
                            
                                $nombreChat = tituloChat;
                            
                                if (item.cn == "")
                                {
                                    $nombreChat = nombreUsuario;
                                }
                                else
                                {
                                    $nombreChat = Asesor;
                                }
                            
                                if (jQuery("#chatbox_"+michatboxtitle).length <= 0)
                                {
                                    createChatBox(chatboxID, michatboxtitle,0, 0, '-' + AsesorId);
                                }
                            
                            
                            
                                $origen=item.fn;
                                if (item.s == 1)
                                {
                                    $origen = username;
                                }
                                
                                jQuery("#chatbox_"+michatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+$color+$nombreChat+'<br>'+item.d+':</font><br></span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
                        
                                //if (item.s == 2)
                                //{
                                    //jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
                                //}
                                //else
                                //{
                                //    jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+$origen+'<br>'+item.d+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
                                //}
                            }
                        });
                
                for (i=0;i<chatBoxes.length;i++) {
                chatboxtitle = chatBoxes[i];
                jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop(jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
                setTimeout('jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop(jQuery("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);', 100); // yet another strange ie bug
                }
                // smith
                //$("#chatbox_"+chatboxtitle).draggable();
                //$(".chatbox_").draggable();
                ////////////////
                setTimeout('chatHeartbeat();',chatHeartbeatTime);
                
                }});
}




/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
