/* Highlight/Unhighlight the element. */
function hl (element)  {   element.style.backgroundColor = "#FFffcc"; }
function uhl (element) {   element.style.backgroundColor = ""; }

/* send us along. */
function goto_page(selectDest, strBaseUrl, f) { 
    var index = f[ selectDest ].selectedIndex;
    if (f[ selectDest ].options[index].value != "0") {
	location = strBaseUrl + f[ selectDest ].options[index].value;
    }
}

// Change location after selection from a drop-down list
function changeLocation(menuObj) {
    var i = menuObj.selectedIndex;
    if (i > 0) {
        window.location = menuObj.options[i].value;
    }
}

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

// 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:bugs@sports-reference.com">let us know what you were trying to do</a>.  Also, we update our database from 4:00-5:00 AM and that can cause the queries to stall.</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];
}

function toggleDisplay(oElm, strClassName ) {
    var strDisplayAppearStyle = '';

    if (!oElm) return;
    // Get a handle on the toggle tooltip.
    var oSpan = document.getElementById(oElm.getAttribute("id") + '_toggle');
 
    // Get a handle on all of the tags with this class in our
    // container.  This comes from prototype
    var arrElements = oElm.getElementsByClassName(strClassName);
    var strClassStatus;
    // Run thru toggling the appearance of each element.
    for (var intCount =0; intCount < arrElements.length; intCount++) {
        arrElements[intCount].style.display = 
               (arrElements[intCount].style.display == "none") ? strDisplayAppearStyle : "none";

        strClassStatus = arrElements[intCount].style.display;
    }

     // We have changed the class's display style, now change what our link shows.    
     // class not displayed we want the option to show.  class displayed option to hide.
    if (strClassStatus == "none") {
         oSpan.innerHTML = oSpan.innerHTML.replace('Hide','Show');
         oSpan.style.backgroundColor = '#FFF655';
     }
    else {
        oSpan.innerHTML = oSpan.innerHTML.replace('Show','Hide');
        oSpan.style.backgroundColor = '';
    }
}

