

var mouseOvers = new Array();
var mouseOuts = new Array();
var W3CDOM = (document.createElement && document.getElementsByTagName);
var isIE = (navigator.appName=="Microsoft Internet Explorer");


function loadRollovers(){
	if (!W3CDOM) return;
	var imgs = document.getElementsByTagName('img');
	for (var i=0;i<imgs.length;i++)
	{
		if( imgs[i].getAttribute( "hasmouse" ) ){
			imgs[i].onmouseover = mouseGoesOver;
			imgs[i].onmouseout = mouseGoesOut;
			var suffix = imgs[i].src.substring(imgs[i].src.lastIndexOf('.'));
			mouseOuts[i] = new Image();
			mouseOuts[i].src = imgs[i].src;
			mouseOvers[i] = new Image();
			mouseOvers[i].src = imgs[i].src.substring(0,imgs[i].src.lastIndexOf('_')) + "_on" + suffix;
			imgs[i].number = i;
		}
	}
}

function mouseGoesOver()
{
	this.src = mouseOvers[this.number].src;
}

function mouseGoesOut()
{
	this.src = mouseOuts[this.number].src;
}



var NewWindow = new function(){
	this.openPopWindow = function( url, name, width, height  ){
		var newPop = window.open( url, name, 'width=' + width + ',height=' + height + ',screenX=100,screenY=100,scrollbars=yes,resizeable=no,location=no,status=no,directories=no,menubar=no,copyhistory=no', true );
		
		if( !newPop )
			alert( 'Please turn off your popup blocker to view this page.' );
		else
			newPop.focus();
	}
}


var StatCalc = new function(){
	
	this.getValue = function (el){
		if( el.value != null ){
			return el.value;	
		}else if( el.innerHTML != null ){
			return el.innerHTML;
		}
	}
	
	this.setValue = function(elID, val ){
		var el = document.getElementById( elID );
		
		if( el.value != null ){
			el.value = val;	
		}else if( el.innerHTML != null ){
			el.innerHTML = val;
		}
	}
	
	this.era = function( count ){
		var ipEl = document.getElementById( 'pitchInningsPitched' + count );
		var erEl = document.getElementById( 'pitchEarnedRuns' + count );
		
		var era = ( this.getValue( ipEl ) != 0 ) ? ( this.getValue( erEl ) / this.getValue( ipEl ) )*7 : 0;
		this.setValue( 'pitchEra' + count, era.toFixed( 2 ) );
		
	}
	
	this.avg = function( count ){
	
		var abEl = document.getElementById( 'hitAtBat' + count );
		var hitsEl = document.getElementById( 'hitHits' + count );
		
		var avg = ( this.getValue( abEl ) != 0 ) ? this.getValue( hitsEl )/ this.getValue( abEl ): 0;
		this.setValue(  'avg' + count, avg.toFixed( 3 ) );
	}
}



var Links = new function(){
	
	this.launchExternal = function (url){
		var result = confirm( "Attempting to load an online page.  If you are online, click ok and the page will load, " + 
		"otherwise click cancel and try again when you are online." );
		
		if( result ){
			document.location.href = url;
		}
	}
}


function $(id){
	return document.getElementById(id);
}