var typowanie=null;
var experci_url = '';

function score_explode(score,key)
{
    if(!score)
        return "-";

    var a = explode("-",score);
    if(!a[key])
        return '-';

    return a[key];

}

function Typuj()
{
    this.typy = new Array();
    
    this.addTyp = function(t)
    {
        this.typy[this.typy.length] = t;
    }

    this.removeTyp = function(game_id)
    {
         o = this.searchType(game_id);
         if(o)
             o = null;
    }

    this.submit = function(sport_id)
    {
        window.onbeforeunload = null;

        //window.onbeforeunload = onBeforeUnloadAction;

        var input_hidden = $('input_hidden');
       
        input_hidden.innerHTML = "";

        for(i=0;i<this.typy.length;i++)
        {
            
            if(sport_id==1 && this.typy[i].type_analiza && (!this.typy[i].game.score_0 || !this.typy[i].game.score_1))
            {
                alert("Podaj wyniki dla spotkań, dla których napisałeś analizę ");
                return;
            }

            if(this.typy[i].type_analiza && !this.typy[i].type_value_id)
            {
                alert("Podaj typy dla dla spotkań, dla których napisałeś analizę ");
                return;
            }

            input = document.createElement("input");
            input.type = "hidden";
            input.name='analiza['+this.typy[i].game.game_id+']';
            input.value = this.typy[i].type_analiza;
            input_hidden.appendChild(input);
            
            input = document.createElement("input");
            input.type = "hidden";
            input.name='wynik['+this.typy[i].game.game_id+'][0]';
            input.value = this.typy[i].game.score_0;
            input_hidden.appendChild(input);

            input = document.createElement("input");
            input.type = "hidden";
            input.name='wynik['+this.typy[i].game.game_id+'][1]';
            input.value = this.typy[i].game.score_1;
            input_hidden.appendChild(input);

            input = document.createElement("input");
            input.type = "hidden";
            input.name='typ['+this.typy[i].game.game_id+']';
            input.value = this.typy[i].type_value_id;
            input_hidden.appendChild(input);
        }

        $('typuj').submit();
    }

    this.searchType = function(game_id)
    {
        for(ind=0;ind<this.typy.length;ind++)
            if(this.typy[ind] && this.typy[ind].game.game_id==game_id)
                return this.typy[ind];

         return false;
    }

    this.typuj = function(element,game_id,value_id,sport_id)
    {
        if(game_id)
            obj = this.searchType(game_id);
        else
            return;

        if(!obj)
        {
            t = new Typy();
            g = new Game();
            g.game_id = game_id;
            t.setGame(g);
            t.type_value_id = value_id;

            this.addTyp(t);
            if(sport_id!=1)
            {
                input = element.parentNode.parentNode.parentNode.getElementsByTagName("input");
                    for(i=0;i<input.length;i++)
                        if(!input[i].checked)
                            input[i].disabled = true;
            }


        }else{

            //sprawdzam czy typowano juz inny rodzaj
            if(element.checked && obj.type_value_id && obj.type_value_id!=value_id && obj.type_value_id < 4)
            {
                switch(obj.type_value_id)
                {
                    case 1:
                        if(value_id==2)
                            obj.type_value_id = 4;
                        else
                            obj.type_value_id = 6;
                    break;
                    case 2:
                        if(value_id==1)
                            obj.type_value_id = 4;
                        else
                            obj.type_value_id = 5;
                    break;
                    case 3:
                        if(value_id==1)
                            obj.type_value_id = 6;
                        else
                            obj.type_value_id = 5;
                    break;
                    default: throw "Nieoznaczony blad";
                }

                    input = element.parentNode.parentNode.parentNode.getElementsByTagName("input");
                    for(i=0;i<input.length;i++)
                        if(!input[i].checked)
                            input[i].disabled = true;
                

            }else if(!element.checked && obj.type_value_id==value_id && obj.type_value_id < 4){
                obj.type_value_id = null;
                if(sport_id!=1)
                {
                    input = element.parentNode.parentNode.parentNode.getElementsByTagName("input");
                        for(i=0;i<input.length;i++)
                            if(!input[i].checked)
                                input[i].disabled = false;
                }
                return false;
            //jesli klikam w typ z podporka
            }else if(obj.type_value_id > 3){
                switch(obj.type_value_id)
                {
                    case 4:
                        if(value_id==1)
                            obj.type_value_id = 2;
                        else
                            obj.type_value_id = 1;
                    break;
                    case 5:
                        if(value_id==2)
                            obj.type_value_id = 3;
                        else
                            obj.type_value_id = 2;
                    break;
                    case 6:
                        if(value_id==1)
                            obj.type_value_id = 3;
                        else
                            obj.type_value_id = 1;
                    break;
                    default: throw "Nieoznaczony blad";
                }
                
                //odblokuj typy
                input = element.parentNode.parentNode.parentNode.getElementsByTagName("input");
                for(i=0;i<input.length;i++)
                    if(!input[i].checked)
                        input[i].disabled = false;
            }else{
                obj.type_value_id = value_id;
                 if(sport_id!=1)
                {
                    input = element.parentNode.parentNode.parentNode.getElementsByTagName("input");
                        for(i=0;i<input.length;i++)
                            if(!input[i].checked)
                                input[i].disabled = true;
                }
            }

        }
    
       return true;
    }

    this.setAnaliza = function(game_id,editor_obj)
    {
        obj = this.searchType(game_id);

        if(!obj){
            t = new Typy();
            g = new Game();
            g.game_id = game_id;
            t.setGame(g);
            obj.type_analiza = editor_obj.getContent();

            this.addTyp(t);
        }else{
            obj.type_analiza = editor_obj.getContent();
        }
    }

    this.setAnalizyCounter=function(o,game_id)
    {
        obj = this.searchType(game_id);

        if(!obj)
            return false;

        o.innerHTML=obj.type_analiza.length;
    }

    this.sprawdz_wynik_typ = function(game_id,element)
    {
        obj = this.searchType(game_id);
        //sprawdz czy wynik zgadza sie z typem
        if(obj.game.score_0 > obj.game.score_1){
            type_accept = new Array(1,4,6);
        }else if(obj.game.score_0==obj.game.score_1){
            type_accept = new Array(2,4,5);
        }else{
            type_accept = new Array(3,5,6);
        }

        index=0;
        for(i=0;i<type_accept.length;i++){

            if(type_accept[i]==obj.type_value_id)
            {
                index++;
            }
        }

        if(index==0){
            select = element.parentNode.getElementsByTagName("select");
            for(k=0;k<select.length;k++)
                select[k].selectedIndex = 0;

            obj.game.score_0 = null;
            obj.game.score_1 = null;
            alert("Wynik powinien zgadzać się z typem");
        }
    }

    this.ustaw_wynik = function(type,game_id,element,value)
    {
        obj = this.searchType(game_id);
        if(!obj){
            var t = new Typy();
            var game = new Game();
            game.game_id = game_id;
            t.setGame(game);
            this.addTyp(t);

        }

        if(!obj.type_value_id){
            element.selectedIndex = 0;
            return alert("Przed wyborem wyniku zaznacz typ spotkania");
        }
        
        obj = this.searchType(game_id);
        if(type==0){
            obj.game.score_0 = value;
            if(obj.game.score_1){
                 this.sprawdz_wynik_typ(game_id,element);
            }
        }else if(!obj.game.score_0){
            alert("Ustaw najpierw wartość dla gospodarza");
            element.selectedIndex=0;
        }else{
            obj.game.score_1 = value;
            this.sprawdz_wynik_typ(game_id,element);
        }
  
        
    }

}

function Set()
{
    this.set_name = null;
    this.set_score = null;
}

function Team()
{
    this.team_name=null;
    this.team_id=null;
    this.team_url=null;
    this.farmer = null;
    this.lineuplist_exists = false;
}

function Scorer()
{
    this.lastName=null;
    this.firstName=null;
    this.min=null;
    this.type = null;
    this.team_id = null;
    this.player_id = null;
    this.goals_normal = null;
}

function Card()
{
    this.firstName = null;
    this.lastName = null;
    this.min = null;
    this.type = null;
    this.team_id = null;
    this.player_id = null;
}

function Substitution()
{
    this.player_in = null;
    this.player_out = null;
    this.player_in_name = null;
    this.player_out_name = null;
    this.min = null;
}
var watched_games = new Array();
function Game()
{
    this.teams = new Array();
    this.game_date = null;
    this.game_score = null;
    this.game_score_o = null;
    this.game_score_g = null;
    this.game_score_p = null;
    this.game_score_h = null;
    this.game_status = null;
    this.status_type = null;
    this.scorer = new Array();
    this.stadion = null;
    this.sedzia = null;
    this.sedzia_country = null;
    this.widzow = null;
    this.livegol_game_id=null;
    this.game_min = 0;
    this.sport_id = null;
    this.sets = new Array();
    this.user_typed_typ = null;
    this.user_typed_wynik = null;
    this.game_id = null;
    this.score_0=null;
    this.score_1 = null;
    this.cards = new Array();
    this.min = null;
    this.serv_time = null;
    this.actual_set = null;
    this.status_name = null;
    this.type_1 = null;
    this.type_x = null;
    this.type_2 = null;
    this.ilosc_typow = null;
    this.ilosc_analiz = null;
    this.substitution = new Array();
    this.domGamesElements = new Array();
    this.minutesCountdown = new Array();
  
    this.info = false;

    this.checkMinutesCountdown = function(game_id)
    {
        for(k=0;k<this.minutesCountdown.length;k++)
            if(this.minutesCountdown[k]['game_id']==game_id)
                return true;
            else
                return false;
    }

    this.raiseEvent = function(name) {
        var args = [];
        for (var i = 1; i < arguments.length; i++)
            args.push(arguments[i]);
        if (typeof this[name] == "function")
            this[name].apply(null, args);
        else if(typeof name == "function")
            name.apply(null,args);
    }


    this.set_watch_game = function(game)
    {
        $('obserwowane').style.display="block";

          jar = new CookieJar({
              expires:3600*24*2, // seconds
              path: '/'
          });
          games = new Array();
          games_cookie = jar.get('livegol_watched_games');
          games = eval('(' + games_cookie + ')');
          if(games && games instanceof Array)
          {
              for(i=0;i<games.length;i++)
              {
                  if(games[i]==game.game_id)
                      return alert("Wybrany mecz jest już dodany do obserwowanych.");
              }

          }else{
              games = new Array();
          }
    
          games[games.length] = game.game_id;
          jar.put('livegol_watched_games', games.toJSON());

           html = '';
           html +='<div class="game_info" style="display:none;" id="observer_'+game.game_id+'">';
           if(game.teams.length ==2 && (game.scorer.length >0 || game.cards.length > 0))
           {
               html +='     <div class="t"> <img src="'+site_url+'/images/livegol/info_a.gif" class="tl" alt="" />';
               html +='      <img src="'+site_url+'/images/livegol/info_b.gif" class="tw" alt="" />';
               html +='       <img src="'+site_url+'/images/livegol/info_d.gif" class="tr" alt="" />';
               html +='  </div>';
               html +=' <div class="inner">';
               html += '       <div class="kl">';
               html +='         <strong>'+game.teams[0].team_name+'</strong>';
               html +='          <div style="height:10px;line-height:10px;"></div>';

               infos = new Array();
               infos[0] = new Array();
               infos[1] = new Array();

               for(z=0;z<game.scorer.length;z++)
               {
                    if(game.scorer[z].team_id == game.teams[0].team_id){
                        infos[0][infos[0].length] = game.scorer[z];
                    }else{
                        infos[1][infos[1].length] = game.scorer[z];
                    }
               }

               for(z=0;z<game.cards.length;z++)
               {
                    if(game.cards[z].team_id == game.teams[0].team_id){
                        infos[0][infos[0].length] = game.cards[z];
                    }else{
                        infos[1][infos[1].length] = game.cards[z];
                    }
               }
           
               if(infos.length > 0)
                   infos[0].sort(function(a,b){return a['min']-b['min']});
               if(infos.length > 1)
                    infos[1].sort(function(a,b){return a['min']-b['min']});
               
               for(z=0;z<infos[0].length;z++)
               {
                   html +='<div class="inf">';
                   if(infos[0][z] instanceof Card)
                       html +='<div></div><img src="'+site_url+'/images/livegol/cardd_'+infos[0][z].type+'.gif" alt="" />'+infos[0][z].min+'\''+infos[0][z].firstName+" "+infos[0][z].lastName;
                   else if(infos[0][z] instanceof Scorer)
                       html +='<div></div><img src="http://www.bukmacherzy.com/new_lay/images/pilka.gif" alt="" />'+infos[0][z].min+'\''+infos[0][z].firstName+" "+infos[0][z].lastName;

                   html +='</div>';
               }

               html +='</div><div style="width:100%;height:1px;line-height:1px;background:#404040;"></div>';
               html +=' <div class="kl" style="background:#1a1a1a;">';
               html +=' <strong>'+game.teams[1].team_name+'</strong>';
               html +='<div style="height:10px;line-height:10px;"></div>';
               for(z=0;z<infos[1].length;z++)
               {
                   html +='<div class="inf">';
                    if(infos[1][z] instanceof Card)
                       html +='<div></div><img src="'+site_url+'/images/livegol/cardd_'+infos[1][z].type+'.gif" alt="" />'+infos[1][z].min+'\''+infos[1][z].firstName+" "+infos[1][z].lastName;
                   else if(infos[1][z] instanceof Scorer)
                       html +='<div></div><img src="http://www.bukmacherzy.com/new_lay/images/pilka.gif" alt="" />'+infos[1][z].min+'\''+infos[1][z].firstName+" "+infos[1][z].lastName;
                   html +='</div>';
               }
               html +='</div></div><div class="b">';
               html += ' <img src="'+site_url+'/images/livegol/info_e.gif" class="tl" alt="" />';
               html += ' <img src="'+site_url+'/images/livegol/info_g.gif" class="tr" alt="" /></div>';
           }

           html+='</div><div id="ob_'+game.game_id+'" class="watch_item">';
           html+='<div class="watch_top">';
           if(game.sport_id==1 && game.status_type==3){
              html+='<img class="ea" src="http://www.bukmacherzy.com/new_lay/images/status_'+game.status_type+'_left_menu.gif" alt="" />';
              html+='<div class="eb">'+game.game_min+'\'</div>';
           }else if(game.status_type==3){

              if(game.sport_id==2 || game.sport_id==5){
                  html+='<img class="ea" src="'+site_url+'/images/livegol/'+game.actual_set+'_set.gif" alt="" />';
                  html+='<div class="eb">&nbsp;</div>';
              }else{ 
                  html+='<img class="ea" src="'+site_url+'/images/status/status_'+game.game_status+'_left_menu.gif" alt="" />';
                  html+='<div class="eb">&nbsp;</div>';
              }
           }else{
              html+='<img class="ea" src="http://www.bukmacherzy.com/new_lay/images/status_'+game.status_type+"_"+lang_code+'.gif" alt="" />';
              html+='<div class="eb">&nbsp;</div>';
           }

           if(game.scorer.length >0 || game.cards.length > 0)
                html+='<img class="ec" src="'+site_url+'/images/livegol/info.gif" onmouseout="show_observe_info('+game.game_id+')" onmouseover="show_observe_info('+game.game_id+')" alt="" />';
           else
                html+='<img class="ec" src="'+site_url+'/images/livegol/info2.gif" alt="Brak informacji o spotkaniu" style="pointer:none;" />';

           html+='<img class="ed" src="'+site_url+'/images/livegol/close_red.gif" onclick="close_observe(this)" alt="" />';
           html+='</div>';

           if(game.teams.length > 2)
               html+='<div class="tt">'+game.teams[0].team_name+', '+game.teams[1].team_name+'</div>';
           else
               html+='<div class="tt">'+game.teams[0].team_name+'</div>';

           wynik_seta = "";
           if((game.sport_id==2 || game.sport_id==5) && game.actual_set && game.sets.length >= game.actual_set)
                wynik_seta = "<span style='font-weight:normal'>(Set "+game.actual_set+": "+game.sets[game.actual_set-1].set_score+')</span>';
           
           html += '  <div class="score">'+game.game_score+wynik_seta+'</div>';

           if(game.teams.length > 2)
               html+='  <div class="ta">'+game.teams[2].team_name+', '+game.teams[3].team_name+'</div>';
           else
               html+='  <div class="ta">'+game.teams[1].team_name+'</div>';

           html+='</div>';

           obserwowane_item = document.getElementById("obserwowane_items");
           obserwowane_item.innerHTML += html;
           document.getElementById("obserwowane").style.display="block";


    }

    this.watch_game = function(game_id)
    {

        for(i=0;i<watched_games.length;i++){
            if(watched_games[i]==game_id)
                return alert("Wybrany mecz jest już dodany do obserwowanych");
        }

        this.getGameInfo(null,game_id,false,"set_watch_game");


    }

    this.addMinutesCountdown = function(game_id,game_date)
    {
        index = this.minutesCountdown.length;
        //document.getElementById(game_id).getElementsByTagName("span").item(0).innerHTML = "";
        this.minutesCountdown[index] = new Array();
        this.minutesCountdown[index]['game_id'] = game_id;
        this.minutesCountdown[index]['obj'] = document.getElementById(game_id).getElementsByTagName("span").item(0);
        this.minutesCountdown[index]['date'] = parseInt(game_date);
    }

    this.addTeam = function(t)
    {
        this.teams[this.teams.length] = t;
    }

    this.addScorer = function(s)
    {
        if(s)
            this.scorer[this.scorer.length] = s;
    }

    this.addCard = function(c)
    {
        this.cards[this.cards.length] = c;
    }

    this.calcTime = function(key)
    {
        obj = this.minutesCountdown[key]['obj'];

        if(obj)
        {
            var d = parseInt(this.minutesCountdown[key]['date']);
 
            roznica = this.serv_time-d;
            min = parseInt(roznica /60);
            sec = parseInt(roznica % 60);

            if(min<10)
                min = "0"+min;
                
            if(sec<10)
                sec = "0"+sec;
            
            if(roznica > 2700 && roznica < 3700)
            {
                obj.innerHTML = "45+";
                return 1;
            }

            if(roznica > 3700)
                min-=16;
            
            if(min>=90){
                obj.innerHTML = "90+";
            }else{
                obj.innerHTML = min+"' "+sec+"s.";
            }
        }
    }

    this.minutCountDown = function()
    {
        for(i=0;i<this.minutesCountdown.length;i++)
        {
            if(this.minutesCountdown[i])
            {
                this.calcTime(i);
            }
        }
 
        this.serv_time++;
        thisObjMinutCountDown = this;
        setTimeout(function(){thisObjMinutCountDown.minutCountDown()},1000);
    }
    
    this.addSet = function(s)
    {
        if(s)
            this.sets[this.sets.length] = s;
    }
    
    this.setGameInfo = function()
    {

        if(this.scorer.length==0 && this.cards.length==0)
        {
            alert("W bazie nie ma strzelców bramek, ani przyznanych kartek");
            return new Array();
        }

        teams_score = new Array(0,0);
        tab = new Array();

        to_show = new Array();
        for(i=0;i<this.scorer.length;i++)
        {
            to_show[to_show.length] = this.scorer[i];
        }

        for(i=0;i<this.cards.length;i++)
        {
            to_show[to_show.length] = this.cards[i];
        }

        if(this.sport_id==4){
            for(j = to_show.length - 1; j > 0; j--)
            {
                p = 1;
                for(i = 0; i < j; i++)
                  if(to_show[i].goals_normal < to_show[i + 1].goals_normal)
                  {
                    x = to_show[i]; to_show[i] = to_show[i + 1]; to_show[i + 1] = x;
                    p = 0;
                  }
                if(p) break;
            }

        }else{
            for(j = 0; j < to_show.length-1; j++)
                for(i = 0; i < to_show.length -1; i++)
                    if(to_show[i].min > to_show[i + 1].min)
                    {
                        x = to_show[i];
                        to_show[i] = to_show[i + 1];
                        to_show[i + 1] = x;
                    }
        }
        div = document.createElement("div");
        for(i=0;i<to_show.length;i++)
        {
            if(this.sport_id!=4){
                div = document.createElement("div");
            }
            
            div.className = "info_row";

            div2 = document.createElement("div");
            div3 = document.createElement("div");
            div4 = document.createElement("div");

            div2.className="t1";
            div3.className="t2";
            div4.className="t3";
            if(this.sport_id==4){div2.className+="a";div3.className+="a";div4.className+="a";}
            img = document.createElement("img");
            farmer = null;
            for(a=0;a<this.teams.length;a++)
            {
                if(this.teams[a].team_id == to_show[i].team_id)
                {
                    farmer = this.teams[a].farmer;
                }
            }

            if(farmer==1){
                if(to_show[i] instanceof Scorer){

                 
                    img.alt="";
                    img.src = "http://www.livegol.pl/images/livegol/pilka_"+to_show[i].type+".gif";

                    show_score = false;
                    if(to_show[i].type=="n" || to_show[i].type=="p" ||to_show[i].type=="g" || to_show[i].type=="o" 
                            || to_show[i].type=="eto" || to_show[i].type=="etg"
                            || to_show[i].type=="eps" || to_show[i].type=="sg"
                            || to_show[i].type=="sgo" || to_show[i].type=="sgp"){
                        show_score=true;
                        teams_score[0]++;
                    }
                    if(this.sport_id==4){
                        div2.innerHTML+=to_show[i].firstName+" "+to_show[i].lastName+" "+to_show[i].goals_normal;
                    }else{
                        div2.appendChild(img);
                        div2.innerHTML+=to_show[i].firstName+" "+to_show[i].lastName+" "+to_show[i].min+"'";
                        if(show_score)
                            div3.innerHTML = teams_score[0]+"-"+teams_score[1];
                        div4.innerHTML = "&nbsp;";
                    }
                    
                    
                    
                }else if(to_show[i] instanceof Card){
                    img.src = "http://www.bukmacherzy.com/new_lay/images/card_"+to_show[i].type+".gif";
                    img.className="ca";
                    div2.appendChild(img);
                    div2.innerHTML+=to_show[i].firstName+" "+to_show[i].lastName+" "+to_show[i].min+"'";
                    div3.innerHTML = "&nbsp;";
                    div4.innerHTML = "&nbsp;";
                }
            }else if(farmer==0){
                if(to_show[i] instanceof Scorer){

                    img.alt="";
                    img.src = "http://www.livegol.pl/images/livegol/pilka_"+to_show[i].type+".gif";

                    show_score = false;
                    if(to_show[i].type=="n" || to_show[i].type=="p" ||to_show[i].type=="g" || to_show[i].type=="o"
                        || to_show[i].type=="eto" || to_show[i].type=="etg"
                        || to_show[i].type=="eps" || to_show[i].type=="sg"
                        || to_show[i].type=="sgo" || to_show[i].type=="sgp"){
                        show_score = true;
                        teams_score[1]++;
                    }
                 
                    if(this.sport_id==4){
                        div4.innerHTML+=to_show[i].firstName+" "+to_show[i].lastName+" "+to_show[i].goals_normal;
                    }else{
                        div4.innerHTML=to_show[i].firstName+" "+to_show[i].lastName+" "+to_show[i].min+"'";
                        if(show_score)
                            div3.innerHTML = teams_score[0]+"-"+teams_score[1];
                        div4.appendChild(img);
                        div2.innerHTML = "&nbsp;";
                    }
                                                    
                    
                    
                }else if(to_show[i] instanceof Card){
                    div4.innerHTML=to_show[i].firstName+" "+to_show[i].lastName+" "+to_show[i].min+"'";
                    img.src = "http://www.bukmacherzy.com/new_lay/images/card_"+to_show[i].type+".gif";
                    img.className="ca";
                    div3.innerHTML = "&nbsp;";
                    div4.appendChild(img);
                    div2.innerHTML = "&nbsp;";
                }
            }
            div.appendChild(div2);
            div.appendChild(div3);
            div.appendChild(div4);

            if(this.sport_id!=4){
                
                tab[tab.length] = div;
            }
        }
      
        if(this.sport_id==4){
            tab[tab.length] = div;
        }
        return tab;
    }

    this.tmp=true;

    this.downGameInfo = function(obj,obj_el,game_id)
    {
        if(obj.src=="http://www.bukmacherzy.com/new_lay/images/wi2.gif"){
            obj.src = "http://www.bukmacherzy.com/new_lay/images/wi.gif";
            a = obj_el.getElementsByTagName("a");
            i = obj_el.getElementsByTagName("img");
            divr = obj_el.parentNode.getElementsByTagName("div");
            
            for(j=0;j<divr.length;j++){
                if(divr.item(j).className=="bonusy_item"){
                   divr[divr.length-1].parentNode.removeChild(divr[divr.length-1]);
                }
            }
            if(a.length>0 && i.length > 0)
            {
                a_obj = a.item(0).cloneNode(true);
                i_obj = i.item(0).cloneNode(true);
                obj_el.innerHTML = "";
                obj_el.appendChild(a_obj);
                obj_el.appendChild(i_obj);
            }
           
        }else{
            obj.src = "http://www.bukmacherzy.com/new_lay/images/wi2.gif";
            this.getGameInfo(obj_el,game_id,true);

        }
        
    }

    this.livescoreSet = function(game_id,game)
    {
        if(!this.domGamesElements[game_id])
            this.domGamesElements[game_id] = document.getElementById(game_id);


        if(this.domGamesElements[game_id])
        {
            switch(parseInt(game.sport_id))
            {
                case 1:
                    div = this.domGamesElements[game_id].getElementsByTagName("div");
                    var a_nodes = this.domGamesElements[game_id].getElementsByTagName("a");
                    if(game.status_type>1)
                        this.domGamesElements[game_id].className = "g"+game.status_type;
                    else
                        this.domGamesElements[game_id].className = "g";

                    //status
                    imgs = div.item(0).getElementsByTagName("img");
                    img = imgs.item(0);

                    img.src = "http://www.bukmacherzy.com/new_lay/images/status_"+game.status_type+"_"+lang_code+".gif";
                    span = div.item(0).getElementsByTagName("span");
     
                    if(game.status_type==3){
                        img.className="l";
                 
                        if(span.length==0){
                            img2= img.cloneNode(true);
                            div.item(0).innerHTML = "";
                            div.item(0).appendChild(img2);
                            span = document.createElement("span");
                            span.innerHTML = g.status_name+" &nbsp;"+g.min+"'";
                            div.item(0).appendChild(span);
                        }else{
                            span.item(0).innerHTML = g.status_name+" &nbsp;"+g.min+"'";
                        }
                       

                    }else{
                        img2 = img.cloneNode(true);
                        div.item(0).innerHTML = "";
                        div.item(0).appendChild(img2);
                        d = new Date(parseInt(game.game_date)*1000);
                        div.item(0).innerHTML += d.format("d-m H:i");
                    }

                    //score
                    if(a_nodes.length>0)
                    {
                        strong = a_nodes.item(0).getElementsByTagName("strong");
                        if(strong.length > 0){
                            if(game.game_score_g && game.game_score_g!=" - ")
                                strong.item(0).innerHTML = game.game_score_g;
                            else if(game.game_score_o && game.game_score_o!=" - ")
                                strong.item(0).innerHTML = game.game_score_o;
                            else
                                strong.item(0).innerHTML = game.game_score;
                        }

                        if(game.game_score_p && game.game_score_p!=" - ")
                        {
                            elem = strong.item(0).parentNode.getElementsByTagName("b").item(1);
                            karne = elem.getElementsByTagName("span");
                            if(karne.length > 0)
                                karne.item(0).innerHTML = "(karne: "+game.game_score_p+")";
                            else
                                elem.innerHTML+="<span class='karne'>(karne: "+game.game_score_p+")</span>";

                        }
                    }
                    
                break;
                case 2:
                     div = this.domGamesElements[game_id].getElementsByTagName("div");
                     
                     div[2].innerHTML = this.explode(game.game_score,0)+"<br />"+this.explode(game.game_score,1);

                     //wpisz sety
                     for(m=0;m<g.sets.length;m++)
                     {

                         ex = this.explode(g.sets[m].set_score,0);
                         ex2 = this.explode(g.sets[m].set_score,1);
                         if(ex)
                            str = ex+"<br />"+ex2;
                         else
                             str = "-<br />-";
                         
                         div[3+m].innerHTML = str;
                     }

                break;
                default:

                    div = this.domGamesElements[game_id].getElementsByTagName("div");
                    var a_nodes = this.domGamesElements[game_id].getElementsByTagName("a");
                    if(game.status_type>1)
                        this.domGamesElements[game_id].className = "g"+game.status_type;
                    else
                        this.domGamesElements[game_id].className = "g";

                    //status
                    imgs = div.item(0).getElementsByTagName("img");


                    //score
                    if(a_nodes.length>0)
                    {
                        strong = a_nodes.item(0).getElementsByTagName("strong");
                        if(strong.length > 0){
                            if(game.game_score_g && game.game_score_g!=" - ")
                                strong.item(0).innerHTML = game.game_score_g;
                            else if(game.game_score_o && game.game_score_o!=" - ")
                                strong.item(0).innerHTML = game.game_score_o;
                            else
                                strong.item(0).innerHTML = game.game_score;
                        }

                        if(game.game_score_p && game.game_score_p!=" - ")
                        {
                            elem = strong.item(0).parentNode.getElementsByTagName("b").item(1);
                            karne = elem.getElementsByTagName("span");
                            if(karne.length > 0)
                                karne.item(0).innerHTML = "(karne: "+game.game_score_p+")";
                            else
                                elem.innerHTML+="<span class='karne'>(karne: "+game.game_score_p+")</span>";

                        }
                    }
            }
        }

    }

    this.explode = function(s,e)
    {
        a = s.split("-");
        return a[e];
    }

    this.getGamesForSport = function(sport_id)
    {

       var date = new Date();
       var thisObjGameForCat = this;
    //"+((date.getTime()/1000)-120)+"
    //cat_id=26;
   
       advAJAX.get({
             url: "/xml/wyniki_xml/?date=1&sport_id="+sport_id,
             timeout : 6000,
             onTimeout : function() {thisObjGameForCat.getGamesForSport(sport_id) },
             retry: 1,
             retryDelay: 5000,
             onRetry : function() {},
             onRetryDelay : function() {},
             onFinalization: function(){ document.getElementById("loading").style.display="none"; },
             onInitialization: function(){document.getElementById("loading").style.display="block";},
             onSuccess : function(obj){
             
                //scorer
                var xml = obj.responseXML;
             
                var games = xml.getElementsByTagName("wyniki");
                for(i=0;i<games.length;i++)
                {
                    items = games[i].getElementsByTagName("i");
                    for(k=0;k<items.length;k++)
                    {
                        if(items[k].hasAttribute("game_id"))
                        {
                            g = new Game();
                            g.game_id = items[k].getAttribute("game_id");
                            g.min = items[k].getAttribute("game_min");
                            g.game_score = items[k].getAttribute("game_score");
                            g.status_type = items[k].getAttribute("status_type");
                            g.game_status = items[k].getAttribute("status_id");
                            g.sport_id = parseInt(items[k].getAttribute("sport_id"));
                            g.game_date = items[k].getAttribute("game_date");
                            g.game_score_g = items[k].getAttribute("game_score_sg");
                            g.game_score_o = items[k].getAttribute("game_score_o");
                            g.game_score_p = items[k].getAttribute("game_score_sp");
                            g.info = items[k].getAttribute("info");
                            g.status_name = items[k].getAttribute("status_name");
                            
                            if(g.sport_id==2)
                            {
                                //pobierz sety
                                sets = items[k].getElementsByTagName("sets");
                                if(sets.length>0)
                                {
                                    setsi = sets.item(0).getElementsByTagName("i");
                                    for(z=0;z<setsi.length;z++)
                                    {
                                        set = new Set();
                                        set.set_name = setsi[z].getAttribute("set_name");
                                        set.set_score = setsi[z].getAttribute("set_score");
                                        g.addSet(set);
                                    }
                                }
                            }

                            if(g.info>0){
                                g.getGameTeams(xml,g);
                                g.scorer = new Array();
                                g.cards = new Array();

                                g.getGameScorers(xml,g);
                                g.getGameCards(xml,g);
                            }
                            
                            thisObjGameForCat.livescoreSet(g.game_id,g);

                        }
                    }

                    
                }


            },
             onError : function(obj) { }
         });
    }

    this.addSub = function(s)
    {
        this.substitution[this.substitution.length] = s;
    }

    this.getGameInfo = function(obj_el,game_id,setGInfo,raiseEventMethodName,check_lineup)
    {

        if(this.tmp && game_id >0)
        {
            this.tmp = false;
            thisObj = new Game();
            if(!check_lineup)
                check_lineup = 0;

            advAJAX.get({
                 url: "/xml/gameInfo/?game_info=1&check_lineup="+check_lineup+"&game_id="+game_id,
                 timeout : 4000,
                 onTimeout : function() { alert("Connection timed out."); },
                 retry: 1,
                 retryDelay: 5000,
                 onRetry : function() { },
                 onRetryDelay : function() {  },
                 onFinalization: function(){ document.getElementById("loading").style.display="none"; },
                 onInitialization: function(){document.getElementById("loading").style.display="block";},
                 onSuccess : function(obj){
                    xml = obj.responseXML;
                   
                    thisObj.game_id = game_id;
                    game = xml.getElementsByTagName("game");
                 
                    if(game.length > 0)
                    {
                        thisObj.status_type = game.item(0).getAttribute("status_type");
                        thisObj.game_date = game.item(0).getAttribute("game_date");
                        thisObj.game_score = game.item(0).getAttribute("game_score");
                        thisObj.status_name = game.item(0).getAttribute("status_name_item");
                        thisObj.game_score_o = game.item(0).getAttribute("game_score_o");
                        thisObj.game_score_g = game.item(0).getAttribute("game_score_sg");
                        thisObj.game_score_p = game.item(0).getAttribute("game_score_sp");
                        thisObj.game_score_h = game.item(0).getAttribute("game_score_ht");
                        thisObj.sport_id = game.item(0).getAttribute("odds_sport_type_sport_id");
                        thisObj.info = game.item(0).getAttribute("info");
                    }
                    thisObj.getGameTeams(xml,thisObj);
                    
                    //sets
                    sets = xml.getElementsByTagName("sets");
                    if(sets.length > 0)
                    {
                        setss = sets.item(0).getElementsByTagName("i");
                        as = 0;
              
                        for(i=0;i<setss.length;i++)
                        {
                            var set = new Set();
                            set.set_name = setss[i].getAttribute("set_name");
                            set.set_score = setss[i].getAttribute("set_score");
                            thisObj.addSet(set);
                            if(set.set_score=="-" && as==0)
                            {
                                as = i+1;
                                if(as > 1)
                                    as--;
                            }
                        }

                        if(as==0)
                            as = 5;

                        thisObj.actual_set = as;

                    }

                    thisObj.scorer = new Array();
                    thisObj.cards = new Array();

                    thisObj.getGameScorers(xml,thisObj);
                    thisObj.getGameCards(xml,thisObj);

                    subs = xml.getElementsByTagName("substitutions");
                    if(subs.length > 0)
                    {
                        for(i=0;i<subs.childNodes;i++)
                        {
                            var sub = new Substitution();
                            sub.min = subs.childNodes[i].getAttribute("min");
                            sub.player_in = subs.childNodes[i].getAttribute("player_in");
                            sub.player_out = subs.childNodes[i].getAttribute("player_out");
                            sub.player_in_name = subs.childNodes[i].getAttribute("player_in_firstname")+" "+subs.childNodes[i].getAttribute("player_in_lastname");
                            sub.player_out_name = subs.childNodes[i].getAttribute("player_out_firstname")+" "+subs.childNodes[i].getAttribute("player_out_lastname");

                            thisObj.addSub(sub);
                        }
                    }
             
                    if(setGInfo){
                        tab = thisObj.setGameInfo();
                        children = obj_el.parentNode.childNodes;
                        html='<div class="bonusy_item" style="clear:both;margin:2px 0 0 2px;">';

                        for(k=0;k<tab.length;k++)
                        {
                           obj_el.appendChild(tab[k]);
                           if(bonusy_items && bonusy_items[k])
                               html+='<a style="text-decoration:none;" href="'+bonusy_items[k][2]+'"><img style="border:0;" src="http://www.bukmacherzy.com/new_lay/images/loga/'+bonusy_items[k][0]+'.gif" alt="" /><span style="color:green;font-weight:bold;font-size:10px;font-family:Tahoma;line-height:20px;">'+bonusy_items[k][1]+'</span> Odbierz »</a><br />';
                        }
        
                        html+='</div>';
                        children[children.length-1].innerHTML+=html;
                    }else{
                        thisObj.raiseEvent(raiseEventMethodName,thisObj);
                    }
               
             },
                 onError : function(obj) {}
             });
        }

        this.tmp = true;
        
    }

    this.getGameTeams = function(xml,obj_to_add)
    {
         teams = xml.getElementsByTagName("teams");

        //teams
        if(teams.length>0)
        {
            t = teams.item(0).getElementsByTagName("i");
            for(i=0;i<t.length;i++)
            {
                team = new Team();
                team.team_url = t[i].getAttribute("team_item_url");
                team.team_id = t[i].getAttribute("team_id");
                team.farmer = t[i].getAttribute("farmer");
                team.team_name = t[i].getAttribute("team_item_name");
                team.lineuplist_exists = t[i].getAttribute("lineuplist_exists");
                obj_to_add.addTeam(team);
            }

        }
    }
    this.getGameCards = function(xml,obj_to_add)
    {
         cards = xml.getElementsByTagName("cards");
        //cards
        if(cards.length > 0)
        {
            c = cards.item(0).getElementsByTagName("i");

            //cards
            for(i=0;i<c.length;i++)
            {
                var card = new Card();
                card.firstName = c[i].getAttribute("player_firstname");
                card.lastName = c[i].getAttribute("player_lastname");
                card.min = c[i].getAttribute("min");
                card.type = c[i].getAttribute("type");
                card.player_id = c[i].getAttribute("player_id");


                card.team_id = c[i].getAttribute("team_id");
                obj_to_add.addCard(card);
            }
        }
    }
    
    this.getGameScorers = function(xml,obj_to_add)
    {
        scorer = xml.getElementsByTagName("scorers");
        //scorer
        if(scorer.length > 0)
        {
            sc = scorer.item(0).getElementsByTagName("i");
            //scorer
            for(i=0;i<sc.length;i++)
            {
                var strzelec = new Scorer();
                strzelec.firstName = sc[i].getAttribute("player_firstname");
                strzelec.lastName = sc[i].getAttribute("player_lastname");
                strzelec.min = parseInt(sc[i].getAttribute("min"));
                strzelec.type = sc[i].getAttribute("goals_type");
                strzelec.team_id = parseInt(sc[i].getAttribute("team_id"));
                strzelec.player_id =parseInt(sc[i].getAttribute("player_id"));
                strzelec.goals_normal = parseInt(sc[i].getAttribute("goals_normal"));
                obj_to_add.addScorer(strzelec);
            }
        }

    }
}

function Typy(p_id,url)
{
    this.user_id = null;
    this.user_login = null;
    this.type_value_id = null;
    this.type_value_name = null;
    this.type_kurs = null;
    this.type_bukmacher_id  = null;
    this.type_bukmacher_name = "";
    this.type_bukmacher_link = null;
    this.type_bukmacher_website = null;
    this.type_analiza = null;
    this.type_wynik = null;

    this.site_url = url;
    this.podserwis_id = p_id;
    this.game = null;
    //ilosc typow np dla x
    this.ilosc_typow = 0;
    this.procent_typow = 0;
    this.setGame = function(g)
    {
        this.game = g;
    }
  
    this.getTypeForGameByUser = function(game_id,user_id)
    {
        thisObj = this;
        advAJAX.get({
             url: "/xml/getPodserwisExpertTypeForGame/?game_id="+game_id+"&user_id="+user_id,
             timeout : 4000,
             onTimeout : function() { alert("Connection timed out."); },
             retry: 1,
             retryDelay: 2000,
             onRetry : function() { alert("Retry connection..."); },
             onRetryDelay : function() { alert("Awaiting retry..."); },
             onSuccess : function(obj){
                 
                 type = obj.responseXML.getElementsByTagName("type");

                 if(type.length > 0){
                     var x = thisObj.set_type(type.item(0));
                     thisObj.setTypeBoxExpert(x);
                 }else{
                    alert("Nie znalazłem typu");
                 }

         },
             onError : function(obj) { }
         });
    }

    this.set_type = function(xml)
    {
        teams = xml.getElementsByTagName("teams");
        if(teams.length > 0){
            teams_node = teams.item(0).getElementsByTagName("i");
            var game = new Game();

            for(i=0;i<teams_node.length;i++)
            {
                team = new Team();
                team.team_name = teams_node[i].getAttribute("team_item_name");
                team.team_id = teams_node[i].getAttribute("odds_teams_team_id");
                team.team_url = teams_node[i].getAttribute("team_item_url");

                game.addTeam(team);
            }

            game_node = teams.item(0).parentNode;
            game.game_date = game_node.getAttribute("game_date");
            game.game_id = game_node.getAttribute("game_id");
            
            var typ = new Typy(this.podserwis_id,this.site_url);
            typ.setGame(game);
            typ.type_value_name = game_node.getAttribute("odd_value_name_name");
            typ.type_analiza = game_node.getAttribute("type_opis");
            return typ;
            
        }else{
            return false;
        }
    }

    this.setTypeBoxExpert = function(typ)
    {
        expert_element = $('games_expert');
        div = expert_element.getElementsByTagName("div");
        wiersz = 0;
        for(i=0;i<div.length;i++)
        {
            if(div[i].className=="row")
            {
                wiersz++;
                
                switch(wiersz)
                {
                    case 1:
                        if(typ.game.teams.length==0)
                            throw "Nie ma drużyn";

                        if(typ.game.teams.length>2)
                            div[i].getElementsByTagName("div").item(1).innerHTML = typ.game.teams[0].team_name+", "+typ.game.teams[1].team_name+" - "+typ.game.teams[2].team_name+", "+typ.game.teams[3].team_name;
                        else
                            div[i].getElementsByTagName("div").item(1).innerHTML = typ.game.teams[0].team_name+" - "+typ.game.teams[1].team_name;
                    break;
                    case 2:
                        div[i].getElementsByTagName("div").item(1).innerHTML = new Date(parseInt(typ.game.game_date)*1000).format("d-m-Y H:i");
                    break;
                    case 3:
                        div[i].getElementsByTagName("div").item(1).innerHTML = typ.type_value_name;
                    break;
                }
            }
            if(div[i].className=="row_analiza")
                div[i].innerHTML = '<span class="analiza">Analiza:</span>'+typ.type_analiza.truncate(500);

        }
    }
}

Typy.getGamesForLeague = function(set_games,podserwis_id,round,season,date,mt,expert,check_user_typed,type_wygladu,remis)
{

  if(!date)
      date="";
  else
      date = date.replace(/-/g,"");

  if(!mt)
    mt = "";

  if(!expert)
      expert="";
if(!check_user_typed)
     check_user_typed="";


   thisObj = this;
    advAJAX.get({

         url: "/xml/getTypy/?p_id="+podserwis_id+"&season_id="+season+"&round="+round+"&expert="+expert+"&date="+date+"&mt="+mt+"&check_user_typed="+check_user_typed,
         timeout : 4000,
         onTimeout : function() { alert("Connection timed out."); },
         retry: 1,
         retryDelay: 2000,
         onRetry : function() { alert("Retry connection..."); },
         onRetryDelay : function() { alert("Awaiting retry..."); },
         onSuccess : function(obj){

         podzial_ligi = false;
         leagues_xml = obj.responseXML.getElementsByTagName("leagues");

         textareas = document.getElementsByTagName("textarea");
         for(k=0;k<textareas.length;k++)
         {
             tinyMCE.get(textareas[k].id).remove();
         }

         if(leagues_xml.length > 0)
            set_games.innerHTML = "";
         else
             return;
         
         var html = "";
         for(league_index=0;league_index<leagues_xml.item(0).childNodes.length;league_index++)
         {
             var league_node = leagues_xml.item(0).childNodes[league_index];
           
             if(league_node.nodeType==1)
             {
                 games_xml = league_node.getElementsByTagName("teams");
                 if(games_xml.length>0)
                 {
                     var league_html = '';
                     league_html+='<div class="league_box">';
                     league_html+='<div class="header">';
                     league_html+='<img src="'+site_url+'/images/new_podserwisy/league_a.gif" class="floatl" alt="" />';
                     league_html+='<div class="title">'+league_node.getAttribute("league_item_name")+'</div>';
                     league_html+='<img src="'+site_url+'/images/new_podserwisy/league_c.gif" alt="" class="floatr" />';
                     league_html+='</div>';
                     league_html+='<div class="text_content">';

                     if(leagues_xml.item(0).childNodes.length>1)
                     {
                        html+=league_html;
                     }
                 
                     for(i=0;i<games_xml.length;i++)
                     {
                        var game_node = games_xml[i].parentNode;
                        var game = new Game();
                        game.sport_id = game_node.getAttribute("odds_sport_type_sport_id");
                        game.game_id = game_node.getAttribute("game_id");
                        if(isdefined(typowanie))
                            saved_game = typowanie.searchType(game.game_id);
                        else
                            saved_game = false;

                        if(!saved_game || saved_game.game.teams.length == 0)
                        {
                            game.status_type = game_node.getAttribute("status_type");
                            game.game_date = game_node.getAttribute("game_date");
                            if(game.status_type==2)
                                game.game_score = ' - ';
                            else
                                game.game_score = game_node.getAttribute("game_score");
                            game.livegol_game_id = game_node.getAttribute("livegol_game_id");
                            game.game_min = game_node.getAttribute("game_min");
                            sets_p = game_node.getElementsByTagName("sets");
                            if(sets_p.length>0)
                            {
                                sets = sets_p.item(0).getElementsByTagName("i");

                                for(seti=0;seti<sets.length;seti++)
                                {
                                    var set_tmp = new Set();
                                    set_tmp.set_name = sets[seti].getAttribute("set_name");
                                    set_tmp.set_score = sets[seti].getAttribute("set_score");

                                    game.addSet(set_tmp);
                                }
                            }

                            teams_nodes = game_node.getElementsByTagName("teams");
                            if(teams_nodes.length > 0)
                            {
                                team_nodes = teams_nodes.item(0).getElementsByTagName("i");

                                for(x=0;x<team_nodes.length;x++)
                                {
                                    team = new Team();
                                    team.team_name = team_nodes.item(x).getAttribute("team_item_name");
                                    team.team_url = team_nodes.item(x).getAttribute("team_item_url");

                                    game.addTeam(team);
                                }
                            }

                            //sprawdz czy typowal
                            if(check_user_typed)
                            {
                                user_typed = game_node.getElementsByTagName("user_typed");
                                if(user_typed.length > 0)
                                {
                                    if(user_typed.item(0).getAttribute("typ"))
                                    {
                                        game.user_typed_typ = user_typed.item(0).getAttribute("typ");
                                        game.user_typed_wynik = user_typed.item(0).getAttribute("wynik")
                                    }

                                }
                            }

                            ilosc_1=0;
                            ilosc_x=0;
                            ilosc_2=0;
                            //types
                            types = game_node.getElementsByTagName("types");
                            if(types.length > 0)
                            {
                                game.ilosc_typow = types.item(0).getAttribute("sum");
                                game.ilosc_analiz = game_node.getAttribute("analizy");
                                types_items = types.item(0).getElementsByTagName("i");
                                for(z=0;z<types_items.length;z++)
                                {
                                    if(types_items[z].getAttribute("odd_value_id")==1)
                                        ilosc_1 = types_items[z].getAttribute("ilosc");
                                    else if(types_items[z].getAttribute("odd_value_id")==2)
                                        ilosc_x = types_items[z].getAttribute("ilosc");
                                    else if(types_items[z].getAttribute("odd_value_id")==3)
                                        ilosc_2 = types_items[z].getAttribute("ilosc");
                                }
                            }

                            //odds
                            odds = game_node.getElementsByTagName("odds");
                            if(odds.length > 0)
                            {
                                odds_item = odds.item(0).getElementsByTagName("i");

                                var type_1 = new Typy();
                                var type_x = new Typy();
                                var type_2 = new Typy();
                                for(a=0;a<odds_item.length;a++)
                                {

                                    if(odds_item[a].getAttribute("odd_name")==1)
                                    {
                                        type_1.type_value_name = 1;
                                        type_1.type_bukmacher_id = odds_item[a].getAttribute("book_id");
                                        type_1.type_bukmacher_name = odds_item[a].getAttribute("book_name");
                                        type_1.type_bukmacher_link = odds_item[a].getAttribute("book_link");
                                        type_1.type_kurs = odds_item[a].getAttribute("max_a");
                                        if(game.ilosc_typow==0)
                                            type_1.procent_typow = 0;
                                        else
                                             type_1.procent_typow = ilosc_1*100/game.ilosc_typow;
                                    }else if(odds_item[a].getAttribute("odd_name")=='x'){
                                        type_x.type_value_name = 1;
                                        type_x.type_bukmacher_id = odds_item[a].getAttribute("book_id");
                                        type_x.type_bukmacher_name = odds_item[a].getAttribute("book_name");
                                        type_x.type_bukmacher_link = odds_item[a].getAttribute("book_link");
                                        type_x.type_kurs = odds_item[a].getAttribute("max_a");
                                        if(game.ilosc_typow==0)
                                            type_x.procent_typow = 0;
                                        else
                                             type_x.procent_typow = ilosc_x*100/game.ilosc_typow;
                                    }else if(odds_item[a].getAttribute("odd_name")==2){
                                        type_2.type_value_name = 1;
                                        type_2.type_bukmacher_id = odds_item[a].getAttribute("book_id");
                                        type_2.type_bukmacher_name = odds_item[a].getAttribute("book_name");
                                        type_2.type_bukmacher_link = odds_item[a].getAttribute("book_link");
                                        type_2.type_kurs = odds_item[a].getAttribute("max_a");
                                        if(game.ilosc_typow==0)
                                            type_2.procent_typow = 0;
                                        else
                                            type_2.procent_typow = ilosc_2*100/game.ilosc_typow;
                                    }

                                }

                                if(!type_1.type_kurs){
                                    type_1.type_bukmacher_name = "-";
                                    type_1.type_kurs = "-";
                                }

                                if(!type_x.type_kurs){
                                    type_x.type_bukmacher_name = "-";
                                    type_x.type_kurs = "-";
                                }

                                if(!type_2.type_kurs){
                                    type_2.type_bukmacher_name = "-";
                                    type_2.type_kurs = "-";
                                }

                                game.type_1 = type_1;
                                game.type_x = type_x;
                                game.type_2 = type_2;
                            }
                              var typ = new Typy();
                              typ.type_value_id = game.user_typed_typ;
                              typ.type_wynik = game.user_typed_wynik;

                              if(saved_game){
                                    typ.type_analiza = saved_game.type_analiza;
                                    typ.type_value_id = saved_game.type_value_id


                                    if(saved_game.game){
                                        if(saved_game.game.score_0)
                                            game.score_0 = saved_game.game.score_0;

                                        if(saved_game.game.score_1)
                                            game.score_1 = saved_game.game.score_1;
                                    }
                                    typowanie.removeTyp(saved_game.game.game_id);
                              }

                              typ.setGame(game);

                           if(saved_game)
                               typowanie.addTyp(typ);
                         }else{
                             typ = saved_game;
                         }
                         
                         html += Typy.setGameHtml(typ,type_wygladu,remis);
                         
                     }
                     if(leagues_xml.item(0).childNodes.length>1)
                     {
                        html+="</div></div>";
                     }
                }
             }
         }
         
         set_games.innerHTML = html;
        
         if(type_wygladu==2)
            Typy.setWYSWIGEditor();
      
     },
         onError : function(obj) {}
     });
}


Typy.setGameHtml = function(typ,type,remis)
{
    pokaz = 0;
    game = typ.game;
    kursy_typowalem = 0;
    html='<div class="league"><div class="status">';
    html+='<img src="'+site_url+'/images/new_podserwisy/status_'+game.status_type+'.gif" alt=""  />'
    html+='</div>';
    
    html+='<div class="gamen">';
    html+='<div class="row">';

    if(game.status_type!=3)
        html+='    <div class="a">'+new Date(parseInt(game.game_date)*1000).format("d-m H:i")+'</div>';
    else
        html+='<div class="a"><img src="'+site_url+'/images/new_podserwisy/game_min.gif" style="float:left;margin:11px 5px 0 14px" alt=""/><span style="float:left;color:#00890e;">'+game.game_min+'min</span></div>';

    if(remis){
        html+='    <div class="b">'+game.teams[0].team_name+'</div>';
        html+='    <div class="c"><div class="z"><div class="y">remis</div></div></div>';
        html+='    <div class="d">'+game.teams[1].team_name+'</div>';
    }else{
       if(game.teams.length>2)
       {
            html+='    <div class="br">'+game.teams[0].team_name+', '+game.teams[1].team_name+'</div>';
            html+='    <div class="dr">+'+game.teams[2].team_name;

            if(game.teams.length>3)
                html+= game.teams[3].team_name;
            html+='</div>';

       }else{
            html+='    <div class="br">'+game.teams[0].team_name+'</div>';
            html+='    <div class="dr">'+game.teams[1].team_name+'</div>';
       }
    }
    html+='</div>';
    html+='<div class="row2">';

    if(type==2)
    {
        
        if(game.status_type==2 && game.game_date > Math.round(new Date().getTime() / 1000) && !typ.game.user_typed_typ ){
            pokaz = 1;
        }
        
        html+=' <div class="score_typowanie">';
        if(game.sport_id==1){
        if(game.user_typed_wynik){
            html+="Typowałem:";
            html+='       <strong>'+game.user_typed_wynik+'</strong>';
        }else if(pokaz){
            html+='   Typuję wynik:';
            html+='<select  name="wynik['+game.game_id+'][0]" onchange="typowanie.ustaw_wynik(0,'+game.game_id+',this,this.options[this.selectedIndex].value)">';
            html+='<option value="">-</option>';
           
            for(index_option=0;index_option<11;index_option++)
            {
                selected = "";
                if(game.score_0==index_option)
                    selected = "selected";
                
                html+=' <option value="'+index_option+'" '+selected+'>'+index_option+'</option>';
            }
            
            html+='</select> : ';

            html+='<select  name="wynik['+game.game_id+'][1]" onchange="typowanie.ustaw_wynik(1,'+game.game_id+',this,this.options[this.selectedIndex].value)">';
            html+='<option value="">-</option>';
           
            for(index_option=0;index_option<11;index_option++)
            {
                 selected = "";
                if(game.score_1==index_option)
                    selected = "selected";

                html+=' <option value="'+index_option+'" '+selected+'>'+index_option+'</option>';
            }
            html+='</select>';
        }
        }
        html+="</div>";
        
    }else{
        html+='    <div class="a">'+game.game_score+'</div>';
    }

        if(game.sport_id==2 && !pokaz)
        {
            html+='<div class="sety">';
            for(set_index = 0;set_index<5;set_index++)
            {
                if(game.sets[set_index])
                    html+='<span class="set">'+score_explode(game.sets[set_index].set_score,0)+'<br />'+score_explode(game.sets[set_index].set_score,1)+'</span>';
            }
            html+='</div>';
        }

        if(remis)
            html+='<div class="b">';
        else if(!pokaz && !remis && game.sport_id==2)
            html+='    <div class="b2">';
        else if(pokaz || ( !remis && game.sport_id!=2))
            html+='    <div class="b3">';

        if(type==2)
        {
    
            if(game.user_typed_typ==1 || game.user_typed_typ==4 || game.user_typed_typ==6)
            {
              html+='  <div class="h" style="color:#b1b1b1;background:url(\''+site_url+'/images/new_podserwisy/typowalem.gif\');">';
              html+='  Typowałem: <strong>x</strong></div>';
              kursy_typowalem = 1;
            }else if(!pokaz){
              html+='  <div class="h"></div>';
            }else{
                checked = "";
                if(typ.type_value_id==1 || typ.type_value_id==4 || typ.type_value_id==6)
                    checked = "checked";

                html+='  <div class="h"><span>Typuję <strong>1</strong>:</span><input type="checkbox" '+checked+' onclick="typowanie.typuj(this,'+game.game_id+',1,'+game.sport_id+')" name="odd['+game.game_id+'][1]" value="1" /></div>';
            }
        }
        
      
        html+='        <div class="e"';
        if (kursy_typowalem==1)
            html+='style="color:#b1b1b1;background:url(\''+site_url+'/images/new_podserwisy/typowalem.gif\')"';
         
        html+='>Kurs: <a target="_blank" href="'+game.type_1.type_bukmacher_link+'">'+game.type_1.type_kurs+'</a></div>';
        html+='        <div class="f"><a target="_blank" href="'+game.type_1.type_bukmacher_link+'">'+game.type_1.type_bukmacher_name+'</a></div>';
        html+='    </div>';
        if(remis)
        {
            html+='    <div class="c">';
            if(type==2)
            {
                if(game.user_typed_typ==2 || game.user_typed_typ==4 || game.user_typed_typ==5)
                {
                  html+='  <div class="h" style="color:#b1b1b1;background:url(\''+site_url+'/images/new_podserwisy/typowalem.gif\')">';
                  html+='  Typowałem: <strong>x</strong></div>';
                  kursy_typowalem = 2;
                }else if(!pokaz){
                  html+='  <div class="h"></div>';
                }else{
                    checked = "";
                    if(typ.type_value_id==2 || typ.type_value_id==4 || typ.type_value_id==5)
                        checked = "checked";

                  html+='  <div class="h"><span>Typuję <strong>x</strong>:</span><input '+checked+' type="checkbox" onclick="typowanie.typuj(this,'+game.game_id+',2,'+game.sport_id+')" name="odd['+game.game_id+'][2]" value="1" /></div>';
                }
            }

            html+='        <div class="e"';
            if (kursy_typowalem==2)
                html+='style="color:#b1b1b1;background:url(\''+site_url+'/images/new_podserwisy/typowalem.gif\')"';

            html+='>Kurs: <a href="'+game.type_x.type_bukmacher_link+'"  target="_blank">'+game.type_x.type_kurs+'</a></div>';
            html+='        <div class="f"><a href="'+game.type_x.type_bukmacher_link+'"  target="_blank">'+game.type_x.type_bukmacher_name.truncate(10)+'</a></div>';
            html+='    </div>';
        }
        if(remis)
            html+='    <div class="d">';
        else if(!remis && !pokaz && game.sport_id==2)
            html+='    <div class="d2">';
        else if (pokaz || (!remis && game.sport_id!=2))
            html+='    <div class="d3">';

        if(type==2)
        {
            if(game.user_typed_typ==3 || game.user_typed_typ==5 || game.user_typed_typ==6)
            {
              html+='  <div class="h" style="color:#b1b1b1;background:url(\''+site_url+'/images/new_podserwisy/typowalem.gif\')">';
              html+='  Typowałem: <strong>x</strong></div>';
              kursy_typowalem = 3;
            }else if(!pokaz){
              html+='  <div class="h"></div>';
            }else{
                checked = "";
                
                if(typ.type_value_id==3 || typ.type_value_id==5 || typ.type_value_id==6)
                    checked = "checked";

              html+='  <div class="h"><span>Typuję <strong>2</strong>:</span><input '+checked+' type="checkbox" onclick="typowanie.typuj(this,'+game.game_id+',3,'+game.sport_id+')" name="odd['+game.game_id+'][3]" value="1" /></div>';
            }
        }
        html+='        <div class="e" ';
        if (kursy_typowalem==2)
            html+='style="color:#b1b1b1;background:url(\''+site_url+'/images/new_podserwisy/typowalem.gif\')"';

        html+='>Kurs: <a href="'+game.type_2.type_bukmacher_link+'" target="_blank">'+game.type_2.type_kurs+'</a></div>';
        html+='        <div class="f"><a href="'+game.type_2.type_bukmacher_link+'" target="_blank">'+game.type_2.type_bukmacher_name+'</a></div>';
        html+='    </div>';
        html+='</div>';
    
    
   //jesli typowanie
    if(type==2)
    {
         html+='    <div class="typowanie">';
         html+='       <div class="row4">';
         html+='           <div class="a">';
         if(game.user_typed_typ)
         {
             html+=' <p style="text-align:center;">';
             html+='    Oddałeś(aś) już typ dla tego spotkania.';
             html+='  </p>';
         }else if(!pokaz){
            html+='    <p style="text-align:center;">Przyjmowanie typów na spotkanie zakończone.</p>';
         }else{
            analiza = '';
            
            if(typ.type_analiza)
                analiza = typ.type_analiza;

            html+='   <textarea name="analiza['+game.game_id+']" cols="" id="'+parseInt(game.game_id).toString()+'" onkeyup="">'+analiza+'</textarea>';
         }
      
        html+='      </div>';
        html+='      </div>';
        html+='      <div class="row5">';
        html+='          <img src="'+site_url+'/images/new_podserwisy/game_box_g.gif" alt="" class="floatl" />';
        if(!game.user_typed_typ && pokaz)
            html+='   <div class="t"> Ilość znaków: <span id="znaki_'+game.game_id+'">'+analiza.length+'</span></div>';
        else
            html+='  <div class="t"></div>';

        html+='          <img src="'+site_url+'/images/new_podserwisy/game_box_i.gif" alt="" class="floatr" />';
        html+='      </div>';
        html+='  </div>';
    
    }else{
         ilosc_typow = parseInt(game.ilosc_typow);
        html+='   <div class="game_info" style="display:none">';
        html+='       <div class="row3">';
        html+='           <div class="a">';
        html+='                <div class="i"><img src="'+site_url+'/images/new_podserwisy/game_box_arrow.gif" alt="" /> Typów: <strong>'+parseInt(game.ilosc_typow)+'</strong></div>';
        html+='                <div class="i"><img src="'+site_url+'/images/new_podserwisy/game_box_arrow.gif" alt="" /> Analiz: <strong>'+parseInt(game.ilosc_analiz)+'</strong></div>';
        html+='<div class="i"><a href="http://www.bukmacherzy.com/'+podserwis_url+'/typy'+experci_url+'/'+game.game_id+'_'+game.teams[0].team_url+'_'+game.teams[1].team_url+'">';
        html+='    <img class="analiza" src="'+site_url+'/images/new_podserwisy/analizy.gif" alt=""/></a></div>';

        html+='            </div>';

       html+='             <div class="b">';
       html+='                <img src="http://www.bukmacherzy.com/new_lay/images/slupki/green_black_'+round_integer(game.type_1.procent_typow,5)+'.gif" alt="" /><br />';
       html+='                 '+Math.round(game.type_1.procent_typow*100)/100+'%';
       html+='                    <div class="typow">Typów: ';
       if(ilosc_typow>0)
            html+=Math.round(ilosc_typow*game.type_1.procent_typow/100);
       else
            html+='0';

       html+='                  </div>';

       html+='             </div>';
       html+='             <div class="c">';
       html+='                 <img src="http://www.bukmacherzy.com/new_lay/images/slupki/gray_black_'+round_integer(game.type_x.procent_typow,5)+'.gif" alt="" /><br />';
       html+='                 '+Math.round(game.type_x.procent_typow*100)/100+'%';
       html+='                    <div class="typow">Typów: ';
       if(ilosc_typow>0)
            html+=Math.round(ilosc_typow*game.type_x.procent_typow/100);
       else
            html+='0';

       html+='                  </div>';

       html+='             </div>';
       html+='             <div class="d">';
       html+='                 <img src="http://www.bukmacherzy.com/new_lay/images/slupki/blue_black_'+round_integer(game.type_2.procent_typow,5)+'.gif" alt="" /><br />';
       html+='                 '+Math.round(game.type_2.procent_typow*100)/100+'%';
        html+='                    <div class="typow">Typów: ';
       if(ilosc_typow>0)
            html+=Math.round(ilosc_typow*game.type_2.procent_typow/100);
       else
            html+='0';

       html+='                  </div>';

       html+='            </div>';
       html+='         </div>';

       if(game.sport_id==1)
       {
        html+='       <div class="row4">';
         html+='           <div class="a">';
         html+='               <div class="info">';

         html+='               </div>';
         html+='               <div class="sklady">';
         html+='                   <a href="http://www.livegol.pl/pilka_nozna/boisko/'+game.livegol_game_id+'/" target="_blank"><img src="'+site_url+'/images/new_podserwisy/sprawdz_sklady.gif" alt="" /></a>';
         html+='               </div>';
         html+='               <div class="strzelcy">';

          html+='              </div>';
          html+='          </div><span style="display:block;clear:both;"></span>';
          html+='      </div>';
          
       }
       html+='  </div>';
         html+='   <div class="row5" onclick="hide_element(searchClassElement(this,\'zwin\'),searchClassElement(this.parentNode,\'game_info\'),1);var G = new Game();G.livegol_game_id = '+game.livegol_game_id+';G.getGameInfo(searchClassElement(this.parentNode,\'strzelcy\',tru));">';
         html+= '      <img src="'+site_url+'/images/new_podserwisy/game_box_g.gif" alt="" class="floatl" />';
         html+='       <div class="t">Kliknij, aby zobaczyć szczegóły spotkania';

         html+= '      </div>';

         html+= '      <img src="'+site_url+'/images/new_podserwisy/game_box_i.gif" alt="" class="floatr" />';
         html+= '      <div class="zwin">';
         html+= '          <span>Rozwiń</span><img src="http://www.bukmacherzy.com/new_lay/images/plus2.gif" alt="" />';
         html+= '      </div>';
         html+= '  </div>';
         html+=' </div>';
    }
    html+= '</div><div style="clear:both;height:1px;line-height:1px;"></div>';

    return html;
   
}

function zzz(obj)
{
    alert(4);
}

function showStatusGames(status)
{
    var games = document.getElementById("games");
   
    var div = games.getElementsByTagName("div");
    for(i=0;i<div.length;i++)
    {
        if(div[i].className=="a")
        {
            img = div[i].getElementsByTagName("img");
           
            if(img.length>0 && status!="all" && (img.item(0).src.search("status_"+status+""))==-1)
            {
                div[i].parentNode.style.display="none";
            }else{
               div[i].parentNode.style.display="block";
            }

        }
    }

}

function is_numeric (mixed_var) {

    if (mixed_var === '') {
        return false;
    }

    return !isNaN(parseInt(mixed_var));
}

Typy.setWYSWIGEditor = function()
{
    tinyMCE.init({
        theme : "advanced",
        mode : "textareas",
        plugins : "bbcode",
        theme_advanced_buttons1 : "bold,italic,underline,undo,redo",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "center",
        theme_advanced_styles : "Code=codeStyle;Quote=quoteStyle",
        content_css : "bbcode.css",
        entity_encoding : "raw",
        add_unload_trigger : false,
        remove_linebreaks : false,
        force_br_newlines : true,
        content_css: ''+site_url+'/css/textarea.css',

        setup: function(ed) {
            ed.onChange.add(function(ed, e) {

                typowanie.setAnaliza(ed.id,ed);
                typowanie.setAnalizyCounter($('znaki_'+ed.id),ed.id);
            });
             
             ed.onKeyUp.add(function(ed, e) {

                typowanie.setAnaliza(ed.id,ed);
                typowanie.setAnalizyCounter($('znaki_'+ed.id),ed.id);
            });
        }
    });


    

}


