var smuGhostTargetX = 100;
var smuGhostTargetY = 100;

function smuGhostMake() {
  if (window.smu) {
    window.smu["ghost"] = new Array();
    
    window.smu.ghost["move"] = true;
    
    var ghostX = smu.cookie.get("ghostX");
    var ghostY = smu.cookie.get("ghostY");
    
    var img = document.createElement("img");
    img.src = "/images/ghost_face_right.gif";
    img.setAttribute("id", "smuGhost");
    img.style.display = "block";
    img.style.position = "absolute";
    
    img.style.left = parseInt(ghostX) + "px";
    img.style.top = parseInt(ghostY) + "px";

    img.style.zIndex = 999999;
    
    document.body.appendChild(img);
    
    window.smu.ghost["update"] = function () {
      
      var img = document.getElementById("smuGhost");
      
      if (window.smu.ghost.move) {
       
        if (window.smu.mouseX > parseInt(img.style.left)) {
          img.style.left = (parseInt(img.style.left) + 1) + "px";
          if (img.src!="/images/ghost_face_right.gif") img.src = "/images/ghost_face_right.gif";
        } else {
          img.style.left = (parseInt(img.style.left) - 1) + "px";
          if (img.src!="/images/ghost_face_left.gif") img.src = "/images/ghost_face_left.gif";
        }
        
        if (window.smu.mouseY < parseInt(img.style.top)) {
          img.style.top = (parseInt(img.style.top) - 1) + "px";
        } else {
          img.style.top = (parseInt(img.style.top) + 1) + "px";
        }
  
        smu.cookie.set("ghostX", img.style.left, 1);
        smu.cookie.set("ghostY", img.style.top, 1);
      } else {
      
        if (img.src != "/images/ghost_happy.gif") img.src = "/images/ghost_happy.gif";
      
      }

      window.setTimeout("window.smu.ghost.update()", 100);
    }
    
    window.smu.ghost["freeze"] = function(e) {
      window.smu.ghost.move = false;
    }
    
    window.smu.ghost["unfreeze"] = function(e) {
      window.smu.ghost.move = true;
    }
    
    window.smu.addEventListener(document.getElementById("smuGhost"), "mouseover", window.smu.ghost.freeze);
    window.smu.addEventListener(document.getElementById("smuGhost"), "mouseout", window.smu.ghost.unfreeze);
    window.smu.ghost.update();
  }
}

function ghostGetCookie(name) {
// Quickly borrowed from http://www.quirksmode.org/js/cookies.html

	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
