// AJAX LIVE STATUS POCTU POSLUCHACU A LIVE CHAT
// NAPSANO PRO RADIO GOTHIC  www.radiogothic.net
// 2008 (c) Ashus www.ashus.ashus.net


var last_song="";
var max_chat_items=30;


function Ajax_init()
	{
	this.loadAjax = function (myurl,str,method,mydiv)
		{
		this.mydiv=mydiv;
	    if (window.XMLHttpRequest)
			{
	        this.xmlHttp = new XMLHttpRequest();
	    	} else if (window.ActiveXObject)
				{
	        try {
	          this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	        	} catch (eror) {
	          this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	        	}
	    	}
		if (this.xmlHttp==null)
			{
			alert ("Your browser does not support AJAX!");
			return;
			}
		var url=myurl;
		var mmethod=method;
		if ((str != "") && (mmethod == 'GET'))
			{
			url=url+"?"+str;
			str=null;
			}

		this.xmlHttp.onreadystatechange = function() { iIncomingData(this, mydiv); };
		this.xmlHttp.open(mmethod,url,true);
		this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		this.xmlHttp.send(str);
		}
	}


iIncomingData = function (xmlHttp,mydiv)
	{
	if (xmlHttp.readyState == 4)
		{
	    if (mydiv == "status")
	        {
		    var recv = xmlHttp.responseText.split("\n",2);
		    var listeners = recv[0];
		    var song = recv[1];

			if ((last_song != song) && (song != null))
				{
					last_song = song;
// 					song = Utf8.decode(song);
					setmarqcontent(song);
				}

			if (listeners != "0")
				{$("#listenerscnt").show();}
				else
				{$("#listenerscnt").hide();}

			var elem = $("#listeners");
			if (elem.html() != listeners)
			    {elem.html(listeners);}


			} else if (mydiv == "chat")
	        {
		    var recv = xmlHttp.responseText;
// 		    recv = Utf8.decode(recv);
		    recv = recv.split("\n");
			var cnt = recv.length;

			for (i=0;i<cnt;i++)
				{
				recv[i] = trim(recv[i]);
				if (recv[i] != '')
				    {
					if (recv[i].substring(0, prefix_chat_post_L) == prefix_chat_post)
					    {
                        var cc = chat_content.innerHTML.split("\n");
                        if (cc.length >= max_chat_items)
                            {
                            var ccnew = "";
							for (o=1;o<max_chat_items;o++)
							    {
                                ccnew = ccnew + cc[o] + "\n";
								}
							ccnew = ccnew + recv[i];
							chat_content.innerHTML = ccnew;
							} else {
							chat_content.innerHTML = chat_content.innerHTML + "\n" + recv[i];
							}

						} else if (recv[i].substring(0, prefix_chat_present_L) == prefix_chat_present)
						{
						chat_present.innerHTML = recv[i];
						}
					}
				}

			} else if (mydiv == "chat_send")
			{
			if (parseInt(xmlHttp.responseText) == 1)
			    {
			    if (chat_text != null)
			        {
	                chat_text.value = "";
	                chat_text.focus();
					}
				// vyjimecne spustime nacteni zmen, at necekame na zobrazeni vlastniho postu ani vterinu
				var ajax_chat_ex = new Ajax_init();
                ajax_chat_ex.loadAjax("ajax.chat.php", "", "POST", "chat");
				}
			if (chat_submit != null)
			    {
				chat_submit.disabled = false;
				}
			} else {

			var divptr = document.getElementById(mydiv);
			if (divptr != null)
				{divptr.innerHTML = xmlHttp.responseText;}
			}
		}
	}


/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/

var Utf8 = {

    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function trim(str)
	{
	return str.replace(/^\s+|\s+$/g, '');
	}

function initAndRunAjax(url, vars, method, id, tout, vars_ft)
	{
	eval('window.ajax_'+id+' = new Ajax_init();');

	var fnc = 'ajax_'+id+'.loadAjax("'+url+'", "'+vars+'", "'+method+'", "'+id+'")';

	if (vars_ft != "")
	    {eval('ajax_'+id+'.loadAjax("'+url+'", "'+vars_ft+'", "'+method+'", "'+id+'")');} else
	    {eval(fnc);}

	if (tout > 0)
		{setInterval(fnc, tout*1000);}
	}


// AJAX INIT


function initAjaxStatus()
	{
	initAndRunAjax("ajax.status.php", "a=1", "POST", "status", 10);
	}

function initAjaxStatusWithoutCounter()
	{
	initAndRunAjax("ajax.status.php", "a=0", "POST", "status", 10);
	}

function initAjaxChat()
	{
	window.prefix_chat_post = '<div class="chat_post">';
	window.prefix_chat_present = '<div class="chat_present_cont">';
	window.prefix_chat_post_L = window.prefix_chat_post.length;
	window.prefix_chat_present_L = window.prefix_chat_present.length;

	window.chat_content = document.getElementById("chat_content");
	window.chat_present = document.getElementById("chat_present");

	initAndRunAjax("ajax.chat.php", "", "POST", "chat", 5, "init=1");
	}

