// Ajax_Initialisierung
document.write('<script type="text/javascript" src="/js/ajax.js"></script>');
document.write('<script type="text/javascript" src="/canvas_diagramme/class_canvas_initialize.js"></script>');

function tippspiel()
{
	this.stat_nr = 1; // Standardstatistik
	this.stat_user = 0; // ausgew. User
	this.slide = false;
	this.slide_prozent = 0;
	this.diagramm_init = false;
	this.dd1 = null;
	this.dd2 = null;
	this.dd3 = null;
	this.myuser = 0;
	this.tippmatrixusercheckerflag = false;
	this.tm_sel_users = 0;
		
	// Bundeslandauswahl neu laden
	this.reloadBundesland = function(div_land, load_div, id_bundesland)
	{
		initializeAJAX();
		if(xmlHttp)
		{
			xmlHttp.open('POST', "/action/ajax_bundeslandloader.php", true);
	
			xmlHttp.onreadystatechange = function() 
			{ 
				if(xmlHttp.readyState == 4)
				{     	
					var response = unescape(xmlHttp.responseText);
					
					if(response != "false")
					{
						if(document.getElementById(load_div))
						{
							document.getElementById(load_div).innerHTML = response;
						}
					}
					else
					{
						location.reload();
					}
				}
			}
			
			xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			// Land abfragen
			if(document.getElementById(div_land))
			{
				idland = document.getElementById(div_land).value;
	
				if(!isNaN(idland) || idland != "")			
					xmlHttp.send("idland=" + idland +"&bundesland_tag=" + id_bundesland);
			}
		}
	}
	
	// Tippspielanmeldung
	this.showAnmeldeForm = function(frm_name, idturnier)
	{
		initializeAJAX();
		if(xmlHttp)
		{
			xmlHttp.open('POST', "/action/ajax_Tippspielanmeldung.php", true);
	
			xmlHttp.onreadystatechange = function() 
			{ 
				if(xmlHttp.readyState == 4)
				{     	
					var response = unescape(xmlHttp.responseText);
					
					if(response != "false")
					{
						spop.setContent(response);
					}
					else
					{
						location.reload();
					}
				}
				else
				{
					spop.setLoader();
				}
			}
			
			xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			spop.setPopupTitle("Tippspielanmeldung");
			spop.setPopupIcon("/images/icon/pencil_go.png");
			spop.showPopup();
			// Anfrage senden
			xmlHttp.send("idturnier=" + idturnier);
			
		}
	}
	
	this.checkTippmatrixUsers = function(maxusers, id)
	{
		if(this.tippmatrixusercheckerflag == false)
		{
			for(var i=0; i<document.getElementsByTagName("input").length; i++)
			{
				if(document.getElementsByTagName("input")[i].type == "checkbox")
				{
					id = document.getElementsByTagName("input")[i].id;
					if(id.substring(0,15) == "tippmatrixuser_")
					{
						if(document.getElementsByTagName("input")[i].checked == true)
						{
							this.tm_sel_users++;
						}
					}
				}
			}
			this.tippmatrixusercheckerflag = true;
		}
		else
		{
			if(document.getElementById(id))
			{
				if(document.getElementById(id).checked == true)
				{
					if(this.tm_sel_users == maxusers)
					{
						document.getElementById(id).checked = false;
						alert("Du kannst maximal " +  maxusers + " Mitspieler selektieren!");
					}
					else
					{
						this.tm_sel_users++;
					}
				}
				else
					this.tm_sel_users--;
			}
		}
	}
	
	this.Tippmatrix = function(what)
	{
		initializeAJAX();
		if(xmlHttp)
		{
			xmlHttp.open('POST', "/action/ajax_reloadTippmatrix.php", true);
	
			xmlHttp.onreadystatechange = function() 
			{ 
				if(xmlHttp.readyState == 4)
				{     	
					var response = unescape(xmlHttp.responseText);
					
					if(response != "false")
					{
						if(what == "showUsers")
						{		
							spop.setContent(response);
						}
						if(what == "saveUsers")
						{
							location.reload();
						}
					}
					else
					{
						//alert("Fehler beim Aktualisieren der Tippmatrix! Bitte überprüfe die selktierten User");
						location.reload();
					}
				}
				else
				{
					spop.setLoader();
				}
			}
			
			xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
			if(what == "showUsers")
			{
				spop.setPopupTitle("Mitspielerauswahl");
				spop.setPopupIcon("/images/icon/user.png");
				spop.showPopup();
			}
			
			var param = "";
			var senden = true;
			if(what == "saveUsers")
			{	
				this.tm_sel_users = 0;
				for(var i=0; i<document.getElementsByTagName("input").length; i++)
				{
					if(document.getElementsByTagName("input")[i].type == "checkbox")
					{
						id = document.getElementsByTagName("input")[i].id;
						if(id.substring(0,15) == "tippmatrixuser_")
						{
							if(document.getElementsByTagName("input")[i].checked == true)
							{
							
								var nam = document.getElementsByTagName("input")[i].name;
								var val = document.getElementsByTagName("input")[i].value;
								param += "&" + nam + "=" + val;
								this.tm_sel_users++;
							}
						}
					}
				}

				if(this.tm_sel_users < 1)
					senden = false;
			}
			//alert(param);
			// Anfrage senden
			if(senden == true)
				xmlHttp.send("do=" + what + param);
			else
				alert("Du hast keine Mitspieler zum Vergleichen selektiert!");
		}
	}
	
	
	
	// Slide-Interval
	this.slideUsers = function(slide_nr, slide_dir, user, nr)
	{
		// Ein- und Ausbelnd-ID prüfen
		if(slide_dir == "rechts")
		{
			var einblend_id = parseInt(slide_nr) - 1;
			var ausblend_id = parseInt(slide_nr) + 2;
			var trans_id	= parseInt(slide_nr) + 1;
		}
		else if(slide_dir == "links")
		{
			var einblend_id = parseInt(slide_nr) + 1;
			var ausblend_id = parseInt(slide_nr) - 2;
			var trans_id	= parseInt(slide_nr) - 1;
		}
		
		// Prüfen ob Elemente existieren
		if(document.getElementById("statistik_user_" + einblend_id) && document.getElementById("statistik_user_" + ausblend_id))
		{
			// Überprüfen ob Einblend-Element bereits 33% erreicht hat
			if(this.slide_prozent >= 33)
			{
				//console.log("sliden beendet");
				document.getElementById("statistik_user_" + einblend_id).style.width = "33%";
				document.getElementById("statistik_user_" + ausblend_id).style.display = "none";
				document.getElementById("statistik_user_" + slide_nr).className = "";
				document.getElementById("statistik_user_" + trans_id).className = "trans";
				
				window.clearInterval(window.slideuser);
				this.slide = false;
				this.slide_prozent = 0;
				
				tippspiel.switchStats(user, nr);
			}
			else
			{
				//console.log("slider: " + this.slide_prozent);
				this.slide_prozent += 1;
				if(this.slide_prozent > 33)
					this.slide_prozent = 33;
				document.getElementById("statistik_user_" + einblend_id).style.display = "";
				document.getElementById("statistik_user_" + einblend_id).style.width = this.slide_prozent + "%";
				document.getElementById("statistik_user_" + ausblend_id).style.width = Math.round(33-this.slide_prozent) + "%";
			}
		}
	}
	
	// Userinfo reloaden
	this.reloadUserInfo = function(user, nr, slide_nr)
	{
		if(!isNaN(user))
		{
			if(document.getElementById("statistik_user_" + slide_nr))
			{
				if(this.slide != true)
				{
					// Sliderichtung
					var bottom_nr = parseInt(slide_nr) - 1;
					var top_nr	= parseInt(slide_nr) + 1;
					var slide_dir = "";
					// Überprüfen ob Top existiert
					if(document.getElementById("statistik_user_" + top_nr))
					{
						// Überprüfen ob Top ausgeblendet
						if(document.getElementById("statistik_user_" + top_nr).style.display != "none") // Nein, eingeblendet
						{
							slide_dir = "rechts";
						}
					}
					// Überprüfen ob Bottom existiert
					if(document.getElementById("statistik_user_" + bottom_nr))
					{
						// Überprüfen ob Bottom ausgeblendet
						if(document.getElementById("statistik_user_" + bottom_nr).style.display != "none") // Nein, eingeblendet
						{
							slide_dir = "links";
						}
						
					}
					if(document.getElementById("statistik_user_" + bottom_nr) && document.getElementById("statistik_user_" + top_nr))
					{
						if(document.getElementById("statistik_user_" + top_nr).style.display != "none" && document.getElementById("statistik_user_" + bottom_nr).style.display != "none") // Nein, eingeblendet
						{
							slide_dir = "";
						}
					}
					// Überprüfen ob nicht der mittlere Player gedruckt wurde
					
					if(slide_dir != "")
					{
						this.slide_prozent = 0;
						this.slide = true;
						window.slideuser = window.setInterval("tippspiel.slideUsers('" + slide_nr + "', '" + slide_dir + "', '" + user + "', '" + nr + "')", 1);
					}
				}
			}
		}/**/
	}
	
	// Switchen und User neu laden
	this.switchStatsAndUser = function(user, nr, slide_nr)
	{	
		this.reloadUserInfo(user, nr, slide_nr);
	}
	
	// Diagramm zeichnen
	this.loadDiagramm  = function(stat_nr, user)
	{
		//console.log("DIAGRAMMLOADER = " + stat_nr + " USER : " + user)
		if(stat_nr == 2)
		{
			tippspiel.dd1 = null;
			tippspiel.dd1 = new diagramm("punkte", "pkt", "tippspiel.dd1");
			tippspiel.dd1.initializeCanvas();
			tippspiel.dd1.setDiagrammart(3);
			tippspiel.dd1.setMargins(40, 50, 20, 30);
			tippspiel.dd1.setColorshema(2);
			tippspiel.dd1.setBackgroundGradient("#FFFFFF", "#F3F1E2");
			tippspiel.dd1.setAnimation(0); 
			tippspiel.dd1.setWerteanzeige(true);
			tippspiel.dd1.setTitle("Platzierungsverlauf");
			tippspiel.dd1.setSubTitle("");
			tippspiel.dd1.showLegende(false);
			tippspiel.dd1.createDiagramm();
		}
		else if(stat_nr == 3)
		{
			tippspiel.dd2 = null;
			tippspiel.dd2 = new diagramm("punkte2", "pkt2", "tippspiel.dd2");
			tippspiel.dd2.initializeCanvas();
			tippspiel.dd2.setDiagrammart(0);
			tippspiel.dd2.setMargins(40, 50, 20, 30);
			tippspiel.dd2.setColorshema(2);
			tippspiel.dd2.setBackgroundGradient("#FFFFFF", "#F3F1E2");
			tippspiel.dd2.setAnimation(0); 
			tippspiel.dd2.setWerteanzeige(true);
			tippspiel.dd2.setTitle("Punkte/Spiel");
			tippspiel.dd2.setSubTitle("");
			tippspiel.dd2.showLegende(false);
			tippspiel.dd2.createDiagramm();
		}
		else if(stat_nr == 4)
		{
			tippspiel.dd3 = null;
			tippspiel.dd3 = new diagramm("punkte3", "pkt3", "tippspiel.dd3");
			tippspiel.dd3.initializeCanvas();
			tippspiel.dd3.setDiagrammart(2);
			tippspiel.dd3.setMargins(40, 50, 20, 30);
			tippspiel.dd3.setColorshema(2);
			tippspiel.dd3.setBackgroundGradient("#FFFFFF", "#F3F1E2");
			tippspiel.dd3.setAnimation(0); 
			tippspiel.dd3.setWerteanzeige(true);
			tippspiel.dd3.setTitle("Punkte/Spiel");
			tippspiel.dd3.setSubTitle("");
			tippspiel.dd3.showLegende(false);
			tippspiel.dd3.createDiagramm();
		}
	}
	
	// Switchen
	this.switchStats = function(user, nr)
	{
		if(!isNaN(user) || user == "a")
		{
			initializeAJAX();
			if(xmlHttp)
			{
				xmlHttp.open('POST', "/action/ajax_getUserStats.php", true);
		
				xmlHttp.onreadystatechange = function() 
				{ 
					if(xmlHttp.readyState == 4)
					{     	
						var response = unescape(xmlHttp.responseText);
						
						if(response != "false")
						{
							// Inhalt setzen
							if(document.getElementById("statistik_content"))
							{
								document.getElementById('statistik_content').innerHTML = response;
								//console.log("Daten für USer " + user + "geholt");
								// Punkte-Canvas laden
								if(tippspiel.stat_nr == 2 || tippspiel.stat_nr == 3 || tippspiel.stat_nr == 4)
								{
									//window.setTimeout("tippspiel.loadDiagramm('" + tippspiel.stat_nr + "', '" + user + "')", 100);
									tippspiel.loadDiagramm(tippspiel.stat_nr, user);
								}
							}
						}
						else
						{
							alert("Fehler bei der Verarbeitung der Daten II!");
							//location.reload();
						}
					}
					else
					{
						
					}
				}
				
				xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
				
				// Registerkarte setzen
				if(document.getElementById("stat_menu" + this.stat_nr))
				{
					document.getElementById("stat_menu" + this.stat_nr).className = "statistik_menu_inactive";
				}
				
				// Nr speichern
				if(!isNaN(nr) && nr != "")
					this.stat_nr = nr;				
				
				// User setzen
				if(!isNaN(user) && user != "")
					this.stat_user = user;
				else
					user = this.stat_user;
				
				if(document.getElementById("stat_menu" + this.stat_nr))
				{
					document.getElementById("stat_menu" + this.stat_nr).className = "statistik_menu_active";
				}
				
				// Anfrage senden
				xmlHttp.send("&do=loadStat&user=" + user + "&stat=" + this.stat_nr);
				
			}
		}/**/
	}
	
	// Popup Fenster mit Statistiken laden
	this.showStats = function(user, nr)
	{
		if(!isNaN(user))
		{
			initializeAJAX();
			if(xmlHttp)
			{
				xmlHttp.open('POST', "/action/ajax_getUserStats.php", true);
		
				xmlHttp.onreadystatechange = function() 
				{ 
					if(xmlHttp.readyState == 4)
					{     	
						var response = unescape(xmlHttp.responseText);
						
						if(response != "false")
						{
							// Inhalt setzen
							spop.setPopupsettings("stats");
							spop.setContent(response);
							
							// Statistik nachladen
							tippspiel.switchStats(user, tippspiel.user_stat);
						}
						else
						{
							//alert("Fehler bei der Verarbeitung der Daten!");
							location.reload();
						}
					}
					else
					{
						spop.setLoader();
					}
				}
				
				xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				
				// Titel setzen
				spop.setPopupTitle("Statistik");
				spop.setPopupIcon("/images/icon/chart_curve.png");
				spop.showPopup();
				
				if(!isNaN(nr))
					this.stat_nr = nr;
				
				// User setzen
				if(!isNaN(user))
					this.stat_user = user;
				
				// Anfrage senden Und Fenster einblenden
				xmlHttp.send("&do=showStat&user=" + user + "&stat=" + nr);
				
			}
		}
	}
	
	// Tippspiel Tabelle filtern
	this.filterTable = function(what)
	{
		// Alle TRs durchlaufen
		for(var i=0; i<document.getElementsByTagName("tr").length; i++)
		{
			var id = document.getElementsByTagName("tr")[i].id;
			
			var arr = id.split("_");
			var typ = arr[arr.length-1];
			//console.log(id + " " + typ);
			
			if(typ == "heute" || typ == "vergangen" || typ == "zukunft")
			{
				switch(what)
				{
					case "alle":
						document.getElementsByTagName("tr")[i].style.display = "";
					break;
					
					case "heute":
					case "vergangen":
					case "zukunft":
						if(typ == what)
							document.getElementsByTagName("tr")[i].style.display = "";
						else
							document.getElementsByTagName("tr")[i].style.display = "none";
					break;/**/
					
					default:
						document.getElementsByTagName("tr")[i].style.display = "";
						
				}
			}
		}
	}
	
	
	// Tippspiel-Auswahl
	this.setTippspielauswahl = function()
	{		
		initializeAJAX();
		if(xmlHttp)
		{
			xmlHttp.open('POST', "/action/ajax_setTippspielauswahl.php", true);
	
			xmlHttp.onreadystatechange = function() 
			{ 
				if(xmlHttp.readyState == 4)
				{   
					var response = unescape(xmlHttp.responseText);
					if(response != "false")
					{
						location.reload();
						/*if(what == "blandabfrage")
						{
							// Titel setzen
							spop.setPopupTitle("Tippspielanmeldung");
							spop.showPopup();
							spop.setContent(response);
						}
						else
						{
							location.reload();
						}*/
					}
					else
					{
						alert("Fehler bei der Auswahl des Tippspiels");
					}
				}
			}
			
			xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			idturnier = document.getElementById("turnierauswahl").value;
			if(!isNaN(idturnier))
			{
				// Bundeslandabfrage
				/*if(what == "blandabfrage")
				{
					
				}*/
				xmlHttp.send("&idturnier=" + idturnier);
			}
		}
	
	}
	
	
	// Tipps abgeben => Überprüfen und markieren
	this.checkTipp = function(idtu)
	{
		var t1 = document.getElementById(idtu + "_tipp1").value;
		var t2 = document.getElementById(idtu + "_tipp2").value;
		
		var error = false;
		if(!isNaN(t1) || t1 == "")
		{
			document.getElementById(idtu + "_tipp1").style.backgroundColor = '#FFF';
		}
		else
		{
			document.getElementById(idtu + "_tipp1").style.backgroundColor = 'red';
			error = true;
		}
		if(!isNaN(t2) || t2 == "")
		{
			document.getElementById(idtu + "_tipp2").style.backgroundColor = '#FFF';
		}
		else
		{
			document.getElementById(idtu + "_tipp2").style.backgroundColor = 'red';
			error = true;
		}
		
		document.getElementById(idtu + "_team1").className = '';
		document.getElementById(idtu + "_team2").className = '';
		if(error == false)
		{
			if(parseInt(t1) > parseInt(t2))
			{
				document.getElementById(idtu + "_team1").className= 'green';
				document.getElementById(idtu + "_team2").className = 'red';
			}
			if(parseInt(t1) < parseInt(t2))
			{
				document.getElementById(idtu + "_team1").className = 'red';
				document.getElementById(idtu + "_team2").className = 'green';
			}
		}
		//console.log(idtu);
	}
}
