// エベントからその発生したものを特定する場合は
// IE：event.srcElement
// NN：event.target
// alert(event.target+ ";   " +event.srcElement);
/**********************************************
 * ブラウザ内擬似ウィンド提供するコンポーネント
 * @param winName    : ウィンドタイトルバーに表示文字列
 **********************************************/
function InnerWin(winName){
    //---- クラスプロパティ設定 ----//
    this.X=0;                        // ドラッグ開始縦位置
    this.Y=0;                        // ドラッグ開始横位置
    this.top =0;                    // コントローラの縦位置(px値を数字化した)
    this.left=0;                    // コントローラの横位置(px値を数字化した)
    this.IS_DOWN=false;             // ドラッグ状態判断
    this.MOVE_ABLE=true;            // ドラッグ状態判断

    // 複数のインスタンスで利用するする場合タスクキー生成 //
    this.winName=winName;
    if(InnerWin.prototype.self!=null){
        InnerWin.prototype.self.close();
    }
	this.key=winName;
    InnerWin.prototype.self=this;
	if(winName!=null){
		//--------------------------------------------//
		// タスク管理テーブルを作成。
		//--------------------------------------------//
		if(!InnerWin.prototype.taskList) InnerWin.prototype.taskList=new Array();
		for(var i=0;InnerWin.prototype.taskList[this.key]!=null;i++) this.key=winName+i;
		InnerWin.prototype.taskList[this.key]=this;	// タスク管理
	}
    this.createWindow(winName, (winName==null)? "nothing" : winName);
}

InnerWin.prototype.createWindow=function(winName, key){
    var table, tbody, tr, td, div, body=document.body;
    this.win=document.createElement("DIV");
    this.win.align="center";
    this.win.style.cssText="POSITION:absolute; left: 95px; top: 100px; overflow: visible; background-color: #DDDDDD; z-index:10000; height:230px; width:190px; border: 2px #FFFFFF outset; display:none";
    this.win.onmousedown=InnerWin.setActive;
    if(winName!=null){
        // タイトル start //
        this.win.appendChild(table=document.createElement("TABLE"));
        table.style.cssText="MARGIN: 0px; WIDTH: 100%";
        table.cellSpacing="0";
        table.cellPadding="0";
        table.border="0";
        table.appendChild(tbody=document.createElement("TBODY"));
        tbody.appendChild(tr=document.createElement("TR"));
          tr.appendChild(td=document.createElement("TD"));
          tr.style.backgroundColor="#000099";
          tr.appendChild(td=document.createElement("TD"));
          this.ctril=td;
            td.style.cssText="FONT-WEIGHT: 500; FONT-SIZE: 12px; VERTICAL-ALIGN: bottom; COLOR: white; BACKGROUND-COLOR: #000099; TEXT-ALIGN: left; cursor:default;";
            td.noWrap="true";
            td.appendChild(document.createTextNode("　" + winName));
            td.onmousedown=InnerWin.mousedown;
/*
            var funcName='if(arguments.length>0) InnerWin.prototype.mousedown(arguments[0],"'+ key +'");else alert("stop");';
            if(document.all) funcName='InnerWin.prototype.mousedown(event, "'+ key +'")';
            td.onmousedown=new Function(funcName);
*/
          tr.appendChild(td=document.createElement("TD"));
              td.style.cssText="MARGIN: 5px; WIDTH: 1%;";
              td.noWrap="true";
              td.align="right";
              td.appendChild(div=document.createElement("DIV"));
                div.title="閉じる"
                div.style.cssText="MARGIN: 0px; FONT-WEIGHT: 900; FONT-SIZE: 12px; VERTICAL-ALIGN: top; WIDTH: 12px; HEIGHT: 12px; BORDER-STYLE: outset; BORDER-width: 2px; BACKGROUND-COLOR: #d3d3d3; TEXT-ALIGN: right; cursor:default;";
                div.appendChild(document.createTextNode("×"));
                div.onclick=InnerWin.close;
                
    // タイトル end //
    }
    // キャンパス start //
    this.win.appendChild(div=document.createElement("DIV"));
      div.style.cssText="margin:3px; BORDER-STYLE:inset; border-width:1px;";
      div.align="center";
        div.appendChild(this.canpas=div=document.createElement("DIV"));
        div.align="center";
        div.style.cssText="font-size:10pt; overflow:auto; height:205px; background-color:white;";
    body.appendChild(this.win);
}

InnerWin.prototype.setMoveAble=function(moveAble){
    this.MOVE_ABLE=moveAble;
}

InnerWin.mousedown=function(event){
	var element=(document.all)? window.event.srcElement : event.target;
// ウインドウを動かさない 2006.09.20 神戸仕様
//    if(InnerWin.prototype.self.MOVE_ABLE) InnerWin.prototype.self.mousedown(event, element.innerHTML.replace(/　/, ""));
// ウインドウを動かさない 2006.09.20 神戸仕様
}
//--------------------------------------------//
// 子ウィンド移動操作開始関数：
//--------------------------------------------//
InnerWin.prototype.mousedown=function(event, key){
    var event=(document.all)? window.event : event;
    var mi=InnerWin.prototype.taskList[key];
    if(!mi) return;
    InnerWin.mi=mi;
    mi.IS_DOWN=true;
    mi.ctril.style.cursor="move";
    mi.X=event.clientX;
    mi.Y=event.clientY;
    mi.left= parseInt(mi.win.style.left);
    mi.top = parseInt(mi.win.style.top);
    document.onmouseup  = mi.mouseup;
    document.onmousemove= mi.mousemove;
}

//--------------------------------------------//
// 子ウィンド移動操作終了関数：
//--------------------------------------------//
InnerWin.prototype.mouseup=function(){
    InnerWin.mi.IS_DOWN=false;
    InnerWin.mi.ctril.style.cursor="default";
    window.status=    "";
    document.onmouseup  = 
    document.onmousemove= "";
}

//--------------------------------------------//
// 子ウィンド移動操作関数：
//--------------------------------------------//
InnerWin.prototype.mousemove=function(event){
    var mi=InnerWin.mi;
    if(mi.IS_DOWN){
        event = (document.all)? window.event : event;
        mi.left+=event.clientX-mi.X;
        mi.top +=event.clientY-mi.Y;
        var style = mi.win.style;
        style.left=mi.left+"px";
        style.top =mi.top +"px";
        mi.X=event.clientX;
        mi.Y=event.clientY;
    }
}


/*--------------------------------------------*/
// 子ウィンド横位置設定関数：
/*--------------------------------------------*/
InnerWin.prototype.setLeft=function(left){this.win.style.left=left+"px";}

/*--------------------------------------------*/
// 子ウィンド縦位置設定関数：
/*--------------------------------------------*/
InnerWin.prototype.setTop=function(top){this.win.style.top=top+"px";}

/*--------------------------------------------*/
// 子ウィンド位置設定関数：
/*--------------------------------------------*/
InnerWin.prototype.setPoint=function(left, top){this.setLeft(left);this.setTop(top);}

/*--------------------------------------------*/
// 子ウィンドサイズ設定関数：
/*--------------------------------------------*/
InnerWin.prototype.setSize=function(width, height){this.win.style.width=width;this.setHeight(height);}

/*--------------------------------------------*/
// 子ウィンド横幅設定関数：
/*--------------------------------------------*/
InnerWin.prototype.setWidth=function(width){
    width=width+"";
    if(width.indexOf("%",0)>0){
        this.win.style.width=width;
    } else this.win.style.width=width+"px";
}

/*--------------------------------------------*/
// 子ウィンド縦幅設定関数：
/*--------------------------------------------*/
InnerWin.prototype.setHeight=function(height){
    this.win.style.height=height+"px";
    var h=(!this.winName)? 8 : 23;
    this.canpas.style.height=(height-h)+"px";
}

/*--------------------------------------------*/
// 子ウィンドの背景色定関数：
/*--------------------------------------------*/
InnerWin.prototype.setBgcolor=function(color){this.canpas.style.backgroundColor=color;}

/*--------------------------------------------*/
// 子ウィンド削除する関数：
/*--------------------------------------------*/
InnerWin.prototype.setContent=function(c){
    this.content=c;
}

/*--------------------------------------------*/
// 子ウィンド開く関数：
/*--------------------------------------------*/
InnerWin.prototype.open=function(key){
    if(!key){
        this.win.style.display="";
        this.win.style.position="absolute";
        this.win.style.zIndex="99";
    }else
    if(InnerWin.prototype.taskList && InnerWin.prototype.taskList[key]){
        InnerWin.prototype.taskList[key].win.style.display="";
        InnerWin.prototype.taskList[key].win.style.position="absolute";
        InnerWin.prototype.taskList[key].win.style.zIndex="99";
    }else
    if(typeof(key.className)=="string"){
        var content=InnerWin.prototype.self.content;
        if(content && content!=key){
            content.close();
        }
        this.win.style.display="";
        this.win.style.position="absolute";
        this.win.style.zIndex="99";
    }
    InnerWin.prototype.self=this;
}

/*****************************************
 * 複数のウィンド操作の切り替え
 *****************************************/
InnerWin.setActive = function(event){
	var element=(document.all)? window.event.srcElement : event.target;
	for(;element.onmousedown!==InnerWin.setActive && element.parentNode;) element = element.parentNode;
	var winObj=null;
	for(var index in InnerWin.prototype.taskList) {
        if(InnerWin.prototype.taskList[index].win===element) InnerWin.prototype.self=InnerWin.prototype.taskList[index];
    }
}

InnerWin.close = function(event){
    InnerWin.prototype.self.close(InnerWin.prototype.self.key, true);
}

/*--------------------------------------------*/
// 子ウィンド閉じる関数：
// key：同時表示中の子ウィンド識別子
// is_cancle：閉じるボタン操作の識別
/*--------------------------------------------*/
InnerWin.prototype.close=function(key, is_cancle){
	if(this.content && this.content.fireClose){
		if(this.content.fireClose(is_cancle)) return;
	}
	if(!key) {this.win.style.display="none";return;}
	if(InnerWin.prototype.taskList[key]) InnerWin.prototype.taskList[key].win.style.display="none";
}

/*--------------------------------------------*/
// 子ウィンド削除する関数：
/*--------------------------------------------*/
InnerWin.prototype.finallize=function(){
    document.body.removeChild(this.win);
    this.content=null;
}

/**********************************************
 * 画面にユーザコンテンツ登録する関数
 **********************************************/
InnerWin.prototype.appendChild=function(contenct){
    this.canpas.appendChild(contenct);
}

/**********************************************
 * 画面にユーザコンテンツ登録する関数
 **********************************************/
InnerWin.prototype.appendToCtrl=function(contenct){
    this.ctrl.appendChild(contenct);
}

InnerWin.prototype.setBorderWidth=function(width){
    var w=parseInt(this.canpas.parentNode.style.margin);
    if(w==width) return;
    if(w-=width) this.canpas.style.height=(parseInt(this.canpas.style.height)+w*2)+"px";
    this.canpas.parentNode.style.margin=width+"px";
}
