﻿jQuery(document).ready(function($) {
    checkTeams();
}
);

function statsHover(caller,message)
{
	position = getPosition(caller);
	var newdiv = document.createElement('div');
	newdiv.setAttribute('class', 'stat_translation');
	newdiv.setAttribute('id','jbn_stats_hover');
	newleft = position.x;
	newtop = position.y-15;
	newleft=newleft+"px";
	newtop=newtop+"px";
	newdiv.style.left=newleft;
	newdiv.style.top=newtop;
	newdiv.innerHTML=message;
	document.body.appendChild(newdiv);
}

function killHover()
{
	var hoverer = document.getElementById('jbn_stats_hover');
	document.body.removeChild(hoverer);
}

////******Get Position***
function getPosition(e)
{
	var left = 0;
    var top  = 0;
	/** Safari fix -- thanks to Luis Chato for this! */
	if (e.offsetHeight == 0) {
		/** Safari 2 doesn't correctly grab the offsetTop of a table row
		    this is detailed here:
		    http://jacob.peargrove.com/blog/2006/technical/table-row-offsettop-bug-in-safari/
		    the solution is likewise noted there, grab the offset of a table cell in the row - the firstChild.
		    note that firefox will return a text node as a first child, so designing a more thorough
		    solution may need to take that into account, for now this seems to work in firefox, safari, ie */
		e = e.firstChild; // a table cell
	}
    while (e.offsetParent){
    	left += e.offsetLeft;
        top  += e.offsetTop;
        e     = e.offsetParent;
    }

    left += e.offsetLeft;
    top  += e.offsetTop;

    return {x:left, y:top};
}

function loadTeams(league,player)
{
	var teamCell = document.getElementById("addJew_team_cell");
	teamCell.innerHTML = "Loading Teams...";
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
	var url = "http://"+document.domain+"/wp-content/plugins/baseball-news/loadTeams.php";
	var result = jQuery.ajax({
					url: url,
				    async:false,
					type: "POST",
					data: "addJew_league="+league+"&id="+player,
					}).responseText;
	teamCell.innerHTML = result;
					
}

function checkTeams()
{
	if(document.getElementById("addJew_league"))
	{
		var selects = document.getElementsByTagName("select");
		for (var i=0; i < selects.length; i++)
		{
		   if (selects[i].getAttribute('name') == 'addJew_league')
 			{
				league = selects[i].value;
   			}
		}
		var inputs = document.getElementsByTagName("input");
		for (var i=0; i < inputs.length; i++)
		{
		   if (inputs[i].getAttribute('name') == 'id')
 			{
				player = inputs[i].value;
   			}
		}	
		loadTeams(league,player);				

	}

}
