var pm_sound = "/cmd/sounds/button_tiny";

var chat_tabs = new Array();

var ch_blinkOrder = 0;

var ch_newMessages = new Array();
var ch_newMessagesWin = new Array();
var chat_tab_closed = new Array();
var ch_chatboxFocus = new Array();
var last_c_messenger = [];

var ch_windowFocus = true;
var ch_typing = 0;
var chat_emj,ch_emojiTxtEditor;
var plholder = lang.pm_emoji_placeholder;
var ch_originalTitle;

var chatHeartbeatCount = 0;
var minChatHeartbeat = 4000;
var maxChatHeartbeat = 33000;
var chatHeartbeatTime = minChatHeartbeat;



// for num count
var chatHeartbeatCount_x = 0;
var minChatHeartbeat_x = 1000;
var chatHeartbeatTime_x = minChatHeartbeat_x;


var chat_markup = '<section id="%chatid" class="chat_tab">\
				   <a title="Close" onclick="closeChatTab(this,event,\'%chatid\');" href="javascript:void(0)" class="tico tico__n-t notifs_close"><span class="glyphicon glyphicon-remove chattab"></span><!--<i class="tico_img ic ic_close"></i>--></a>\
				   <div class="chat_sub_tab">\
				   <div class="chat_header_uname">\
				   <a href="javascript:void(0)" class="maxminchattab" onclick="toggleChatBoxGrowth(\'%chatid\');" >&nbsp;</a>\
				   <div class="chat_user_img"><img src="/getPhoto?p=%photo&sz=small" /></div>\
				   <a href="/user/%uid" hrefattr="true">%user_fullname</a>\
				   <div data-tipsy-orientation="e" style="display:none;" rel="tipsy" id="chat_active_now" class="ic_msg_header_online" title="'+ lang.active_now +'"><i class="msg_online_ic"></i></div>\
				   <span id="chat_tab_unread_count" class="chatTab_unread_count"></span>\
				   </div>\
				   <div class="chat_cnt">\
				   <div class="chat_msgs nano"><div class="chat_ovr nano-content">\
				   <div id="hooKMsgkLoadingChat" style="color:#fff;"><span class="app_loading"></span></div>\
					</div></div>\
					<div class="chat_txt_separator"></div>\
				   <div class="chat_textarea"><textarea placeholder="'+lang.pm_emoji_placeholder+'..." id="txt_%uid"></textarea>\
				   <div class="chat_bottom_ics" data-chat-id="t%chatid">\
					<a class="chat_sm_btn" href="javascript:;"></a>\
					</div>\
					<div id="WD_tracks_t%chatid" class="WD_attached_tracks _CHAT"><ul></ul></div>\
					<div id="t%chatid" class="jb_attached_files _tchat"></div>\
				   </div>\
				   </div></div></section>';
		
		
function getTabPos(chat_id){
	//return ((ga('#page_layout').width() - ( ga('.nav_tool_friends_online').width() + ga('#'+chat_id).width())) -20);
	return ga('.nav_tool_friends_online').width();//(ga('.nav_tool_friends_online').width() + ga('#'+chat_id).width()) -20;
}

function buildMap(){
	
	setTimeout(function ()
						{
							// build map
							if (ga('.chat_msgs')
								.find('.map-box')
								.length) ga('.chat_msgs')
								.find('.map-box')
								.each(function ()
								{
									var _t_data_ka = $(this)
										.find('.hookMapData')
										.html();
									var d_lc_map_dt = objHook(_t_data_ka);
									postPopupBuildMap(_t_data_ka, 'feed-tip-map-loc-' + d_lc_map_dt['pl-i']);
								});
						}, 100);
}

		function checkForOnlineUser(user_id){
			
		var chatTab = ga('#chat_'+user_id);
		var send = jAjax('/cmd.php','post',{'cmd':'is-online-user','uid':user_id});
		send.done(function(d){
		if(d) chatTab.find('#chat_active_now').show(); else chatTab.find('#chat_active_now').hide();
		});
			
		}
		
	function startChat(){
			var chat_id = escape(ga('#chat_id').val());
			
		if(chat_tabs.indexOf(chat_id) > -1) ga('#'+chat_id).find('.emoji-wysiwyg-editor').remove();
		
	
	

		
		if(!ga('#'+chat_id).find('textarea').hasClass('emojed')) { 
			  // create emoji text editor
	           chat_emj = ga('#'+chat_id).find('textarea').emojiarea({
                wysiwyg: true,
                buttonLabel: 'Emojis'+ (new Date()).getTime(),
                buttonPosition: 'after',
                button: '#messenger__emoji__stickers'
            });
			ga('#'+chat_id).find('textarea').addClass('emojed');
			
		}
			ch_emojiTxtEditor = ga('#'+chat_id).find('.emoji-wysiwyg-editor');
				
			chat_tabs.push(chat_id);
			 
            // send message by pressing ENTER key
            ch_emojiTxtEditor.off('keyup.tchatsend').on('keyup.tchatsend', function(e) {
				e.stopPropagation();
                if (e.keyCode == 13) return t_send(this,chat_emj, e,chat_id);
				
				
				if( trim( ga('#txt_'+chat_id))){
					
					ga('.uMailWrite__button_send').addClass('uMailWrite__button_send_active');
				} else {
					
					ga('.uMailWrite__button_send').removeClass('uMailWrite__button_send_active');
				}
            });
			ga('.uMailWrite__button_send').on('click',function(e){ 
				 return t_send(ch_emojiTxtEditor,chat_emj, e,chat_id);
				//ch_emojiTxtEditor.trigger('keyup.tchatsend');
			});
			
    ch_emojiTxtEditor.addClass('__placeholder_ex').html(plholder);
	
	
	
	ch_emojiTxtEditor.on(hasTouchStartEvent ? 'touchend' : 'click', function(e){

			var $this = $(this);
			
				if($this.hasClass('__placeholder_ex')){
				$this.removeClass('__placeholder_ex').empty();}
	
	

	}).on('blur',function(e){ var $this = ga(this); 

				if(!$.trim(chat_emj.val()))
					$this.html(plholder).addClass('__placeholder_ex');

	});
	setTimeout(function(){
		if(ga('.mailScrap__items.mailScrap__items_peer').length)
		scrollTChat();
		
		},500);
	
	if(ga('#conversation_start').length){
		chatHeartBeatTime = 10000;
		ga('#conversation_start').remove();
	}
	setTimeout(function(){chatHeartBeat(chat_id);},chatHeartbeatTime);
	ch_typingHead(chat_id);
	}	
		
function new_chat_window(elem,evt,is_session){
    
    if(!is_session){
	
			evt.stopPropagation();
			var el = ga(elem), uch = el.data('uch');
	}else
		    var uch = is_session;
		
	
	var user_id = uch.id, user_fullname = uch.fullname, user_photo = uch.photo;
	var chat_id = 'chat_'+user_id;
	var b = ga('body');
	///chat_user_id = user_id;
	
	
	const index = chat_tab_closed.indexOf(chat_id);
    
    if (index !== -1) {
        chat_tab_closed.splice(index, 1);
		chatHeartBeat(user_id);
    }
		
	
	
if (ga("#"+chat_id).length > 0) {
		if (ga("#"+chat_id).hasClass('__none')) {
			
		chat_tabs.push(chat_id);
		createCookie('chat_session', chat_tabs,1);
			removeAllChatFocusClass();
			ga("#"+chat_id).addClass('_focus').removeClass('__none _min');
			
			restructureChatTabs();
		} else if(ga('#'+chat_id).hasClass('_min')){
			 
			toggleChatBoxGrowth(chat_id);
			
		}
		if(!is_session)ga("#"+chat_id+" .emoji-wysiwyg-editor").focus();

		return;
	}
	
	
	if( chat_tabs.indexOf(chat_id) == -1 )
	{
		
		b.append( rp(chat_markup,{"%photo" : user_photo, "%chatid" : chat_id, "%uid" : user_id, "%user_fullname" : user_fullname}) );
		
		checkForOnlineUser( user_id );

		removeAllChatFocusClass();
		ga('#'+chat_id).addClass('_focus').css('bottom', '0px');

		
	var chat_position_calc = getTabPos(chat_id);

		var chatTabslength = 0;

		/*
	for (a in chat_tabs) {
		var _chtabx = chat_tabs.a;
		alert(a);
		if (!ga("#"+_chtabx).hasClass('__none')) {
			chatTabslength++;
		}
	}*/
	$.each(chat_tabs, function(a){
		
		if (!ga("#"+chat_tabs[a]).hasClass('__none')) 
			chatTabslength++;
	});

	if (chatTabslength == 0) {
		ga("#"+chat_id).css('right', chat_position_calc +'px');
	} else {
		var posleft = chat_position_calc + ((chat_tabs.length)*(270+7));
		ga('#'+chat_id).css('right', posleft +'px');
	}
	
	

/*
	if (chat_tabs.length == 0) {
		ga('#'+chat_id).css('left', chat_position_calc +'px');
	} else {
		var width = chat_position_calc - ((chat_tabs.length)*(270+7)); ///(chat_tabs.length)*(270+7)+20;
		ga('#'+chat_id).css('left', width+'px');
	}
	*/
 

	
	
	ch_chatboxFocus[chat_id] = false;

	ga("#"+chat_id).find('[contenteditable="true"]').blur(function(){
		ch_chatboxFocus[chat_id] = false;
		///$("#chatbox_"+chatboxtitle+" .chatboxtextarea").removeClass('chatboxtextareaselected');
	}).focus(function(){
		ch_chatboxFocus[chat_id] = true;
		ch_newMessages[chat_id] = false;
		ga("#"+chat_id).find('.chat_header_uname').removeClass('highlight_chat_v');
		////$("#chatbox_"+chatboxtitle+" .chatboxtextarea").addClass('chatboxtextareaselected');
	});
	
	
	
	
	
			if(!is_session) ch_emojiTxtEditor.focus();	
		
		
		
		chat_tabs.push(chat_id);
		
		
		createCookie('chat_session', chat_tabs,1);

		
		getChatMessages(user_id);
		
	} else {
		
		ga('#'+chat_id).focus();
		
	}
	
	
	scrollTChat(chat_id);
	ch_typingHead(chat_id);
	setTimeout(function(){chatHeartBeat(user_id);},chatHeartbeatTime);
	
}

// generate chat messages
function getChatMessages(user_id){
	
	var send = jAjax('/chat.php', 'post', {cmd:'getChatMessages', uid:user_id});
	send.done(function(d){
		
		ga('#chat_'+user_id+' .chat_msgs>.chat_ovr').html(d);

		//nanoScrollStart();
		setTimeout(function(){scrollTChat('chat_'+user_id);
		startVenoBox();},1000);
		buildMap();
	});
	
	
}
function chatTabSend(el,e,chat_id,str,transparent){
	
	return t_send(false, el, e, chat_id, str, transparent);
}


// send message
function t_send(t_emoji, el, e, chat_id, str, transparent){
	
	e.preventDefault();
	e.stopPropagation();
		 
	var msg = str ? str : ga('#'+chat_id).find('textarea').val(),
	rand_msg_id = Math.round(Math.random() * (99999 - 99)) + 99, 
	msg_markup = '<li %m_transparent id="chat_msg_%mid">\
	<div class="cnt_w_chat_m _me _chat_msg_sending">\
	<div rel="tipsy" class="chat_w_msg from-me " title="'+lang.just_now+'">%text</div>\
	</div></li><div class="clear"></div>';
	
	
	  msg_markup = '<div class="msgm msg_id_%mid _me from-me %m_transparent" id="%mid" onclick="selectMsg(\'%mid\');">\
\
\
    <a class="msg__avatar avatar avatar_1 avatar_size_m" hrefattr="true" href="/user/'+_U.i+'" style="background-image : url(/getPhoto?p='+_U.p+'&sz=small)">\
\
    </a>\
\
    <div class="msg__content">\
        <div class="msg__head">\
            <a class="msg__author" hrefattr="true" href="/user/{$user.id}">'+_U.fn+'</a>\
            <div class="msg__date">'+lang.just_now+'</div>\
\
            <div class="msg__check icon icon_check">\
\
            </div>\
\
\
            <div class="msg__star icon icon_star icon_blue">\
\
            </div>\
\
        </div>\
        <div class="msg__text">\
\
            <div class="msg_item _msg single_msg_item " data-id="">\
\
                <div class="mi_cont">\
                    <div class="mi_head">\
\
\
\
                    </div>\
                    <div class="mi_body">\
\
                        <div class="mi_text">%text</div>\
\
\
\
                    </div>\
                </div>\
            </div>\
        </div>\
    </div>\
    <div class="msg__restore">\
\
    </div>\
\</div>';
	
	t_emoji = t_emoji ? ga(t_emoji) : '';

	msg = msg.replace(/^\s+|\s+$/g,"");

	// check for images
	var images_cage = ga('#'+chat_id).find('.jb_attached_files._tchat');
	
	if(images_cage.children().length){
		
        // contain images 
		var im_cage_count = 0;
        for (var i = 0; i < images_cage.children().length; i++) {
            var bj_ph = images_cage.find('.attach-photo_del:eq(' + i + ')');
            var jb_ph = bj_ph.attr('id').split('_')[1];
            var _attached_m = bj_ph.data('attached') ? 1 : 0;
            var inceput = im_cage_count == 0 ? '[divstart]' : '';
            var sfarsit = im_cage_count + 1 == images_cage.children().length ? '[divend]' : '';
            msg += inceput + '[img]' + escape(jb_ph) + (_attached_m ? '&from=attach&' : '') + '[/img]' + sfarsit;
            im_cage_count++;
        }
		
	}
	
	
	// check for tracks
	var tracks_cage = ga('#'+chat_id).find('.WD_attached_tracks._CHAT>ul');
	
	if(tracks_cage.children().length){
		
        // contain songs
        tracks_cage.children().each(function() {
            var $this = ga(this);
            var track_id = escape($this.attr('id').split('-')[1]);

            msg += '[postSong]' + track_id + '[/postSong]';
       
        });
		
	}
	
	
	if ( t_emoji && ( !$.trim(msg) || msg === lang.pm_emoji_placeholder) ) {
        t_emoji.addClass('_emoji-empty-focus').focus();
        return;
    }
	
	if(t_emoji)
	t_emoji.empty();

	ga('#'+chat_id).find('#chat_items_p').append( rp(msg_markup,{"%m_transparent" : transparent ? 'm_transparent' : '', "%mid" : rand_msg_id, "%text" : str_smilies(msg)}) );
	ga('#'+chat_id).find('._chempty42').remove();
	
	// remove tracks
	tracks_cage.empty();
	
	//remove images
	images_cage.empty();
	
	// scroll down chat
	scrollTChat(chat_id);
	
	///var send = jAjax('/chat.php', 'post', {cmd:'send', msg:msg, uid:escape(chat_id.replace('chat_','')) });
	
	ga('#'+chat_id).find('._seen_msg').remove();
    var ac = {'action':'sendMessage','to':escape(chat_id),'text':msg, 'bg': transparent ? 'no' : 'yes', 'view_as':'json'};
	var send = jAjax(pm__action, 'post', ac);
	
	send.done(function(d){ 
		//return;
		 var res = validateJson(d);
		 
		 
		if (res.response === 'success') {
				ga('._chempty42').remove();
			
			   ga('#'+rand_msg_id).removeAttr('id').attr('id','msg_id_'+res.msgid);
			   var message = ga('#msg_id_'+res.msgid);
               message.find('._chat_msg_sending').removeClass('_chat_msg_sending');
			   message.find('.msg__date').text(res.time);//.data('original-title',res.time);
			   message.find('.mi_text').html(res.text.replace(/\\/g, ""));
			  
			   scrollTChat(chat_id);
			   startVenoBox();
            } else if (res.response == 'blacklist'){
				return displayErr(lang.pm_user_to_blacklist.replace('%s',ga('#pm_header_dtl').text()));
			}else if (res.response !== 'success') {
                return displayErr(res.response);
            } else {
                return displayErr(lang.pm_error_deliv);
            }
		 
		
	});
	
	
}

// close chat tab
function closeChatTab(elem,ev,chatid){
	
	const index = chat_tabs.indexOf(chatid);
    
    if (index !== -1) {
        chat_tabs.splice(index, 1);
    }
	
	var c_chatTabs = readCookie('chat_session');
	var new_chat_session = removeValue(c_chatTabs, chatid, ',')
	ga('#'+chatid).addClass('__none');
	restructureChatTabs();
	chat_tab_closed.push(chatid);
	createCookie('chat_session', new_chat_session,1);
}


function chatApMsg(message_txt, sended_at, chat_id, msgid,bg) {

	
	var msg_txt_markup = '<li '+bg+' id="chat_msg_'+msgid+'">\
	<div class="cnt_w_chat_m">\
	<div rel="tipsy" class="chat_w_msg from-they _ml" title="'+sended_at+'">'+message_txt+'</div></div></li><div class="clear"></div>';
	
	var recipient_dt = ga('#recipient_dt').data('recipient');
	
	var m = '<div class="msgm msg_id_'+msgid+' '+bg+'" '+bg+'>\
\
\
    <a class="msg__avatar avatar avatar_1 avatar_size_m" hrefattr="true" href="/user/'+recipient_dt.uid+'" style="background-image : url(/getPhoto?p='+recipient_dt.photo+'&sz=small)">\
\
    </a>\
\
    <div class="msg__content">\
        <div class="msg__head">\
            <a class="msg__author" hrefattr="true" href="/user/'+recipient_dt.uid+'">'+recipient_dt.uname+'</a>\
            <div class="msg__date">'+sended_at+'</div>\
\
            <div class="msg__check icon icon_check">\
\
            </div>\
\
\
            <div class="msg__star icon icon_star icon_blue">\
\
            </div>\
\
        </div>\
        <div class="msg__text">\
\
            <div class="msg_item _msg single_msg_item " data-id="">\
\
                <div class="mi_cont">\
                    <div class="mi_head">\
\
\
\
                    </div>\
                    <div class="mi_body">\
\
                        <div class="mi_text">'+message_txt+'</div>\
\
\
\
                    </div>\
                </div>\
            </div>\
        </div>\
    </div>\
    <div class="msg__restore">\
\
    </div>\
\</div>';
   
	ga('#'+chat_id).find('#chat_items_p').append( m );


    // scroll down to the last message
    scrollTChat(chat_id);
		
}
			function chatWindowTurnOnOriginalFavicon(){
				
				ga('#favicon').attr({'href':ga('#favicon').attr('original-href') + '?v'+ Math.random()});
				
			}
			function strip(str)
{
 var doc = new DOMParser().parseFromString(html, 'text/html');
   return doc.body.textContent || "";
}
function chatHeartBeat(chat_user_id) { 

	var chatTabId = chat_user_id;
	var chatWindow = ga('.messenger__layer#'+chat_user_id+' #chat_items_p');//ga('#'+chatTabId).find('.chat_ovr');
	const index = ga('#'+chat_user_id).length;
    //console.log(chat_tab_closed);
    if (!ga('#'+chat_user_id).length) {
		
        return;
    }
	
	var ch_itemsfound = 0;
	
	if (ch_windowFocus == false) {
 
		var ch_blinkNumber = 0;
		var ch_titleChanged = 0;
		for (x in ch_newMessagesWin) {
			if (ch_newMessagesWin[x] == true) {
				++ch_blinkNumber;
				if (ch_blinkNumber >= ch_blinkOrder) {
					document.title = '* * * * * * * * * *';//x+' says...';
					ga('#favicon').attr({'href':ga('#favicon').attr('pm-href')+ '?v'+ Math.random()});
					ch_titleChanged = 1;
					break;	
				}
			}
		}
		
		if (ch_titleChanged == 0) {
			document.title = ch_originalTitle;
			chatWindowTurnOnOriginalFavicon();
	
			ch_blinkOrder = 0;
		} else {
			++ch_blinkOrder;
		}

	} else {
		for (x in ch_newMessagesWin) {
			ch_newMessagesWin[x] = false;
		}
	}

	for (x in ch_newMessages) {
		if (ch_newMessages[x] == true) {
			if (ch_chatboxFocus[x] == false) { 
				//FIXME: add toggle all or none policy, otherwise it looks funny
				//ga('#'+chatTabId).find('.chat_header_uname').toggleClass('highlight_chat_v');
				
			}
		}
	}
	
	// online user
	checkForOnlineUser( chat_user_id );
 
    // typing
    var sTyping = jAjax(pm__action, 'post', 'action=typing&to=' + escape(chat_user_id) + '&tp=' + escape(ch_typing) + '&view_as=json');
    sTyping.done(function(data) {
      
 


        var rsp = validateJson(data);


        if (rsp.typing === '1' && chat_user_id === rsp.user) {
            if (chatWindow.find('[data-utyping="true"]').length == 0)
                chatWindow.append('<div class="toCenterTtp" data-utyping="true"><span class="ic12 ico-inline ic12_typing"></span><span class="msg_seen_tx">' + lang.typing + ' ...</span></div>');
				scrollTChat(chatTabId);
        } else if (rsp.typing !== '1') {

           chatWindow.find('[data-utyping="true"]').remove();

        }

    });


    // update messages on active conversation
    var req = jAjax(pm__action, 'post', 'action=chatheartbeat&to=' + escape(chat_user_id) + '&view_as=json');

    req.done(function(data) {


		var i_c = validateJson(data);
		var unread_count = i_c.count_unread;
		
		// Seen 
		if(unread_count <= 0 && !chatWindow.find('._seen_msg').length && chatWindow.find('ul li:last>div:first').hasClass('_me')){
				
										  chatWindow.find('ul').after('<div class="_seen_msg">Seen</div>'); 
			}else if( unread_count > 0 ) { 
								 chatWindow.find('._seen_msg').remove(); 
		
		}
		
		
        for (var items = i_c.d, i = 0; i < items.length; ++i) {

            if (items.length) {
                var c = items[i],
                    message_id = c.id,
                    message_txt = c.msg.replace(/\\/g, ""),
                    from_user = c.from,
                    sended_at = c.time,
                    last_msg_by = c.lastby,
					bg = c.bg == 'no' ? true : false,
                    recipient_online = c.uonline,
                    message_read = c.read;
				
					chatWindow.find('._seen_msg').remove(); 
					chatApMsg(message_txt, sended_at, chatTabId, message_id, bg ? 'm_transparent' : '');
					ch_newMessages[chatTabId] = true;
                    ch_newMessagesWin[chatTabId] = true;
					buildMap();
					ga('[data-utyping="true"]').remove();
					
					
                ch_itemsfound += 1;
                if (ch_windowFocus == false) turnOnSound();
				 
            }
        }

        chatHeartbeatCount++;

        if (ch_itemsfound > 0) {
            chatHeartbeatTime = minChatHeartbeat;
            chatHeartbeatCount = 1;		
        } else if (chatHeartbeatCount >= 10) {
            chatHeartbeatTime *= 2;
            chatHeartbeatCount = 1;
            if (chatHeartbeatTime > maxChatHeartbeat) {
                chatHeartbeatTime = maxChatHeartbeat;
            }
        }

		
				if( ! ga('#'+chatTabId).hasClass('_focus') && ch_itemsfound > 0){
				ga('#'+chatTabId).find('#chat_tab_unread_count').html('('+ch_itemsfound+')');
				//ga('#'+chatTabId).find('.chat_header_uname').toggleClass('highlight_chat_v');
			}
		
        setTimeout(function(){chatHeartBeat(chat_user_id);}, chatHeartbeatTime);
      //  console.log("Chat Time",chatHeartbeatTime);
	//	console.log(data);
    });


}
/* check for messages */
function messengerCount() {
var itfound = 0, last_count_msg;
var new_messages_tophead = parseInt(ga('._msg.m_notif_space').find('span').text());
    var send = jAjax('/messages.php', 'post', {'action':'check-new-msg-count'});

    send.done(function(r) {
 
        var d = validateJson(r);

        if (d['response'] == '1' && d['count'] > 0) {

			
			if( ga('#messenger-contact-list').length ) {
				
				for(var i=0;i< d.users.length;i++){
					var e_us = d.users[i];
			 
					if(ga('#mess_'+e_us.fromuser).length){
						ga('#mess_'+e_us.fromuser).find('.convo__unread').removeClass('oval_hidden').html('+'+e_us.count);
						ga('#mess_'+e_us.fromuser).find('.convo__text').html(e_us.msg);
						
					}
					//console.log(e_us);
					
				}
				
			}
			 
			//menu_bubble_notif['messages'] = d.count;
			if(d.count !== new_messages_tophead && d.count > 0){
            ga('.m_notif_space').each(function(){
				ga(this).html( ga(this).hasClass('tophead') ? '<span>' + d['count'] +'</span>': b(d['count']));
			});
			 
			}
			last_c_messenger.msg = d.count;
			
			//if(d.count !== last_c.msg){
           /* ga('.m_notif_space').each(function(){
				
				if(last_count_msg != d['count'])
				ga(this).html( ga(this).hasClass('tophead') ? '<span>' + d['count'] +'</span>': b(d['count']));
			
				last_count_msg = d['count'];
			});*/
			itfound += 1;
			//}
			//last_c.msg = d.count;

        } else {
			ga('.m_notif_space').empty();
		}

        chatHeartbeatCount_x++;

        if (itfound > 0) {
            chatHeartbeatTime_x = minChatHeartbeat_x;
            chatHeartbeatCount_x = 1;
        } else if (chatHeartbeatCount_x >= 10) {
            chatHeartbeatTime_x *= 2;
            chatHeartbeatCount_x = 1;
            if (chatHeartbeatTime_x > maxChatHeartbeat) {
                chatHeartbeatTime_x = maxChatHeartbeat;
            }
        }

        setTimeout('messengerCount();', chatHeartbeatTime_x);
    });

}
// restructure chat tabs
function restructureChatTabs() {
	align = 0;

	///for (var x in chat_tabs) {
		$.each(chat_tabs,function(x){
		var chat_id = chat_tabs[x];
		var chat_position_calc = getTabPos(chat_id);
		
		if (!ga("#"+chat_id).hasClass('__none')) {
			
			if (align == 0) {
				
				ga("#"+chat_id).css('right', chat_position_calc +'px');
			} else {
				var posleft = chat_position_calc + ((align)*(270+7));
				ga("#"+chat_id).css('right', posleft+'px');
				
				
			}
			align++;
		}
	///}
	});
}

function rp(str, map){

  var re = new RegExp(Object.keys(map).join("|"),"gi");
	str = str.replace(re, function(matched){
		return map[matched];
	});
  
   return str;
}
function remove_hide_convers() {

    var cur_u = ga('.messenger__layer');
    cur_u.css('opacity','0.3');
        ajaxLoading();
        setTimeout(function() {
            ga('.toolbar_back.mail').trigger('click');
			removeAjaxLoad();
        }, 500);
 

}
function startChatSession(){
	/*
	var ss_chat_list = readCookie('chat_session');

	var send = jAjax('/chat.php', 'post', {cmd:'chatSessionGetUserInfo', chat_list:ss_chat_list});
	send.done(function(d){
	var r = validateJson(d);


	//for(var x in r)
		$.each(r,function(x){
				new_chat_window(false,false,r[x]);
		});
	
	});*/
	
	var $d = ga('.messenger__layer');
		
            // menu dropdown edit
            var m_sound = $d.find('[uid="enDisSound"]'),
                m_block = $d.find('[uid="blockOpponent"]'),
                m_unblock = $d.find('[uid="unBlockOpponent"]'),
                m_hide = $d.find('[uid="uidHideDialog"]'),
                m_delet = $d.find('[uid="retFromDialog"]');
	
	var recipientId = ga('#chat_id').val();
           // delete conversation
            m_delet.on('click touchstart', function(e) {
                e.preventDefault();
                e.stopImmediatePropagation();

                confirm_act(jprintf(lang.pm_confirm_delete_conversation, ga('#pm_header_dtl').text()),
                    function(event) {

                        var send = jAjax(pm__action, 'post', 'action=delConversation&to=' + escape(recipientId));
                        send.done(function(res) {

                            if (res == '1') {

                                remove_hide_convers();
                            } else {
                                displayErr(res);
                            }
                        }); //ajax


                    }); //confirm

            });

            // hide the conversation
            m_hide.on('click touchstart', function(e) {
                e.preventDefault();
                e.stopImmediatePropagation();
		 
                var send = jAjax(pm__action, 'post', 'action=hideConversation&to=' + escape(recipientId));
                send.done(function(res) {

                    if (res == '1') {

                        remove_hide_convers();
                    } else {
                        displayErr(res);
                    }


                });

            });

            // add user to blacklist
            m_block.on('click touchstart', function(e) {
                e.preventDefault();
                e.stopImmediatePropagation();


                confirm_act(jprintf(lang.pm_to_blacklist, ga('#pm_header_dtl').text()),
                    function(event) {

                        var send = jAjax(pm__action, 'post', 'action=uToBlackList&to=' + escape(recipientId));
                        send.done(function(res) {

                            if (res === '1') {

                                remove_hide_convers();
                            } else if (res === '0') {
                                displayErr(lang.pm_err_us_to_blacklist);
                            } else if (res !== '1' && res !== '0') displayErr(res);

                        }); // ajax

                    }); // confirmation

            }); // click
	 
	startChat();
	 
}

function removeAllChatFocusClass(){
	
		ga('.chat_cnt').each(function(){
		ga(this).closest('section').removeClass('_focus');
	});
	
}
function gchatsc(chat_id){
	return  ga('.messenger .scrollView');

}
function pm_sound_enable() {
    return readCookie('dk_pm_sound') === 'on' || !readCookie('dk_pm_sound') ? 1 : 0;
}
// sound
function turnOnSound() {

    /*$('#sound_notif_new_msg').remove();
    if (pm_sound_enable()) {alert('sound');$('body').append('<embed id="sound_notif_new_msg" src="' + pm_sound + '" autostart="true" hidden="true" loop="false"></embed>');}
*/
    if (pm_sound_enable()) {
        ga('#sound_notif_new_msg').remove();

        ga('<audio id="sound_notif_new_msg" autoplay="true" style="display:none;">\
    <source src="' + pm_sound + '.ogg" type="audio/ogg">\
    <source src="' + pm_sound + '.mp3" type="audio/mpeg">\
	</audio>').appendTo('body');
        setTimeout(function() {
            ga('#sound_notif_new_msg').remove();

        }, 300);
    }

}
// automatically scroll down the scrollbar
function scrollTChat(chat_id) {

  //  mdialog_hide_loader ? $ch_sc.scrollTop($ch_sc[0].scrollHeight) : setTimeout(function(){$ch_sc.scrollTop($ch_sc[0].scrollHeight)}, 800);


		gchatsc(chat_id).on('scroll.ImageLoad',function(e){
			//alert('este');
			var $this = $(this);
			
			$this.find('img:not(.__inited)').each(function(){
				var _$this = $(this);
				if(isElementInViewport(_$this))
				_$this.attr('src',_$this.data('original')).removeAttr('data-original').load(function(){
					
					ga(this).removeAttr('style').addClass('__inited');
					
				});
			});
			
		});
		 gchatsc(chat_id).scrollTop(gchatsc(chat_id)[0].scrollHeight+100);
		 
  
}

// toggle chat box
function toggleChatBoxGrowth(chat_id) {
	if (ga('#'+chat_id).hasClass('_min')) {  
		
		var minimizedChatBoxes = new Array();
		
		if (readCookie('chattab_minimized')) {
			minimizedChatBoxes = readCookie('chattab_minimized').split(/\|/);
		}

		var newCookie = '';

		for (i=0;i<minimizedChatBoxes.length;i++) {
			if (minimizedChatBoxes[i] != chat_id) {
				newCookie += chat_id+'|';
			}
		}

		newCookie = newCookie.slice(0, -1)


		createCookie('chattab_minimized', newCookie, 1);
		ga('#'+chat_id).removeClass('_min');

	} else {
		
		var newCookie = chat_id;

		if (readCookie('chattab_minimized')) {
			newCookie += '|'+readCookie('chattab_minimized');
		}


		createCookie('chattab_minimized',newCookie,1);
		ga('#'+chat_id).addClass('_min');

	}
	
}

function startVenoBox(){
	
	
var chat_venoOptions = {

    bgcolor: '#5dff5e',         // default: '#fff'
    numeratio: true,            // default: false


    // is called when opening is finished
    post_open_callback  : function(content, blocknum, blocktitle){
				ga('body').attr('style','overflow:hidden!important');
	},



    // is called after finished closing. 
    post_close_callback : function(content,blocknum,blocktitle){
        ga('body').removeAttr('style');
    }
}

  ga('.chat_tab .venobox').venobox(chat_venoOptions); 
	
}
// delete message
function delMsg(o, e) {
    e.preventDefault();
    var $o = ga(o),
        m_id = $o.attr('id');
		
    confirm_act(lang.pm_delete_confirm,
        function(event) {

            var s_d = jAjax('/messages.php', 'post', 'action=del&i=' + escape(m_id) + '&view_as=json');
            s_d.done(function(r) {

                if (r === '1') {
                   ga('.msg_id_'+m_id).slideUp(function(){ga(this).remove()});//.html('<i>' + lang.pm_deleted_succ + '</i>').slideUp();
                   
                } else
                    return displayErr(r);

            });

        });

}

// dropDown
function md_enb_dropDown(ev) {
    ev.preventDefault();
    ev.stopPropagation();
    var $t = ga(ev.target);
    var $d = ga('.messenger_st_block');
 
    var removeD = function() {

        $d.removeClass('__o_act');

    }

    var addD = function() {

        $d.addClass('__o_act'); //.css(css);
    }


    if ($d.hasClass('__o_act'))
        removeD();
    else
        addD();

    ga(document).on('click.pmDialog', function(e) {
        e.stopPropagation();
        removeD()
    });

}
// select message
function selectMsg(id){
	var _msg = ga('.msg_id_'+id);
	
	if(!_msg.hasClass('selected')){
		
		_msg.addClass('selected');
		var _m_html = ga('<section class="msg_editor_mode"></section>');
		_m_html.append('<div class="msg_editor_ics"><div onclick="delMsg(this,event);" id="'+id+'" ontouchstart="delMsg(this,event);" class="delete32 ic32"></div><div class="sel-ic ic32"></div></div>');
		
		_msg.prepend(_m_html);
		
	} else {
		_msg.removeClass('selected');
		_msg.find('.msg_editor_mode').remove();
	}
	
}

ga(document).ready(function(){
	

	ch_originalTitle = document.title;
	
	ga([window, document]).blur(function(){
		ch_windowFocus = false;
	}).focus(function(){
		ch_windowFocus = true;
		document.title = ch_originalTitle;
	});
	resizeMessenger();
	 setTimeout('messengerCount();', chatHeartbeatTime_x);
});

// focus chat tab
ga(document).off('click.chattab').on('click.chattab', '.chat_cnt', function(e){
	e.stopPropagation();
	removeAllChatFocusClass();
	
	ga(this).closest('section').addClass('_focus');
	ga(this).closest('section').find('#chat_tab_unread_count').html('');
	ga(this).closest('section').find('.chat_header_uname').removeClass('highlight_chat');
	
});

ga(document).off('click.chatTabBlur').on('click.chatTabBlur', 'body,html', function(e){
	ga('.chat_cnt').each(function(){
		ga(this).closest('section').removeClass('_focus');
	});
});


/* Live typing notification
---------------------------- */

function c_doneTyping() {
    ch_typing = 0;
    return ch_typing;
}

function ch_typingHead() {

    var c_typingTimer;
    var c_doneTypingInterval = 500;


    ch_emojiTxtEditor.off('keypress.tchat_write').on('keyup.tchat_write', function(e) {
        ch_typing = 1;

        clearTimeout(c_typingTimer);
        c_typingTimer = setTimeout(c_doneTyping, c_doneTypingInterval);

    });

    ch_emojiTxtEditor.off('keydown.tchat_write').on('keydown.tchat_write', function() {
        clearTimeout(c_typingTimer);
        ch_typing = 0;

    });

}
