/*
To use this .js, add the following line between <body> and </body> in your html files.
      <script src="cx.js"></script>
   * Note: It must be in the body part otherwise Opera cannot find DocumentBody.
   * Note: This line cannot be writen as because MSIE failed to handle it.
      <script src="cx.js" />
and use this following two sets of attributes to set the note texts
      <tag1 wcIsNoteBox [wcNoteIndex[="startindex"]] [wcNoteNoList] [wcNoteList="c1"] [wcNoteItem="c2"] [wcNoteTerm="c3"]>
      <tag2 wcNoteText="note text" [wcNoteIndex="index"] [wcNoteNoList] [wcNoteNoPopup]>
   Attribute [wcIsNoteBox] indicates that, if wcnotenolist is not set, a list of note texts will be appended to the end of the element.
      It is set automatically for <body>.
   Attribute [wcNoteNoList] indicates that
      1) no note text list is generated for tag1;
      2) do not add tag2 to the list.
   Attribute wcNoteIndex indicates that
      1) the list has index started from startindex or 1;
      2) change the index of tag2 and its successors.
   Attribute wcNoteNoPopup prevents showing popup note box.
   Attributes [wcNoteList], [wcNoteItem], [wcNoteTerm] change the class for the list. They accept empty strings.

   * note: attribute names are case-sensitive in Internet Explorer.
The behavior can be changed by modifying the varibles & "constants", and CSS linked with your html files.
*/

// ============================= global variable =============================

var wcNoteBox;
var IsOpera, IsSafari, IsFirefox, IsMSIE, IsNetscape, IsOtherBrowser;
var CurrentMousePositionX=null;
var CurrentMousePositionY=null;
var DocumentBody;

// for blogger.com
  var BloggerShowStr = "";
  var BloggerHideStr = "";
  var BloggerIsItem = true;

//"constants"
var wcNoteBoxPrefix     = "";
var wcNoteBoxItemPrefix = "";
var wcNoteBoxItemInfix1 = ":";
var wcNoteBoxItemInfix2 = " ";
var wcNoteBoxitemSuffix = "";
var wcNoteBoxSuffix     = "";
var wcHintPause         = 500;
var wcHintAutoHide      = 12000;
var wcHintShortPause    = 100;
var wcHintHidePause     = 200;

// can be overrided by using <tag isnotebox wcnotelist="newclass" wcnoteitem="newclass" wcnoteitem="newclass">
var wcNoteBoxLinkClass  = "wcNoteLinkWrap";
var wcNoteBoxListClass  = "wcNoteListWrap";
var wcNoteBoxItemClass  = "wcNoteBoxItem";
var wcNoteBoxTermClass  = "wcNoteBoxTerm";
var wcNoteBoxClass      = "wcNoteBox";
var wcHideThisClass     = "wcHideThis";
var wcShowThisClass     = "wcShowThis";

// debug
var wcDebugging=true;
var wcDebugMsgClass     = "wcDebugMessage";

// internal data
var wcStrIsNoteBox      = "wcIsNoteBox";
var wcStrNoteText       = "wcNoteText";
var wcStrNoteIndex      = "wcNoteIndex";
var wcStrNoteNoList     = "wcNoteNoList";
var wcStrNoteNoPopup    = "wcNoteNoPopup";
var wcStrNoteItem       = "wcNoteItem";
var wcStrNoteTerm       = "wcNoteTerm";
var wcStrNoteList       = "wcNoteList";
var wcStrNoteLink       = "wcNoteLink";
var wcStrFullPost       = "wcFullPost";

var wcStrIsHideBox      = "wcHideBox";
var wcStrIsHideBoxTitle = "wcHideBoxTitle";
var wcStrHideBoxVisible = "wcHideBoxVisible";
var wcStrHideString     = "wcHideString";
var wcStrShowString     = "wcShowString";
var wcStrHideClass      = "wcHideClass";
var wcStrShowClass      = "wcShowClass";

var wcStrHideThis       = "[Collapse]";
var wcStrShowThis       = "[Expand]";

// should never be changed by other codes
var wcStrIDPrefix       = "wc_Hide_Box_z";
var wcStrBlogIDPrefix   = "wc_Blog_ID_z";
var wcStrNoteHandled    = "wcNoteHandled";
var wcStrHideBoxHandled = "wcHideBoxHandled";
var wcNoteBoxElement    = null;
var wcNoteBoxShowing    = false;
var wcTimeOutHide       = null;
var wcTimeOutShow       = null;
var wcDisplayShow       = "";

var     // image is in a figure box, figure is in a body box
  wcMaxFigureWidth      = 600;   // px 
  wcMinFigureWidth      = 50;    // px
  wcContainerPadding    = 2*10;  // Container paddings + borders
  wcFigureMargin        = 2*5;   // Figure margins
  wcFigurePadding       = 2*3;   // Figure paddings + borders
  wcAlignedImageMargin  = 7;     // Image margins
  wcImageMargin         = 2*1;   // Image margins
  wcImagePadding        = 2*0;   // Image paddings + borders

// ============================= General functions =============================

var tempCode;

function playmp3(n,filename)
{
//  var mp3="http://files.myopera.com/poemsky/lyricmp3/"+filename;
  var mp3=filename;
//  if (IsSafari) playmp3_3(mp3);
//  else if (IsOpera) playmp3_2(mp3);
//  else playmp3_1(mp3);
  playmp3_1(mp3);
}

function playmp3_3(mp3)
{
  var s="<object style='vertical-align:text-bottom;' type='audio/mpeg' data='"+mp3+"' width='260' height='20'>";
  s+="<param name='src' value='"+mp3+"' />";
  s+="<param name='autostart' value='false' />";
  s+="</object>";
  tempCode=s;
  document.write(s);
}

function playmp3_2(mp3)
{
  var swf="http://files.myopera.com/poemsky/swf/flvplayer.swf";
  var s="<object style='vertical-align:text-bottom;' type='application/x-shockwave-flash' data='"+swf+"' width='260' height='20'>";
  s+="<param name='src' value='"+swf+"' />";
  s+='<param name="flashvars" value="&file='+mp3+'&width=260&height=20&allowfullscreen=false">';
  s+="</object>";
  tempCode=s;
  document.write(s);
}

function playmp3_1(mp3) // not supported by Opera and Safari
{
  var swf="http://www.hotlinkfiles.com/files/925561_ol1rx/player.swf";
  var src=swf+"?soundFile="+mp3;
  var s="<object style='vertical-align:text-bottom;' type='application/x-shockwave-flash' data='"+src+"' width='240' height='20'>";
  s+="<param name='src' value='"+src+"' />";
  s+="</object>";
  tempCode=s;
  document.write(s);
}

function wcHasString(str, sub)
{
  return str.indexOf(sub) != -1;
}

function wcStartTagText(tag, theclass)
{
  return "<"+tag+" class='"+theclass+"'>";
}

function wcChangeImage(id, url)
{
   var img=document.getElementById(id);
   if (img!=null) img.src=url;
}

function wcCrapIEHasAttribute(elm, attr)
{
  var value=elm.getAttribute(attr);
  return ((value!=null) && (typeof value != "undefined"));
}

function wcHasAttribute(elm, attr)
{
  if (IsMSIE) return wcCrapIEHasAttribute(elm, attr)
  else return elm.hasAttribute(attr);
}

function wcGetAttribute(elm, attr)
{
  if (IsMSIE) return (wcCrapIEHasAttribute(elm,attr)) ? elm.getAttribute(attr) : ""
  else return (elm.hasAttribute && elm.hasAttribute(attr)) ? elm.getAttribute(attr) : "";
}

function wcSetAttribute(elm, attr, value)
{
  elm.setAttribute(attr,value);
}

function wcNullToZero(a)
{
  return a ? a : 0;
}

function wcMinAnswer(a,b,c) // return the mininum non-null number or 0
{
  a = wcNullToZero(a);
  b = wcNullToZero(b);
  c = wcNullToZero(c);
  if (a==0) a = (b!=0) ? b : c;
  if ((a>b) && (b!=0)) a = b;
  if ((a>c) && (c!=0)) a = c;
  return a;
}

function wcMidAnswer(a,b,c) // return the mininum non-null number or 0
{
  a = wcNullToZero(a);
  b = wcNullToZero(b);
  c = wcNullToZero(c);
  if (b>a) 
  {
    var d=a; a=b; b=d;
  }
  if (c>a) 
  {
    var d=a; a=c; c=d;
  }
  if (c>b) 
  {
    var d=b; b=c; c=d;
  }
  return (c==0 || b==0) ? a : b;
}

function wcWindowWidth()
{
	return wcMidAnswer(window.innerWidth,
       document.documentElement ? document.documentElement.clientWidth : 0,
       document.body ? document.body.clientWidth : 0);
}

function wcWindowHeight()
{
	return wcMinAnswer(window.innerHeight,
       document.documentElement ? document.documentElement.clientHeight : 0,
       document.body ? document.body.clientHeight : 0);
}

function wcScrollLeft()
{
	return wcMinAnswer(window.pageXOffset,
		   document.documentElement ? document.documentElement.scrollLeft : 0,
       document.body ? document.body.scrollLeft : 0);
}

function wcScrollTop() 
{
	return wcMinAnswer(window.pageYOffset,
       document.documentElement ? document.documentElement.scrollTop : 0,
       document.body ? document.body.scrollTop : 0);
}

function wcToggleElementID(id)
{
  var elm = document.getElementById(id);
  elm.style.display = (elm.style.display == "none") ? wcDisplayShow : "none";
}

function wcShowElementID(id)
{
  var elm = document.getElementById(id);
  elm.style.display = wcDisplayShow;
}

function wcForceSetVisible(elm, visible)
{
  if (visible)
  {
    var s=elm.tagName.toLowerCase();
    if (s=="span" || s=="a") elm.style.display="inline"
    else elm.style.display="block";
  } else elm.style.display = "none";
}

function wcForceSetVisibleID(id, visible)
{
  var elm = document.getElementById(id);
  wcForceSetVisible(elm,visible)
}

function wcSetVisible(elm, visible)
{
  elm.style.display = (visible) ? wcDisplayShow : "none";
}

function wcHideElementID(id)
{
  var elm = document.getElementById(id);
  elm.style.display = "none";
}

function wcShowElement(elm)
{
  elm.style.display = wcDisplayShow;
}

function wcShowElement(elm)
{
  elm.style.display = wcDisplayShow;
}

function wcHideElement(elm)
{
  elm.style.display = "none";
}

function wcPseudoShowElement(elm)
{
  elm.style.visibility = "hidden";
  elm.style.display = wcDisplayShow;
}

function wcShowPseudoVisibleElement(elm)
{
  elm.style.visibility = "visible";
  elm.style.display = wcDisplayShow;
}

function wcAppendChildElement(parent, tag, theclass, html)
{
  if (tag=="") tag="span";
  var elm = document.createElement(tag);
  elm.className = theclass;
  elm.innerHTML = html;
  parent.appendChild(elm);
  return elm;
}

function wcAppendElement(tag, theclass, html)
{
  return wcAppendChildElement(DocumentBody, tag, theclass, html);
}

function wcStrToInt(s, def)
{
  try
  {
    if (s=="") return def;
    var i=parseInt(s);
    return (isNaN(i)) ? def : i;
  } catch(err)
  {
    return def;
  }
}

function wcTrueEvent(evt)
{
  return evt = (evt) ? evt : ((window.event) ? window.event : "");
}

function wcEventTarget(evt)
{
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  if (evt)
  {
    var node;
    if (evt.target)
      node = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target
    else node = evt.srcElement;
    return (node);
  } else return "";
}

function wcGetAllElements(elm)
{
  var cNodes=elm.getElementsByTagName("*");
  if ((IsMSIE) && (cNodes.length==0))
    cNodes = elm.all;
  return cNodes
}

function wcGetPositionTop(elm)
{
  var pr   = elm.offsetParent;
  var top  = elm.offsetTop;
  while (pr)
  {
    top  += pr.offsetTop;
    pr   =  pr.offsetParent;
  }
  return top;
}

function wcGetPositionLeft(elm)
{
  var pr   = elm.offsetParent;
  var left = elm.offsetLeft;
  while (pr)
  {
    left += pr.offsetLeft;
    pr   =  pr.offsetParent;
  }
  return left;
}

function mouseX(evt)
{
  if (evt.pageX) return evt.pageX
  else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
  else return null;
}
function mouseY(evt)
{
  if (evt.pageY) return evt.pageY
  else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?  document.documentElement.scrollTop : document.body.scrollTop);
  else return null;
}

function wcSetIEMaxWidth(elm,width)
{
  if (IsMSIE)
  {
    if (elm.style.width!="") elm.style.width="";
    if (elm.offsetWidth>width)  // used be clientWidth
      elm.style.width=width+"px";
  }
}

function wcFixIEMaxWidth(elm)
{
  if (IsMSIE)
  {
    var width=wcStrToInt(elm.style.maxWidth,-1);
    if (width<=0) return;
    wcSetIEMaxWidth(elm,width);
  }
}

// ============================= debug codes =============================

function wcAppendDebugMessage(s)
{
  if ((s=="") || !wcDebugging) return;
  wcAppendElement("div", wcDebugMsgClass, s);
}

function wcSetStatus(s)   // firefox turns this off and can be turned on only by users
{
  if (IsFirefox || IsOpera) document.title=s;
  else window.status=s;
}

function wcListAllAttributes(elm)  // not work in crappy MSIE
{
  var attrs=elm.attributes;
  var list = elm.nodeName;
  if (elm.className!="") list += "."+elm.className;
  list += " has " + attrs.length + " attributes.<br/>";

  for (var i = 0; i < attrs.length; i++)
  {
    list += "&nbsp;"+ "&nbsp;"+ attrs[i].name + "==" + attrs[i].value +"<br/>";
  }
  wcAppendElement("div", wcDebugMsgClass, list);
}

function wcPrintBrowser()
{
  if (IsOpera) document.write("Opera<br/>");
  if (IsSafari) document.write("Safari<br/>");
  if (IsFirefox) document.write("Firefox<br/>");
  if (IsMSIE) document.write("MSIE<br/>");
  if (IsNetscape) document.write("Netscape<br/>");
  if (IsOtherBrowser) document.write("Other<br/>");
}

// ============================= Genrral functions, use only once =============================

function wcGetBrowser()
{
  var s = navigator.userAgent.toLowerCase();
  IsOpera = wcHasString(s,"opera");
  IsSafari = wcHasString(s,"safari");
  IsFirefox = (IsOpera)? false: wcHasString(s,"firefox");
  IsMSIE = (IsOpera)? false: wcHasString(s,"msie");
  IsNetscape = wcHasString(s,"netscape");
  IsOtherBrowser = !(IsOpera || IsSafari || IsFirefox || IsMSIE || IsNetscape);
}

// ============================= Handy local functions =============================

function IsNoteSection(elm)
{
  return wcHasAttribute(elm,wcStrIsNoteBox);  //case-sensitive for IE
}

function GetNoteText(elm)
{
  return wcGetAttribute(elm,wcStrNoteText);
}

function SetNoteText(elm,value)
{
  wcSetAttribute(elm,wcStrNoteText,value);
}

function GetNoteParent(elm)
{
  var pr=elm;
  while ((pr!=DocumentBody) && ! IsNoteSection(pr))
  {
    pr = pr.parentNode;
  }
  return pr;
}

function IsHideBox(elm)
{
  return wcHasAttribute(elm,wcStrIsHideBox);  //case-sensitive for IE
}

function IsHideBoxTitle(elm)
{
  return wcHasAttribute(elm,wcStrIsHideBoxTitle);
}

function wcGetUniqueName(prefix,i)
{
  var s;
  var id;
  do
  {
    s = prefix + i++;
    id = document.getElementById(s);
  } while (id!=null);
  return s;
}

// ============================= major codes =============================

function AdjustImageSize(elm)
{ 
  var Nodes = wcGetAllElements(elm);
  var j=0;
  var Err=""
  for (var i = 0; i<Nodes.length; i++)
  {
    var node=Nodes[i];
      if (node.tagName.toLowerCase()!="img") continue;
    var nClass=node.className.toLowerCase();
    var align;                                   // how to adjust size
      if (nClass=="wcimgleft" || nClass=="wcimgright") align=1;
      else if (nClass=="wcimgcenter") align=2;
      else continue;
    var fNode=node.parentNode;
    var fClass=fNode.className.toLowerCase();
      if (fClass!="wcfigureleft" && fClass!="wcfigureright" && fClass!="wcfigurecenter") continue;
    var bNode=fNode.parentNode;
    j++;

    var ImgMargin = wcImageMargin + (align==1 ? wcAlignedImageMargin : 0);
    var width = node.offsetWidth;
    var fWid = fNode.offsetWidth;
    var bWid = bNode.offsetWidth-wcContainerPadding-wcFigureMargin;
    var min = wcFigurePadding + ((width+ImgMargin>wcMinFigureWidth) ? width+ImgMargin : wcMinFigureWidth); 
    var max = (bWid>wcMaxFigureWidth+wcFigurePadding) ? wcMaxFigureWidth+wcFigurePadding : bWid;
//    var s = j+": W="+node.offsetWidth+" fW="+fNode.offsetWidth+" bW="+(bNode.offsetWidth-wcContainerPadding-wcFigureMargin)+" min="+min+" max="+max;

    if (align==1) fWid=max;
    else if (align==2) {if (fWid>min) fWid=min;}
    if (fWid>max) fWid=max;
    if (width+ImgMargin+wcFigurePadding>fWid) width=fWid-wcFigurePadding-ImgMargin;
//    s += "-> fwid="+fWid;
    
    /*if (!IsOpera && !IsMSIE) */fWid-=wcFigurePadding;
    try
    {
      if (node.offsetWidth!=width) node.style.width=(width-wcImagePadding)+"px";
    }
    catch(err) {Err += node.src+"<1> "}
    try
    {
      if (fNode.offsetWidth!=fWid) fNode.style.width=(fWid)+"px";
    }
    catch(err) {Err += node.src+"<2> "}
//    s += "-> fwid="+fWid+" width="+width+" -> W="+node.offsetWidth+" fW="+fNode.offsetWidth+" bW="+(bNode.offsetWidth-wcContainerPadding-wcFigureMargin);
//    wcSetStatus(s);
  }
  if (Err != "") wcSetStatus(Err);
}

function bgTogglePost(id, asItem)
{
  var elm = document.getElementById(id);
  var Nodes = wcGetAllElements(elm);
  for (var i=0; i<Nodes.length; i++)
  {
    var s=Nodes[i].className;
	if ( wcHasAttribute(Nodes[i],wcStrFullPost)) wcForceSetVisible(Nodes[i],asItem)
    else if (s=="fullpost") wcForceSetVisible(Nodes[i],asItem)
    else if (s=="intro") wcForceSetVisible(Nodes[i],!asItem);
  }
}

function ScanForBloggerHide(elm, IsItem)
{
  if (BloggerIsItem) return;
  var cNodes = wcGetAllElements(elm);
  var idIndex=1;
  for (var i = 0; i<cNodes.length; i++)
  {
    var node=cNodes[i];
    if (node.className=="post-body")
    {
      // wcSetAttribute(node,wcStrHideBoxHandled,"1");
      var ShowStr   = BloggerShowStr;
      var HideStr   = BloggerHideStr;
      var ShowClass = wcShowThisClass;
      var HideClass = wcHideThisClass;
      var dNodes=wcGetAllElements(node);
      var HasFullPost=false;
      var ExpandBox=null;
      
      var ID=node.id;
      if ( (ID==null) || (typeof ID=="undefined")) ID="";
      if (ID=="")
      {
        ID=wcGetUniqueName(wcStrBlogIDPrefix,idIndex++);
        node.id=ID;
      }
      
      for (var j=0; j<dNodes.length; j++)
      {
        var s=dNodes[j].className;
        if (s=="fullpost") HasFullPost=true;
		else if (s=="readmorebox") wcForceSetVisible(dNodes[j],HasFullPost);  // the best way is to scan twice, but since it is always at the end, does not matter
        else if (s=="expandbox") ExpandBox=dNodes[j];
      }
      
      if (HasFullPost && (ExpandBox!=null))
      {
        var hID=wcGetUniqueName(wcStrBlogIDPrefix,idIndex++);
        var sID=wcGetUniqueName(wcStrBlogIDPrefix,idIndex++);
        wcForceSetVisible(ExpandBox,true);
        bgTogglePost(ID,false);
        var sonclick = 'wcHideElementID("'+sID+'");' + 'wcShowElementID("'+hID+'");' + 'bgTogglePost("'+ID+'",true)';
        var honclick = 'wcHideElementID("'+hID+'");' + 'wcShowElementID("'+sID+'");' + 'bgTogglePost("'+ID+'",false)';
        var hs="<span id='"+hID+"' style='display:none' onclick='"+honclick+"' class='"+HideClass+"'>"+HideStr+"</span>";
        var ss="<span id='"+sID+"' onclick='"+sonclick+"' class='"+ShowClass+"'>"+ShowStr+"</span>";
        ExpandBox.innerHTML=ExpandBox.innerHTML+hs+ss;
      }
    }
  }
}

function ScanForHideBox(elm)
{
  var cNodes = wcGetAllElements(elm);
  var idIndex=1;
  for (var i=0; i<cNodes.length; i++)
  {
    var node=cNodes[i];
    var visible=wcHasAttribute(node,wcStrHideBoxVisible);
    if (IsHideBox(node) && !wcHasAttribute(node,wcStrHideBoxHandled))
    {
      wcSetAttribute(node,wcStrHideBoxHandled,"1");
      var ShowStr   = (wcHasAttribute(node,wcStrShowString)) ? wcGetAttribute(node,wcStrShowString) : wcStrShowThis;
      var HideStr   = (wcHasAttribute(node,wcStrHideString)) ? wcGetAttribute(node,wcStrHideString) : wcStrHideThis;
      var ShowClass = (wcHasAttribute(node,wcStrShowClass))  ? wcGetAttribute(node,wcStrShowClass)  : wcShowThisClass;
      var HideClass = (wcHasAttribute(node,wcStrHideClass))  ? wcGetAttribute(node,wcStrHideClass)  : wcHideThisClass;
      var dNodes=wcGetAllElements(node);
      var TitleNode=null;
      var ID=node.id;
      if ( (ID==null) || (typeof ID=="undefined")) ID="";
      if (ID=="")
      {
        ID=wcGetUniqueName(wcStrIDPrefix,idIndex++);
        node.id=ID;
      }
      for (var j=0; j<dNodes.length; j++)
        if (IsHideBoxTitle(dNodes[j])) TitleNode=dNodes[j];

      var hideboxid=wcGetUniqueName(wcStrIDPrefix,idIndex++);
      var hidebox=document.createElement(node.tagName);
      hidebox.id=hideboxid;
      hidebox.className = node.className;
      wcSetVisible(hidebox,!visible);
      wcSetVisible(node,visible);
      var hidestr="<span class='"+ShowClass+"' onClick=\"wcHideElementID('"+hideboxid+"');wcShowElementID('"+ID+"')\">"+ShowStr+"</span>";
      if (TitleNode!=null)  // Copy titlenode to hidebox, events are not copied
      {
        var temp=wcAppendChildElement(hidebox,TitleNode.tagName,TitleNode.className,hidestr+TitleNode.innerHTML); 
        temp.onclick = "wcHideElementID('"+hideboxid+"');wcShowElementID('"+ID+"')";
        TitleNode.onclick = "wcHideElementID('"+ID+"');wcShowElementID('"+hideboxid+"')";
      }
      else hidebox.innerHTML = hidestr;
      node.parentNode.insertBefore(hidebox,node);
      var showstr="<span class='"+HideClass+"' onClick=\"wcHideElementID('"+ID+"');wcShowElementID('"+hideboxid+"')\">"+HideStr+"</span>";
      if (TitleNode!=null)
      {
        TitleNode.innerHTML=showstr+TitleNode.innerHTML; // cause the loss of events
      }
      else node.innerHTML=showstr+node.innerHTML;  // cause the loss of events
    }
  }
  ReScanForNoteNode(elm);
}

function ReScanForNoteNode(elm)
{
  var cNodes = wcGetAllElements(elm);
  for (var i=0; i<cNodes.length; i++)
  {
    var node=cNodes[i];
    var s=GetNoteText(node);
    if ((s!="" ) && !wcHasAttribute(node,wcStrNoteNoPopup))
    {
      node.onmouseout  = MouseMoveOutNote;
      node.onmouseover = MouseMoveOverNote;
      node.onclick = MouseMoveOverNote;
    }
  }
}

function ScanForNoteNode(elm)
{
  var ItemClass = (wcHasAttribute(elm,wcStrNoteItem)) ? wcGetAttribute(elm,wcStrNoteItem) : wcNoteBoxItemClass;
  var ItemTerm  = (wcHasAttribute(elm,wcStrNoteTerm)) ? wcGetAttribute(elm,wcStrNoteTerm) : wcNoteBoxTermClass;
  var NoteList  = (wcHasAttribute(elm,wcStrNoteList)) ? wcGetAttribute(elm,wcStrNoteList) : wcNoteBoxListClass;
  var NoteLink  = (wcHasAttribute(elm,wcStrNoteLink)) ? wcGetAttribute(elm,wcStrNoteLink) : wcNoteBoxLinkClass;
  var Listit    = !wcHasAttribute(elm,wcStrNoteNoList);
  var HasIndex  = (wcHasAttribute(elm,wcStrNoteIndex));
  var Index     = (HasIndex) ? wcStrToInt(wcGetAttribute(elm,wcStrNoteIndex),1) : 1;

  var list = "";
  var cNodes = wcGetAllElements(elm);
  var Infix1=wcNoteBoxItemInfix1;
  var Infix2=wcNoteBoxItemInfix2;

  for (var i=0; i< cNodes.length; i++)
  {
    var node=cNodes[i];
    if (IsNoteSection(node)) ScanForNoteNode(node)
    else
    {
      var s=GetNoteText(node);
      if ((s!="" ) && !wcHasAttribute(node,wcStrNoteHandled))
      {
  	    if (!wcHasAttribute(node,wcStrNoteNoList))
        {
          var t = node.innerHTML ;
          if (t=="") 
          {
            Infix1="";
            Infix2="";
          }
          if (HasIndex)
          {
            if (wcHasAttribute(node,wcStrNoteIndex))
              Index = wcStrToInt(wcGetAttribute(node,wcStrNoteIndex),Index);
            if (t=="") 
              node.innerHTML = "" + Index; 
            t = Index + ". " + t;
            Index++;
          }
          if (node.className=="") node.className=NoteLink;
          list += wcStartTagText("div",ItemClass) + wcStartTagText("span",ItemTerm) + wcNoteBoxItemPrefix + t + Infix1 + "</span>" + Infix2 + s + wcNoteBoxitemSuffix +"</div>";
        }
        wcSetAttribute(node,wcStrNoteHandled,"1");
        if (!wcHasAttribute(node,wcStrNoteNoPopup))
        {
          node.onmouseout  = MouseMoveOutNote;
          node.onmouseover = MouseMoveOverNote;
          node.onclick = MouseMoveOverNote;
        }
      }
    }
   }
  if (Listit && (list != ""))
  {
    var eee=wcAppendChildElement(elm, "div", NoteList, wcNoteBoxPrefix + list + wcNoteBoxSuffix);
    wcSetAttribute(eee,wcStrFullPost,"1");
  }
}

function wcDoHideNoteBox(NoteBox)
{
  wcHideElement(NoteBox);
}

function wcPositionNoteBox(elm,nb)
{
  var MaxWidth  = wcWindowWidth()-2;
  var MaxHeight = wcWindowHeight()-2;
  var PageTop  = wcScrollTop();
  var PageLeft = wcScrollLeft();
  var MouseX = CurrentMousePositionX-PageLeft;
  var MouseY = CurrentMousePositionY-PageTop;
  var Width  = nb.offsetWidth;
  var Height = nb.offsetHeight;
  var left=MouseX+8;
//    if (left+Width>=MaxWidth) left=MouseX-Width;
//    if (left<=0) left=MaxWidth-Width;
    if (left+Width>=MaxWidth) left=MaxWidth-Width;
    if (left<=0) left=1;
  var top=MouseY+16; // one line;
    if (top+Height>=MaxHeight) top=MouseY-Height-16;
	if (top<=0) top=1;
  nb.style.top=(PageTop+top)+"px";
  nb.style.left=(PageLeft+left)+"px";
}

function wcDoShowNoteBox(elm, nb)
{
  if (elm==null) return;
  var s=GetNoteText(elm);  //s cannot be empty
//  var pr=GetNoteParent(elm);
  wcHideElement(nb);
  nb.style.top = (wcScrollTop()+1)+"px";
  nb.style.left = (wcScrollLeft()+1)+"px";
  var wid=wcWindowWidth()*0.6;
  if (wid>500) wid=500;
  if (wid<100) wid=100;
  nb.style.maxWidth = wid+"px";  // crappy IE 1.5 does not support
  nb.innerHTML = s;
  wcPseudoShowElement(nb);
  wcFixIEMaxWidth(nb); // the element must be visible before we can do it
  wcPositionNoteBox(elm,nb);
  wcShowPseudoVisibleElement(nb);
}

// ============================= Event handlers =============================

function wcCancelHide()
{
  if (wcTimeOutHide!=null)
  {
    clearTimeout(wcTimeOutHide);
    wcTimeOutHide=null;
  }
}

function wcCancelShow()
{
  if (wcTimeOutShow!=null)
  {
    clearTimeout(wcTimeOutShow);
    wcTimeOutShow=null;
  }
}

function wcHideNote()
{
  wcTimeOutHide=null;
  wcNoteBoxElement=null;
  wcNoteBoxShowing=false;
  wcDoHideNoteBox(wcNoteBox);
}

function wcShowNote()
{
  wcScrollTop();
  wcTimeOutShow=null;
  wcNoteBoxShowing=true;
  wcDoShowNoteBox(wcNoteBoxElement,wcNoteBox);
  if (wcHintAutoHide!=0) 
    wcTimeOutHide=setTimeout("wcHideNote()",wcHintAutoHide);
}

function MouseMoveOverNote(evt)
{
  evt = wcTrueEvent(evt);
  var node=wcEventTarget(evt);
  if (node)
  {
    if (node==wcNoteBoxElement) wcCancelHide(); // maybe visible, maybe waiting to be showed, do not care
    else
    {
      var s=GetNoteText(node);
      if (s=="") return;
      var pause=wcHintPause;
      if (wcNoteBoxElement!=null)
      {
        if (wcNoteBoxShowing) pause=wcHintShortPause;  // if already visible, use shorter pause
        wcCancelHide();  // hide it immediately
        wcHideNote();
      }
      wcCancelShow();
      wcNoteBoxElement = node;
      wcTimeOutShow=setTimeout("wcShowNote()",pause);
    }
  }
}

function MouseMoveOverNoteBox(evt)
{
  wcCancelHide();
}

function MouseMoveOutNote(evt)
{
  wcCancelShow();
  wcCancelHide();
  if (wcTimeOutHide==null)
    wcTimeOutHide=setTimeout("wcHideNote()",wcHintHidePause);
}

function MonitorMousePosition(evt)
{
  evt = wcTrueEvent(evt);
  CurrentMousePositionX=mouseX(evt)
  CurrentMousePositionY=mouseY(evt)
}

function DocumentLoaded ()
{
//  wcListAllAttributes(document.body);
  DocumentBody = document.getElementsByTagName("body").item(0);
  document.onmousemove = MonitorMousePosition;
  try
  {
    ScanForNoteNode(DocumentBody);
    wcNoteBox=wcAppendElement("div",wcNoteBoxClass);
    wcHideElement(wcNoteBox);
    wcNoteBox.style.position = "absolute";
    wcNoteBox.style.zIndex = "9999";
    wcNoteBox.onmouseout  = MouseMoveOutNote;
    wcNoteBox.onmouseover = MouseMoveOverNoteBox;
  } catch(err) {}
  try
  {
    AdjustImageSize(DocumentBody);
  } catch(err) {}
  try
  {
    ScanForHideBox(DocumentBody);
  } catch(err) {}
  ScanForBloggerHide(DocumentBody);
}

// ============================= initialization =============================

function init()
{
  wcGetBrowser();
  window.onload=DocumentLoaded;
}

init();

