var xmlHttp8 = [];
var xmlHttp27 = [];
var minutes = [];
var seconds = [];
var resyncSeconds = [];
var skipResync = [];
var liveHomeGoals = [];
var liveAwayGoals = [];
var liveGoalsResync = [];
var liveMatchTimeResync = [];
var liveTextArray = [];
var lastShownAction = [];
var nextShownAction = [];

function print_r (array, return_val) {
    // Prints out or returns information about the specified variable  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/print_r    // +   original by: Michael White (http://getsprink.com)
    // +   improved by: Ben Bryan
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +      improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // -    depends on: echo
    // *     example 1: print_r(1, true);
    // *     returns 1: 1
    
    var output = "", pad_char = " ", pad_val = 4, d = this.window.document;    var getFuncName = function (fn) {
        var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
        if (!name) {
            return '(Anonymous)';
        }        return name[1];
    };
 
    var repeat_char = function (len, pad_char) {
        var str = "";        for (var i=0; i < len; i++) {
            str += pad_char;
        }
        return str;
    }; 
    var formatArray = function (obj, cur_depth, pad_val, pad_char) {
        if (cur_depth > 0) {
            cur_depth++;
        } 
        var base_pad = repeat_char(pad_val*cur_depth, pad_char);
        var thick_pad = repeat_char(pad_val*(cur_depth+1), pad_char);
        var str = "";
         if (typeof obj === 'object' && obj !== null && obj.constructor && getFuncName(obj.constructor) !== 'PHPJS_Resource') {
            str += "Array\n" + base_pad + "(\n";
            for (var key in obj) {
                if (obj[key] instanceof Array) {
                    str += thick_pad + "["+key+"] => "+formatArray(obj[key], cur_depth+1, pad_val, pad_char);                } else {
                    str += thick_pad + "["+key+"] => " + obj[key] + "\n";
                }
            }
            str += base_pad + ")\n";        } else if (obj === null || obj === undefined) {
            str = '';
        } else { // for our "resource" class
            str = obj.toString();
        } 
        return str;
    };
 
    output = formatArray(array, 0, pad_val, pad_char); 
    if (return_val !== true) {
        if (d.body) {
            this.echo(output);
        }        else {
            try {
                d = XULDocument; // We're in XUL, so appending as plain text won't work; trigger an error out of XUL
                this.echo('<pre xmlns="http://www.w3.org/1999/xhtml" style="white-space:pre;">'+output+'</pre>');
            }            catch (e) {
                this.echo(output); // Outputting as plain text may work in some plain XML
            }
        }
        return true;    } else {
        return output;
    }
}

function jumpToTime(startMin, startSec, jumpToMin, jumpToSec, homeTeamNo, awayTeamNo, homeHex, awayHex, homeTeamName, awayTeamName, matchNo) {
    
    // Cast startSec and jumpToSec to string
    startSec = "" + startSec;
    jumpToSec = "" + jumpToSec;

    if(startSec.length < 2) { startSec = "0" + startSec; }
    if(jumpToSec.length < 2) { jumpToSec = "0" + jumpToSec; }
    
    var startMinSec = parseFloat(startMin+"."+startSec);
    var jumpMinSec = parseFloat(jumpToMin+"."+jumpToSec);
    
    var thisMinFloat = null;
    
    for(var i=0; i<liveTextArray[matchNo].length; i++) {
        thisMinFloat = parseFloat(liveTextArray[matchNo][i][0]);
        if(thisMinFloat > startMinSec && thisMinFloat < jumpMinSec) {
            printCommentary(liveTextArray[matchNo][i], homeTeamNo, awayTeamNo, homeHex, awayHex, homeTeamName, awayTeamName, matchNo);
        }
    }

}

function getMatchTimer2(matchNo, matchServerNo, matchEngineVersion, divSelector, watchAsLive) {
	
	var result;
	var resultArray;
	var xmlHttp8temp;
	
	result = null;

    if(watchAsLive == null) {
        watchAsLive = false;
    }
    

	xmlHttp8temp=GetXmlHttpObject();

	if(xmlHttp8[matchNo] == null) {
		xmlHttp8[matchNo] = [];
	}

	xmlHttp8[matchNo][divSelector] = xmlHttp8temp;
	
	if (xmlHttp8temp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	
	if(watchAsLive == false) {
		
		var timestamp = Math.round(new Date().getTime() / 1000);

		// This function may be called from more than 1 instance (the top menu and the live page).
		// Ensure this can only be called every 1 minute
		if(liveMatchTimeResync[matchNo] == null || liveMatchTimeResync[matchNo] < (timestamp - 30)) {	
		
			liveMatchTimeResync[matchNo] = timestamp;

			// checking code goes here
			var url="/modules/includes/match/ajax/resyncMatchTimerAndScore.php";
			url=url+"?matchNo="+matchNo+"&matchServerNo="+matchServerNo+"&randNum="+Math.random();
			xmlHttp8temp.onreadystatechange=function () {
				if ((xmlHttp8temp.readyState == 4 || xmlHttp8temp.readyState == "complete") && xmlHttp8temp.responseText != "error") {
					stateChangedMatchTimerAndScore(matchNo, matchServerNo, matchEngineVersion, divSelector);
				}
			};
			
			xmlHttp8temp.open("GET",url,true);
			xmlHttp8temp.send(null);
	
			setTimeout("getMatchTimer2("+matchNo+","+matchServerNo+","+matchEngineVersion+",'"+divSelector+"');", 60000);
			
	    }
    }
}

function stateChangedMatchTimerAndScore(matchNo, matchServerNo, matchEngineVersion, divSelector)  { 

	var result;
	var resultArray;
	
	var xmlHttp8temp = xmlHttp8[matchNo][divSelector];
	
	result = xmlHttp8temp.responseText;
	
	if(result.length > 0) {
		
		result = result.split("--BREAK--");
		
		timeData = result[0].split("#");
		
		// Update the divSelectors for the match time
		for(i in minutes[matchNo]) {
			minutes[matchNo][i] = timeData[1];
			seconds[matchNo][i] = timeData[2];
		}
		
		// Now we update the match score for the divSelectors
		scoreData = result[1].split("#");
		liveHomeGoals[matchNo] = parseInt(scoreData[0], 10);
		liveAwayGoals[matchNo] = parseInt(scoreData[1], 10);
        
		if(isNaN(liveHomeGoals[matchNo]) == false && isNaN(liveAwayGoals[matchNo]) == false) {
			for(i in minutes[matchNo]) {
				if(document.getElementById("homeScoreline_"+i)) {
					document.getElementById("homeScoreline_"+i).innerHTML=liveHomeGoals[matchNo];
					document.getElementById("awayScoreline_"+i).innerHTML=liveAwayGoals[matchNo];
				}
			}
		}

	}
	
 
}

function getLiveMatchScore(zminutes, zseconds, matchNo, matchServerNo, matchEngineVersion, divSelector, updateScore) {
	
	var xmlHttp27temp;
	
	xmlHttp27temp=GetXmlHttpObject();

	if(xmlHttp27[matchNo] == null) {
		xmlHttp27[matchNo] = [];
	}

	xmlHttp27[matchNo][divSelector] = xmlHttp27temp;
	
	if (xmlHttp27temp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 
	
	// This function may be called from more than 1 instance (the top menu and the live page).
	// Ensure this can only be called every 1 minute
	if(liveGoalsResync[matchNo] == null || liveGoalsResync[matchNo] < (((parseInt(zminutes) * 60) + parseInt(zseconds)) - 30)) {	
		
		liveGoalsResync[matchNo] = (parseInt(zminutes) * 60) + parseInt(zseconds);

		// checking code goes here
		var url="/modules/includes/match/ajax/getMatchScore.php";
		url=url+"?matchNo="+matchNo+"&matchServerNo="+matchServerNo+"&mev="+matchEngineVersion+"&minutes="+zminutes+"&seconds="+zseconds+"&randNum="+Math.random();

		xmlHttp27temp.onreadystatechange=function () {
			
			if ((xmlHttp27temp.readyState == 4 || xmlHttp27temp.readyState == "complete") && xmlHttp27temp.responseText != "error") {
			
				var result;
				result = xmlHttp27temp.responseText;
				result = result.split("#");
				liveHomeGoals[matchNo] = parseInt(result[0], 10);
				liveAwayGoals[matchNo] = parseInt(result[1], 10);
		        
				if(updateScore == true && isNaN(liveHomeGoals[matchNo]) == false && isNaN(liveAwayGoals[matchNo]) == false) {
					if(document.getElementById("homeScoreline_"+divSelector)) {
						document.getElementById("homeScoreline_"+divSelector).innerHTML=liveHomeGoals[matchNo];
						document.getElementById("awayScoreline_"+divSelector).innerHTML=liveAwayGoals[matchNo];
					}
				}
			
			}
		};

		xmlHttp27temp.open("GET",url,true);
		xmlHttp27temp.send(null);
	
	} else {

		// Just use the current value
		if(document.getElementById("homeScoreline_"+divSelector)) {
			document.getElementById("homeScoreline_"+divSelector).innerHTML=liveHomeGoals[matchNo];
			document.getElementById("awayScoreline_"+divSelector).innerHTML=liveAwayGoals[matchNo];
		}
		
	}

}

function printCommentary(result, homeTeamNo, awayTeamNo, homeHex, awayHex, homeTeamName, awayTeamName, matchNo)  { 

	var newContent;
	var timeArray = result[0].split(".");
	timeArray[0] = parseInt(timeArray[0], 10);
	timeArray[1] = parseInt(timeArray[1], 10);

	if(result[1] == "SP") {
	
		newContent = "<tr><td colspan='3' style='padding-top:7px;'>"+result[2]+"</td></tr>";

		document.getElementById("matchCommentary").innerHTML = "<table width='100%' cellspacing='0px' cellpadding='0px'>" + newContent + "</table>" + document.getElementById("matchCommentary").innerHTML;

	} else {
	
	if(timeArray[0] >= 137 && timeArray[1] >= 0) {
		timeArray[0] -= 17;
	 } else if((timeArray[0] == 121 && timeArray[1] > 0) || timeArray[0] > 121) {
		timeArray[0] -= 17;
	 } else if(timeArray[0] == 121 && timeArray[1] == 0) {
		timeArray[0] -= 16;
	 } else if(timeArray[0] >= 106) {
		timeArray[0] -= 16;
	 } else if(timeArray[0] >= 60) {
		timeArray[0] -= 15;
	 }
	
	if(timeArray[0] < 10 && timeArray[1] >= 0) {
		timeArray[0] = "0" + timeArray[0];
	}
	
	if(timeArray[1] < 10) {
		timeArray[1] = "0" + timeArray[1];
	}
	
	
	switch (result[1]) {
		case "N":
			if(result[5] == homeTeamNo) {
				newContent = "<tr><td width='3%' valign='middle' align='center'><img src='/includes/matchTeamBlob.php?hex=" + homeHex + "' style='border:solid; border-width:1px; border-color:#000000;'></td><td class='matchNormal' style='padding-left:8px;' width='10%'><b>" + timeArray[0] + ":" + timeArray[1] + "</b></td><td width='87%' class='matchNormal' style='padding-right:4px;padding-left:2px;'>"+result[2]+"</td></tr>";
			} else {
				newContent = "<tr><td width='3%' valign='middle' align='center'><img src='/includes/matchTeamBlob.php?hex=" + awayHex + "' style='border:solid; border-width:1px; border-color:#000000;'></td><td class='matchNormal' style='padding-left:8px;' width='10%'><b>" + timeArray[0] + ":" + timeArray[1] + "</b></td><td width='87%' class='matchNormal' style='padding-right:4px;padding-left:2px;'>"+result[2]+"</td></tr>";	
			}
			break;
		case "G":
			var teamName;
			if(result[2] == homeTeamNo) {
				teamName = homeTeamName;	
				// Update homeGoals count
				if(isNaN(liveHomeGoals[matchNo])) {
					liveHomeGoals[matchNo] = 0;
				}
				liveHomeGoals[matchNo] = parseInt(liveHomeGoals[matchNo], 10);
				liveHomeGoals[matchNo] = liveHomeGoals[matchNo] + 1;
				document.getElementById("homeScoreline_ingame").innerHTML = liveHomeGoals[matchNo];
			} else if(result[2] == awayTeamNo) {
				teamName = awayTeamName;	
				// Update homeGoals count
				if(isNaN(liveAwayGoals[matchNo])) {
					liveAwayGoals[matchNo] = 0;
				}
				liveAwayGoals[matchNo] = parseInt(liveAwayGoals[matchNo], 10);
				liveAwayGoals[matchNo] = liveAwayGoals[matchNo] + 1;
				document.getElementById("awayScoreline_ingame").innerHTML = liveAwayGoals[matchNo];
			}
			newContent = "<tr><td width='3%' valign='middle' align='center'><img src='/includes/matchTeamBlob.php?hex=" + homeHex + "' style='border:solid; border-width:1px; border-color:#000000;'></td><td width='10%' style='background-color:#000000; color:gold; padding-left:6px; padding-top:4px;' valign='top'><b>" + timeArray[0] + ":" + timeArray[1] + "</b></td><td width='87%' style='background-color:#000000; color:gold; padding-left:4px; padding-top:4px; padding-bottom:4px;'>GOAL - " + result[4] + " (" + teamName + ")<br/>" + result[3] + "</td></tr>";
			break;
		case "Y":
			newContent = "<tr><td width='3%' valign='middle' align='center'><img src='/includes/matchTeamBlob.php?hex=" + homeHex + "' style='border:solid; border-width:1px; border-color:#000000;'></td><td width='10%' style='background-color:yellow; padding-left:6px; padding-top:4px;' valign='top'><b>" + timeArray[0] + ":" + timeArray[1] + "</b></td><td width='87%' style='background-color:yellow; padding-left:4px;padding-top:4px; padding-bottom:4px;'><img src='/images/misc/yellowCardMini.png'> "+result[2]+"</td></tr>";
			break;
		case "R":
			newContent = "<tr><td width='3%' valign='middle' align='center'><img src='/includes/matchTeamBlob.php?hex=" + homeHex + "' style='border:solid; border-width:1px; border-color:#000000;'></td><td width='10%' style='background-color:red; color:#FFFFFF; padding-left:6px; padding-top:4px;' valign='top'><b>" + timeArray[0] + ":" + timeArray[1] + "</b></td><td width='87%' style='background-color:red; color:#FFFFFF; padding-left:4px;padding-top:4px; padding-bottom:4px;'>SENT OFF<br/>"+result[2]+"</td></tr>";
			break;
		case "X":
		case "HT":
		case "HTR": 
		case "HTET": 
		case "ETS": 
		case "PNS": 
		case "HTETR":
			newContent = "<tr><td width='3%' valign='middle' align='center'>&nbsp;</td><td width='10%' style='background-color:#CCCCCC; padding-left:6px; padding-top:4px;' valign='top'><b>" + timeArray[0] + ":" + timeArray[1] + "</b></td><td width='87%' style='background-color:#CCCCCC;padding-left:4px;padding-top:4px; padding-bottom:4px;'>"+result[2]+"</td></tr>";
			break;
		default:
			newContent = "<tr><td width='3%' valign='middle' align='center'><img src='/includes/matchTeamBlob.php?hex=" + homeHex + "' style='border:solid; border-width:1px; border-color:#000000;'></td><td width='10%' style='padding-left:6px; padding-top:4px;' valign='top'><b>" + timeArray[0] + ":" + timeArray[1] + "</b></td><td width='87%' style='padding-left:4px; padding-top:5px; padding-bottom:5px;'>"+result[2]+"</td></tr>";
	}
	
	document.getElementById("matchCommentary").innerHTML = "<table width='100%' cellspacing='0px' cellpadding='0px'>" + newContent + "</table>" + document.getElementById("matchCommentary").innerHTML;

	}	

	if(result[1] == "HT" || result[1] == "HTET") {
	
		newContent = "<tr><td colspan='3'>-------------------------------------------------------------------------------</td></tr>";
		document.getElementById("matchCommentary").innerHTML = "<table width='100%' cellspacing='0px' cellpadding='0px'>" + newContent + "</table>" + document.getElementById("matchCommentary").innerHTML;
		
	} 

}


function loadLiveText(matchNo, matchServerNo, matchEngineVersion, init) {
    
    // This function will load
    // - The live text, which is added to the liveTextArray[matchNo]
    // - The live performance, which updates the screen
    
    if(init == false) {
    
		// Update the live performance data
		var teamIndex = new Array();
		teamIndex[0] = "H";
		teamIndex[1] = "A";
	
		var overallIndex = new Array();
		overallIndex[0] = "OV";
		overallIndex[1] = "DF";
		overallIndex[2] = "MF";
		overallIndex[3] = "AT";
	
		// Add the ajaxLoad icons for the livePerformance
		for (x in teamIndex) {
			for(var y=1; y<=11; y++) {
				performanceID = "performance_"+teamIndex[x]+"_"+y;
				if(document.getElementById(performanceID)) {
					document.getElementById(performanceID).innerHTML = "<img src='/images/icons/ajaxLoad2.gif' border='0px' /> <span style='color:#009900;'>Updating</span>";
				}
			}
			for (y in overallIndex) {
				performanceID = "performance_"+teamIndex[x]+"_"+overallIndex[y];
				if(document.getElementById(performanceID)) {
					document.getElementById(performanceID).innerHTML = "<img src='/images/icons/ajaxLoad4.gif' border='0px' />";
				}
			}
		}
		
    }
    
    var url = "/modules/includes/match/ajax/loadLiveText.php?matchNo="+matchNo+"&matchServerNo="+matchServerNo+"&mev="+matchEngineVersion+"&randNum="+Math.random();
    loadedLiveText = ajaxLoadURL(url, null, false);
    
    // We have 2 parts of the liveTextArray, the first part is the live text, the second is the live performance
    loadedLiveText = loadedLiveText.split("--BREAK--");
    
    // Update the live text array
    liveTextArray[matchNo] = loadedLiveText[0];
    liveTextArray[matchNo] = liveTextArray[matchNo].split("###");
    for(var i=0; i<liveTextArray[matchNo].length; i++) {
        liveTextArray[matchNo][i] = liveTextArray[matchNo][i].split("##");
    }
	
	if(init == false) {
	
		// Update the live performance text
		livePerformanceArray = loadedLiveText[1];
		livePerformanceArray = livePerformanceArray.split("+");

		for(i = 0; i < livePerformanceArray.length; i++){
			performanceValueArray = livePerformanceArray[i].split("=>");
			performanceID = "performance_" + performanceValueArray[0];
			if(document.getElementById(performanceID)) {
				document.getElementById(performanceID).innerHTML = performanceValueArray[1];
			}
		}
	
	}

}

function loadLivePerformance(matchNo, matchServerNo, matchEngineVersion) {
	/*
	var teamIndex = new Array();
	teamIndex[0] = "H";
	teamIndex[1] = "A";
	
	var overallIndex = new Array();
	overallIndex[0] = "OV";
	overallIndex[1] = "DF";
	overallIndex[2] = "MF";
	overallIndex[3] = "AT";
	
	// Add the ajaxLoad icons
	for (x in teamIndex) {
		for(var y=1; y<=11; y++) {
			performanceID = "performance_"+teamIndex[x]+"_"+y;
			if(document.getElementById(performanceID)) {
				document.getElementById(performanceID).innerHTML = "<img src='/images/icons/ajaxLoad2.gif' border='0px' /> <span style='color:#009900;'>Updating</span>";
			}
		}
		for (y in overallIndex) {
			performanceID = "performance_"+teamIndex[x]+"_"+overallIndex[y];
			if(document.getElementById(performanceID)) {
				document.getElementById(performanceID).innerHTML = "<img src='/images/icons/ajaxLoad4.gif' border='0px' />";
			}
		}
	}
	
	xmlHttpLivePerformance=GetXmlHttpObject();

	if (xmlHttpLivePerformance==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	
    var url = "/modules/includes/match/ajax/loadLivePerformance.php?matchNo="+matchNo+"&matchServerNo="+matchServerNo+"&mev="+matchEngineVersion+"&randNum="+Math.random();
    
    xmlHttpLivePerformance.onreadystatechange=function () {
		if ((xmlHttpLivePerformance.readyState == 4 || xmlHttpLivePerformance.readyState == "complete") && xmlHttpLivePerformance.responseText != "error" && xmlHttpLivePerformance.responseText != "MEMBER_ERROR") {
			livePerformanceArray = xmlHttpLivePerformance.responseText;
			livePerformanceArray = livePerformanceArray.split("+");
    
			for(i = 0; i < livePerformanceArray.length; i++){
				performanceValueArray = livePerformanceArray[i].split("=>");
				performanceID = "performance_" + performanceValueArray[0];
				if(document.getElementById(performanceID)) {
					document.getElementById(performanceID).innerHTML = performanceValueArray[1];
				}
			}
		}
	};
	xmlHttpLivePerformance.open("GET",url,true);
	xmlHttpLivePerformance.send(null);
    
    */
}

function displayStatus(init, initMins, initSecs, matchType, matchNo, matchServerNo, matchEngineVersion, divSelector, updateScore, homeTeamNo, awayTeamNo, homeHex, awayHex, homeTeamName, awayTeamName, watchAsLive) {

	var tempMinutes;
	var tempSeconds;
	
	if(init == true) {
	
	    if(initSecs <= 49) {
	        resyncSeconds[matchNo] = 50 + Math.floor(Math.random()*9);
	        skipResync[matchNo] = false;
	    } else {
	        resyncSeconds[matchNo] = 50 + Math.floor(Math.random()*9);
	        skipResync[matchNo] = initMins;
	    }
	    
		if(minutes[matchNo] == null) {
			minutes[matchNo] = [];
			seconds[matchNo] = [];
		}
		minutes[matchNo][divSelector] = initMins;
		seconds[matchNo][divSelector] = initSecs;
		
		liveTextArray[matchNo] = [];
        loadLiveText(matchNo, matchServerNo, matchEngineVersion, true);
		
		//getLiveMatchScore(minutes[matchNo][divSelector], seconds[matchNo][divSelector], matchNo, matchServerNo, matchEngineVersion, divSelector, updateScore);
	} else if(seconds[matchNo][divSelector] == 0) {
		//getLiveMatchScore(minutes[matchNo][divSelector], seconds[matchNo][divSelector], matchNo, matchServerNo, matchEngineVersion, divSelector, updateScore);
	}
	
	if(minutes[matchNo][divSelector] < 0 || (minutes[matchNo][divSelector] == 0 && seconds[matchNo][divSelector] < 0)) {
		
		tempMinutes = Math.abs(minutes[matchNo][divSelector]);
		tempSeconds = Math.abs(seconds[matchNo][divSelector]);
		if(tempMinutes < 10) {
			tempMinutes = "0" + tempMinutes;
		}
		if(tempSeconds < 10) {
			tempSeconds = "0" + tempSeconds;
		}
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>Kick off in " + tempMinutes  + ":" + tempSeconds + "</font>";	
		
	} else if(minutes[matchNo][divSelector] < 45) {
		
		tempMinutes = Math.abs(minutes[matchNo][divSelector]);
		tempSeconds = Math.abs(seconds[matchNo][divSelector]);
		if(tempMinutes < 10) {
			tempMinutes = "0" + tempMinutes;
		}
		if(tempSeconds < 10) {
			tempSeconds = "0" + tempSeconds;
		}
		
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>Current Time: <b>" +tempMinutes + "</b>:<b>" + tempSeconds + "</b></font>";	
	} else if(minutes[matchNo][divSelector] < 60) {
		
		if(seconds[matchNo][divSelector] > 49) { tempSeconds = "0" + (59-seconds[matchNo][divSelector]); } else { tempSeconds = (59-seconds[matchNo][divSelector]); }
		
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>Current Time: <b>Half Time</b> (Game restarts in "+((60-minutes[matchNo][divSelector])-1)+":"+tempSeconds+")</font>";
		
	} else if(minutes[matchNo][divSelector] < 105) {
		
		tempMinutes = Math.abs(minutes[matchNo][divSelector] - 15);
		tempSeconds = Math.abs(seconds[matchNo][divSelector]);
		
		if(tempSeconds < 10) {
			tempSeconds = "0" + tempSeconds;
		}
		
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>Current Time: <b>" + tempMinutes + "</b>:<b>" + tempSeconds + "</b></font>";
			
	} else if(minutes[matchNo][divSelector] < 106 && (matchType == "C" || matchType == "P") && liveHomeGoals[matchNo] == liveAwayGoals[matchNo] /* scores level */) {
		
		if(seconds[matchNo][divSelector] > 49) { tempSeconds = "0" + (59-seconds[matchNo][divSelector]); } else { tempSeconds = (59-seconds[matchNo][divSelector]); }
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>Current Time: <b>Full Time</b> (Extra time starts in 0:"+tempSeconds+")</font>";	
		
	} else if(minutes[matchNo][divSelector] < 106) {
		
		if(seconds[matchNo][divSelector] > 49) { tempSeconds = "0" + (59-seconds[matchNo][divSelector]); } else { tempSeconds = (59-seconds[matchNo][divSelector]); }
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>Current Time: Ending Match</font>";	
	
	} else if(minutes[matchNo][divSelector] < 121) {
		
	tempMinutes = minutes[matchNo][divSelector] - 16;
	tempSeconds = seconds[matchNo][divSelector];
	if(tempMinutes < 10) {
		tempMinutes = "0" + tempMinutes;
	}
	if(tempSeconds < 10) {
		tempSeconds = "0" + tempSeconds;
	}
	
	document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>Current Time: <b>" + tempMinutes + "</b>:<b>" + tempSeconds + "</b> (Extra time)</font>";	
	
	} else if(minutes[matchNo][divSelector] < 122) {
		
		if(seconds[matchNo][divSelector] > 49) { tempSeconds = "0" + (59-seconds[matchNo][divSelector]); } else { tempSeconds = (59-seconds[matchNo][divSelector]); }
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>Current Time: <b>Half Time</b> (Extra time restarts in 0:"+tempSeconds+")</font>";	
	} else if(minutes[matchNo][divSelector] < 137) { 
		
		tempMinutes = minutes[matchNo][divSelector] - 17;
		tempSeconds = seconds[matchNo][divSelector];
		if(tempMinutes < 10) {
			tempMinutes = "0" + tempMinutes;
		}
		if(tempSeconds < 10) {
			tempSeconds = "0" + tempSeconds;
		}
		
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>Current Time: <b>" + tempMinutes + "</b>:<b>" + tempSeconds + "</b> (Extra time)</font>";	
	
	} else if(minutes[matchNo][divSelector] == 137 && liveHomeGoals[matchNo] != liveAwayGoals[matchNo]) {
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>Current Time: Ending Match</font>";
	} else if(minutes[matchNo][divSelector] == 137) {
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>Current Time: <b>Full Time</b> (Penalty Shootout starts in 0:"+tempSeconds+")</font>";	
	} else if(minutes[matchNo][divSelector] < 152) { 
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>Current Time: Penalty Shootout in progress...</font>";	
	}

	// Here we show commentary (only for the live match text timer)
	if(init == false && divSelector == "ingame") {
	    var tempSecs = ""+seconds[matchNo][divSelector];
	    if(tempSecs.length < 2) { tempSecs = "0" + tempSecs; }
	    var commentaryMinSec = minutes[matchNo][divSelector]+"."+tempSecs;
	    
	    for(var i=0; i<liveTextArray[matchNo].length; i++) {
            if(liveTextArray[matchNo][i][0] == commentaryMinSec) {
                printCommentary(liveTextArray[matchNo][i], homeTeamNo, awayTeamNo, homeHex, awayHex, homeTeamName, awayTeamName, matchNo);
            }
        }
	}
	
	
	
	// We reload the commentary between 50 and 58 seconds, depending on the resyncSeconds value
	if(seconds[matchNo][divSelector] == resyncSeconds[matchNo]) {
	    if(skipResync[matchNo] != initMins) {
	        // We are not skipping this minute
	        resyncSeconds[matchNo] = 50 + Math.floor(Math.random()*9);
	        skipResync[matchNo] = initMins;
	        loadLiveText(matchNo, matchServerNo, matchEngineVersion, false);
	    }
	}
	
	seconds[matchNo][divSelector]++;
	if(seconds[matchNo][divSelector] == 60) {
		seconds[matchNo][divSelector] = 0;
		minutes[matchNo][divSelector]++;
	} else if(seconds[matchNo][divSelector] == 0) {
		seconds[matchNo][divSelector] = -59;
		minutes[matchNo][divSelector]++;
		
		// Case that we have gone from KO in 0:01 to 0:01
		if(minutes[matchNo][divSelector] == 1) {
			seconds[matchNo][divSelector] = 0;
			minutes[matchNo][divSelector] = 0;
		}
	}

	setTimeout("displayStatus(" + false + ", " + minutes[matchNo][divSelector] + "," + seconds[matchNo][divSelector] + ",'" + matchType + "',"+matchNo+","+matchServerNo+","+matchEngineVersion+",'"+divSelector+"', "+updateScore+", '"+homeTeamNo+"', '"+awayTeamNo+"', '"+homeHex+"', '"+awayHex+"', \""+homeTeamName+"\", \""+awayTeamName+"\");", 985);
	
}

function displayStatusMini(init, initMins, initSecs, matchType, matchNo, matchServerNo, matchEngineVersion, divSelector, updateScore) {
	
	var tempMinutes;
	var tempSeconds;
	
	if(init == true) {
		if(minutes[matchNo] == null) {
			minutes[matchNo] = [];
			seconds[matchNo] = [];
		}
		minutes[matchNo][divSelector] = initMins;
		seconds[matchNo][divSelector] = initSecs;
		
		//getLiveMatchScore(minutes[matchNo][divSelector], seconds[matchNo][divSelector], matchNo, matchServerNo, matchEngineVersion, divSelector, updateScore);
		
	} else if(seconds[matchNo][divSelector] == 0) {
		//getLiveMatchScore(minutes[matchNo][divSelector], seconds[matchNo][divSelector], matchNo, matchServerNo, matchEngineVersion, divSelector, updateScore);
	}
	
	if(minutes[matchNo][divSelector] < 0 || (minutes[matchNo][divSelector] == 0 && seconds[matchNo][divSelector] < 0)) {
		
		tempMinutes = Math.abs(minutes[matchNo][divSelector]);
		tempSeconds = Math.abs(seconds[matchNo][divSelector]);
		if(tempMinutes < 10) {
			tempMinutes = "0" + tempMinutes;
		}
		if(tempSeconds < 10) {
			tempSeconds = "0" + tempSeconds;
		}
		
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>KO in:" + tempMinutes + ":"+ tempSeconds + "</font>";	
		
	} else if(minutes[matchNo][divSelector] < 45) {
		
		tempMinutes = Math.abs(minutes[matchNo][divSelector]);
		tempSeconds = Math.abs(seconds[matchNo][divSelector]);
		if(tempMinutes < 10) {
			tempMinutes = "0" + tempMinutes;
		}
		if(tempSeconds < 10) {
			tempSeconds = "0" + tempSeconds;
		}
		
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'><b>" +tempMinutes + "</b>:<b>" + tempSeconds + "</b></font>";	
	} else if(minutes[matchNo][divSelector] < 60) {
		
		if(seconds[matchNo][divSelector] > 49) { tempSeconds = "0" + (59-seconds[matchNo][divSelector]); } else { tempSeconds = (59-seconds[matchNo][divSelector]); }
		
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'><b>HT</b> (45:00)";
		
	} else if(minutes[matchNo][divSelector] < 105) {
		
		tempMinutes = minutes[matchNo][divSelector] - 15;
		tempSeconds = seconds[matchNo][divSelector];
		
		if(tempSeconds < 10) {
			tempSeconds = "0" + tempSeconds;
		}
		
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'><b>" + tempMinutes + "</b>:<b>" + tempSeconds + "</b></font>";
			
	} else if(minutes[matchNo][divSelector] < 106 && (matchType == "C" || matchType == "P") && liveHomeGoals[matchNo] == liveAwayGoals[matchNo] /* scores level */) {
		
		if(seconds[matchNo][divSelector] > 49) { tempSeconds = "0" + (59-seconds[matchNo][divSelector]); } else { tempSeconds = (59-seconds[matchNo][divSelector]); }
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'><b>90:00</b> (ET)</font>";	
		
	} else if(minutes[matchNo][divSelector] < 106) {
		
		if(seconds[matchNo][divSelector] > 49) { tempSeconds = "0" + (59-seconds[matchNo][divSelector]); } else { tempSeconds = (59-seconds[matchNo][divSelector]); }
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>FT</font>";	
	
	} else if(minutes[matchNo][divSelector] < 121) {
		
	tempMinutes = minutes[matchNo][divSelector] - 16;
	tempSeconds = seconds[matchNo][divSelector];
	if(tempMinutes < 10) {
		tempMinutes = "0" + tempMinutes;
	}
	if(tempSeconds < 10) {
		tempSeconds = "0" + tempSeconds;
	}
	
	document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'><b>" + tempMinutes + ":" + tempSeconds + "</b> (ET)</font>";	
	
	} else if(minutes[matchNo][divSelector] < 122) {
		
		if(seconds[matchNo][divSelector] > 49) { tempSeconds = "0" + (59-seconds[matchNo][divSelector]); } else { tempSeconds = (59-seconds[matchNo][divSelector]); }
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'><b>105:00</b> (ET)</font>";	
	} else if(minutes[matchNo][divSelector] < 137) { 
		
		tempMinutes = minutes[matchNo][divSelector] - 17;
		tempSeconds = seconds[matchNo][divSelector];
		if(tempMinutes < 10) {
			tempMinutes = "0" + tempMinutes;
		}
		if(tempSeconds < 10) {
			tempSeconds = "0" + tempSeconds;
		}
		
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'><b>" + tempMinutes + "</b>:<b>" + tempSeconds + "</b> (ET)</font>";	
	
	} else if(minutes[matchNo][divSelector] == 137 && liveHomeGoals[matchNo] != liveAwayGoals[matchNo]) {
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'>FT</font>";
	} else if(minutes[matchNo][divSelector] == 137) {
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'><b>FT</b> (Penalties)</font>";	
	} else if(minutes[matchNo][divSelector] < 152) { 
		document.getElementById("matchTime_"+divSelector).innerHTML="<font style='font-size:11px;'><b>FT</b> (Penalties)</font>";	
	}
	
	seconds[matchNo][divSelector]++;
	if(seconds[matchNo][divSelector] == 60) {
		seconds[matchNo][divSelector] = 0;
		minutes[matchNo][divSelector]++;
	} else if(seconds[matchNo][divSelector] == 0) {
		seconds[matchNo][divSelector] = -59;
		minutes[matchNo][divSelector]++;
		
		// Case that we have gone from KO in 0:01 to 0:01
		if(minutes[matchNo][divSelector] == 1) {
			seconds[matchNo][divSelector] = 0;
			minutes[matchNo][divSelector] = 0;
		}
	}

	setTimeout("displayStatusMini(" + false + ", " + minutes[matchNo][divSelector] + "," + seconds[matchNo][divSelector] + ",'" + matchType + "',"+matchNo+","+matchServerNo+","+matchEngineVersion+",'"+divSelector+"', "+updateScore+");", 985);
	
}

