// Variables for the tooltip.




var years_reg = new Array (
                                "1954", "1954", "1956", "1957", "1958", "1959", 
"1960", "1961", "1962", "1963", "1964", "1965", "1966", "1967", "1968", "1969", 
"1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", 
"1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", 
"1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", 
"2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009"
);

var years_as = new Array (
"1933", "1934", "1935", "1936", "1937", "1938", "1939", 
"1940", "1941", "1942", "1943", "1944", "1945", "1946", "1947", "1948", "1949", 
"1950", "1951", "1952", "1953", "1954", "1955", "1956", "1957", "1958", "1959", 
"1960", "1961", "1962", "1963", "1964", "1965", "1966", "1967", "1968", "1969", 
"1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", 
"1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", 
"1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", 
"2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009"
);

var years_post = new Array (
			"1903", "1904", "1905", "1906", "1907", "1908", "1909", 
"1910", "1911", "1912", "1913", "1914", "1915", "1916", "1917", "1918", "1919", 
"1920", "1921", "1922", "1923", "1924", "1925", "1926", "1927", "1928", "1929", 
"1930", "1931", "1932", "1933", "1934", "1935", "1936", "1937", "1938", "1939", 
"1940", "1941", "1942", "1943", "1944", "1945", "1946", "1947", "1948", "1949", 
"1950", "1951", "1952", "1953", "1954", "1955", "1956", "1957", "1958", "1959", 
"1960", "1961", "1962", "1963", "1964", "1965", "1966", "1967", "1968", "1969", 
"1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", 
"1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", 
"1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", 
"2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008"
);


function populate(formSuffix, setTo)
{
    var box1 = document.getElementById("min_year_" + formSuffix);
    var box2 = document.getElementById("max_year_" + formSuffix);
    // Handle the two years were weren't a postseason.
    var years_disabled = new Array;
    list = new Array();
    if (setTo == 'years_as') {
	list = years_as;
    }
    else if (setTo == 'years_post') {
	list = years_post;
	years_disabled['1994'] = 1;
	years_disabled['1904'] = 1;
    }
    else {
	list = years_reg;
    }

    box1.options.length = 0;
    box2.options.length = 0;
    for(i = 0; i < list.length; i++)	{
	box1.options[i] = new Option(list[i],list[i]);
	box2.options[i] = new Option(list[i],list[i]);
	//	if (years_disabled[list[i]]) {
	//   box1.options[i].disabled = true;
	//    box2.options[i].disabled = true;
	//}
    }
}




// This is where the fade will start, if you want it to be faster and
// start with a lighter color, make this number smaller It corresponds
// directly to the FadeSteps below
var intStartFadeAt = 15;
var intFadeInterval = 60;
 
// This is list of steps that will be used for the color to fade out
var arrFadeSteps = new Array();
arrFadeSteps[1] = "ff";
arrFadeSteps[2] = "f7";
arrFadeSteps[3] = "ee";
arrFadeSteps[4] = "e6";
arrFadeSteps[5] = "dd";
arrFadeSteps[6] = "d5";
arrFadeSteps[7] = "cc";
arrFadeSteps[8] = "c4";
arrFadeSteps[9] = "bb";
arrFadeSteps[10] = "b3";
arrFadeSteps[11] = "aa";
arrFadeSteps[12] = "a2";
arrFadeSteps[13] = "99";
arrFadeSteps[14] = "91";
arrFadeSteps[15] = "88";

// This is the recursive function call that actually performs the fade
function DoFade(intColorId, strElm) {
 
    oElm = document.getElementById(strElm);
    if (!oElm) return;
 
    if (intColorId >= 1) {
        oElm.style.backgroundColor = "#ffff" +  arrFadeSteps[intColorId];
 
        // If it's the last color, set it to transparent
        if (intColorId==1) {
            oElm.style.backgroundColor = "transparent";
        }
        intColorId--;
 
        // Wait a little bit and fade another shade
        setTimeout("DoFade("+intColorId+",'"+strElm+"')", intFadeInterval);
    }
}




// found here: http://www.somacon.com/p143.php  THANKS!
function setRadioValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function getRadioValue(radioObj) {
	if(!radioObj)
	    return '';
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
	    return radioObj.value;
	}
	for(var i = 0; i < radioLength; i++) {
	    if(radioObj[i].checked == true) {
		return radioObj[i].value;
	    }
	}
}

function preShareIt (elClicked) {

    var formText;
    var txtTitle = $('qTitle').innerHTML;

    var formText = '<form id=share name=share method=post action="/play-index/saveQuery.cgi"><h3>Save and share the report you generated.</h3><P>Non-subscribers and subscribers alike can see the reports you generate.</big></P><P><b>Title of your generated report:</b><br><input type=text name=titler id=titler size=60 maxlength=255 value="'+txtTitle+'">';
    
    formText = formText + '<P><b>Notes on your report (shown with report):</b><br><textarea name=notes id=notes cols=60 rows=10></textarea>';
    formText = formText + '<br>Please keep it clean. Sorry, but no &gt; or &lt; symbols or html tags are allowed at the moment.';
    formText = formText + '<P><input type=button onclick="ShareIt();" value="Save Comments and Report!">  A link will be created for your report.</form>';

    popupTextNoURL(elClicked, formText);

}

function ShareIt () {
    var urlShare = 'http://www.baseball-reference.com/play-index/saveQuery.cgi';
    var pbody =  Form.serialize("share");

    // Add the tool used
    pbody = pbody + '&toolused=' + escape(window.location.pathname + window.location.search);


    //alert(urlShare + "?" + pbody);

    // Add the query.
    if ($('headerOut')) {
	pbody = pbody + '&headerOut=' + escape($('headerOut').innerHTML);
    }
    else {
	pbody = pbody + '&headerOut=';
    }
    // Add the query.
    if ($('dataOut')) {
	pbody = pbody + '&dataOut=' + escape($('dataOut').innerHTML);
    }
    else {
	pbody = pbody + '&dataOut=';
    }


    var newAjax  = new Ajax.Request(urlShare,
	{
	    method:     'post',
	    postBody:   pbody,
	    onSuccess:  setTooltipData,
	    onLoading:  setTooltipWait,
	    onFailure:  setTooltipFailed
	}
				    );
}

// SPLIT YEAR-BY-YEAR PRINT OUT
function getPitchSplitData(elemUserClicked) {
    var url     = "/play-index/psplit_bit.cgi";
    var params  = "html=1&p=" + escape(elemUserClicked.id);
    getSplitData( elemUserClicked, url, params);
}
function getBatSplitData(elemUserClicked) {
    var url     = "/play-index/bsplit_bit.cgi";
    var params  = "html=1&p=" + escape(elemUserClicked.id);
    getSplitData( elemUserClicked, url, params);
}
function getSplitData(elemUserClicked, url, params) {
    var arrValsPassed = elemUserClicked.id.split('|');
    // Inter-League causes problems here.
    divTARGETname   = 'fill' + arrValsPassed[ 2 ] + '_' +  arrValsPassed[ 0 ];
    var strLoading = "               <span class=tooltip>Loading....</span>\n";
    if (($(divTARGETname).innerHTML != strLoading) && 
	($(divTARGETname).innerHTML != '') &&
	(arrValsPassed[ 4 ] == undefined)) {
	$(divTARGETname).innerHTML = '';
	return;
    }

    // Use the params to get the data from the server.
    // Replace the plus signs in the params with an encoded value.
    var newParams = params.replace(/\+/g,'%2B');
    params = newParams;

    $(divTARGETname).innerHTML = strLoading;
    var newAjax  = new Ajax.Request(url,
	{
	    method: 'get',
	    parameters: params,
	    onComplete: setDivData
	}
				    );
}
function setDivData(eventData) {            
    $(divTARGETname).innerHTML = eventData.responseText + 
	"<pre><span class=closeLink onclick=\"$('" + divTARGETname + "').innerHTML='';\">Close [x]</span></pre>";
}

