var INITTOP = 625;
var WINDOW = 250;
var INITBOTTOM = INITTOP + WINDOW;

function scrollPlays(speed) {
	var scrPlays = document.forms[0].scrPlays_pos.value;
	if (scrPlays == "") document.forms[0].scrPlays_pos.value = INITTOP;

	if (speed == 0)
		clearScroll();
	else
		intervalID = window.setInterval("scroll(" + speed + ");",50);
}

function scroll(speed) {
	var top = document.getElementById('topRow').offsetTop;
	var bot = document.getElementById('botRow').offsetTop;
	var div_length = bot - top;

	var scrPlays = document.forms[0].scrPlays_pos.value;

	if (speed < 0) {
		if ((INITTOP - parseInt(scrPlays)) <= (parseInt(div_length) - WINDOW))
			movePlays(speed, scrPlays);
	}
	else if (speed > 0) {
		if (INITTOP - parseInt(scrPlays) > 0 )
			movePlays(speed, scrPlays);
	}
}

function movePlays(speed, scrPlays) {
	var divObj = document.getElementById('scrPlays').style;
	divObj.top = (parseInt(speed) + parseInt(scrPlays)) + "px";
	document.forms[0].scrPlays_pos.value = (parseInt(speed) + parseInt(scrPlays));
}

function clearScroll() {
	window.clearInterval(intervalID);
}

function gotoBottom() {
	clearScroll();
	var top = document.getElementById('topRow').offsetTop;
	var bot = document.getElementById('botRow').offsetTop;
	var div_length = bot - top;
	movePlays(0, INITBOTTOM - div_length);
	
}

function gotoTop() {
	clearScroll();	
	movePlays(0, INITTOP);
	
}

function highlightTableRows(tableId, firstrow) {
	var previousClass = null;
    	var table = document.getElementById(tableId); 
    	var tbody = table.getElementsByTagName("tbody")[0];
    
    	var msg = 'View Completed Games for This Week';
    	
	if (tableId == 'in_progress') msg = 'View Live Statistics for This Game In Progress';
    	else if (tableId == 'completed') msg = 'View Game Summary for This Completed Game';
    
   	if (tbody == null) {
        	var rows = table.getElementsByTagName("tr");
    	} else {
        	var rows = tbody.getElementsByTagName("tr");
    	}

    	// add event handlers so rows light up and are clickable
    	for (i=firstrow; i < rows.length; i++) {
		rows[i].onmouseover = function() { previousClass=this.className; this.className+=' highlight'; window.status=msg};
       		rows[i].onmouseout = function() { this.className=previousClass; window.status='Done'};
       		rows[i].onclick = function() {
           		var cell = this.getElementsByTagName("td")[0];
           		var link = cell.getElementsByTagName("a")[0];
				if (link != null) {
					var week = link.getAttribute("week");
					var type = link.getAttribute("type");
					var game = link.getAttribute("game");
					var year = link.getAttribute("year");
					var stat = link.getAttribute("status");

					if (tableId == 'in_progress')
						location.href = link.getAttribute("href") + "?method=GetPlayByPlay&object=cfl_live.CFLLivePlayByPlayScoreboard&game_id=" + game + "&view_year=" + year;
       					else if (tableId == 'completed')
						location.href = link.getAttribute("href") + "?method=GetPlayByPlay&object=cfl_live.CFLLivePlayByPlayScoreboard&game_id=" + game + "&view_year=" + year + "&completed=" + stat;
					else
						location.href = link.getAttribute("href") + "?method=GetGames&object=cfl_live.CFLLivePlayByPlayScoreboard&weekNum=" + week + "&weekType=" + type;
				
					this.style.cursor="wait";
				}
       		}
    	}
}

function highlightTableCells(tableId, firstcell) {
	var previousClass = null;
    	var table = document.getElementById(tableId); 
    	var tbody = table.getElementsByTagName("tbody")[0];
    
    	var msg = 'View Completed Games for This Week';
    	
   	if (tbody == null)
        	var rows = table.getElementsByTagName("tr");
	else
        	var rows = tbody.getElementsByTagName("tr");

    	// add event handlers so rows light up and are clickable
    	for (i=0; i < rows.length; i++) {
		var cells = rows[i].getElementsByTagName("td");
		for (j=firstcell; j < cells.length; j++) {
			cells [j].onmouseover = function() { previousClass=this.className; this.className+=' highlight'; window.status=msg};
       			cells [j].onmouseout = function() { this.className=previousClass; window.status='Done'};
       			cells [j].onclick = function() {
           			//var cell = this.getElementsByTagName("td")[0];
           			var link = this.getElementsByTagName("a")[0];
				if (link != null) {
					var week = link.getAttribute("week");
					var type = link.getAttribute("type");
					var game = link.getAttribute("game");
					var year = link.getAttribute("year");
					var stat = link.getAttribute("status");

					if (tableId == 'in_progress')
						location.href = link.getAttribute("href") + "?method=GetPlayByPlay&object=cfl_live.CFLLivePlayByPlayScoreboard&game_id=" + game + "&view_year=" + year;
       					else if (tableId == 'completed')
						location.href = link.getAttribute("href") + "?method=GetPlayByPlay&object=cfl_live.CFLLivePlayByPlayScoreboard&game_id=" + game + "&view_year=" + year + "&completed=" + stat;
					else
						location.href = link.getAttribute("href") + "?method=GetGames&object=cfl_live.CFLLivePlayByPlayScoreboard&weekNum=" + week + "&weekType=" + type;
				
					this.style.cursor="wait";
				}
			}
       		}
    	}
}
