PaperWindow=function(id,width,height){this.ctor(id,width,height);}
PaperWindow.windows=new Object;
PaperWindow.prototype={
	ctor:function(id,width,height){PaperWindow.windows[id]=this;
		this.title="";this.isVisible=true;
		this.id=id;this.width=width;this.x=0;this.y=0;this.height=height;this.opacity=70;},
	init:function(){
		this.win=document.getElementById(this.id);
		this.bar=document.getElementById(this.id+"_bar");
		this.bar.style.cursor="move";
	},
	bindXMLLoader:function(xmlLoader){this.xmlLoader=xmlLoader;},
	write:function(){
		var str="";
		str+='<div id="'+this.id+'" style="position:absolute;filter:alpha(opacity='+this.opacity+');-moz-opacity:'
			+this.opacity/100+';opacity:'+this.opacity/100+';width:'+this.width+'px;height:'+this.height+'px;left:'
			+this.x+'px;top:'+this.y+'px;border:solid 1px black;background-color:#CCCCFF;';
		if(this.isVisible==false){str+='display:none;';}
		str+='">';
		str+='<div id="'+this.id+'_bar" style="background-color:gray;">';
		str+='<span style="border:solid blue 1px;background-color:white;"><a href="javascript:void(0);" onclick="PaperWindow.close(\''+this.id+'\');">X</a></span>'+this.title+'</div>';
		if(this.view){str+=this.view.write();}
		str+='</div>';
		return str;
	},
	setPosition:function(x,y){this.x=x;this.y=y;this.win.style.left=x+'px';this.win.style.top=y+'px';},
	setVisible:function(isVisible){
		this.isVisible=isVisible;
		if(isVisible){this.win.style.display="block";}
		else{this.win.style.display="none";}},
	mousemove:function(ev){
		if(this.captured==true){
			this.win.style.left=(parseInt(this.win.style.left)+(ev.clientX-this.lastX))+"px";
			this.win.style.top=(parseInt(this.win.style.top)+(ev.clientY-this.lastY))+"px";
			this.lastX=ev.clientX;this.lastY=ev.clientY;}},
	mousedown:function(ev){
		var target="";
		if(ev.target){target=ev.target;}
		else if(ev.srcElement){target=ev.srcElement;}
		if(target&&(target==this.bar)){
			this.captured=true;this.lastX=ev.clientX;this.lastY=ev.clientY;
	}},
	mouseup:function(ev){this.captured=false;},
	close:function(){this.setVisible(false);}
}

PaperWindow.close=function(id){PaperWindow.windows[id].close();}
PaperWindow.mousemove=function(){var ev=arguments.callee.caller.arguments[0] || window.event;
	for(var key in PaperWindow.windows){PaperWindow.windows[key].mousemove(ev);}return false;}
PaperWindow.mousedown=function(){var ev=arguments.callee.caller.arguments[0] || window.event;
	for(var key in PaperWindow.windows){PaperWindow.windows[key].mousedown(ev);}return false;}
PaperWindow.mouseup=function(){var ev=arguments.callee.caller.arguments[0] || window.event;
	for(var key in PaperWindow.windows){PaperWindow.windows[key].mouseup(ev);}return false;}
PaperWindow.viewHandler=function(cmd){
	for(var key in PaperWindow.windows){
		var window=PaperWindow.windows[key];
		if(window.view&&window.view.viewHandler){window.view.viewHandler(cmd);}}}

PaperCtrlView=function(){}
PaperCtrlView.prototype={
	bindXMLLoader:function(xmlLoader){this.xmlLoader=xmlLoader;},
	write:function(){
		var str="";
		str+='<div align="center"><a href="http://shinbun.us" target="shinbun.us"><img src="'+this.imgDirPath+'stars.gif" alt="shinbun.us" title="shinbun.us"/></a></div>';
		if(this.xmlLoader){
			str+="ページ:";
			str+=this.xmlLoader.writePrevTag("&lt;&lt;前へ");
			str+=this.xmlLoader.writeNextTag("次へ&gt;&gt;");
			str+='<br/>';
		}
		return str;
	}
}

PaperCtrlWindow=function(id,width,height){this.ctor(id,width,height);}
PaperCtrlWindow.prototype={
	ctor:function(id,width,height){
		this.window=new PaperWindow(id,width,height);
		this.window.title="操作ウインドウ";
		this.view=new PaperCtrlView;
		this.view.imgDirPath="mt-static/paper/img/";
		this.window.view=this.view;
	},
	init:function(){this.window.init();},
	write:function(){
		if(this.x){this.window.x=this.x;}
		if(this.y){this.window.y=this.y;}
		if(this.imgDirPath){this.view.imgDirPath=this.imgDirPath;}
		return this.window.write();},
	bindXMLLoader:function(xmlLoader){this.view.xmlLoader=xmlLoader;}
}

// this is for compatibility
PaperCtrlWindow.close=function(id){PaperWindow.windows[id].close();}
PaperCtrlWindow.mousemove=function(){var ev=arguments.callee.caller.arguments[0] || window.event;
	for(var key in PaperWindow.windows){PaperWindow.windows[key].mousemove(ev);}return false;}
PaperCtrlWindow.mousedown=function(){var ev=arguments.callee.caller.arguments[0] || window.event;
	for(var key in PaperWindow.windows){PaperWindow.windows[key].mousedown(ev);}return false;}
PaperCtrlWindow.mouseup=function(){var ev=arguments.callee.caller.arguments[0] || window.event;
	for(var key in PaperWindow.windows){PaperWindow.windows[key].mouseup(ev);}return false;}


PaperColumnView=function(owner){this.ctor(owner);}
PaperColumnView.prototype={
	ctor:function(owner){this.owner=owner;},
	init:function(){
		this.viewTag=document.getElementById(this.owner.id+'_view');},
	write:function(){
		var str="";
		str+='<div id="'+this.owner.id+'_view" style="background-color:white;margin:10px;overflow:auto;width:'
			+(this.owner.window.width-20)+'px;height:'
			+(this.owner.window.height-40)+'px;">';
		str+='</div>';
		return str;
	},
	viewHandler:function(cmd){
		this.owner.window.setVisible(true);
		switch(cmd){
		case "column1":
			this.owner.setPosition(100,100);
			this.viewTag.innerHTML=this.xmlLoader.paper.getColumn(0,'body').innerText;break;
		case "column2":
			this.owner.setPosition(100,350);
			this.viewTag.innerHTML=this.xmlLoader.paper.getColumn(1,'body').innerText;break;
		case "column3":
			this.owner.setPosition(200,350);
			this.viewTag.innerHTML=this.xmlLoader.paper.getColumn(2,'body').innerText;break;
		case "column4":
			this.owner.setPosition(100,600);
			this.viewTag.innerHTML=this.xmlLoader.paper.getColumn(3,'body').innerText;break;
		case "column5":
			this.owner.setPosition(200,600);
			this.viewTag.innerHTML=this.xmlLoader.paper.getColumn(4,'body').innerText;break;
		default:break;
		}
	}
}

PaperColumnWindow=function(id,width,height){this.ctor(id,width,height);}
PaperColumnWindow.prototype={
	ctor:function(id,width,height){
		this.window=new PaperWindow(id,width,height);
		this.window.title="記事ウインドウ";
		this.window.isVisible=false;
		this.window.opacity=90;
		this.view=new PaperColumnView(this);
		this.window.view=this.view;
	},
	init:function(){this.window.init();this.view.init();},
	setPosition:function(x,y){this.window.setPosition(x,y);},
	write:function(){return this.window.write();},
	bindXMLLoader:function(xmlLoader){this.view.xmlLoader=xmlLoader;
		var paper=xmlLoader.paper;
		if(!paper.getColumn(0,"title").href){
			paper.getColumn(0,"title").onclick="PaperWindow.viewHandler('column1');";}
		if(!paper.getColumn(1,"title").href){
			paper.getColumn(1,"title").onclick="PaperWindow.viewHandler('column2');";}
		if(!paper.getColumn(2,"title").href){
			paper.getColumn(2,"title").onclick="PaperWindow.viewHandler('column3');";}
		if(!paper.getColumn(3,"title").href){
			paper.getColumn(3,"title").onclick="PaperWindow.viewHandler('column4');";}
		if(!paper.getColumn(4,"title").href){
			paper.getColumn(4,"title").onclick="PaperWindow.viewHandler('column5');";}
	}
}
