//--------------------------------------------------------------------
// AISデモ用オブジェクト (v1.01)
//--------------------------------------------------------------------
//                                               http://www.oas.co.jp/
var p_aisdemos = new Array();

function objAISDemo( aispath, aissrc, imagename, onmousedwn, onloadimg,
  opt_addopt )
{
  // private properties
  this.display = false;
  this.url = '';
  this.ourl = '';
  this.aispath   = aispath;
  this.aissrc    = aissrc;
  this.imagename = imagename;
  this.orgSignX = 1;
  this.orgSignY = -1;
  this.onmoused = onmousedwn;
  this.onloadimg = onloadimg;
  this.arrItem  = new Array();
  this.loading = '';
  
  this.ido   = 0;
  this.kdo   = 0;
  this.s_ido = '';
  this.s_kdo = '';
  
  this.cx  = 0;
  this.cy  = 0;
  this.kei = 1;
  this.mpp = 1;
  this.wid = 0;
  this.hgt = 0;
  this.ux  = this.wid * this.mpp * this.orgSignX ;
  this.uy  = this.hgt * this.mpp * this.orgSignY ;
  this.q   = 80;
  
  this.addopt = (opt_addopt)? opt_addopt: '';
  
  this.addItem  = oAISDemo_addItem;     // addItem(name) : AIS表示時に共に表示/非表示するオブジェクトの登録
  this.refresh  = oAISDemo_refresh;     // refresh([flg]) : 再表示するかどうか。flgが true なら必ず再表示
  this.setInfo  = oAISDemo_setInfo;     // setInfo(el,nl,sc,sw,sh) : 緯度,経度,MAPIONスケール,幅,高さ を登録
  this.convStr  = oAISDemo_cnvStr;      // convStr(str[,dlm]) : 緯度経度文字列をAISフォーマットに変換
  this.setUrlEN = oAISDemo_setUrlEN;    // setUrlEN(w,h,sz,el,nl) : AISパラメータを緯度経度で指定
  this.setUrlXY = oAISDemo_setUrlXY;    // setUrlXY(w,h,sz,x,y) : AISパラメータを平面直角座標で指定
  this.setSrc   = oAISDemo_setSrc;      // setSrc(src) : ソース名を指定
  this.setScale = oAISDemo_setScale;    // setScale(sc) : スケールを指定
  this.setWidth  = oAISDemo_setWidth;   // setWidth(w) : 幅を指定
  this.setHeight = oAISDemo_setHeight;  // setHeight(h) : 高さを指定
  this.setQuality = oAISDemo_setQuality; // oAISDemo_setQuality(q) : JPEGの品質を指定
  this.setAddOpt = oAISDemo_setAddOpt;
  this.moveDir  = oAISDemo_moveDir;     // moveDir(dir,moveratio) : 方向を指定して移動
  this.moveDirXY = oAISDemo_moveDirXY;  // moveDirXY(moveratio_x,moveratio_y) : 方向を指定して移動
  this.moveTo   = oAISDemo_moveTo;      // moveTo(x,y) : 位置を平面直角座標で指定して移動
  this.setLoadingItem = oAISDemo_setLoadingItem; // setLoadingItem(name) : 読込み時に表示するオブジェクト名の登録
  this.setLoading     = oAISDemo_setLoading;     // setLoading(flg) : 読込み時に表示するオブジェクトの表示/非表示
  
  this.refreshImage = oAISDemo_refreshImage;     // refreshImage() : このオブジェクトに登録された AISパラメータを利用して画像の表示
  this.setVisible   = AISDemo_setVisible;        // setVisible(name,flg) : オブジェクトの表示/非表示
  
  // handmade onload event
  this.cachetmr = 0;
  this.cacheimg = new Image();
  this.cacheerr = AISDemo_onLoadError;  // onLoadError() : 読み込みエラー
  
  // save private
  this.index = p_aisdemos.length;
  p_aisdemos[this.index] = this;
  
  this.debugmode = false;               // private:DEBUGMODE
  this.setDebug  = AISDemo_setDebug;    // DEBUG出力テキストエリアの設置
  this.debug     = AISDemo_putDebug;    // DEBUG出力テキストエリアへ出力
}
function oAISDemo_addItem(name)
{
  var l = this.arrItem.length;
  this.arrItem[l] = name;
}
function oAISDemo_setLoadingItem(name)
{
  if (name) {this.loading = name;}
}
function oAISDemo_cnvStr(str,delim)
{
  var dlm = (delim)? delim: '/';
  var ars = str.split( dlm );
  var r = ars[0];
  if (ars.length > 0)
  {
    r += ':' + ars[1];
    if (ars.length > 1)
    {
      r += ':' + ars[2];
    }
  }
  return r;
}
function oAISDemo_refreshImage() // キャッシュから読み込みます
{
//  document.images[this.imagename].onmousedown = eval( this.onmoused );
//  document.images[this.imagename].src = this.ourl;
  this.setLoading(false);
}
function oAISDemo_refresh(flg)
{
  if (this.display)
  {
    if (this.ourl != this.url || (!!flg))
    {
      var d = new Date();
      var adddmy = '' + ((!!flg)? ('&dmy='+d.getSeconds()+'.'+d.getMinutes()+'.'+d.getHours()):'');
      
      this.ourl = this.url + adddmy;
      
//    var img = new Image();
//    img.onload = eval( this.onloadimg );
//    img.src = this.ourl;
      // handmade onload event
      
      this.cacheimg.onerror = this.cacheerr;
      
      this.cacheimg.src = this.ourl;
      if (this.cachetmr)
      {
        clearTimeout(this.cachetmr);
      }
      this.cachetmr = setTimeout( 'AISDemo_onLoadCheck('+this.index+')', 500 );
      
      this.setLoading( true );
    }
    else
    {
      this.setLoading( false );
    }
  }
  else
  {
    this.setLoading( false );
  }
  
  var l = this.arrItem.length;
  for (var i=0; i<l; i++)
  {
    this.setVisible( this.arrItem[i], this.display );
  }
}
function oAISDemo_setInfo(el,nl,sc,sw,sh)
{
  var ais_sw = parseInt( sw );
  var ais_sh = parseInt( sh );
  var ais_sz = parseFloat( sc );
  
  if (ais_sz <= 0)
  {
    this.display = false;
    return ;
  }
  this.setUrlEN( ais_sw, ais_sh, ais_sz, el, nl )
}
function oAISDemo_setUrlEN(width,height,size,el,nl)
{
  var url = this.aispath;
  if (this.aissrc.length)
  {
    url += '?src=' + this.aissrc;
    url += '&width=' + width;
  }
  else
  {
    url += '?width=' + width;
  }
  url += '&height=' + height;
  url += '&q=' + this.q;
  url += '&lat=' + this.convStr( nl );
  url += '&lon=' + this.convStr( el );
  url += '&mpp=' + size;
  url += this.addopt;
  
  var oUTM = new objUTM();
  oUTM.setDeg( oUTM.conv(nl), oUTM.conv(el), 0  );
  oUTM.calcDegToUTM();
  
  this.s_ido = nl;
  this.s_kdo = el;
  this.ido   = oUTM.conv(nl);
  this.kdo   = oUTM.conv(el);
  
  this.url = url;
  this.cx  = oUTM.y;
  this.cy  = oUTM.x;
  this.kei = oUTM.kei;
  this.mpp = size;
  this.wid = width;
  this.hgt = height;
  this.ux  = this.wid * this.mpp * this.orgSignX ;
  this.uy  = this.hgt * this.mpp * this.orgSignY ;
  this.display = true;
}
function oAISDemo_setUrlXY(width,height,size,x,y)
{
  var url = this.aispath;
  if (this.aissrc.length)
  {
    url += '?src=' + this.aissrc;
    url += '&width=' + width;
  }
  else
  {
    url += '?width=' + width;
  }
  url += '&height=' + height;
  url += '&q=' + this.q;
  url += '&cx=' + x;
  url += '&cy=' + y;
  url += '&mpp=' + size;
  url += this.addopt;
  
  var oUTM = new objUTM();
  oUTM.setUTM( y, x, this.kei );
  oUTM.calcUTMToDeg();
  
  this.ido   = oUTM.ido;
  this.kdo   = oUTM.kdo;
  this.s_ido = oUTM.sido;
  this.s_kdo = oUTM.skdo;
  
  this.url = url;
  this.cx  = x;
  this.cy  = y;
  this.mpp = size;
  this.wid = width;
  this.hgt = height;
  this.ux  = this.wid * this.mpp * this.orgSignX ;
  this.uy  = this.hgt * this.mpp * this.orgSignY ;
  this.display = true;
}
function oAISDemo_setSrc(srcname)
{
  this.aissrc = srcname;
  this.setUrlXY( this.wid, this.hgt, this.mpp, this.cx, this.cy );
}
function oAISDemo_setScale(scale)
{
  var sc = parseFloat( scale );
  if (! isNaN(sc))
  {
    this.mpp = sc;
    this.setUrlXY( this.wid, this.hgt, this.mpp, this.cx, this.cy );
  }
}
function oAISDemo_setWidth(width)
{
  var val = parseInt( width );
  if (! isNaN(val))
  {
    this.wid = val;
    this.setUrlXY( this.wid, this.hgt, this.mpp, this.cx, this.cy );
  }
}
function oAISDemo_setHeight(height)
{
  var val = parseInt( height );
  if (! isNaN(val))
  {
    this.hgt = val;
    this.setUrlXY( this.wid, this.hgt, this.mpp, this.cx, this.cy );
  }
}
function oAISDemo_setQuality(quality)
{
  var val = parseInt( quality );
  if (! isNaN(val))
  {
    this.q = val;
    this.setUrlXY( this.wid, this.hgt, this.mpp, this.cx, this.cy );
  }
}
function oAISDemo_setAddOpt(addopt)
{
  this.addopt = (addopt)? addopt: '';
}
function oAISDemo_moveDir( dir, moveratio )
{
  var cx = this.cx;
  var cy = this.cy;
  var dx = this.ux; // 符号情報入り
  var dy = this.uy;
  var mr = (moveratio>0.0)? moveratio: 1.0;
  
  dx *= mr;
  dy *= mr;
  
  switch(dir)
  {
  case 1:  cx -= dx;  cy += dy;  break;
  case 2:             cy += dy;  break;
  case 3:  cx += dx;  cy += dy;  break;
  case 4:  cx -= dx;             break;
  case 6:  cx += dx;             break;
  case 7:  cx -= dx;  cy -= dy;  break;
  case 8:             cy -= dy;  break;
  case 9:  cx += dx;  cy -= dy;  break;
  default:   this.display = false; return;
  }
  
  this.setUrlXY( this.wid, this.hgt, this.mpp, cx, cy );
}
function oAISDemo_moveDirXY( moveratio_x, moveratio_y )
{
  var cx = this.cx;
  var cy = this.cy;
  var dx = this.ux;
  var dy = this.uy;
  
  dx *= moveratio_x;
  dy *= moveratio_y;
  
  cx += dx;
  cy += dy;
  
  this.setUrlXY( this.wid, this.hgt, this.mpp, cx, cy );
}
function oAISDemo_moveTo(x,y)
{
  var cx = this.cx;
  var cy = this.cy;
  
  cx += this.ux * (x - this.wid / 2) / this.wid;
  cy += this.uy * (y - this.hgt / 2) / this.hgt;
  
  this.setUrlXY( this.wid, this.hgt, this.mpp, cx, cy );
}
function oAISDemo_setLoading(start)
{
  this.setVisible( this.loading, start );
}
function AISDemo_setDebug()
{
  if (! this.debugmode)
  {
    this.debugmode = true;
    document.write( '<form name="aisdemo_debug">' );
    document.write( '<textarea name="t" cols=80 rows=20>デバッグ出力されます</textarea>' );
    document.write( '</form>' );
  }
}
function AISDemo_putDebug(s)
{
  if (this.debugmode)
  {
    if (s != null)
    {
      var t = document.aisdemo_debug.t.value;
      document.aisdemo_debug.t.value = t + '\n' + s;
    }
    else
    {
      document.aisdemo_debug.t.value = '';
    }
  }
}
function AISDemo_setVisible(name,flg)
{
  var s = (!! flg)? 'inherit': 'hidden';
  
  if ((''+name).length)
  {
    if (document.getElementById)
    {
      var o = document.getElementById( name );
      if (o) {o.style.visibility = s;}
    }
    else if (document.all)
    {
      var o = document.all[ name ];
      if (o) {o.style.visibility = s;}
    }
    else if (document.layers)
    {
      var o = document.layers[ name ];
      if(o) {o.visibility = s;}
    }
  }
}
function AISDemo_onLoadCheck(index)
{
  if (0<=index && index<p_aisdemos.length)
  {
    var othis = p_aisdemos[index];
    
    clearTimeout(othis.cachetmr);
    othis.cachetmr = 0;
    
    if (othis.cacheimg.complete)
    {
      if (othis.onmoused)
      {
        document.images[othis.imagename].onmousedown = eval( othis.onmoused );
      }
      document.images[othis.imagename].src = othis.ourl;
      
      othis.refreshImage();
      if (othis.onloadimg)
      {
        var f = eval( othis.onloadimg );
        if(f) { f(); }
      }
    }
    else
    {
      othis.cachetmr = setTimeout( 'AISDemo_onLoadCheck('+index+')', 500 );
    }
  }
}
function AISDemo_onLoadError()
{
  window.status('画像読込時にエラーになりました.');
  othis.cacheimg.complete = true;
}