var dockImages = new Array();
var dock;
var doc_lock;
var dock_image_over;
var dock_mod_x = true;
var dock_mod_y = true;
var dock_stretch = 0.5; // this is the percentage of growth
var dock_profile = new Array();

// use the dock_profile array to set up the bump shape.  The numbers should all add up to 1
// and the first number should always be zero.  Each number represents the percentage drop in
// size from the next one closer to the active icon.

var dock_profiles = new Array();

dock_profiles[0] = [0, 0.5, 0.3, 0.2];
dock_profiles[1] = [0, 0.4, 0.3, 0.15, 0.10, 0.05];
dock_profiles[2] = [0, 0.7, 0.2, 0.1];
dock_profiles[3] = [0, 0.9, 0.05, 0.05];
dock_profiles[4] = [0, 0.1, 0.15, 0.2, 0.4, 0.1, 0.05];

dock_profile = dock_profiles[0];

function prepDock()
{
  dock_lock = false;
  dock = document.getElementById('dock');

  dock.onmouseout = new Function('restoreDock()');

  for (var i = 0; i < 12; i++)
  {
    name = 'img' + (i + 1);
    dockImages[i] = document.getElementById(name);
  }

  for (var i = 0; i < dockImages.length; i++)
  {
    dockImages[i].origSrc = dockImages[i].src;
    if (dockImages[i].getAttribute('onsrc'))
    {
      dockImages[i].onImage = new Image();
      dockImages[i].onImage.src = dockImages[i].getAttribute('onsrc');
    }
    dockImages[i].number = i;
    dockImages[i].onmousemove = new Function('magnify(this)');
    dockImages[i].origWidth = dockImages[i].width;
    dockImages[i].origHeight = dockImages[i].height;
    dockImages[i].isDock = true;
  }
}

function restoreDock(event)
{
  if (dock_lock) return false;
  if (window.event) event = window.event;
  if (!event) return false;
  if (!event.toElement) return false;
  if (event.toElement.isDock) return false;
  for (i = 0; i < dockImages.length; i++)
  {
    img = dockImages[i];
    img.src = img.origSrc;
    img.width = img.origWidth;
    img.height = img.origHeight;
  } 

}

function restoreDockSmooth(c)
{
  // this function is not used since it's a little buggy still.
  steps = 3;
  if (!c) c = 0;
  if (dock_lock && (c == 0)) return false;
  dock_lock = true;
  if (c == steps)
  {
  
    for (i = 0; i < dockImages.length; i++)
    {
      dockImages[i].width = dockImages[i].origWidth;
      dockImages[i].height = dockImages[i].origHeight;
    } 
    
    dock_lock = false;
    return false;
  }
  else
  {
    for (i = 0; i < dockImages.length; i++)
    {
      dockImages[i].width = (dockImages[i].origWidth * c + dockImages[i].width * (steps - c)) / steps;
      dockImages[i].height = (dockImages[i].origHeight * c + dockImages[i].height * (steps - c)) / steps;
    } 
    
  }
  
  c++;
  setTimeout('restoreDockSmooth(' + c + ')', 10);

}

function magnify(img)
{
  if (dock_lock) return false;
  dock_lock = true;
  dock_image_over = img;
  if (window.event) Event = window.event;

  if (img.onImage)
    img.src = img.onImage.src;

  // if the browser cannot detect offsetX, only change to the rollover.
  if (!Event.offsetX) 
  {
    for (var i = 0; i < dockImages.length; i++)
      if (i != img.number)
        dockImages[i].src = dockImages[i].origSrc;
    
    dock_lock = false;
    return false;
  } 

  // restore all icons we're NOT looking at
  for (var i = 0; i < dockImages.length; i++)
  {
    // ignore those within two of the selected icon
    if ((i - img.number > 3) || (i - img.number < -3))
    {
      dockImages[i].src = dockImages[i].origSrc;
      dockImages[i].width = dockImages[i].origWidth;
      dockImages[i].height = dockImages[i].origHeight;
    }
  }

  max = 1 + dock_stretch;  

  // resize the icon we are hovering over;
  if (dock_mod_x) img.width = img.origWidth * max;
  if (dock_mod_y) img.height = img.origHeight * max;
  
  Event.cancelBubble = true;
  
  // this is the distance from the left edge, in %
  percentage = Event.offsetX / img.width;
  
  if (percentage > 1) percentage = 1;
  i_percentage = 1 - percentage;
  //window.status = 'percentage: ' + percentage + ', i_percentage: ' + percentage;

  i = img.number;

  // update the 3 to the left
  max = 1 + dock_stretch;  
  for (j = 1; j < dock_profile.length; j++)
  {
    img = dockImages[i - j];

    max -= dock_stretch * dock_profile[j - 1];
    range = dock_profile[j] * dock_stretch;
  
    if (img)
    {
      img.src = img.origSrc;
      if (dock_mod_x) img.width = Math.floor(img.origWidth * (max - percentage * range));
      if (dock_mod_y) img.height = Math.floor(img.origHeight * (max - percentage * range));
    }
  }

  // update the 3 to the right
  max = 1 + dock_stretch;  
  for (j = 1; j < dock_profile.length; j++)
  {
    img = dockImages[i + j];
    
    max -= dock_stretch * dock_profile[j - 1];
    range = dock_profile[j] * dock_stretch;
  
    if (img)
    {
      img.src = img.origSrc;
      if (dock_mod_x) img.width = Math.floor(img.origWidth * (max - i_percentage * range));
      if (dock_mod_y) img.height = Math.floor(img.origHeight * (max - i_percentage * range));
    }
  }


  dock_lock = false;
}

function mouseX(e)
{
	var posx = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
		posx = e.pageX;
	else if (e.clientX || e.clientY)
		posx = e.clientX + document.body.scrollLeft;
  return posx;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
