function CLive(ins)
{
    this.ins = ins ? ins : '';
    this.x   = false;
    this.d   = {};
    this.td  = 2;
    this.cooh = false;
    this.coos = false;

   	this.cd = {' ':'',',':'',"'":'','"':'',':':'',';':'','/':'','-':'',
   	'а':0,'я':1,'о':2,'у':3,'ю':3,'р':4,'и':5,'ы':6,'е':7,'ё':7,'э':7,'к':8,'й':9,
   	'б':'b','в':'v','г':'g','д':'d','ж':'a','з':'z','л':'l','м':'m','н':'n','п':'p',
   	'с':'s','т':'t','ф':'f','х':'x','ц':'h','ч':'e','ш':'o','щ':'u','ь':'r','ъ':'y'};

    var me   = this;

    this.getXML = function()
    {
   	    var dt = $(inst(this.ins)+'_dt');
   	    if(dt)
   	    {
   	        dt = this.dtURL(dt.value);
   	        if(dt) dt = '?d='+dt;
   		}
   		else dt = '';
    	(new CAjax(true)).req('/live/xml'+dt,function(x){if(x){
    	    me.x = x;
    		me.parseXML();
   		}});
    };

    this.dtURL = function(dt)
    {
        if(dt.indexOf('/')>-1)
        {
        	dt = dt.split('/');
        	if(!dt[2]) dt = '';
        	else
        	{
        	    dt[3] = dt[0];
        	    dt[0] = dt[1];
        	    dt[1] = dt[3];
        	}
        }
        else
        {
        	dt = dt.split('.');
        	if(!dt[2]) dt = '';
        }
        if(dt) dt = dt[2]+'-'+dt[1]+'-'+dt[0];
        return dt;
    };

    this.parseXML = function()
    {
        var n = me.x.selectNodes('/live/tour');
        var len = n.length;
        if (len==0)
        {
            $(me.ins+'data').innerHTML = '';
            $(me.ins+'hidden').innerHTML = '';
            $(me.ins+'hidden_cnt').innerHTML = '';
            windowsLineUp();
            return;
        }
        var s = '', h = '', st, f, so=0, ho=0, ttl, num, ln, m, j;
        for(var i=0;i<len;i++)
        {
        	ttl = n[i].getAttribute('title');
        	st = me.getStat(me.cl(ttl));
            if(st==1) continue;
        	num = n[i].getAttribute('id');
          	f = '';
          	m = n[i].selectNodes('match');
          	ln = m.length;
          	if(ln==0) continue;
          	for(j=0;j<ln;j++) f += me.rowNode(m[j],j==ln-1);
            if (st==0)
            {
            	ho++;
            	h += me.claster(ttl, f, num, ho%2 ? ' odd' : '', 'none', 'show');
            }
            else
            {
        	    so++;
        	    s += me.claster(ttl, f, num, so%2 ? ' odd' : '', 'block', 'hide');
        	}
        }
        for(var i=0;i<len;i++)
        {
        	ttl = n[i].getAttribute('title');
        	st = me.getStat(me.cl(ttl));
            if(st!=1) continue;
        	num = n[i].getAttribute('id');
          	f = '';
          	m = n[i].selectNodes('match');
          	ln = m.length;
          	if(ln==0) continue;
          	for(j=0;j<ln;j++) f += me.rowNode(m[j],j==ln-1);
       	    so++;
       	    s += me.claster(ttl, f, num, so%2 ? ' odd' : '', 'none', 'hide');
        }
        $(me.ins+'data').innerHTML = s;
        $(me.ins+'hidden').innerHTML = h;
        $(me.ins+'hidden_cnt').innerHTML = ho;
        windowsLineUp();
    };

    this.rowNode = function(n,last)
    {
    	var cl = last ? '' : ' class="live"';
    	var s = {};
    	var ell = xText(n.selectSingleNode('el'));
    	var st = xText(n.selectSingleNode('st'));
    	var id = n.getAttribute('id');
    	switch(st)
    	{
    		case 'inprogress':
   		    if(ell=='false'){s.im = '2'; s.tm = '&nbsp;'; } else {s.im = '1'; s.tm = ell.replace("'","<blink>'</blink>"); } break;

    		case 'notstarted': s.im = '0'; s.tm = me.myTime(xText(n.selectSingleNode('sd')));
    		s.sc = '<span style="cursor:pointer;" onmousedown="drag_new(event, \'/live/odds/'+id+'\')" '+
    		'onclick="windowNew(\'/live/odds/'+id+'\', event)"><img src="/img/live/1x2.gif" width="16" height="16" alt="1x2" title="1 x 2"/></span>';
    		break;

    		case 'finished'  : s.im = '3'; s.tm = '&nbsp;'; break;

    		case 'cancelled': default : s.im = '4'; s.tm = '&nbsp;'; s.sc = '&nbsp;'; break;
    	}
        if(typeof(s.sc)=='undefined') s.sc = xText(n.selectSingleNode('team1/rs')) + ' - ' + xText(n.selectSingleNode('team2/rs'));
    	if(s.sc)
    	{
        	s.ht = n.selectSingleNode('team1/ht');
       		s.ht = s.ht ? '<span style="color:#777;font-size:10px;"> (' + xText(s.ht) + '-' + xText(n.selectSingleNode('team2/ht')) + ')</span>' : '';
       		s.sc += s.ht;
       		if(st!='notstarted')
                s.sc = '<span style="cursor:pointer;" onmousedown="drag_new(event, \'/live/info/'+id+'\')" onclick="windowNew(\'/live/info/'+id+'\', event)">'+s.sc+'</span>';
        }

        s.t1 = xText(n.selectSingleNode('team1/team'));
        s.t2 = xText(n.selectSingleNode('team2/team'));

        var cards = ''; var y = n.selectSingleNode('team1/rc'); if(y){y = parseInt(xText(y));
        	if(y>0) cards += '<div style="margin-top:4px;float:right;background:#F00;width:4px;height:6px;border:#777 1px solid;margin-right:6px;cursor:help;" title="'+y+'"></div>';
        }
        y = n.selectSingleNode('team1/y2c'); if(y){y = parseInt(xText(y));
        	if(y>0)	cards += '<div style="margin-top:4px;float:right;background:#EE0;width:2px;height:6px;margin-left:0px;margin-right:6px;cursor:help;border-top:#777 1px solid;border-right:#777 1px solid;border-bottom:#777 1px solid;" title="'+y+'"></div>'+
        	'<div style="margin-top:4px;float:right;background:#F00;width:2px;height:6px;margin-right:0px;cursor:help;border-top:#777 1px solid;border-left:#777 1px solid;border-bottom:#777 1px solid;" title="'+y+'"></div>';
        }
        y = n.selectSingleNode('team1/yc'); if(y){y = parseInt(xText(y));
        	if(y>0)	cards += '<div style="margin-top:4px;float:right;background:#EE0;border:#777 1px solid;width:4px;height:6px;margin-right:6px;cursor:help;" title="'+y+'"></div>';
        }
        if(cards) s.t1 = '<div style="float:right">'+s.t1+'</div>' + cards;

        cards = ''; y = n.selectSingleNode('team2/rc'); if(y){y = parseInt(xText(y));
        	if(y>0)	cards += '<div style="margin-top:4px;float:left;background:#F00;width:4px;height:6px;border:#777 1px solid;margin-right:6px;cursor:help;" title="'+y+'"></div>';
        }
        y = n.selectSingleNode('team2/y2c'); if(y){y = parseInt(xText(y));
        	if(y>0)	cards += '<div style="margin-top:4px;float:left;background:#F00;width:2px;height:6px;margin-right:0px;cursor:help;border-top:#777 1px solid;border-left:#777 1px solid;border-bottom:#777 1px solid;" title="'+y+'"></div>'+
        	'<div style="margin-top:4px;float:left;background:#EE0;width:2px;height:6px;margin-left:0px;margin-right:6px;cursor:help;border-top:#777 1px solid;border-right:#777 1px solid;border-bottom:#777 1px solid;" title="'+y+'"></div>';
        	;
        }
        y = n.selectSingleNode('team2/yc'); if(y) {y = parseInt(xText(y));
        	if(y>0) cards += '<div style="margin-top:4px;float:left;background:#EE0;border:#777 1px solid;width:4px;height:6px;margin-right:6px;cursor:help;" title="'+y+'"></div>';
        }
        if(cards) s.t2 = '<div style="float:left;margin-right:6px;">'+s.t2+'</div>' + cards;



    	return '<tr><td width="5%" align="center" valign="top"'+cl+'><img src="/img/live/'+s.im+
    	       '.gif" style="margin-top:4px;" alt=""/></td>'+
               '<td width="10%" nowrap="nowrap" align="center" valign="top"'+cl+'>'+s.tm+'</td>'+
               '<td width="35%" align="right" valign="top"'+cl+'>'+s.t1+'</td>'+
               '<td width="5%" align="center" valign="top"'+cl+'>-</td>'+
               '<td width="35%" valign="top"'+cl+'>'+s.t2+'</td>'+
               '<td width="10%" align="center" valign="top"'+cl+' nowrap="nowrap">'+s.sc+'</td></tr>';
    };

    this.claster = function(ttl, d, i, odd, vis, btn)
    {
    	return '<div id="'+me.ins+'live_'+i+'" class="live'+odd+'">'+
               '<div class="live_hide"><img src="/img/live/'+btn+'.gif" alt="" onclick="live'+me.ins+'.hide('+i+')"/></div>'+
               '<div><span class="ajax strong" onclick="live'+me.ins+'.toggle('+i+')" id="'+me.ins+'ttl_'+i+'">'+ttl+'</span></div>'+
               '<div id="'+me.ins+'live_'+i+'_info" style="margin-top:5px;display:'+vis+'"><table width="100%" cellspacing="0" cellpadding="3">'+d+
               '</table></div></div>';
    };

    this.getStat = function(s)
    {
        if(!me.cooh) { me.cooh = coo.get('liveh'); me.cooh = me.cooh ? me.cooh : ''; }
        if(!me.coos) { me.coos = coo.get('lives'); me.coos = me.coos ? me.coos : ''; }
        if (me.cooh.indexOf(s)>-1) return 0;
        if (me.coos.indexOf(s)>-1) return 2;
        return 1;
    };

    this.showAll = function()
    {
    	this.cooh = '';
    	coo.remove('liveh','/');
    	this.parseXML();
    	this.tab(0);
    };

    this.hideAll = function()
    {
    	var el = $(ins+'data').getElementsByTagName('SPAN');
    	var len = el.length, num, f, t;
    	for(var i=0;i<len;i++)
    	{
    		if (!el[i].id) continue;
    		num = parseInt(el[i].id.replace(me.ins+'ttl_',''));
    		if (isNaN(num) || !num) continue;
    		f = $(me.ins+'live_'+num+'_info');
    		if(!f) continue;
            if (f.style.display != 'none') continue;
            t = this.cl(el[i].innerHTML);
            f = this.getStat(t);
            if (f>0) this.cooh += t;
    	}
    	coo.set('liveh',this.cooh,'/');
    	this.parseXML();
    };

    this.tab = function(t)
    {
    	var t1 = 'block', t2 = 'none';
    	if (t) {t1 = 'none'; t2 = 'block';}
    	$(this.ins+'hidden_m').style.display = t2;
    	$(this.ins+'hidden').style.display = t2;
    	$(this.ins+'data_m').style.display = t1;
    	$(this.ins+'data').style.display = t1;
    	windowsLineUp();
    };

    this.hide = function(num)
    {
        var t = this.cl($(this.ins+'ttl_'+num).innerHTML);
    	var st = this.getStat(t);
    	if (st>0)
    		this.cooh += t;
    	else
    	    this.cooh = this.cooh.replace(t,'');
    	coo.set('liveh',this.cooh,'/');
        this.parseXML();
    };

    this.toggle = function(num)
    {
        var el = $(this.ins+'live_'+num+'_info');
        var t = this.cl($(this.ins+'ttl_'+num).innerHTML);
    	var st = this.getStat(t);
        if (el.style.display=='none')
        {
            el.style.display = 'block';
            if (st==1) this.coos += t;
    	}
    	else
    	{
            el.style.display = 'none';
            if (st==2) this.coos = this.coos.replace(t,'');
    	}
    	coo.set('lives',this.coos,'/');
    	windowsLineUp();
    };

    this.myTime = function(t)
    {
    	t = t.split(' ')[1];
    	var d = me.td; d += typeof(window.tz)!='undefined' ? window.tz : 0;
    	if(d==0) return t;
    	t = t.split(':');
    	t[0] = t[0] - 0 + d;
    	if (t[0]<0) t[0] += 24;
    	if (t[0]>23) t[0] -= 24;
    	return t.join(':');
    };

    this.cl = function(s)
    {
    	s = s.toLowerCase(s);
    	for(var i in me.cd) s = s.replace(new RegExp(i,'g'), me.cd[i]);
    	return s+'0';
    };

    this.getXML();
};


