function ShowOff() {
    var WSASRankTable = document.getElementById("WSASRankTable");
	var pid = getQuerystring("PID", null);
    var el;
    
    if (pid != null) {
        el = document.getElementById("pid" + pid);
    }
    if (pid == null || el == null) {
        el = WSASRankTable.getElementsByTagName("td")[0];
    }

	var fakeEventArgs = new Object();

	fakeEventArgs.target = el;
    fakeEventArgs.srcElement = el;
	MouseMoveHandler(fakeEventArgs);
}

function MouseMoveHandler(eventArgs) {
	var el;
		if (eventArgs.target) {
			el = eventArgs.target
		} else {
			el = eventArgs.srcElement;
		}
    clearHighlight();
	if (el.id.substring(0, 3) == "pid") {
        highlightedElement = el;

		var pid = el.id.substring(3,9);
        var url = "Detail.php?PID=" + pid + "&FullName=" + escape(el.innerHTML) 
            + "&Pos=" + currentPos
            + "&Year=" + currentYear + "&Week=" + currentWeek;
        //alert(url);
        makeRequest("GET", url);

        var target = document.getElementById("WSASDetailDiv");
        if (target.innerHTML == null || target.innerHTML == "" || target.innerHTML.length < 5) {
            target.innerHTML = WORKING_MESSAGES[0];
        } else {
            target.innerHTML = getRandomMessage();
            bringIntoView(WSASNav);
        }
	}
}

var WORKING_MESSAGES = new Array ("Here we go...", "Hold on, will ya?", "Not so rough! I'm workin on it!",
"Gimme a sec, will ya?", "Him? Fugghetaboutit!", "Isn't he related to Charles Bronson?",
"Isn't he in the CFL?", "Oh you think so?", "You've got HIM?! Alright then...", "Coming right up!",
"How bout you get us some beers while I query?", "BBQ!!1!11!!", "I thought he played tennis...",
"Okay, but you're not gonna like it...", "Your click is my command", "The answer is 42.",
"Dave... What do you think you're doing, Dave?", "Only if you're not a Cowboys fan...", "My brain is still warming up...",
"Did you really mean him?", "Would you like fries with that?", "Thank you, come again!",
"I've got yer stats -- right 'ere!", "Waiting muzak not found. Please tell Microsoft about this error."
);

function getRandomMessage() {
    var i = Math.floor(Math.random() * (WORKING_MESSAGES.length - 1)) + 1;
    return WORKING_MESSAGES[i];
}

var highlightedElement;
function clearHighlight() {
    if (highlightedElement != null & highlightedElement != "undefined") {
        highlightedElement.parentNode.style.backgroundColor = highlightedElement.lastBackround;
        highlightedElement = null;
    }
}

function setHighlight(el) {
    clearHighlight();
    if (el != null && el != "undefined") {
        el.lastBackround = el.parentNode.style.backgroundColor;
        el.parentNode.style.backgroundColor = "yellow";
        highlightedElement = el;
    }
}

function AlternateClickHandler(pid) {
//    clearHighlight();

    var el = document.getElementById("pid" + pid);
    setHighlight(el);

    bringIntoView(el);

}

function bringIntoView(el) {
    var PagePos = parseInt(document.body.scrollTop) || parseInt(window.pageYOffset);
    var WindowHeight = parseInt(document.body.clientHeight || window.innerHeight || document.documentElement.clientHeight)
    var ElPos = AbsoluteTop(el);

    if (ElPos < PagePos || ElPos >= PagePos + WindowHeight) {
      el.scrollIntoView();
    }
}

function makeRequest(fMethod, fUrl, fParams) {
		fMethod = fMethod.toUpperCase();
       if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }

      http_request.open(fMethod, fUrl, true);
      if (fMethod == "POST") {
      	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				http_request.setRequestHeader("Content-length", fParams.length);
				http_request.setRequestHeader("Connection", "close");
      } else {
      	fParams = null;
      }
      http_request.onreadystatechange = function() {
          reqStateChange();
      }
      http_request.send(fParams);
}
function reqStateChange() {
    var target = document.getElementById("WSASDetailDiv");
    if (http_request.readyState == 4) {
        target.innerHTML = http_request.responseText;
        setHighlight(highlightedElement);
    } else {
        var rv = " ";
        for (var i = 0; i < http_request.readyState; i++) {
            rv += ".";
        }
        target.innerHTML += rv;
    }
      //alert(http_request.readyState);
}

function AbsoluteTop(el) {
	var rv = 0;
	var o = el;
	while (o) {
		rv += o.offsetTop;
		o = o.offsetParent;
	}
	return rv;
}

function selectNav(pos) {
    var el = document.getElementById("WSAS" + pos + "Link");
    el.style.border="2px solid #0066FF";
    el.style.backgroundColor = "#A0DDFF";
    el.style.padding="4px";
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return null;
}

function getQuerystring(key, _default)
{
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return _default;
  else
    return qs[1];
}
