// Segment 3 neu laden, nach Load-Grafik
function loadSegment3(element) {
    x_McSegment3ClassTennis.update(element, gl_vars, gl_lang, gl_gvars, updateSegment3Response);
}

// Segment 4 neu laden, nach Load-Grafik
function loadSegment4(element) {
    x_McSegment4ClassTennis.update(element, gl_vars, gl_lang, gl_gvars, updateSegment4Response);
}

// Segment 4 Turnier incl Spielfeld+Ergebnisbaum neu laden
function loadSegment4Tournament(element1, element2, element3) { //per Turnier-ID
	tournament_selected = 	element1; // global
	year_selected		=	element2; //
	try {
	    x_McSegment4ClassTennis.updateTournament(element1, element2, element3, gl_vars, gl_lang, gl_gvars, updateSegment4TournamentResponse);
	}
	catch(e) {
		alert_error(e);
	}
}

// Segment 4 Turnier (Spielfeld+Ergebnisbaum) neu laden, Response
function updateSegment4TournamentResponse(response) {
	try {
	//	Inhalt einfügen
		addContent(document.getElementById('mc_content_modul2_segment4_ergebnisse_board'),  response[0]);
		addContent(document.getElementById('mc_content_modul2_segment4_ergebnisse_rounds'), response[1]);
		addContent(document.getElementById('mc_content_modul2_segment4_ergebnisse_tournaments_dropdowns'), response[2]);
		var round_group_id = response[2];
		selectRound(round_group_id);
	}
	catch(e) {
		alert_error(e, 'updateSegment4TournamentResponse');
	}
}

// Segment 4 Liste (Ergebnisbaum) neu laden
function loadSegment4TournamentRound(tournament_id, round_group_id, year) {
	try {
	    x_McSegment4ClassTennis.updateTournamentRound(tournament_id, round_group_id, year, gl_vars, gl_lang, gl_gvars, updateSegment4TournamentRoundResponse);
	}
	catch(e) {
		alert_error(e, 'loadSegment4TournamentRound');
	}
}

function updateSegment4TournamentRoundResponse(response) {
	try {
	//	Inhalt einfügen
		addContent(document.getElementById('mc_content_modul2_segment4_ergebnisse_rounds'), response[0]);
		divname 	= response[1];
		selectRound(divname);
	}
	catch(e) {
		alert_error(e, 'updateSegment4TournamentRoundResponse');
	}
}

// Scoreboard neu laden
function updateScoreboard() {
    x_McScoreboardTennis.update(gl_vars, gl_lang, gl_gvars, updateScoreboardResponse);
}

// Commentary neu laden, nach Load-Grafik
function loadCommentary() {
    x_McCommentaryClassTennis.update(gl_vars, gl_lang, updateCommentaryResponse);
}

// Auf neuen Kommentar prüfen, wenn ja, aktualisieren
function checkCommentary() {
    x_McCommentaryClassTennis.checkForNew(gl_vars, gl_lang, document.getElementById('lastrefresh').value, checkCommentaryResponse);
}

// PHP in_array
Array.prototype.in_array = function(needle){
	for(var i=0; i<this.length; i++){
		if(needle===this[i])
			return true;
	}
	return false;
}

/** Turnier wechseln, über Dropdownliste (onchange) */
function select_tournament_mc5(tournament_id, year, year_changed) {
	try {
		tournament_selected = tournament_id;	//global
		year_selected		= year;
	// neues Turnier laden
		loadSegment4Tournament(tournament_id, year, year_changed);
	// Zählpixel aktualisieren
	    if(document.getElementById('mc5_update_zp').value != "") {
	        updateZp();
	    }
	}
	catch(e) {
		alert_error(e, 'select_tournament_mc5');
	}
}

/** Runde eines Turniers wechseln, über 'Spielfeld' */
function select_tournament_round_mc5(tournament_id, round_group_id, year) {
	try {
		tournament_selected 	= tournament_id;	// global zuweisen
		year_selected 			= year;				//
		loadSegment4TournamentRound(tournament_id, round_group_id, year);
	// Zählpixel aktualisieren
	    if(document.getElementById('mc5_update_zp').value != "") {
	        updateZp();
	    }
	}
	catch(e) {
		alert_error(e,'select_tournament_round_mc5');
	}
}

/** beim 1. Laden: Runde highlighten */
function initRound() {
	try {
		if(typeof group_selected != "undefined" && typeof round_selected != "undefined") {
			divname 	= (group_selected != "" && round_selected.indexOf("_") == -1) ?  round_selected+"_"+group_selected : round_selected;
			round_selected = divname;
			highlight(divname);
		}
	}
	catch(e) {
		alert_error(e,'initRound');
	}
}
/** angeklickte Runde speichern und highlighten */
function selectRound(divname) { //z.B. runde1_2
	try {
		var round_group_selected_old 	= round_selected;		// z.B. achtelfinale, runde1 oder runde2_1
		var round_group_selected_new 	= divname; 				//
		round_selected 					= round_group_selected_new;	// global
		delight(round_group_selected_old);
		highlight(round_group_selected_new);
	}
	catch(e) {
		alert_error(e,'selectRound');
	}
}

/** Runde wechseln (onclick): Liste laden, gewählte Runde speichern und highlighten */
function changeRound(round_group_id, tournament_id, year) {
	try {
		select_tournament_round_mc5(tournament_id, round_group_id, year);
	}
	catch(e) {
		alert_error(e, 'changeRound');
	}
}
/** farblich markieren (mouseover) */
function highlight(divname) {
	try {
		if(document.getElementById(divname) && typeof(document.getElementById(divname)) != "undefined") {
            if(gl_client == 'sportch') {
                document.getElementById(divname).style.backgroundColor	= "#cccccc";
            } else {
                document.getElementById(divname).style.backgroundColor	= "#eb9b22";
            }
		}
	}
	catch(e) {
		alert_error(e, 'highlight');
	}
}
/** farblich normalisieren (mouseout) */
function delight(divname) {
	try {
		var round_group_selected = round_selected;
		if(group_selected != "" && round_selected.indexOf("_") == -1 && round_selected.indexOf("finale") == -1) {
			round_group_selected = round_selected+"_"+group_selected;
		}
		if(divname != round_group_selected) {
			if(document.getElementById(divname) && typeof(document.getElementById(divname)) != "undefined") {
				document.getElementById(divname).style.backgroundColor	= "";
			}
		}
	}
	catch(e) {
		alert_error(e, 'delight: '+divname);
	}
}
/** Fehlerausgabe */
function alert_error(e, functionname) {
/*	if(typeof(functionname) == "undefined") {
		alert(e);
	}
	else {
		alert(e+' (Funktion '+functionname+')');
	}
*/
}

function updateSegment1LiveScore(matchid) {
    x_McScoreboardTennis.updateLiveErgebnisse(matchid, gl_vars, gl_lang, gl_gvars, updateSegment1LiveScoreResponse);
}

function updateSegment1LiveScoreResponse(response) {
    // Inhalt einfügen
    if(response[1]) {
        addContent(document.getElementById("live_ergebnisse"), response[0]);
        addContent(document.getElementById("livescores_turnier"), response[1]);
    }
    else {
        addContent(document.getElementById("live_ergebnisse"), response);
    }
    
    // Zählpixel aktualisieren
    if(document.getElementById('mc5_update_zp').value != "") {
        updateZp();
    }
}

// Open livescores
function livescoresPopup(sport) {
    window.open("/sportal/live-ergebnisse/index.php?sport=" + sport, "livescores", "width=1022,height=690,top=0,left=0,scrollbars=auto,resizable=no,toolbar=no,location=no");
}

// Timeline neu laden
function updateTimeline() {
    x_McCommentaryClassTennis.updateTimeline(gl_vars, gl_lang, gl_gvars, updateTimelineResponse);
}

document.onload = initRound();

