// go to archive
function goArchive() {
	var archive_menu = document.getElementById('archive');
	if ( archive_menu.selectedIndex != 0 ) {
		document.location.href = "http://" + document.domain + "/schedule.jsp?md=" + archive_menu.options[archive_menu.selectedIndex].value;
	}
	
}

// generic tab function
// tab(this_tab = id of active tab, arr_tabs = name of tab array to clear, [img_tabs = true/false] );
function tab() {
	var this_tab = arguments[0];
	var arr_tabs = arguments[1];
	var img_tabs = arguments[2];
	var curr_tab;
	

	// reset all tabs
	for(var i=0; i<arr_tabs.length; i++) {
		document.getElementById(arr_tabs[i]).style.display = 'none';
		curr_tab = document.getElementById('tab_'+arr_tabs[i]);
		if(img_tabs == true) {
			curr_tab     = curr_tab.getElementsByTagName('img')[0];
			curr_tab.src = curr_tab.src.replace("_on","");
		}
		else {
			curr_tab.className = curr_tab.className.replace("current","");
		}
	}
	
	// show and style current tab
	document.getElementById(this_tab).style.display = 'block';
	curr_tab = document.getElementById('tab_'+this_tab);
	curr_tab.blur();
	if(img_tabs == true) {
		curr_tab     = curr_tab.getElementsByTagName('img')[0];
		curr_tab.src = curr_tab.src.replace(".gif","_on.gif");
	}
	else {
		curr_tab.className = (curr_tab.className.length > 0) ? curr_tab.className + " current" : "current";
	}
}

function tabload(arr) {
	var tabimgs = new Array(arr.length * 2);
	var imgpath = (arguments.length == 1) ? '/images/btn_' : arguments[1] + '/btn_';
	for(var t=0; t<arr.length; t++) { 
		var tabimg = new Image();
		tabimg.src = imgpath + arr[t] + '.gif';
		var tabimg = new Image();
		tabimg.src = imgpath + arr[t] + '_on.gif';
	}
}


// ON DEMAND GRID
function Grid(gridId) {
	this.gridId           = gridId;
	this.elGrid           = document.getElementById(gridId).getElementsByTagName('table')[0];
	this.elGridBody       = ( !!!this.elGrid.getElementsByTagName('tbody')[0] ) ? document.createElement('tbody') : this.elGrid.getElementsByTagName('tbody')[0];
	this.elGrid.className = "epg"; 
	this.oGridData;
}

Grid.prototype.writeGrid = function() {

	this.oGridData   = eval( this.gridId );

	// ARCHIVE
	if ( this.oGridData.showArchiveOrBlurb == 'archive' ) {

		for ( var i = 0; i < this.oGridData.matches.length - 1; i++ ) {
			var match = this.oGridData.matches[i];

			var elMatchCell                 = document.createElement('td');
			elMatchCell.className           = 'match';
			elMatchCell.innerHTML           = match.player1_name + match.player1_seed + " vs " +  match.player2_name + match.player2_seed;

			var elMatchArchiveCell          = document.createElement('td');
			elMatchArchiveCell.className    = 'archive';
			elMatchArchiveCell.innerHTML    = ( match.hasArchiveVideo == false ) ? ( match.isArchivePending == true ) ? "Archive coming soon" : "&nbsp;" : "<a href=\"" + match.archiveVideoUrl + "\"><span>&raquo;</span> Watch Now</a>";

			//var elMatchHighlightsCell       = document.createElement('td');
			//elMatchHighlightsCell.className = 'highlights';
			//elMatchHighlightsCell.innerHTML = ( match.hasHighlights == false ) ? ( match.isHighlightsPending == true ) ? "Highlights coming soon" : "&nbsp;" : "<a href=\"" + match.highlightsUrl + "\"><span>&raquo;</span> Watch Now</a>";

			var elMatchRow       = document.createElement('tr');
			elMatchRow.className = ( i % 2 == 0 ) ? "" : "zebra"; 
			elMatchRow.appendChild( elMatchCell );
			elMatchRow.appendChild( elMatchArchiveCell );
			//elMatchRow.appendChild( elMatchHighlightsCell );

			this.elGridBody.appendChild( elMatchRow );
			
		}
	}

	// BLURB
	else if ( this.oGridData.showArchiveOrBlurb == 'blurb' ) {
		var elMatchCell                 = document.createElement('td');
		elMatchCell.className           = 'empty';
		elMatchCell.innerHTML           = this.oGridData.blurb;

		var elMatchRow       = document.createElement('tr');
		elMatchRow.appendChild( elMatchCell );
		this.elGridBody.appendChild( elMatchRow );
	}

	this.elGrid.appendChild( this.elGridBody );
}

Grid.prototype.writeEmptyGrid = function() {
	var elEmptyGridCell       = document.createElement('td');
	elEmptyGridCell.className = "empty";
	elEmptyGridCell.setAttribute("colspan","3");
	elEmptyGridCell.innerHTML = "There are no matches for today. Please select another day."

	var elMatchRow       = document.createElement('tr');
	elMatchRow.appendChild( elEmptyGridCell );

	this.elGridBody.appendChild( elMatchRow );

	this.elGrid.appendChild( this.elGridBody );
} 