/* - - - - - - - - - - - - - - - - - - - - - - -
 michal@bajer.net.pl
 2009-09-28
 2009-11-02,03,04,11
 2010-02-26
 2010-06-01,19
 2010-07-05
 2011-02-10
 - - - - - - - - - - - - - - - - - - - - - - - */
var xmlhttpList;
var timeRefreshView=20*1000;  //60
var match=new Array();

function loadDataView(norefresh){
xmlhttpList=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttpList=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttpList=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttpList!=null)
  {
         xmlhttpList.onreadystatechange=stateChangeView;//alert('/panel/plugins/live/ajax/view.php'+'?sessid='+sessid+(id_live==null?'':'&id_live='+id_live)+cmsUrlDatetime());
         xmlhttpList.open("GET",'/panel/plugins/live/ajax/view.php'+'?sessid='+sessid+(id_live==null?'':'&id_live='+id_live)+cmsUrlDatetime(),true);
         xmlhttpList.send(null);
         
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
  if (norefresh==null){
  setTimeout('loadDataView()', timeRefreshView);
  }

}
function stateChangeView()
{
if (xmlhttpList.readyState==4)
  {// 4 = "loaded"
  if (xmlhttpList.status==200)
    {// 200 = "OK"
            if (xmlhttpList.responseText.length==0||xmlhttpList.responseText=='null'){
                return;
            }//alert(xmlhttpList.responseText);
            var l=unserialize(xmlhttpList.responseText);//alert('refresh');
                 id_live=l['id_live'];
                 if (_('match')==null)return;
                 o=_('match').innerHTML;
                 o=o.replace('%7B','{');o=o.replace('%7D','}');o=o.replace('%7B','{');o=o.replace('%7D','}');
                 o=o.replace('%7B','{');o=o.replace('%7D','}');o=o.replace('%7B','{');o=o.replace('%7D','}');
                 o=o.replace('%7B','{');o=o.replace('%7D','}');o=o.replace('%7B','{');o=o.replace('%7D','}');
                 //o=o.replace(/{id_live}/g,id_live);
                    for(k1 in l){ //alert(l[k1]);
                      o=o.replace('{'+k1+'}',l[k1]==null?'':l[k1]);
                    }
                    o=o.replace('{host_link}',l['host_link']);o=o.replace('{host_link}',l['host_link']);
                    o=o.replace('{guest_link}',l['guest_link']);o=o.replace('{guest_link}',l['guest_link']);
                    o=o.replace('{host_link}',l['host_link']);o=o.replace('{host_link}',l['host_link']);
                    o=o.replace('{guest_link}',l['guest_link']);o=o.replace('{guest_link}',l['guest_link']);
                    o=o.replace('{host_name}',l['host_name']);o=o.replace('{host_name}',l['host_name']);
                    o=o.replace('{guest_name}',l['guest_name']);o=o.replace('{guest_name}',l['guest_name']);
                    o=o.replace('{is_host_link}',l['is_host_link']);o=o.replace('{is_host_link}',l['is_host_link']);
                    o=o.replace('{\/is_host_link}',l['/is_host_link']);o=o.replace('{\/is_host_link}',l['/is_host_link']);
                    o=o.replace('{is_guest_link}',l['is_guest_link']);o=o.replace('{is_guest_link}',l['is_guest_link']);
                    o=o.replace('{\/is_guest_link}',l['/is_guest_link']);o=o.replace('{\/is_guest_link}',l['/is_guest_link']);
                    o=o.replace('{is_host_link}',l['is_host_link']);o=o.replace('{is_host_link}',l['is_host_link']);
                    o=o.replace('{\/is_host_link}',l['/is_host_link']);o=o.replace('{\/is_host_link}',l['/is_host_link']);
                    o=o.replace('{is_guest_link}',l['is_guest_link']);o=o.replace('{is_guest_link}',l['is_guest_link']);
                    o=o.replace('{\/is_guest_link}',l['/is_guest_link']);o=o.replace('{\/is_guest_link}',l['/is_guest_link']);
                    o=o.replace('{is_host_link}',l['is_host_link']);o=o.replace('{is_host_link}',l['is_host_link']);
                    o=o.replace('{\/is_host_link}',l['/is_host_link']);o=o.replace('{\/is_host_link}',l['/is_host_link']);
                    o=o.replace('{is_guest_link}',l['is_guest_link']);o=o.replace('{is_guest_link}',l['is_guest_link']);
                    o=o.replace('{\/is_guest_link}',l['/is_guest_link']);o=o.replace('{\/is_guest_link}',l['/is_guest_link']);
                    o=o.replace('{is_host_link}',l['is_host_link']);o=o.replace('{is_host_link}',l['is_host_link']);
                    o=o.replace('{\/is_host_link}',l['/is_host_link']);o=o.replace('{\/is_host_link}',l['/is_host_link']);
                    o=o.replace('{is_guest_link}',l['is_guest_link']);o=o.replace('{is_guest_link}',l['is_guest_link']);
                    o=o.replace('{\/is_guest_link}',l['/is_guest_link']);o=o.replace('{\/is_guest_link}',l['/is_guest_link']);
                    _('match').innerHTML=o;

                    if (l['icon']!=null){
                       i=l['icon'];
                       i=i.replace('ICON_IN_PROGRESS',ICON_IN_PROGRESS);
                       i=i.replace('ICON_CLOCK',ICON_CLOCK);
                       i=i.replace('ICON_START_1HALF_MATCH',ICON_START_1HALF_MATCH);
                       i=i.replace('ICON_END_1HALF_MATCH',ICON_END_1HALF_MATCH);
                       i=i.replace('ICON_START_2HALF_MATCH',ICON_START_2HALF_MATCH);
                       i=i.replace('ICON_END_MATCH',ICON_END_MATCH);
                       i=i.replace('ICON_END_2HALF_MATCH',ICON_END_2HALF_MATCH);
                       i=i.replace('ICON_START_1HALF_EXTRA',ICON_START_1HALF_EXTRA);
                       i=i.replace('ICON_END_1HALF_EXTRA',ICON_END_1HALF_EXTRA);
                       i=i.replace('ICON_START_2HALF_EXTRA',ICON_START_2HALF_EXTRA);
                       i=i.replace('ICON_END_2HALF_EXTRA',ICON_END_2HALF_EXTRA);
                       i=i.replace('ICON_GOAL_OWN',ICON_GOAL_OWN);
                       i=i.replace('ICON_GOAL_PENALTY',ICON_GOAL_PENALTY);
                       i=i.replace('ICON_GOAL',ICON_GOAL);
                       i=i.replace('ICON_YELLOW_CARD',ICON_YELLOW_CARD);
                       i=i.replace('ICON_RED_CARD',ICON_RED_CARD);
                       i=i.replace('ICON_YELLOW_CARD',ICON_YELLOW_CARD);
                       i=i.replace('ICON_YELLOW_RED_CARD',ICON_YELLOW_RED_CARD);
                       i=i.replace('ICON_ASSIST',ICON_ASSIST);
                       i=i.replace('ICON_SUBSTITUTION',ICON_SUBSTITUTION);
                       icon=i;
                    }else{
                       icon=ICON_IN_PROGRESS;
                    }
                 if (_('icon')!=null){
                     _('icon').innerHTML=icon;
                 }
                 if (_('event')!=null){
                     if (l['lastEvent']!=null){
			minute=l['lastEvent']['minute'];
                        _('event').innerHTML=(minute!=null?minute+'\' ':'')+(_('icon')==null?icon+' ':'')+l['lastEvent']['text'].replace(/\\/g,'');
                     }
                 }else{
                     if (l['lastEvent']!=null){
                     l['events'][-1]=l['lastEvent'];
                     }
                 }
                 if (_('host_goalscorers')!=null){
                     hg='';hgl=l['host_goalscorers'];
                     for(k2 in hgl){
                           o=LIVE_SZABLON_MECZ_STRZELEC;
                           o=o.replace('{\/is_link}',hgl[k2]['/is_link']);o=o.replace('{is_link}',hgl[k2]['is_link']);
                           m='';
                           for(k3 in hgl[k2]['minutes']){
                               m=m+hgl[k2]['minutes'][k3]+'\' ';
                               if (hgl[k2]['minutes_pen'][k3]!=null) m=m+'(K) ';
                           }
                           for(k3 in hgl[k2]['minutes_own']){
                               m=m+hgl[k2]['minutes_own'][k3]+'\'(S) ';
                           }
                           o=o.replace('{minutes}',m);
                           for(k21 in hgl[k2]){
                             o=o.replace('{'+k21+'}',hgl[k2][k21]);
                           }
                           hg=hg+o;
                     }
                    _('host_goalscorers').innerHTML=hg;
                 }
                 if (_('guest_goalscorers')!=null){
                     hg='';hgl=l['guest_goalscorers'];
                     for(k2 in hgl){
                           o=LIVE_SZABLON_MECZ_STRZELEC;
                           o=o.replace('{\/is_link}',hgl[k2]['/is_link']);o=o.replace('{is_link}',hgl[k2]['is_link']);
                           m='';
                           for(k3 in hgl[k2]['minutes']){
                               m=m+hgl[k2]['minutes'][k3]+'\' ';
                               if (hgl[k2]['minutes_pen'][k3]!=null) m=m+'(K) ';
                           }
                           for(k3 in hgl[k2]['minutes_own']){
                               m=m+hgl[k2]['minutes_own'][k3]+'\'(S) ';
                           }
                           o=o.replace('{minutes}',m);
                           for(k21 in hgl[k2]){
                             o=o.replace('{'+k21+'}',hgl[k2][k21]);
                           }
                           hg=hg+o;
                     }
                    _('guest_goalscorers').innerHTML=hg;
                 }//events
                 c=l['events_count'];_('events').innerHTML='';
                 for(ke in l['events']){
                      e=l['events'][ke];
                      id_event=e['id_event'];            //alert(l['lastEvent']['id_event']);
                      if (_('event'+id_event)==null){
                         o=c%2!=0?LIVE_SZABLON_MECZ_WYDARZENIE:LIVE_SZABLON_MECZ_WYDARZENIE1;
                         for(ke1 in e){
                           o=o.replace('{'+ke1+'}',e[ke1]==null?'':e[ke1]);
                         }
                         o=o.replace('{icon}',ICONS[e['id_icon']]);
                         o=o.replace(/\\/g,'');
                         _('events').innerHTML=o+_('events').innerHTML;
                         c++;
                      }

                 }
                 //alert(_('events').innerHTML);
                 _('result').innerHTML=l['result'];
                 //host first team
                 if (_('host_first_team')!=null){
                     for(pf in l['host_first_team']){
                        p=l['host_first_team'][pf];
                        id_player=p['id_player'];
                        if (_('player'+id_player+'')==null){
                           o=LIVE_SZABLON_MECZ_PILKARZ;
                           o=o.replace('{id_player}',id_player);o=o.replace('{id_player}',id_player);
                           o=o.replace('{\/is_link}',p['/is_link']);o=o.replace('{is_link}',p['is_link']);
                           for(pf1 in p){
                             o=o.replace('{'+pf1+'}',p[pf1]);
                           }
                           _('host_first_team').innerHTML=_('host_first_team').innerHTML+o;
                        }
                        //events
                        for(pe in p['events']){
                             e=p['events'][pe];
                             id_event=e['id_event'];
                             if (_('player_event'+id_event+'')==null){
                                o=LIVE_SZABLON_MECZ_PILKARZ_WYDARZENIE;
                                o=o.replace('{id_event}',id_event);
                                for(pe1 in e){
                                  o=o.replace('{'+pe1+'}',e[pe1]);
                                }
                                _('player_events'+id_player).innerHTML=_('player_events'+id_player).innerHTML+o;
                             }
                               
                        }
                     }
   
                 }
                 //guest first team
                 if (_('guest_first_team')!=null){
                     for(pf in l['guest_first_team']){
                        p=l['guest_first_team'][pf];
                        id_player=p['id_player'];
                        if (_('player'+id_player+'')==null){
                           o=LIVE_SZABLON_MECZ_PILKARZ;
                           o=o.replace('{id_player}',id_player);o=o.replace('{id_player}',id_player);
                           o=o.replace('{\/is_link}',p['/is_link']);o=o.replace('{is_link}',p['is_link']);
                           for(pf1 in p){
                             o=o.replace('{'+pf1+'}',p[pf1]);
                           }
                           _('guest_first_team').innerHTML=_('guest_first_team').innerHTML+o;
                        }
                        //events
                        for(pe in p['events']){
                             e=p['events'][pe];
                             id_event=e['id_event'];
                             if (_('player_event'+id_event+'')==null){
                                o=LIVE_SZABLON_MECZ_PILKARZ_WYDARZENIE;
                                o=o.replace('{id_event}',id_event);
                                for(pe1 in e){
                                  o=o.replace('{'+pe1+'}',e[pe1]);
                                }
                                _('player_events'+id_player).innerHTML=_('player_events'+id_player).innerHTML+o;
                             }

                        }
                     }

                 }
                 //host backup
                 if (_('host_backup')!=null){
                     for(pf in l['host_backup']){
                        p=l['host_backup'][pf];
                        id_player=p['id_player'];
                        if (_('player'+id_player+'')==null){
                           o=LIVE_SZABLON_MECZ_PILKARZ_REZERWA;
                           o=o.replace('{id_player}',id_player);o=o.replace('{id_player}',id_player);
                           o=o.replace('{\/is_link}',p['/is_link']);o=o.replace('{is_link}',p['is_link']);
                           for(pf1 in p){
                             o=o.replace('{'+pf1+'}',p[pf1]);
                           }
                           _('host_backup').innerHTML=_('host_backup').innerHTML+o;
                        }
                        //events
                        for(pe in p['events']){
                             e=p['events'][pe];
                             id_event=e['id_event'];
                             if (_('player_event'+id_event+'')==null){
                                o=LIVE_SZABLON_MECZ_PILKARZ_WYDARZENIE;
                                o=o.replace('{id_event}',id_event);
                                for(pe1 in e){
                                  o=o.replace('{'+pe1+'}',e[pe1]);
                                }
                                _('player_events'+id_player).innerHTML=_('player_events'+id_player).innerHTML+o;
                             }
                        }
                     }
                 }
                 //guest backup
                 if (_('guest_backup')!=null){
                     for(pf in l['guest_backup']){
                        p=l['guest_backup'][pf];
                        id_player=p['id_player'];
                        if (_('player'+id_player+'')==null){
                           o=LIVE_SZABLON_MECZ_PILKARZ_REZERWA;
                           o=o.replace('{id_player}',id_player);o=o.replace('{id_player}',id_player);
                           o=o.replace('{\/is_link}',p['/is_link']);o=o.replace('{is_link}',p['is_link']);
                           for(pf1 in p){
                             o=o.replace('{'+pf1+'}',p[pf1]);
                           }
                           _('guest_backup').innerHTML=_('guest_backup').innerHTML+o;
                        }
                        //events
                        for(pe in p['events']){
                             e=p['events'][pe];
                             id_event=e['id_event'];
                             if (_('player_event'+id_event+'')==null){
                                o=LIVE_SZABLON_MECZ_PILKARZ_WYDARZENIE;
                                o=o.replace('{id_event}',id_event);
                                for(pe1 in e){
                                  o=o.replace('{'+pe1+'}',e[pe1]);
                                }
                                _('player_events'+id_player).innerHTML=_('player_events'+id_player).innerHTML+o;
                             }
                        }
                     }

                 }//alert(l['status']);
                    if (l['status']!=null){
                        if(_('status')!=null){
                            _('status').innerHTML=l['status'];
                        }
                    }
                 if (_('minute')!=null){
                    if (l['minute']!=null){
                        if (l['minute']!=''){
                        _('minute').innerHTML=l['minute']+'\'';
                        }
                    }
                 }
                 match=l;
                 _('match').style.visibility='visible';
                 document.title=l['title']+' '+(l['subtitle']==null?'':l['subtitle']);

            if(typeof(stateChangeViewExternal) != 'undefined'){
                 if (stateChangeViewExternal){
                     stateChangeViewExternal();
                 }
            }
    }
  else
    {
    alert("Problem retrieving data:" + xmlhttpList.statusText);
    }
  }
}
