﻿function loadBest(xml, type)
{
	var divik = document.createElement("div");
	if( xml == null || xml.length == 0)
	{
		return divik;
	}
	var title = document.createElement("div");
	title.className="best-" +type+"-title";
	divik.appendChild(title);
	var block = document.createElement("div");
	block.className="best-block";
	divik.appendChild(block);
	
	for(var i=1; i< xml.length+1; i++)
	{
		var n = xml[i-1];
		
		var row = document.createElement("div");
		row.className="best-row" + (i%2 ?"-off": "") + " clear catch";
		
		var num =document.createElement("div");
		num.className="best-num";
		num.appendChild( document.createTextNode(i));
		row.appendChild(num);
		
		var name = document.createElement("div");
		name.className="best-name2";
		//name.appendChild( document.createTextNode(n.getAttribute("name") + " (<span class=\"best-blue\">" + n.getAttribute("tname") + "</span>)"));
		name.innerHTML=n.getAttribute("text1") + " (<span class=\"best-blue\">" + n.getAttribute("text2") + "</span>)";
		row.appendChild(name);
		var res = document.createElement("div");
		res.className="best-point";
		res.appendChild( document.createTextNode(n.getAttribute("points")));
		row.appendChild(res);
		block.appendChild(row);
	}
	
	return divik;
}
function bildTableGames( xml, div_name, title, arrColors )
{
	    var div_allgames  = document.createElement( "div");
		div_allgames.className="div-allgames";
		var panel = createPanelForTable(title, "");
		div_allgames.appendChild( panel);
		var table = document.createElement("table");
		table.className="mytable";
		//table.className="red";
		var thead = document.createElement("thead");
		table.appendChild( thead );
		var tr = document.createElement("tr");
		var th0=document.createElement("td");
		
		th0.appendChild(document.createTextNode(""));
		tr.appendChild(th0 );
		var th1=document.createElement("td");
		
		th1.appendChild(document.createTextNode(""));
		tr.appendChild(th1 );
		var th2=document.createElement("td");
		th2.appendChild(document.createTextNode("מש'"));
		tr.appendChild( th2);
		var th3=document.createElement("td");
		th3.appendChild(document.createTextNode("נצ'"));
		tr.appendChild( th3);
        var th5=document.createElement("td");
		th5.appendChild(document.createTextNode("הפ'"));
		tr.appendChild( th5);
		var th8=document.createElement("td");
		th8.appendChild(document.createTextNode("הפרש"));
		tr.appendChild( th8);		
		var th6=document.createElement("td");
		th6.appendChild(document.createTextNode("יחס"));
		tr.appendChild( th6);
		var th7=document.createElement("td");
		th7.appendChild(document.createTextNode("נק'"));
		tr.appendChild( th7);
		thead.appendChild( tr );
		//table.appendChild( tr );
		th1.className="table-text";
		th2.className="table-text center";
		th3.className="table-text center";
		th5.className="table-text center";
		th6.className="table-text center";	
		th8.className="table-text center";					
		th7.className="table-text center";				
		//alert("kyky");
		var tbody = document.createElement( "tbody");
		table.appendChild( tbody);
        
	for (i = 0; i < xml.length; i++)
	 {
	 	var points = (parseInt( xml[i].getAttribute("winers")) *2)+ parseInt(xml[i].getAttribute("faild"));
	 	var trRow = document.createElement("tr");
		trRow.className="mytr" + ( arrColors == "" ? (i%2): arrColors[i+1]);
		var td0 = document.createElement( "td");
		td0.appendChild(document.createTextNode(i+1));
		td0.className="table-text";
		trRow.appendChild( td0);
		var td1 = document.createElement( "td");
		td1.appendChild(document.createTextNode(xml[i].getAttribute("name")));
		td1.className="table-text";
		trRow.appendChild( td1);
		var td2 = document.createElement( "td");
		td2.className="center";
		td2.appendChild(document.createTextNode(xml[i].getAttribute("count")));
		trRow.appendChild( td2);
		var td3 = document.createElement( "td");
		td3.className="center";
		td3.appendChild(document.createTextNode(xml[i].getAttribute("winers")));
		trRow.appendChild( td3);	
		var td5 = document.createElement( "td");
		td5.className="center";
		td5.appendChild(document.createTextNode(xml[i].getAttribute("faild")));
		trRow.appendChild( td5);	
		var td6 = document.createElement( "td");
		td6.className="center";
		td6.appendChild(document.createTextNode(xml[i].getAttribute("scopeminus") + "-" +xml[i].getAttribute("scopeplus")));
		trRow.appendChild( td6);	
		var td8 = document.createElement( "td");
		td8.className="center";
		var efresh =  xml[i].getAttribute("scopeplus") - xml[i].getAttribute("scopeminus");
		td8.appendChild(document.createTextNode(( efresh > 0 ? "+" + efresh: efresh)));
		trRow.appendChild( td8);							
		var td7 = document.createElement( "td");
		td7.className="center";
		td7.appendChild(document.createTextNode( points ));
		trRow.appendChild( td7);
		tbody.appendChild( trRow );								

	 }
	 //table.appendChild(tbody);
	 div_allgames.appendChild( table );
	 $(div_name).appendChild( div_allgames);
}
var globCountPreiods=0;
function getcountPeriods( xml )
{
 return   xml[xml.length-1].getAttribute("period");	
}
function bildTablePeriod(xml, intPeriod)
{
	var divik = document.createElement("div");
	var divTitlePeriod = document.createElement("div");
	divTitlePeriod.className="table-period-title";
	var table = document.createElement("table");
	table.className="mytable-period";
	var tbody=document.createElement("tbody");
	var flag=0;
	for(i=0; i < xml.length; i++)
	{
	var cerPriod = xml[i].getAttribute("period");
		if ( cerPriod == intPeriod)
		 {
		
		if( flag == 0 )
		{
			flag =1;
			divTitlePeriod.appendChild( document.createTextNode( "מחזור מסי " + intPeriod + " " +  xml[i].getAttribute("cdate")));
		}
		 var tr = document.createElement("tr");
		 tr.className="tr-period" + ( i% 2 ? "-off" : "" );
		 var td1 = document.createElement("td");		 
		 td1.appendChild( document.createTextNode( xml[i].getAttribute("name1")));
		 var td2 = document.createElement("td");
		 td2.appendChild( document.createTextNode( xml[i].getAttribute("scope1")));
		 var td3 = document.createElement("td");
		 var td= document.createElement("td");
		 td.appendChild( document.createTextNode("-"));
		 td.className="center";
		 td3.appendChild( document.createTextNode( xml[i].getAttribute("scope2")));
		 var td4 = document.createElement("td");
		 td4.appendChild( document.createTextNode( xml[i].getAttribute("name2")));	
		 td1.className="td-name";
		 td4.className="td-name";
		 td.className="td-digit";
		 td2.className="td-digit";
		 td3.className="td-digit";
		 tr.appendChild(td1);
		 tr.appendChild(td2);
		 tr.appendChild(td);
		 tr.appendChild(td3);
		 tr.appendChild(td4);	 		 		 
		 tbody.appendChild( tr );
		}
		
	}
	table.appendChild( tbody);
	divik.appendChild( divTitlePeriod);
	divik.appendChild( table);
	return divik;
}
function reloadPeriods(intPeriod, xml)
{
 var divik = $("tables-period");
 divik.innerHTML="";
 var table1 = bildTablePeriod( xml, intPeriod );
 var table2 = bildTablePeriod( xml, parseInt( intPeriod) +1 );
 var tit1= createPanelForTableGreey("המחזור שהיה" , "");
 var tit2= createPanelForTableGreey("המחזור הבא" , "");
 divik.appendChild(tit1);
 divik.appendChild( table1 );
 divik.appendChild(tit2);
 divik.appendChild( table2 );
}

function bildPanelPeriods( xml) 
{
	var divik = document.createElement("div");
	divik.className="catch clear periods-cange-div";//numbers_bg_cntr.jpg
	var table= document.createElement("table");
	table.className="periods-change"
	var tbody= document.createElement("tbody");
	var tr= document.createElement("tr");
	tr.className="panel-periods";
	for( i=1; i<= globCountPreiods; i++ )
	{
		var td= document.createElement("td");
		td.className="td-panel-periods";
		td.appendChild( document.createTextNode(i));
	    td.onclick=function(){ reloadPeriods(this.innerHTML, xml)};
		tr.appendChild( td);
	}
	tbody.appendChild( tr);
	table.appendChild( tbody);
	var pan = createPanelForTable("מחזורי ליגה", "");
	divik.appendChild( pan );
	divik.appendChild( table);
	
	return divik;
	
}
function getLastPeriod( xml )
{
	for(var i=0; i< xml.length; i++)
	{
		if( xml[i].getAttribute("game_id") == "")
		 return xml[(i > 0 ? i-1 : 0 )].getAttribute("period");
	}
	return getcountPeriods( xml);
}
function bildPeriodGames( xml, divName ) 
{
	var divik = document.createElement("div");
	divik.className="tables-period right";
	globCountPreiods= getcountPeriods( xml );
	var lastPeriod = getLastPeriod( xml);

	//alert( lastPeriod);
var table1 = bildTablePeriod( xml, parseInt( lastPeriod) );
var table2 = bildTablePeriod( xml,  parseInt( lastPeriod) +1 );
var tit1= createPanelForTableGreey("המחזור שהיה" , "");
var tit2= createPanelForTableGreey("המחזור הבא" , "");
 $(divName).appendChild(tit1);
 $(divName).appendChild( table1 );
 $(divName).appendChild(tit2);
 $(divName).appendChild( table2 );
 var panelPeriods = bildPanelPeriods( xml );
 $("table-all").appendChild( panelPeriods);
	
}

