
var pixelsPerAtBat=5;
var nullLines=3;
var pictureOffsetLeft=0;

var homeTeamName="Home:";
var visitorTeamName="Visitor:";


function fix(id,value){
   document.getElementById(id).innerHTML=value;
}

function decodePitches(x){
  var ans="";
  for (var i=0;i<x.length;i++){
  var c=x.charAt(i);
  if (c=='F'){
    ans+='foul ';
  } else if (c=='S'){
    ans+='swung ';
  } else if (c=='T'){
    ans+='taken ';
  } else if (c=='B'){
    ans+='ball ';
  }
}
return ans;
}


function logIt(evt,e)
{
    var atBat=Math.floor((e.clientX-pictureOffsetLeft)/pixelsPerAtBat);
    var line=lines[atBat+nullLines];
    var parts=line.split(',');
    
    fix('action', parts[15]+' pitches to '+parts[0]);
    fix('pitches',decodePitches(parts[4]));
    fix('result',parts[14]);
    fix('onfirst',parts[1]);
    fix('onsecond',parts[2]);
    fix('onthird',parts[3]);
    fix('homeScore',homeTeamName+parts[6]);
    fix('visitorScore',visitorTeamName+parts[7]);
    fix('outs',parts[10]);
    fix('inning',parts[11]);
     
    if (e.preventDefault)
	e.preventDefault();
    else
	e.returnValue= false;
     return false;
}

function showmesg(t)
{
   var x=document.getElementById("msg");
   x.innerHTML=t;
}

var getMyComments;

function getStuff(url, place) {
    // branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		getMyComments = new XMLHttpRequest();
		getMyComments.onreadystatechange = postStuff;
		getMyComments.urla=url;
		getMyComments.open("GET", url, true);
		getMyComments.div=place;
		getMyComments.send(null);
    // branch for IE/Windows ActiveX version
	} else {
		if (window.ActiveXObject) {
			runningMSIE = true;
			getMyComments = new ActiveXObject("Microsoft.XMLHTTP");
			if (getMyComments) {
				getMyComments.onreadystatechange = postStuff;
				getMyComments.urla=url;
				getMyComments.div=place;
				getMyComments.open("GET", url, true);
				getMyComments.send();
			}
		}
	}
}

function postStuff() { 
	if (getMyComments.readyState == 4) {
        // only if "OK"
		if (getMyComments.status == 200) {
	 		pullApartStuff(getMyComments.responseText);
		} else {
			alert("There was a problem retrieving the XML data:\n" + getMyComments.statusText);
		}
	}
} 

var lines;

function pullApartStuff(x){
    lines=x.split('\n');
	homeTeamName=lines[1].substring(3);
	visitorTeamName=lines[2].substring(3);
	homeTeamName=homeTeamName.substring(0,homeTeamName.indexOf('}'))+':';
	visitorTeamName=visitorTeamName.substring(0,visitorTeamName.indexOf('}'))+':';

}

function init(){
  getStuff(jffile,null);
  pictureOffsetLeft=document.getElementById('graph').offsetLeft;
  var drag = document.getElementById("graph");
  drag.addEventListener("mousemove",logIt , true);
  drag.addEventListner("touchmove",logIt,true);
  drag.addEventListener("click", function(evt) 
    { evt.preventDefault(); }, false);
}




