/* Rotating wheel of death (xanax) */

var wheelDelay = 100;
var wheelCurrent = 0;
var wheelTotal = 10;
var wheelImages = null;
var wheelTimer = null;
var wheelBaseUrl = null;

$(document).ready(startWheelAnimation);

function setupWheelAnimation(baseUrl)
{
    wheelBaseUrl = baseUrl;
}

function startWheelAnimation()
{
    if (!wheelImages) {
	wheelImages = new Array();
	for (var i = 0; i < wheelTotal; i++) {
	    wheelImages[i] = new Image (239, 390);
	    wheelImages[i].src = wheelBaseUrl + 'wheel' + (i + 1) + '.png';
	}
    }

    if (document.getElementById && document.getElementById('wheel')) {
	wheelTimer = window.setInterval(stepWheelAnimation, wheelDelay);
    }
}

function stopWheelAnimation()
{
    if (wheelTimer) {
	window.clearInterval(wheelTimer);
	wheelTimer = null;
    }
}

function stepWheelAnimation()
{
    document.getElementById('wheel').src = wheelImages[wheelCurrent].src;
    wheelCurrent++;
    if (wheelCurrent >= wheelTotal) wheelCurrent = 0;
}

/* Block balls (davidc) */

var blockMinBlink = 1000;
var blockMaxBlink = 6000;

 // param is the width/height, default is 128
function createBlockBall(resDir, body, size)
{
  var theBlock = $('<DIV>');

  theBlock.css('width', size + 'px');
  theBlock.css('height', size + 'px');

  theBlock.css('position', 'relative');

  $('<IMG>').attr('src', resDir + 'balls/BlockHead/body' + body + '.png').
    attr('width', size).attr('height', size).
    appendTo(theBlock);

  var scale = size / 128;

  $('<IMG>').attr('src', resDir + 'balls/BlockHead/eyeSqr.png').
    attr('width', 44 * scale).attr('height', 44 * scale).
    css('position', 'absolute').
    css('top', (40 * scale) + 'px').
    css('left', (17 * scale) + 'px').
    appendTo(theBlock);

  var pupil = $('<IMG>').attr('src', resDir + 'balls/_generic/pupil1.png').
    attr('width', 10 * scale).attr('height', 10 * scale).
    css('position', 'absolute').
    css('top', (58 * scale) + 'px').
    css('left', (35 * scale) + 'px').
    appendTo(theBlock);

  $(document).mousemove(function (e) {
      var centerX = 40 * scale;
      var centerY = 63 * scale;
      var pupilDist = 10 * scale;

      var pupilCenter = {
        x: theBlock.offset().left + centerX,
	y: theBlock.offset().top + centerY
      };

      var angle = Math.atan2(e.pageY - pupilCenter.y, e.pageX - pupilCenter.x);

      pupil.css('left', (centerX + (pupilDist * Math.cos(angle)) - (pupilDist/2)) + 'px').
	css('top',  (centerY + (pupilDist * Math.sin(angle)) - (pupilDist/2)) + 'px');
    });

  // Setup blinking

  var scheduleBlink = function () {
    window.setTimeout(blink, blockMinBlink + (Math.random() * (blockMaxBlink - blockMinBlink)));
  }

  var unblink = function () {
    pupil.show();
    scheduleBlink();
  }

  var blink = function () {
    pupil.hide();
    window.setTimeout(unblink, 100);
  }

  $(scheduleBlink);

  return theBlock;
}


/* Goofans Badges mouseover/mouseout */

var goofansCurrentBadgeHover = null;

function goofansBadgeOver(img, url)
{
  if (goofansCurrentBadgeHover) {
    goofansBadgeOut();
  }

  goofansCurrentBadgeHover = $('<DIV>').addClass('user_badge_hover');
  goofansCurrentBadgeHover.append($('<IMG>').attr('src', url));
  $(img).parent().prepend(goofansCurrentBadgeHover);
}


function goofansBadgeOut()
{
  if (goofansCurrentBadgeHover) {
    goofansCurrentBadgeHover.remove();
    goofansCurrentBadgeHover = null;
  }
}

