// ランドマークカテゴリ
//var G_category = new Array();
//G_category.default  = 0;
//G_category.address  = 1;
//G_category.station  = 2;
//G_category.landmark = 3;


var G_landmark = new Array();
function addLandmark(x,y,t,hr,ht,bk,bkt,flg_noput)
{
  var id = G_landmark.length;
//  var obj = new objLandmark(G_canvas, id, G_canvas.l+G_canvas.w+10, G_canvas.t+id*23, x, y, t, hr, ht, bk, bkt, flg_noput );
  var obj = new objLandmark(G_canvas, id, G_canvas.l+G_canvas.w-120, G_canvas.t+id*23-5, x, y, t, hr, ht, bk, bkt, flg_noput );
  G_landmark[ id ] = obj;
  return obj;
}
function setVisibleAllLandmark(b)
{
  b = !!b;
  for (var i=0; i<G_landmark.length; i++)
  {
    var o = G_landmark[i];
    setVisibleLAYER( o.objtop.style , b);
  }
}
function setVisibleAllLandmarkLine(b)
{
  b = !!b;
  for (var i=0; i<G_landmark.length; i++)
  {
    var o = G_landmark[i];
    o.setLineVisible( b );
  }
}
function toggleLandmarkLine(id)
{
  if(0<=id && id<G_landmark.length)
  {
    var o = G_landmark[id];
    
    o.linevisible ^= true;
    o.setLineVisible( o.linevisible );
  }
}
function objLandmark(oc,id,sx,sy, x,y,t,hr,ht, bk,bkt, opt_noput)
{
  this.canvas = oc;
  this.name = 'lm_' + id;
  this.id = id;
  this.x = x;
  this.y = y;
  this.t = t;
  this.sx = this.orgx = sx;
  this.sy = this.orgy = sy;
  this.href = hr;
  this.hreftarget = ht;
  this.book = bk;
  this.booktarget = bkt;
  this.linevisible = true;
  this.moveMark = objLandmark_moveMark;
  this.moveLine = objLandmark_moveLine;
  this.defaultPos = objLandmark_defaultPosition;
  this.targetPos = objLandmark_targetPosition;
  this.drawOnce = objLandmark_putHtml;
  this.getHtml  = objLandmark_getHtml;
  this.setEvent = objLandmark_setEvent;
  this.setLineVisible = objLandmark_setLineVisible;
  this.getTitleByte = objLandmark_getTitleByte;
  
  this.afterOnMDN = null;
  this.afterOnMMV = null;
  this.afterOnMUP = null;
  
  this.lmid = '';
  this.lmtp = '';
  this.lmdt = '';
  
  this.rect_bg   = "#ffcccc";
  this.rect_fg   = "#0000ff";
  this.arrow_txt = this.t + 'の場所を示します';
  this.owin_txt  = this.t + 'の登録URLを開きます';
  this.note_txt  = this.t + 'の登録情報を表示します';
  this.book_txt  = this.t + 'をMADO-Bookで閲覧します';
  
  if (!opt_noput) this.drawOnce();
  
}
function objLandmark_getHtml(b)
{
  var ov = (!!b)? 'visible': 'hidden';
  var oc = this.canvas;
  
  var arrx = 5;
  var txtx = 15; // arrx+arrw(=10)
  var winx = 0;
  var urlx = 0;
  var madx = 0;
  var txt = this.t;
  if (this.href.length>0) { winx=txtx; txtx+=16; txt='　&nbsp;'+txt; }
  if (this.lmdt.length>0) { urlx=txtx; txtx+=16; txt='　&nbsp;'+txt; }
  if (this.book.length>0) { madx=txtx; txtx+=16; txt='　&nbsp;'+txt; }
  
  var txth = 20;
  var txtb = this.getTitleByte();
  var txtw = txtx + txtb * 12/2 + txth/2; // MAGIC NUMBERの塊 :)
  
  var ret = 
      '<SPAN CLASS="floatLandmark" ID="'+this.name+'"'
    + ' style="position:absolute; left:'+this.orgx+'; top:'+this.orgy+'; visibility:'+ov+'; z-index:'+getNextDecZIndex()+';">'
    + '<v:line id="'+this.name+'_arrow"'
    + ' from="0,14"'
    + ' to="'+(this.x+oc.l-this.orgx)+','+(this.y+oc.t-this.orgy)+'"'
    + ' strokecolor="red" strokeweight="4">'
    + '<v:stroke endarrow="oval" opacity="0.7"/>'
    + '</v:line>'
    + '<SPAN ID="'+this.name+'_rect">'
    + '<v:roundrect style="position:relative; left:0px; top:0px; width:'+txtw+'px; height:'+txth+'px;" fillcolor="'+this.rect_bg+'"'
    + ' arcsize="0.5" >'
    + '<v:path textboxrect="3000, -150, 30000, 1000;"/>'
    + '<v:textbox style="cursor:default;font-family:\'ＭＳ ゴシック\',\'MS-Gothic\',\'Osaka-Mono\',monospace;font-size:12px;color:'+this.rect_fg+';">'
    + txt+'</v:textbox>'
    + '<v:shadow on="true" offset="2px,2px" color="#cccccc"/>'
    + '</v:roundrect></SPAN>'
    + '<img id="'+this.name+'_aimg"'
    + ' src="./image/arrow.gif" style="cursor:hand;position:absolute;left:'+arrx+'px;top:5px;width:10px;height:16px;"'
    + ' title="'+this.arrow_txt+'"/>'
    ;
//    + '<v:textbox style="cursor:default;font-family:monospace,\'MS-Gothic\',\'Osaka-Mono\';font-size:12px;color:'+this.rect_fg+';">'
  // block  oval diamond
  // textboxrectの単位って?
  if (this.href.length>0)
  {
    ret += '<img id="'+this.name+'_oimg"' ;
    ret += ' src="./image/owin.gif" style="cursor:hand;position:absolute;left:'+winx+'px;top:5px;width:16px;height:16px;"' ;
    ret += ' title="'+this.owin_txt+'"/>' ;
  }
  if (this.lmdt.length>0)
  {
    ret += '<img id="'+this.name+'_nimg"' ;
    ret += ' src="./image/note.gif" style="cursor:hand;position:absolute;left:'+urlx+'px;top:5px;width:16px;height:16px;"' ;
    ret += ' title="'+this.note_txt+'"/>' ;
  }
//  if (this.book.length>0)
//  {
//    ret += '<img id="'+this.name+'_bimg"' ;
//    ret += ' src="./image/book.gif" style="cursor:hand;position:absolute;left:'+madx+'px;top:5px;width:16px;height:16px;"' ;
//    ret += ' title="'+this.book_txt+'"/>' ;
//  }
  ret += '</SPAN>\n';
  return ret;
}

function objLandmark_getTitleByte()
{
  var ret = 0;
  var txt = this.t;
  var len = txt.length;
  
  for (var i=0; i<len; i++) {
    if (escape(txt.charAt(i)).length >= 4)
    {
      // 全角文字
      ret+=2;
    }
    else
    {
      //半角文字
      ret++;
    }
  }
  return ret;
}
function objLandmark_setEvent()
{
  this.objtop  = null;
  this.objrect = null;
  this.objline = null;
  this.objaimg = null;
  this.objoimg = null;
  this.objnimg = null;
  this.objbimg = null;
  if (document.getElementById)
  {
    this.objtop  = document.getElementById( this.name );
    this.objrect = document.getElementById(this.name + '_rect');
    this.objline = document.getElementById(this.name + '_arrow');
    this.objaimg = document.getElementById(this.name + '_aimg');
    this.objoimg = document.getElementById(this.name + '_oimg');
    this.objnimg = document.getElementById(this.name + '_nimg');
    this.objbimg = document.getElementById(this.name + '_bimg');
    
    if (this.objrect && is_ie5_5up)
    {
      this.objrect.onmousedown = Function( 'objLandmark_mdown(' + this.id + ')' );
      this.objrect.onmouseup   = objLandmark_mup;
    }
    if (this.objaimg)
    {
      this.objaimg.onmousedown = Function( 'objLandmark_mdown_arrow(' + this.id + ')' );
      this.objaimg.onmouseover = Function( 'objLandmark_statusline("' + this.arrow_txt + '")' );
      this.objaimg.onmouseout  = Function( 'objLandmark_statusline("")' );
    }
    if (this.objoimg)
    {
      this.objoimg.onmousedown = Function( 'objLandmark_mdown_owin(' + this.id + ')' );
      this.objoimg.onmouseover = Function( 'objLandmark_statusline("' + this.owin_txt + '")' );
      this.objoimg.onmouseout  = Function( 'objLandmark_statusline("")' );
    }
    if (this.objnimg)
    {
      this.objnimg.onmousedown = Function( 'objLandmark_mdown_note(' + this.id + ')' );
      this.objnimg.onmouseover = Function( 'objLandmark_statusline("' + this.note_txt + '")' );
      this.objnimg.onmouseout  = Function( 'objLandmark_statusline("")' );
    }
    if (this.objbimg)
    {
      this.objbimg.onmousedown = Function( 'objLandmark_mdown_book(' + this.id + ')' );
      this.objbimg.onmouseover = Function( 'objLandmark_statusline("' + this.book_txt + '")' );
      this.objbimg.onmouseout  = Function( 'objLandmark_statusline("")' );
    }
  }
}
function objLandmark_putHtml()
{
  document.write( this.getHtml(true) );
  this.setEvent();
  // setVisibleAllLandmark();
  setVisibleLAYER( this.objtop.style, true);
}
function objLandmark_setLineVisible(b)
{
  b = !!b;
  
  this.linevisible = b;
  if(this.objline)
  {
    setVisibleLAYER( this.objline.style , this.linevisible );
  }
}
function objLandmark_defaultPosition()
{
  this.moveMark( this.orgx , this.orgy );
  this.moveLine();
}
function objLandmark_targetPosition()
{
  var oc = this.canvas;
  this.moveMark( this.x + oc.l - 7, this.y + oc.t - 10 );
  this.moveLine();
}
function objLandmark_moveMark( xpos, ypos )
{
  moveLAYER( this.objtop.style, xpos, ypos );
  this.sx = xpos; this.sy = ypos;
}
function objLandmark_moveLine()
{
  if (this.objline)
  {
    setVisibleLAYER( this.objline.style , false );
    var oc = this.canvas;
    this.objline.to = ''+(this.x + oc.l - this.sx)+','+(this.y + oc.t - this.sy);
    setVisibleLAYER( this.objline.style , true && this.linevisible);
  }
}

var G_dragobj = null;
function objLandmark_mdown(id)
{
  if (event.button == 1)
  {
    // 左クリックのみ
    G_dragobj = new setDragObject(id);
    if (G_dragobj)
    {
      zindexLAYER(G_dragobj.obj, getNextZIndex());
      G_dragobj.ofsx = parseInt(G_dragobj.obj.left) - ( event.clientX + document.body.scrollLeft );
      G_dragobj.ofsy = parseInt(G_dragobj.obj.top) -  ( event.clientY + document.body.scrollTop );
      
      if (G_dragobj.landmark.afterOnMDN) { G_dragobj.landmark.afterOnMDN(G_dragobj.landmark); }
      
      return false;
    }
  }
}
function objLandmark_mup(e)
{
  if (G_dragobj) {
    G_dragobj.landmark.moveLine();
    if (G_dragobj.landmark.afterOnMUP) { G_dragobj.landmark.afterOnMUP(G_dragobj.landmark); }
    G_dragobj = null;
  }
}

function setDragObject(id)
{
  this.landmark = G_landmark[id];
  this.obj = G_landmark[id].objtop.style;
  
  this.ofsx = 0;
  this.ofsy = 0;
  
  this.oline = G_landmark[id].objline;
  if (this.oline)
  {
    this.oline.to = this.oline.from ;
    setVisibleLAYER(this.oline.style, false);
  }
}

function mousemoveLandmark()
{
  if (G_dragobj)
  {
    movetoX=(document.body.scrollLeft+event.clientX) + G_dragobj.ofsx;
    movetoY=(document.body.scrollTop+event.clientY) + G_dragobj.ofsy;
    
    G_dragobj.landmark.moveMark( movetoX, movetoY );
    if (G_dragobj.landmark.afterOnMMV) { G_dragobj.landmark.afterOnMMV(G_dragobj.landmark); }
    return true;
  }
  return false;
}
function isDragging()
{
  return (G_dragobj)? true: false;
}

function objLandmark_mdown_arrow(id)
{
  if (event.button == 1)
  {
    // 左クリックのみ
    toggleLandmarkLine(id);
  }
}

function objLandmark_mdown_owin(id)
{
  if (event.button == 1)
  {
    // 左クリックのみ
    if(0<=id && id<G_landmark.length)
    {
      var o = G_landmark[id];
      var w = window.open(o.href, o.hreftarget);
    }
  }
}
function objLandmark_mdown_note(id)
{
  if (event.button == 1)
  {
    // 左クリックのみ
    if(0<=id && id<G_landmark.length)
    {
      var o = G_landmark[id];
      if (o.lmdt.length > 0)
      {
        var f = eval( o.lmdt );
        if(f) { f(id); }
      }
      else
      {
        var w = window.open(o.href, o.hreftarget, 'fullscreen=no,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=yes');
      }
    }
  }
}
function objLandmark_mdown_book(id)
{
  if (event.button == 1)
  {
    // 左クリックのみ
    if(0<=id && id<G_landmark.length)
    {
      var o = G_landmark[id];
      var w = window.open(o.book, o.booktarget, 'resizable=yes,width=850,height=650');
    }
  }
}
function objLandmark_statusline(txt)
{
  window.status = txt;
}
