//Date: 10/30/2006
//Andy Patel

function ge()
{
  var ea;
  for( var i = 0; i < arguments.length; i++ ) {
    var e = arguments[i];
    if( typeof e == 'string' )
      e = document.getElementById(e);
    if( arguments.length == 1 )
      return e;
    if( !ea )
      ea = new Array();
    ea[ea.length] = e;
  }
  return ea;
}

function show()
{
  for( var i = 0; i < arguments.length; i++ ) {
    var element = ge(arguments[i]);
    if (element && element.style) element.style.display = '';
  }
  return false;
}

function hide()
{
  for( var i = 0; i < arguments.length; i++ ) {
    var element = ge(arguments[i]);
    if (element && element.style) element.style.display = 'none';
  }
  return false;
}

//////////////////////////

// Get Absolute X Position of HTML Element
function findX(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;
}

// Get Absolute Y Position of HTML Element
function findY(obj)
{
  var curtop = 0;
  if(obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  }
  else if (obj.y)
    curtop += obj.y;
  return curtop;
}

function mousePosX(e)
{
  var posx = 0;
  if (!e) var e = window.event;
  if (e.pageX)
    posx = e.pageX;
  else if (e.clientX && document.body.scrollLeft)
    posx = e.clientX + document.body.scrollLeft;
  else if (e.clientX && document.documentElement.scrollLeft)
    posx = e.clientX + document.documentElement.scrollLeft;
  else if (e.clientX)
    posx = e.clientX;
  return posx;
}

function mousePosY(e)
{
  var posy = 0;
  if (!e) var e = window.event;
  if (e.pageY)
    posy = e.pageY;
  else if (e.clientY && document.body.scrollTop)
    posy = e.clientY + document.body.scrollTop;
  else if (e.clientY && document.documentElement.scrollTop)
    posy = e.clientY + document.documentElement.scrollTop;
  else if (e.clientY)
    posy = e.clientY;
  return posy;
}


///////////////////////////////////


var frameWidth = 18;
var frameHeight = 18;

function showBoxAt(percentX, percentY, picName)
{
  
  thePhoto = ge(picName);
  if( thePhoto == null )
    alert('null photo');
  thePhotoX = findX(thePhoto);
  thePhotoY = findY(thePhoto);
  
  ge('tagframeinside').innerHTML = "+ " + thePhotoX + "." + thePhotoY + "<br>";
  ge('tagframeinside').innerHTML += "% " + percentX + "." + percentY + "<br>";
  ge('tagframeinside').innerHTML += "d " + thePhoto.width + "." + thePhoto.height + "<br>";
  ge('tagframeinside').innerHTML += '= ' + thePhoto.width + "." + thePhoto.height + "<br>";
  
  newX = thePhotoX + percentX - (frameWidth/2);
  newY = thePhotoY + percentY - (frameHeight/2);
  ge('tagframeinside').innerHTML += "> " + newX + "." + newY;
  ge('tagframeinside').innerHTML = "";
  
  imageMinX = findX(thePhoto);
  imageMinY = findY(thePhoto);
  imageMaxX = imageMinX + thePhoto.width;
  imageMaxY = imageMinY + thePhoto.height;
  
  // Before constraints
  frameX = imageMinX + percentX;
  frameY = imageMinY + percentY;
  
  // Apply Constraints
  if(frameX < imageMinX + (frameWidth/2))
    frameX = imageMinX + (frameWidth/2);
  if(frameY < imageMinY + (frameHeight/2))
    frameY = imageMinY + (frameHeight/2);
  if(frameX > imageMaxX - (frameWidth/2))
    frameX = imageMaxX - (frameWidth/2);
  if(frameY > imageMaxY - (frameHeight/2))
    frameY = imageMaxY - (frameHeight/2);
//  alert("+" + frameX + " " + frameY);
  
  // Move and minus 8 for the frame border
  ge('tagframe').style.left = (frameX - (frameWidth/2) - 8) + "px";
  ge('tagframe').style.top = (frameY - (frameHeight/2) - 8) + "px";
  show('tagframe');
}

function showBoxAt1(percentX, percentY, picName)
{
  
  thePhoto = ge(picName);
  if( thePhoto == null )
    alert('null photo');
  thePhotoX = findX(thePhoto);
  thePhotoY = findY(thePhoto);
  
  ge('tagframeinside').innerHTML = "+ " + thePhotoX + "." + thePhotoY + "<br>";
  ge('tagframeinside').innerHTML += "% " + percentX + "." + percentY + "<br>";
  ge('tagframeinside').innerHTML += "d " + thePhoto.width + "." + thePhoto.height + "<br>";
  ge('tagframeinside').innerHTML += '= ' + ((percentX * thePhoto.width)/100) + "." + ((percentY * thePhoto.height)/100) + "<br>";
  
  newX = thePhotoX + ((percentX * thePhoto.width)/100) - (frameWidth/2);
  newY = thePhotoY + ((percentY * thePhoto.height)/100) - (frameHeight/2);
  ge('tagframeinside').innerHTML += "> " + newX + "." + newY;
  ge('tagframeinside').innerHTML = "";
  
  imageMinX = findX(thePhoto);
  imageMinY = findY(thePhoto);
  imageMaxX = imageMinX + thePhoto.width;
  imageMaxY = imageMinY + thePhoto.height;
  
  // Before constraints
  frameX = imageMinX + ((percentX * thePhoto.width)/100);
  frameY = imageMinY + ((percentY * thePhoto.height)/100);
  
  // Apply Constraints
  if(frameX < imageMinX + (frameWidth/2))
    frameX = imageMinX + (frameWidth/2);
  if(frameY < imageMinY + (frameHeight/2))
    frameY = imageMinY + (frameHeight/2);
  if(frameX > imageMaxX - (frameWidth/2))
    frameX = imageMaxX - (frameWidth/2);
  if(frameY > imageMaxY - (frameHeight/2))
    frameY = imageMaxY - (frameHeight/2);
//  alert("+" + frameX + " " + frameY);
  
  // Move and minus 8 for the frame border
  ge('tagframe').style.left = (frameX - (frameWidth/2) - 8) + "px";
  ge('tagframe').style.top = (frameY - (frameHeight/2) - 8) + "px";
  show('tagframe');
}

function hideBox()
{
  hide('tagframe');
}

document.write('<div id="tagframe" style="display: none;"><div id="tagframeinside" onmousedown="frameMouseDown(event);" onmouseup="frameMouseUp()"></div></div>');

