/***
グローバルシンボルだけは宣言しておく。（既に使用済みでもvarは問題なし）
***/
var org;   
/***
プロパティがなかったら追加する。
***/
/***
すでにある場合はオブジェクトかどうか判断して例外発生させた方がいいらしい
***/
if (!org) org = {};   
if (!org.SoulDivas) org.SoulDivas = {};   
if (!org.SoulDivas.foo) org.SoulDivas.MascotEngineLibrary = {};

(function() {
  /*
  構造体定義
  var dat=0;dat<ary.length;dat++
  */
  function Rect(Left,Top,Width,Height){
    this.Top = Top-0;
    this.Left = Left-0;
    this.Width = Width-0;
    this.Height = Height-0;
  }
  function Point(X,Y){
    this.x = X;
    this.y = Y;
    this.X = X;
    this.Y = Y;
    this.Left = X;
    this.Top = Y;
    this.width = X;
    this.height = Y;
  }

  function UnitBase(){
    this.eventList = new Array;
    this.activeEventList = new Array;
    this.images = new Array;
    this.move = new Array;
    this.stopList = new Array;
    this.loopcount=0;
    this.nowevent = new Array;
    
    this.setNowevent = function (str){this.nowevent=str;};
    this.getNowevent = function (){return this.nowevent;};
    this.setLoopcount = function (num){this.loopcount=num;};
    this.getLoopcount = function (){return this.loopcount;};
    this.setEventList = function (ary){this.eventList = new Array;for (dat in ary){this.eventList[dat] = ary[dat];}};
    this.getEventList = function (){return this.eventList;};
    this.setActiveEventList = function (ary){this.activeEventList = new Array;for (dat in ary){this.activeEventList[dat] = ary[dat];}};
    this.getActiveEventList = function (){return this.activeEventList;};
    this.setImage = function (ary){this.images = new Array;for (dat in ary){this.images[dat] = ary[dat].slice(0);}};
    this.getImage = function (str){return this.images[str];};
    this.getImageAll = function (){return this.images;};
    this.setMove = function (ary){this.move = new Array;for (dat in ary){this.move[dat] = ary[dat].slice(0);}};
    this.getMove = function (str){return this.move[str];};
    this.setStopList = function (ary){this.stopList = new Array;for (dat in ary){this.stopList[dat] = ary[dat].slice(0);}};
    this.getStopList = function (){return this.stopList;};
  }
  
  
  function loadCheck(){
    
  }
  
  /***
  ループアニメの処理　25/1000秒　に一度
  ***/
  function animeLoopTimerTick(){
    try{
      
      var strTag = "";
      
      var intLoopCount;
      var key;
      var pnt=new Point(0,0);
      
      var intAnimationLoopCounter = 0;
        
      var minLocation = new Point(MoveArea.Width,MoveArea.Height);
      var maxLocation = new Point(-1,-1);
      
      var ub= new UnitBase();
      
      var aryAnimationLoopCounter=new Array;
      
      var flgTagExisting=true;
      
      intLoopCount=0;
      for (key in CharacterNameList){
        key = CharacterNameList[key];
        ub = aryUnitBaseList[key];
        intAnimationLoopCounter = (intAnimationCnt+intLoopCount) % 10;
        aryAnimationLoopCounter[key] = intAnimationLoopCounter;
        
        pnt = locationList[key];
        if( pnt==undefined )pnt=new Point(Math.floor(Math.random()*(MoveArea.Width)),Math.floor(Math.random()*(MoveArea.Height)));
        
        if(intAnimationLoopCounter==0 || moveList[key] == undefined){
          if(intLoopCount==0)scrollTop  = document.body.scrollTop  || document.documentElement.scrollTop;
          var event = new Array; 
          event = ub.getEventList();
          
          if( imageList[key]==undefined )imageList[key]=new Array;
          
          if( intAnimationLoopCounter + 10 < imageList[key].length ){
            ub.setLoopcount(ub.getLoopcount()+10);
            imageList[key]=ub.getImage(ub.getNowevent()).slice(ub.getLoopcount());
            moveList[key]=ub.getMove(ub.getNowevent()).slice(ub.getLoopcount());
          }else{
            var ran = Math.floor(Math.random()*(event.length));
            var eventID = event[ran];
            var aeve = new Array();
            if( activeEvent[key]["tataku"]==1 ){
              activeEvent[key]["tataku"]=0;
              aeve=ub.getActiveEventList();
              if( aeve["tataku"].length != 0 ){
                ran = Math.floor(Math.random()*(aeve["tataku"].length));
                eventID = aeve["tataku"][ran];
              }
            }else if(activeEvent[key]["naderu"]>=20){
              activeEvent[key]["naderu"]=0;
              aeve=ub.getActiveEventList();
              if( aeve["naderu"].length != 0 ){
                ran = Math.floor(Math.random()*(aeve["naderu"].length));
                eventID = aeve["naderu"][ran];
              }
            }
            imageList[key]=ub.getImage(eventID);
            moveList[key]=ub.getMove(eventID);
            ub.setNowevent(eventID);
            ub.setLoopcount(0);
          }
        }
        
        var pntMove = moveList[key][intAnimationLoopCounter];
        pnt.x += pntMove.x*numSize;
        pnt.y += pntMove.y*numSize;
        
        locationList[key] = pnt;
        
        bmp = new Point(100,100)
        
        var intScaleW = bmp.width*numSize;
        var intScaleH = bmp.height*numSize;
        
        if( MoveArea.Width-intScaleW<pnt.x )pnt.x=MoveArea.Width-intScaleW;
        if( MoveArea.Height-intScaleH<pnt.y )pnt.y=MoveArea.Height-intScaleH;
        if( pnt.x<0 )pnt.x=0;
        if( pnt.y<0 )pnt.y=0;
        
        //alert(MoveArea.Height);
        
        /*
        if( maxLocation.x < pnt.x + intScaleW )maxLocation.x = pnt.x + intScaleW;
        if( maxLocation.y < pnt.y + intScaleH )maxLocation.y = pnt.y + intScaleH;
        if( minLocation.x > pnt.x )minLocation.x = pnt.x;
        if( minLocation.y > pnt.y )minLocation.y = pnt.y;
        */
        try{
          var obj = document.getElementById("img" + key);
          obj.src=obj.src;
        }catch(e){
          flgTagExisting=false;
        }
        intLoopCount++;
      }
      
      intLoopCount=0;
      for (key in CharacterNameList){
        key = CharacterNameList[key];
        ub = aryUnitBaseList[key];
        intAnimationLoopCounter = aryAnimationLoopCounter[key];
        pnt = locationList[key];
        if( flgTagExisting ){
          var obj = document.getElementById("img" + key);
          obj.style.left=pnt.x + "px";
          obj.style.top=pnt.y + "px";
          obj.src=imageList[key][intAnimationLoopCounter];
        }else{
          strTag += "<img id='img" + key + "' src='" + imageList[key][intAnimationLoopCounter] + "' style='left:" + (pnt.x-minLocation.x) + "px;top:" + (pnt.y-minLocation.y) + "px;position:absolute;' />\n";
        }
        intLoopCount++;
      }
      /*
      cellYukkuri.style.left   = minLocation.x;
      cellYukkuri.style.top    = minLocation.y;
      cellYukkuri.style.width  = (maxLocation.x-minLocation.x);
      cellYukkuri.style.height = (maxLocation.y-minLocation.y);
      */
      document.getElementById('org_SoulDivas_MascotEngineLibrary_cellYukkuri').style.top    = scrollTop;
      if( flgTagExisting ){
      }else{
        var strPreLoad="";
        for (key in CharacterNameList){
          var key = CharacterNameList[key];
          ub = aryUnitBaseList[key];
          var images = ub.getImageAll();
          for (var ikey in images){
            for (var index in images[ikey]){
              strPreLoad += "<img style='display:none;' width='1' height='1' src='" + images[ikey][index] + "' />";
            }
          }
        }
        document.getElementById('org_SoulDivas_MascotEngineLibrary_cellPreLoad').innerHTML = strPreLoad;
        document.getElementById('org_SoulDivas_MascotEngineLibrary_cellYukkuri').innerHTML = strTag;
      }
      
      //txtDbg.value=strTag;
      
      intAnimationCnt++;
    }catch(err){
      //alert(err.description);
      //alert(err);
    }
    setTimeout("org.SoulDivas.MascotEngineLibrary.animeLoopTimerTick()",100);
  }
  function onMouseMove(){
    mousePoint.x = event.clientX;
    mousePoint.y = event.clientY;
  }
  
  function setMoveArea(intX,intY,intWidth,intHeight){
    MoveArea = new Rect(intX,intY,intWidth,intHeight);
  }
  
  var mainTimer;
  
  var CharacterNameList = new Array();
  var aryUnitBaseList   = new Array();
  var locationList      = new Array();
  var moveList          = new Array();
  var imageList         = new Array();
  var activeEvent       = new Array();
  
  var MoveArea = new Rect(0,0,1024,768);
  
  var intAnimationCnt=0;
  var numSize=1;
  
  var mousePoint=new Point(-1,-1);
  
  var scrollTop  = 0;
  
  var flgPreload = false;
  
  var cel = org.SoulDivas.MascotEngineLibrary;
  cel.Rect = Rect;
  cel.Point = Point;
  cel.UnitBase = UnitBase;
  cel.animeLoopTimerTick = animeLoopTimerTick;
  cel.onMouseMove = onMouseMove;
  
  cel.mainTimer = mainTimer;
  cel.CharacterNameList = CharacterNameList;
  cel.aryUnitBaseList = aryUnitBaseList;
  cel.locationList = locationList;
  cel.moveList = moveList;
  cel.imageList = imageList;
  cel.activeEvent = activeEvent;
  
  cel.intAnimationCnt = intAnimationCnt;
  cel.numSize = numSize;
  
  cel.mousePoint = mousePoint;
  
  cel.scrollTop = scrollTop;
  cel.flgPreload = flgPreload;
  
  cel.setMoveArea = setMoveArea;
})();
try{
  var s=document.createElement( "SPAN" );
  s.id="org_SoulDivas_MascotEngineLibrary_cellYukkuri";
  s.style.position="absolute";
  s.style.left="0px";
  s.style.top="0px";
  s.style.width="1px";
  s.style.height="1px";
  document.body.appendChild(s);

  var s=document.createElement( "SPAN" );
  s.id="org_SoulDivas_MascotEngineLibrary_cellPreLoad";
  s.style.position="absolute";
  s.style.left="0px";
  s.style.top="0px";
  s.style.width="1px";
  s.style.height="1px";
  document.body.appendChild(s);
}catch(err){
  document.write("<span id='org_SoulDivas_MascotEngineLibrary_cellYukkuri' style='position:absolute;left:0px;top:0px;width:1px;height:1px;'></span>");
  document.write("<span id='org_SoulDivas_MascotEngineLibrary_cellPreLoad' style='position:absolute;left:0px;top:0px;width:1px;height:1px;'></span>");
}
