// Variables for the tooltip.
var divTOOLTIP;
var elemCLICKED;
var boolINITED = 0;
var arrTOOLTIPcoords;
var arrTOOLTIP_NewCoords;
var intTOOLTIPoffsetTop  = 18;
var intTOOLTIPoffsetLeft = 15;
var intTOOLTIPspecialLeftOffset = 0;
var divTARGETname;
var PREVCLICKED = 0;
var ImageCount = 13;
var TIMEDOUTmessage = '<strong>We apologize, but this question may have taken too long to answer causing the server to quit before finishing.  Please try again and if this happens again, please <a href="mailto:fs@hockey-reference.com">let us know what you were trying to do</a>.</strong>';

// We store the current location of the tooltip in an array.
function initTooltip() {
    divTOOLTIP = document.getElementById("tooltip");
    if (boolINITED != 1) {
	arrTOOLTIPcoords = findPos(divTOOLTIP);
	// Preset some of the style data for later use.
	divTOOLTIP.style.margin          = "0em";
	divTOOLTIP.style.padding         = "10px";
	//divTOOLTIP.style.paddingLeft     = "1em";
    }	
    boolINITED = 1;
}

// Run the Ajax request for this tooltip request.
function getTooltipData(url, params) {
    initTooltip();
    arrTOOLTIP_NewCoords = findPos(elemCLICKED);
    //alert('here');

    var newAjax  = new Ajax.Request(url,
	{
	    method: 'get',
	    parameters: params,
	    onSuccess:  setTooltipData,
	    onLoading:  setTooltipWait,
	    onFailure:  setTooltipFailed
	}
				    );
}

// Update the tooltip with the new data.
function setTooltipData(eventData, isNotAjax) {            
    // Output the data into our tooltip.
    divTOOLTIP.style.backgroundColor = "#fff";
    divTOOLTIP.style.border          = "1px solid #aaa";
    var offsetSpecialLeft = intTOOLTIPoffsetLeft + intTOOLTIPspecialLeftOffset;
    divTOOLTIP.style.left            = arrTOOLTIP_NewCoords[0] + offsetSpecialLeft + "px";
    divTOOLTIP.style.top             = arrTOOLTIP_NewCoords[1] + intTOOLTIPoffsetTop  + "px";
    if (isNotAjax) {
	divTOOLTIP.innerHTML = eventData;
    }
    else {
	divTOOLTIP.innerHTML = eventData.responseText;
    }
    
}

// Update the tooltip in the case where our request failed.
function setTooltipFailed(eventData) {            
    divTOOLTIP.style.backgroundColor = "#fff";
    divTOOLTIP.style.border          = "1px solid #aaa";
    var offsetSpecialLeft = intTOOLTIPoffsetLeft + intTOOLTIPspecialLeftOffset;
    divTOOLTIP.style.left            = arrTOOLTIP_NewCoords[0] + offsetSpecialLeft + "px";
    divTOOLTIP.style.top             = arrTOOLTIP_NewCoords[1] + intTOOLTIPoffsetTop  + "px";
    divTOOLTIP.innerHTML = '<p class="small_text bold_text">We apologize, but this request failed.</p>' + '<span class="tooltip small_text" onclick="clearTooltipData();">[x] Close</span>\n';
}

// Update the tooltip with the new data.
function setTooltipWait(eventData) {            
    // Print out a loading page for the user.
    divTOOLTIP.style.backgroundColor = "#fff";
    divTOOLTIP.style.border          = "1px solid #aaa";
    var offsetSpecialLeft = intTOOLTIPoffsetLeft + intTOOLTIPspecialLeftOffset;
    divTOOLTIP.style.left            = arrTOOLTIP_NewCoords[0] + offsetSpecialLeft + "px";
    divTOOLTIP.style.top             = arrTOOLTIP_NewCoords[1] + intTOOLTIPoffsetTop + "px";
    divTOOLTIP.innerHTML = '<img src="/images/ajax-loader.gif" width="100" height="100">';
}

// Return the tooltip to its previous location and make it empty.
function clearTooltipData() {            
    intTOOLTIPspecialLeftOffset = 0;
    divTOOLTIP.innerHTML    = "";
    divTOOLTIP.style.border = "none";
    var offsetSpecialLeft = intTOOLTIPoffsetLeft + intTOOLTIPspecialLeftOffset;
    divTOOLTIP.style.left   = arrTOOLTIPcoords[0];
    divTOOLTIP.style.top    = arrTOOLTIPcoords[1];
}        

// http://www.quirksmode.org/js/findpos.html
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop  = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop  += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

// Create a tiny URL from a cgi script URL
function makeTinyURL(elemUserClicked, scriptURL) {
    PREVCLICKED = 1;
    intTOOLTIPspecialLeftOffset = 0;
    elemCLICKED = elemUserClicked;
    var url     = "/pp/make_tiny_url.cgi";
    var params  = "url=" + escape(scriptURL);
    getTooltipData(url, params);
}

// Print out the cumulative stats for the player.
function getCumStats(elemUserClicked, player, year, date_game, is_playoff_game) {
    PREVCLICKED = 1;
    intTOOLTIPspecialLeftOffset = 0;
    elemCLICKED = elemUserClicked;
    var url     = "/pp/pgl_cum_stats.cgi";
    var params  = "player=" + escape(player) + "&year=" + escape(year) + "&date_game=" + escape(date_game) + "&is_playoff_game=" + escape(is_playoff_game);
    getTooltipData(url, params);
}

// Set a single season on the Player Game Finder search form.
function pglFinderSetSingleSeason() {
    document.pgl_finder.year_max.selectedIndex = document.pgl_finder.year_min.selectedIndex;
}

// Set player age on the Player Game Finder search form.
function pglFinderSetAge(x, y, offset) {
    document.pgl_finder.age_min.selectedIndex = x - offset;
    document.pgl_finder.age_max.selectedIndex = y - offset;
}

// Reset the Player Game Finder additional criteria.
function pglFinderResetCriteria() {
    document.pgl_finder.c1stat.selectedIndex = 0;
    document.pgl_finder.c1comp.selectedIndex = 0;
    document.pgl_finder.c1val.value = '';
    document.pgl_finder.c2stat.selectedIndex = 0;
    document.pgl_finder.c2comp.selectedIndex = 0;
    document.pgl_finder.c2val.value = '';
    document.pgl_finder.c3stat.selectedIndex = 0;
    document.pgl_finder.c3comp.selectedIndex = 0;
    document.pgl_finder.c3val.value = '';
    document.pgl_finder.c4stat.selectedIndex = 0;
    document.pgl_finder.c4comp.selectedIndex = 0;
    document.pgl_finder.c4val.value = '';
}

// Reset the Player Game Finder search form.
function pglFinderFormReset() {
    document.pgl_finder.match[0].checked = true;
    document.pgl_finder.year_min.selectedIndex = 0;
    document.pgl_finder.year_max.selectedIndex = document.pgl_finder.year_max.length - 1;
    document.pgl_finder.age_min.selectedIndex = 0;
    document.pgl_finder.age_max.selectedIndex = document.pgl_finder.age_max.length - 1;
    document.pgl_finder.team_id.selectedIndex = 0;
    document.pgl_finder.opp_id.selectedIndex = 0;
    document.pgl_finder.game_location[2].checked = true;
    document.pgl_finder.game_result[2].checked = true;
    document.pgl_finder.pos.selectedIndex = 0;
    document.pgl_finder.c1stat.selectedIndex = 0;
    document.pgl_finder.c1comp.selectedIndex = 0;
    document.pgl_finder.c1val.value = '';
    document.pgl_finder.c2stat.selectedIndex = 0;
    document.pgl_finder.c2comp.selectedIndex = 0;
    document.pgl_finder.c2val.value = '';
    document.pgl_finder.c3stat.selectedIndex = 0;
    document.pgl_finder.c3comp.selectedIndex = 0;
    document.pgl_finder.c3val.value = '';
    document.pgl_finder.c4stat.selectedIndex = 0;
    document.pgl_finder.c4comp.selectedIndex = 0;
    document.pgl_finder.c4val.value = '';
    document.pgl_finder.order_by.selectedIndex = 3;
    document.pgl_finder.order_by_asc.checked = false;
}

// Set a single year on the Player Playoff Finder search form.
function pplFinderSetSingleYear() {
    document.ppl_finder.year_max.selectedIndex = document.ppl_finder.year_min.selectedIndex;
}

// Set years on the Player Playoff Finder search form.
function pplFinderSetYears(x, y, offset) {
    document.ppl_finder.year_min.selectedIndex = x - offset;
    document.ppl_finder.year_max.selectedIndex = y - offset;
}

// Set player age on the Player Playoff Finder search form.
function pplFinderSetAge(x, y, offset) {
    document.ppl_finder.age_min.selectedIndex = x - offset;
    document.ppl_finder.age_max.selectedIndex = y - offset;
}

// Reset the Player Playoff Finder additional criteria.
function pplFinderResetCriteria() {
    document.ppl_finder.c1stat.selectedIndex = 0;
    document.ppl_finder.c1comp.selectedIndex = 0;
    document.ppl_finder.c1val.value = '';
    document.ppl_finder.c2stat.selectedIndex = 0;
    document.ppl_finder.c2comp.selectedIndex = 0;
    document.ppl_finder.c2val.value = '';
    document.ppl_finder.c3stat.selectedIndex = 0;
    document.ppl_finder.c3comp.selectedIndex = 0;
    document.ppl_finder.c3val.value = '';
    document.ppl_finder.c4stat.selectedIndex = 0;
    document.ppl_finder.c4comp.selectedIndex = 0;
    document.ppl_finder.c4val.value = '';
}

// Reset the Player Playoff Finder search form.
function pplFinderFormReset() {
    document.ppl_finder.sum[0].checked = true;
    document.ppl_finder.year_min.selectedIndex = 0;
    document.ppl_finder.year_max.selectedIndex = document.ppl_finder.year_max.length - 1;
    document.ppl_finder.age_min.selectedIndex = 0;
    document.ppl_finder.age_max.selectedIndex = document.ppl_finder.age_max.length - 1;
    document.ppl_finder.franch_id.selectedIndex = 0;
    document.ppl_finder.is_active[2].checked = true;
    document.ppl_finder.is_hhof[2].checked = true;
    document.ppl_finder.pos.selectedIndex = 0;
    document.ppl_finder.c1stat.selectedIndex = 0;
    document.ppl_finder.c1comp.selectedIndex = 0;
    document.ppl_finder.c1val.value = '';
    document.ppl_finder.c2stat.selectedIndex = 0;
    document.ppl_finder.c2comp.selectedIndex = 0;
    document.ppl_finder.c2val.value = '';
    document.ppl_finder.c3stat.selectedIndex = 0;
    document.ppl_finder.c3comp.selectedIndex = 0;
    document.ppl_finder.c3val.value = '';
    document.ppl_finder.c4stat.selectedIndex = 0;
    document.ppl_finder.c4comp.selectedIndex = 0;
    document.ppl_finder.c4val.value = '';
    document.ppl_finder.order_by.selectedIndex = 5;
    document.ppl_finder.order_by_asc.checked = false;
}

// Set a single season on the Player Season Finder search form.
function pslFinderSetSingleSeason() {
    document.psl_finder.year_max.selectedIndex = document.psl_finder.year_min.selectedIndex;
}

// Set seasons on the Player Season Finder search form.
function pslFinderSetSeasons(x, y, offset) {
    document.psl_finder.year_min.selectedIndex = x - offset;
    document.psl_finder.year_max.selectedIndex = y - offset;
}

// Set player seasons on the Player Season Finder search form.
function pslFinderSetPlayerSeasons(x, y, offset) {
  document.psl_finder.season_start.selectedIndex = x - offset;
  document.psl_finder.season_end.selectedIndex = y - offset;
}

// Set player age on the Player Season Finder search form.
function pslFinderSetAge(x, y, offset) {
    document.psl_finder.age_min.selectedIndex = x - offset;
    document.psl_finder.age_max.selectedIndex = y - offset;
}

// Reset the Player Season Finder additional criteria.
function pslFinderResetCriteria() {
    document.psl_finder.c1stat.selectedIndex = 0;
    document.psl_finder.c1comp.selectedIndex = 0;
    document.psl_finder.c1val.value = '';
    document.psl_finder.c2stat.selectedIndex = 0;
    document.psl_finder.c2comp.selectedIndex = 0;
    document.psl_finder.c2val.value = '';
    document.psl_finder.c3stat.selectedIndex = 0;
    document.psl_finder.c3comp.selectedIndex = 0;
    document.psl_finder.c3val.value = '';
    document.psl_finder.c4stat.selectedIndex = 0;
    document.psl_finder.c4comp.selectedIndex = 0;
    document.psl_finder.c4val.value = '';
}

// Reset the Player Season Finder search form.
function pslFinderFormReset() {
    document.psl_finder.sum[0].checked = true;
    document.psl_finder.year_min.selectedIndex = 0;
    document.psl_finder.year_max.selectedIndex = document.psl_finder.year_max.length - 1;
    document.psl_finder.season_start.selectedIndex = 0;
    document.psl_finder.season_end.selectedIndex = 23;
    document.psl_finder.age_min.selectedIndex = 0;
    document.psl_finder.age_max.selectedIndex = document.psl_finder.age_max.length - 1;
    document.psl_finder.franch_id.selectedIndex = 0;
    document.psl_finder.is_active[2].checked = true;
    document.psl_finder.is_hhof[2].checked = true;
    document.psl_finder.pos.selectedIndex = 0;
    document.psl_finder.c1stat.selectedIndex = 0;
    document.psl_finder.c1comp.selectedIndex = 0;
    document.psl_finder.c1val.value = '';
    document.psl_finder.c2stat.selectedIndex = 0;
    document.psl_finder.c2comp.selectedIndex = 0;
    document.psl_finder.c2val.value = '';
    document.psl_finder.c3stat.selectedIndex = 0;
    document.psl_finder.c3comp.selectedIndex = 0;
    document.psl_finder.c3val.value = '';
    document.psl_finder.c4stat.selectedIndex = 0;
    document.psl_finder.c4comp.selectedIndex = 0;
    document.psl_finder.c4val.value = '';
    document.psl_finder.order_by.selectedIndex = 5;
    document.psl_finder.order_by_asc.checked = false;
}

function GetPlayerNames(form_id, dummy) {
    var elem = document.getElementById(escape(form_id)).elements;
    var init = elem['init' + escape(dummy)].value;
    var target = 'names' + escape(dummy);
    var url = "/pp/get_player_names.cgi";
    var params = "html=1&init=" + escape(init) + "&form_id=" + escape(form_id) + "&dummy=" + escape(dummy);
    var myAjax = new Ajax.Updater(target, url,
        {
            method: 'get',
            parameters: params
        }
    );
}

function GetPlayerYears(form_id, dummy) {
    var elem = document.getElementById(escape(form_id)).elements;
    var player = elem['p' + escape(dummy)].value;
    var target = 'years' + escape(dummy);
    var url = "/pp/get_player_years.cgi";
    var params = "html=1&player=" + escape(player) + "&form_id=" + escape(form_id) + "&dummy=" + escape(dummy);
    var myAjax = new Ajax.Updater(target, url,
        {
            method: 'get',
            parameters: params
        }
    );
}

function ClearSelection(form_id, dummy) {
    document.getElementById('names' + escape(dummy)).innerHTML = '';
    if (form_id == 'pcm_finder') {
        document.getElementById('years' + escape(dummy)).innerHTML = '';
    }
    var elem = document.getElementById(escape(form_id)).elements;
    elem['init' + dummy].selectedIndex = 0;
}
