/* Insert a flash player into a document */
function embedSong(song) {
  document.writeln('<embed src="audio/mp3player.swf" width="300" height="68" bgcolor="#FFFFFF"');
  document.writeln('type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"');
  document.writeln('flashvars="file=', song, '&backcolor=0xFFFFFF&frontcolor=0x000000&showeq=true&volume=40" />');
}

/* Get the size of the document body */
function getBodySize() {
  return [document.body.scrollWidth, document.body.scrollHeight];
}

/* Find the mouse position */
function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
      curtop = obj.offsetTop
      while (obj = obj.offsetParent) {
        curleft += obj.offsetLeft
          curtop += obj.offsetTop
      }
  }
  return [curleft,curtop];
}

function calculatePosition(large_image) {
  image = large_image.thumbnail;
  if (large_image.width > large_image.height) {
    if (large_image.width > 640) {
      large_image.height = large_image.height / large_image.width * 640;
      large_image.width = 640;
    }
  } else {
    if (large_image.height > 640) {
      large_image.width = large_image.width / large_image.height * 640;
      large_image.height = 640;
    }
  }
  relative_left = image.offsetLeft - (large_image.width - image.width) / 2;
  relative_top = image.offsetTop - (large_image.height - image.height) / 2;
  var pos = findPos(image.parentNode);
  body_W = body_W_H[0] - 10; // -10 for some padding-like effect
  body_H = body_W_H[1] - 10;
  if (relative_left + large_image.width + pos[0] > body_W ) {
    relative_left = body_W - large_image.width - pos[0];
  }
  if (relative_top + large_image.height + pos[1] > body_H) {
    relative_top = body_H - large_image.height - pos[1];
  }
  if (relative_left + pos[0] < 10) {
    relative_left = 10 - pos[0];
  }
  if (relative_top + pos[1] < 10) {
    relative_top = 10 - pos[1];
  }
  return [relative_left, relative_top];
}

function switchPictureHelper() {
  offset = calculatePosition(this);
  this.style.left = offset[0];
  this.style.top = offset[1];
}

body_W_H = null;
function switchPicture() {
  body_W_H = getBodySize();
  var images = this.getElementsByTagName('img');
  var image = images[0];
  if (images.length == 1) {
    /* Only one image; show the large image then */
    var large_image = document.createElement('img');
    large_image.alt = image.alt;
    large_image.title = '';
    large_image.style.position = 'absolute';
    large_image.thumbnail = image;
    large_image.src = this.getAttribute('large');
    large_image.onload = switchPictureHelper;

   offset = calculatePosition(large_image);
   large_image.style.left = offset[0];
   large_image.style.top = offset[1];


   this.appendChild(large_image);
    tip_elt = this.getElementsByTagName('div');
    if (tip_elt.length > 0) {
      t = tip_elt[0];
      t.innerHTML = 'Click to restore';
      t.style.visibility = 'visible';
    }
    this.parentNode.style.zIndex = 20;
  } else {
    this.removeChild(images[1]);
    tip_elt = this.getElementsByTagName('div');
    if (tip_elt.length > 0) {
      this.removeChild(tip_elt[0]);
    }
    this.parentNode.style.zIndex = 0;
  }
}


function makeTip() {
  var tip = document.createElement('div');
  tip.innerHTML = 'Click to enlarge';
  tip.style.background = 'pink';
  tip.style.opacity = 0.8;
  tip.style.filter = 'alpha(opacity=80)';
  tip.style.position = 'absolute';
  tip.style.width = '8em';
  tip.style.zIndex = 10;
  tip.style.padding = 0;
  tip.style.margin = 0;
  tip.style.fontSize = '0.8em';
  tip.style.color = 'black';
  // This initial position should be fixed.
  tip.style.left = 90;
  tip.style.top = '50px';
  return tip;
}

function showTip() {
  tip_elt = this.getElementsByTagName('div');
  if (tip_elt.length == 0) {
    t = makeTip();
    this.appendChild(t);
  } else {
    if (tip_elt[0].show == 1) {
      tip_elt[0].style.visibility = 'visible';
    }
  }
}

function hideTip() {
  tip_elt = this.getElementsByTagName('div');
  if (tip_elt.length > 0) {
    tip_elt[0].style.visibility = 'hidden';
  }
}

function moveTip(e) {
  var posx = 0;
  var posy = 0;
  if (!e) var e = window.event;
  if (e.pageX || e.pageY) 	{
    posx = e.pageX;
    posy = e.pageY;
  }
  else if (e.clientX || e.clientY) 	{
    posx = e.clientX + document.body.scrollLeft
      + document.documentElement.scrollLeft;
    posy = e.clientY + document.body.scrollTop
      + document.documentElement.scrollTop;
  }
  pos = findPos(this);
  tip_elt = this.getElementsByTagName('div');
  if (tip_elt.length > 0) {
    tip_elt[0].style.left = posx - pos[0] + 5;
    tip_elt[0].style.top = posy - pos[1];
    tip_elt[0].style.visibility = 'visible';
  } else {
    this.appendChild(makeTip());
  }
}

function downButton() {
  img = this.getElementsByTagName('img');
  this.className = "button_down";
  img[0].style.display = 'none';
  img[1].style.display = 'inline';
  // The following hack is necessary for IE to show it in the status bar
  li = this.getElementsByTagName('a');
  window.defaultStatus = li[0].href;
}
function upButton() {
  img = this.getElementsByTagName('img');
  this.className = "button";
  img[1].style.display = 'none';
  img[0].style.display = 'inline';
  window.defaultStatus = '';
}

function initButton() {
  elt = document.getElementsByTagName('span');
  for (var i = 0; i < elt.length; i++) {
    if (elt[i].className == 'button') {
      img = elt[i].getElementsByTagName('img');
      if (img.length > 1) {
        elt[i].onmouseover = downButton;
        elt[i].onmouseout = upButton;
      }
    }
  }
}

function makeChineseButton(link, style, text_1, text_2) {
  quoted_text_1 = "'" + text_1 + "'";
  quoted_text_2 = "'" + text_2 + "'";
  str = '<a class="ver" href="' + link + '">' +
'<div style="' + style + '" class="ver">' + text_1 + '</div></a>';
  document.writeln(str);
}




