﻿// ZVEZDATA.com IM 
// by Theodosios Kariotis JR.
// All rights reserved!
// ******************************************************
function readCookieM(name){var nameEQ = name + "="; var ca = document.cookie.split(';');for(var i=0;i < ca.length;i++){var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);} return '';}
var im_NewMessages = 0;

function im_hightlight(id){
    var hldiv = document.getElementById('im_highlighted');
    hldiv.style.display="block";
    document.getElementById('im_hl_avatar').src='http://static.zvezdata.com/m/'+id+',s.jpg';
    
    try{clearTimeout(dehightlightTimeout);}
    catch(err){}
    dehightlightTimeout = setTimeout('im_dehighlight()', 3000);
}

function im_dehighlight(){
    try{
        var hldiv = document.getElementById('im_highlighted');
        hldiv.style.display="none";
    }
    catch(err){}
}


// Open chat window functions
// ******************************************************
function im_openChat(id){
    try{
        window.open('im.aspx?chat='+id,'zvezdata_im_'+id,'scrollbars=no,directories=no,resizable=no,top=0,left=0,location=0,width=300,height=300,status=no');
    }
    catch(err){alert('Please disable your pop-up blocker!');}
}

function im_openChatBt(id){
    try{
        window.open('im.aspx?chat='+id,'zvezdata_im_'+id,'scrollbars=no,directories=no,resizable=no,top=0,left=0,location=0,width=300,height=300,status=no');
        document.getElementById('im_chat_notification_'+id).style.display="none";
        document.getElementById('im_notification_new_messages').style.display="none";
    }
    catch(err){
        document.getElementById('im_chat_notification_'+id).style.display="inline";
        alert('Please disable your pop-up blocker!');
    }
}

// Other chat functions
// ******************************************************

function im_goBottom(){document.getElementById('im_chat_conversation').scrollTop = document.getElementById('im_chat_conversation').scrollHeight;}

function im_sendMessage(u,id){
    var inf = document.getElementById('im_chat_message');
    if(inf.value.length == 0){return false;}
    inf.disabled = true;
    call('ajax/im.aspx?a=new&r='+id, 'im_sendMessageRSP','&message='+inf.value);
    im_display_message(u, inf.value, 'out');
    setTimeout('im_goBottom()', 200);
    inf.disabled = false;
    inf.value="";
    inf.focus();
    return false;
}

function im_sendMessageRSP(){
    //var inf = document.getElementById('im_chat_message');
    //inf.disabled = false;
    //inf.value="";
    //inf.focus();
}

function im_display_message(u, m, d){
    var mc = document.getElementById('im_chat_conversation');
    var imdiv = document.createElement('div');
	if(d == 'in'){imdiv.className = 'im_message_in';}
	else{imdiv.className = 'im_message_out';}
    imdiv.innerHTML = '<b>'+u+':</b> '+m;
    mc.appendChild(imdiv);
}


// ******************************************************

function im_getMessages(id){
    call('ajax/im.aspx?a=get&r='+id, 'im_getMessagesRSP','');
}

function im_getMessagesRSP(data){
	var root = data.childNodes;
	if(root[1] != null){var status_node = root[1].firstChild; var i = 1;}
	else {var status_node = root[0].firstChild; var i = 0};
	if(status_node.getAttribute('value') != 1){
    	var tmp = root[i].childNodes;
    }
    else{
		var xml_nodes = root[i].childNodes;
		for(j = 0; j < xml_nodes.length; j++){
			if(xml_nodes[j].nodeName == 'zvezdata_im'){
				message_nodes = xml_nodes[j].childNodes;
                if(message_nodes.length > 0){
                    var im_sound = document.getElementById('im_sound');
	                im_sound.innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="1" height="1" id="im" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="im.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="im.swf" quality="high" bgcolor="#ffffff" width="1" height="1" name="im" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
                    document.title = 'НОВО СЪОБЩЕНИЕ!!!';
                }
				for(i = 0; i<message_nodes.length; i++){
                    im_display_message(message_nodes[i].getAttribute('username'), message_nodes[i].firstChild.nodeValue, 'in');
                }
            }
        }
    }
    im_goBottom();
    return;
}


// ******************************************************
function isNull(val){return(val==null);}

function im_updateNewMessages(){
    var tmp = unescape(readCookieM('ZvezdataIM_new'));
    if(im_NewMessages == tmp){return;}
    else{im_NewMessages = tmp}
    
    var tmpHTML = '<div id="im_highlighted"><a href="#" title="Затвори и изтрий разговора!">Затвори!</a><br /><img id="im_hl_avatar" src="" /></div>';
    try{
        if(!isNull(tmp) && tmp.length >0){
            var nm = tmp.split(",");
            var c = 0;
            for(var i in nm){
                if(nm[i] != 0){
                    tmpHTML += '<img id="im_chat_notification_'+nm[i]+'" src="http://static.zvezdata.com/m/'+nm[i]+',s.jpg" class="im_new_message" onMouseOver="im_hightlight('+nm[i]+');" onclick="im_openChatBt('+nm[i]+')" />';
                    c++;
                }
            }
            if(c > 0){
               tmpHTML += ' <span id="im_notification_new_messages">ИМАТЕ НОВИ СЪОБЩЕНИЯ!!!</span>';
               document.getElementById('zvezdata_im_holder').style.display="block";
            }
            else{
                document.getElementById('zvezdata_im_holder').style.display="none";
            }
        }
        else{
            document.getElementById('zvezdata_im_holder').style.display="none";
        }
    }
    catch(err){}
    //document.write(ToNumber(nm[i]));
    document.getElementById('zvezdata_im_bar').innerHTML = tmpHTML;
}