<?php
//TODO Pie chart pre vyhry prehry spravit

$oForm = new cForm();

include_once($this->path['plugins'] . "classes/hrac.class.php");
$oHrac = new hracClass($this->oDb, $this->oDbS);

$ret ='';
$form= '';

//ak budem chciet profil niekoho ineho dam tam ine
$idHrac=$this->oSession->idUser;
$meno =$this->oSession->firstName;
$priez=$this->oSession->surName;

if (isset($_FILES['novaFotka']) && $_FILES['novaFotka']['size'] > 0 ) {
// Temporary file name stored on the server
  $tmpName  = $_FILES['novaFotka']['tmp_name'];  

  // Read the file 
  $fp      = fopen($tmpName, 'r');
  $data = fread($fp, filesize($tmpName));

  fclose($fp); 
  $encoded = chunk_split(base64_encode($data));
  if ($oHrac->zmenaFotky($idHrac,$encoded)) ;
}

$imgdata= $oHrac->dajFoto($idHrac);
$imgdata=str_replace("+", "%2B",$imgdata);
$ret .='<div id="foto"> <i class="icon-edit-sign" id="zmenSign"></i> <img src="http://www.cms.badmintonovaskola.sk/plugins/profil/zdrojFoto.tmpl.php?o='. urlencode($imgdata).'" alt="foto"> </div>'.$form;

$form='<div id="zmenaFoto"> ';
$form .= '<form method="post" action="'.$this->oUrl->href().'"  enctype="multipart/form-data"'.'>'."\n";
$form .= $oForm->uploadBox("Zmen Fotku","novaFotka");
$form .= '</form> </div>'."\n";



$ret .=$form;
$KlubData= $oHrac->getKlubInfo($_SESSION['klubId']);
$ret .=  '<div id="profil"> <h2> Hráčsky profil </h2> <h3> Meno, Priezvisko: '. $meno . ', '.$priez. '</h3> <h4> vek: '.$_SESSION['vek']. '</h4> <h4> klub: '.$KlubData['nazov']. '</h4> <h5> šport: '.$KlubData['sport']. '</h5>';


$ret .='</div>';
$ret .='<div id="ucasti"> <h2> Hrac sa zucastnil na treningoch </h2>';

$pocet= $oHrac->getTreningyCount($idHrac);



$i=1;
if (isset($_POST['od']) && !empty($_POST['od']) )
		$q= $oHrac->getTreningy($idHrac, $_POST['od']);
else 
	$q= $oHrac->getTreningy($idHrac, 0);

/*
 * vypisuje treningy
 */
while ($row = $q->fetch())
{
		 $ret .= '<a href="#" onclick="detailTreningu('.$row['id'].'); return false;" > <i class="icon-eye-open"></i> '.$i.'.trening dna: '.$row['datum'] .'</a> ';
		 $ret .='<br>';
		$i++;	
}
if ($i==1) $ret .='<p> niesu evidovanie ziadne treningy</p>';


//ak viac ako 
if (($pocet/5) >= 1)
{  
	$ret .= '<div id="paging" >';

for ($j=0; $j<$pocet/5; $j++){
	$ret .='<div class="page"><a href="#" onclick="$(\'#odHidden\').val(\''.$j.'\'); $(\'#subHidden\').submit(); return false;">'.($j+1).'</a></div>';
} 
$ret .= '<form method="post" action="'.$this->oUrl->href().'" id="subHidden" enctype="multipart/form-data"'.'>'."\n";
$ret .= '<input class="nodisp" type="hidden" id="odHidden" value="" name="od">';
$ret .= '</form></div>';
}
$ret .='</div> <div class="clear"></div> ';

$ret .='<div id="uspesnostZapasov">';
$ret .='</div>';

$ret .='<div id="treningVypis">';
$ret .='<img id="ajaxLoad" alt="ajaxLoad" src="/images/icons/ajax.gif"> ';
$ret .='</div> <div class="clear"></div> ';
$ret .='</div>';



echo $ret;
?>

<script type="text/javascript" src="/plugins/profil/js/jqxcore.js"></script>
<script type="text/javascript" src="/plugins/profil/js/jqxchart.js"></script>
<script type="text/javascript" src="/plugins/profil/js/jqxdata.js"></script>


 <script type="text/javascript">
        $(document).ready(function () {
 
            var data= <?php echo $oHrac->ZdrojeGraf($idHrac); ?>;
            // prepare jqxChart settings
            var settings = {
                title: "Úspešnosť v zápasoch",
                description: "",
                enableAnimations: true,
                showLegend: false,
                legendPosition: { left: 520, top: 140, width: 100, height: 100 },
                padding: { left: 5, top: 5, right: 5, bottom: 5 },
                titlePadding: { left: 0, top: 0, right: 0, bottom: 10 },
                source: data,
                colorScheme: 'scheme02',
                seriesGroups:
                    [
                        {
                            type: 'pie',
                            showLabels: true,
                            series:
                                [
                                    { 
                                        dataField: 'Pocet',
                                        displayText: 'Nazov',
                                        labelRadius: 100,
                                        initialAngle: 15,
                                        radius: 130,
                                        centerOffset: 0,
                                        formatSettings: { sufix: '%', decimalPlaces: 1 }
                                    }
                                ]
                        }
                    ]
            };

            // setup the chart

            $('#uspesnostZapasov').jqxChart(settings);
            

        });
    </script>
<script>
//ukazovanie podrobnosti treningov
function detailTreningu(id){
	//vymazem stary detail
	$('#treningDetail').remove();
	$('#ajaxLoad').css("display","block");
	spustiAjaxDetail(id, function(results) {
		var trening = jQuery.parseJSON(results);
		treningZaklad=trening[0];
		//[0] hlavny popis
		//[1] treningy
		
		//not null
		if (trening){
			str = '<a href= "#" onclick="$(\'#treningDetail\').remove(); return false;" > <i class="icon-eye-close" id="zavriVypis"></i> </a>';
			//DAtum vidi aj tak
			//str += '<h2>Datum: '+treningZaklad[0].datum+'</h2>';
			str+= '<h2>Trener: </h2>'+treningZaklad[0].meno+' '+treningZaklad[0].priezvisko+' ('+treningZaklad[0].login+')<br>';
			str+= '<h2>Trvanie: </h2>'+treningZaklad[0].trvanie+' hod <br>';
			str+= '<h2>Popis: </h2>'+treningZaklad[0].popis+'<br>';
			str+= '<h2>Last edit: </h2>'+treningZaklad[0].last_mod +'<br>';
			zapasy= trening[1];
			//console.debug(zapasy);
			for (var i = 0; i < zapasy["zaklad"].length; i++) {
				
				vytaz = zapasy["zaklad"][i].vytaz;				
				
				str+= '<div class="detailZapas">Západ hrali:<br>';

				//povypisuje teamA
				for (var j = 0; j < zapasy["teamA"][i].length; j++) {
					str += '<h2>'+zapasy["teamA"][i][j]["meno"]+'</h2>';
					}
				str+=' vs. ';

				//povypisuje teamB
				for (var j = 0; j < zapasy["teamB"][i].length; j++) {
					str += '<h2>'+zapasy["teamB"][i][j]["meno"]+'</h2> ';
					}
				
				str+= '<br>Vysledok zápasu: <h2>'+zapasy["zaklad"][i].vysledok+'</h2><br>';
				if (vytaz=='A') str+= 'Vytazny tim zápasu: <h2>'+zapasy["teamA"][i][0]["meno"]+'<br>';
				else str+= 'Vytazny tim zápasu: <h2>'+zapasy["teamB"][i][0]["meno"]+'</h2>';
				str+='</div>';
				
				
			}
		}
			
		else {
			str='<h2>Tréning nenájdený!</h2>';
		}
		$('#ajaxLoad').css("display","none");
		$("#treningVypis").append('<div id="treningDetail" >'+str+'</div> ');
		
	});
	
}


function spustiAjaxDetail( id, odozva){
	$.post('/plugins/profil/ajax.php', 
			  { 
			    "id": id
			  },
			  function(data,status){
				  odozva(data);
			  });
}

$(function() {
	
	//ukazovanie tlacitka na menenie profilovky
	$('#foto img, #zmenaFoto ').mouseenter(function() {
		
			$('#zmenaFoto').css('display','block');
		
		})
		
		.mouseleave(function() {
			
			$('#zmenaFoto').css('display','none');
		
		});
	});
</script>