/* Copyright (c) http://www.noma-soft.com/ */
XMLPaperMgr=function(){this.ctor();}
XMLPaperMgr.adurl="http://shinbun.us/blogstars/ad.php";
XMLPaperMgr.blogurl="";
XMLPaperMgr.count=0;
XMLPaperMgr.objects=new Object;
XMLPaperMgr.nextPage=function(tag,uid){XMLPaperMgr.objects[uid].nextPage(tag);}
XMLPaperMgr.prevPage=function(tag,uid){XMLPaperMgr.objects[uid].prevPage(tag);}
XMLPaperMgr.getElmValue=function(elm){
	if(elm.nodeValue!=null){return elm.nodeValue;}
	else if(elm.firstChild!=null){
		if(elm.firstChild.nodeValue){return elm.firstChild.nodeValue;}
		else{return "";}}
	else{return "";}}
XMLPaperMgr.prototype={
	ctor:function(){
		this.loaded=false;
		this.xmlIndex=0;this.xmlfiles=new Array;
		this.uid=XMLPaperMgr.count++;XMLPaperMgr.objects[this.uid]=this;
		this.paper=new Paper;this.objects=new Array;this.paperIndex=0;this.paperCount=0;this.pageNum=1;this.colIndex=0;},
	setup:function(desc){
		this.objects.push(new PaperPluginView("paper_object1",'',''));
		this.objects.push(new PaperPluginView("paper_object2",'',''));
		this.objects.push(new PaperPluginView("paper_object3",'',''));
		this.objects.push(new PaperPluginView("paper_object4",'',''));
		this.objects.push(new PaperPluginView("paper_object5",'',''));
		this.paper.bindObject(0,this.objects[0]);
		this.paper.bindObject(1,this.objects[1]);
		this.paper.bindObject(2,this.objects[2]);
		this.paper.bindObject(3,this.objects[3]);
		this.paper.bindObject(4,this.objects[4]);
		this.paper.modify();
	},
	registerXML:function(xmlfile){
		this.xmlfiles.push(xmlfile);},
	loadTop:function(){this.xmlIndex=0;this.paperIndex=0;
		this.load(this.xmlfiles[0],0);},
	load:function(filename,idx){
		if((this.filename==filename)&&(idx>this.paperCount)){return false;}
		this.paperIndex=idx;
		this.filename=filename;
		var xmlhttp=createXMLHttpRequest2();
		if(xmlhttp){
			if(xmlhttp.async){xmlhttp.async=false;}
			var e;
			try{
				xmlhttp.open('GET',filename,false);
				xmlhttp.send(null);
				var xmlDoc=xmlhttp.responseXML;
				var paperTags=xmlDoc.getElementsByTagName("paper");
				this.paperCount=paperTags.length;
				if(this.paperIndex==-1){this.paperIndex=this.paperCount-1;}
				if(this.paperIndex<this.paperCount){
					this.parsePaper(paperTags[this.paperIndex]);
					this.paper.modify();
					var adbox=this.paper.getAds();
					var ad=XMLPaperMgr.adurl;ad+="?";
					ad+="blog="+XMLPaperMgr.blogurl+"&";
					ad+="&width="+adbox.width_;
					ad+="&height="+adbox.height_;
					adbox.innerText='<iframe src="'+ad+'" width="'+adbox.width_+'" height="'+adbox.height_+'" marginheight="0" marginwidth="0" frameborder="0" scrolling="no"></iframe>';
					this.loaded=true;
					return true;
				}else{return false;}
			}catch(e){
				this.setup(null);
				return true;
			}
		}else{window.alert("fail to createXMLHttpRequest()");return false;}},
	isFirstPaper:function(){return ((this.paperIndex==0)&&(this.xmlIndex==0));},
	isLastPaper:function(){
		return ((this.paperIndex==(this.paperCount-1))
			&&((this.xmlfiles.length==0)||this.xmlIndex==(this.xmlfiles.length-1)));},
	loadFromHtml:function(){
		var tag;
		var prefix="htmldata_";
		tag=document.getElementById(prefix+"title");if(tag){this.paper.getTitle("label").innerText=tag.value;}
		tag=document.getElementById(prefix+"subtitle");if(tag){this.paper.getTitle("sub").innerText=tag.value;}
		tag=document.getElementById(prefix+"author");if(tag){this.paper.getHeader("left").innerText=tag.value;}
		tag=document.getElementById(prefix+"link");if(tag){this.paper.getHeader("center").innerText=tag.value;}
		tag=document.getElementById(prefix+"date");if(tag){this.paper.getHeader("right").innerText=tag.value;}
		tag=document.getElementById(prefix+"rayout");
		if(tag){this.paper.loadStyle(tag.value);this.paper.setStyle(this.paper.setting);}
		for(var idx=0;idx<5;idx++){
			var prefix2=prefix+"col"+idx+"_";
			tag=document.getElementById(prefix2+"title");
			if(tag){this.paper.getColumn(idx,"title").innerText=tag.value;}
			tag=document.getElementById(prefix2+"link");if(tag){}
			if(tag){this.paper.getColumn(idx,"title").href=tag.value;}
			tag=document.getElementById(prefix2+"body");if(tag){}
			if(tag){this.paper.getColumn(idx,"body").innerText=tag.value;}
			tag=document.getElementById(prefix2+"meta");
			if(tag){this.paper.loadColumnStyle(idx,tag.value);}
			tag=document.getElementById(prefix2+"object");if(tag){}
			if(tag){
				var ctrl=this.objects[idx].ctrl;
				ctrl.sendReleased();
				ctrl.selectPlugin(tag.getAttribute("type"),tag.value);
				ctrl.sendCaptured();
			}
		}
		this.paper.modify();
		var adbox=this.paper.getAds();
		var ad=XMLPaperMgr.adurl;ad+="?";
		ad+="blog="+XMLPaperMgr.blogurl+"&";
		ad+="&width="+adbox.width_;
		ad+="&height="+adbox.height_;
		adbox.innerText='<iframe src="'+ad+'" width="'+adbox.width_+'" height="'+adbox.height_+'" marginheight="0" marginwidth="0" frameborder="0" scrolling="no"></iframe>';
		this.loaded=true;
		},
	parsePaper:function(paperTag){
		if(!this.isFirstPaper()){this.paper.getFooter("left").innerText='<a href="javascript:void(0)" onclick="XMLPaperMgr.prevPage(this,'+this.uid+')">&lt;&lt;</a>';}
		else{this.paper.getFooter("left").innerText="";}
		this.paper.getFooter("center").innerText=this.pageNum+'ページ';
		if(!this.isLastPaper()){this.paper.getFooter("right").innerText='<a href="javascript:void(0)" onclick="XMLPaperMgr.nextPage(this,'+this.uid+')">&gt;&gt;</a>';}
		else{this.paper.getFooter("right").innerText='';}
		this.colIndex=0;
		var iter=paperTag.firstChild;
		while(iter!=null){
			switch(iter.tagName){
			case "title":this.paper.getTitle("label").innerText=XMLPaperMgr.getElmValue(iter);break;
			case "subtitle":this.paper.getTitle("sub").innerText=XMLPaperMgr.getElmValue(iter);break;
			case "author":this.paper.getHeader("left").innerText=XMLPaperMgr.getElmValue(iter);break;
			case "link":this.paper.getHeader("center").innerText=XMLPaperMgr.getElmValue(iter);break;
			case "body":/*TODO*/break;
			case "date":this.paper.getHeader("right").innerText=XMLPaperMgr.getElmValue(iter);break;
			case "rayout":this.parseRayout(iter);break;
			case "column":this.parseColumn(iter);break;
			default:break;}
			iter=iter.nextSibling;}},
	parseRayout:function(rayoutTag){
		this.paper.loadStyle(XMLPaperMgr.getElmValue(rayoutTag));this.paper.setStyle(this.paper.setting);},
	parseLink:function(linkTag){window.alert(linkTag.nodeValue);},
	parseColumn:function(columnTag){
		var iter=columnTag.firstChild;
		while(iter!=null){
			switch(iter.tagName){
			case "title":this.paper.getColumn(this.colIndex,"title").innerText=XMLPaperMgr.getElmValue(iter);break;
			case "link":this.paper.getColumn(this.colIndex,"title").href=XMLPaperMgr.getElmValue(iter);break;
			case "body":this.paper.getColumn(this.colIndex,"body").innerText=XMLPaperMgr.getElmValue(iter);break;
			case "meta":this.paper.loadColumnStyle(this.colIndex,XMLPaperMgr.getElmValue(iter));break;
			case "object":
				if(this.colIndex==this.objects.length){
					this.objects.push(new PaperPluginView("paper_object"+(this.colIndex+1),
						iter.getAttribute("type"),XMLPaperMgr.getElmValue(iter)));
					this.paper.bindObject(this.colIndex,this.objects[this.colIndex]);}
				else{
					var ctrl=this.objects[this.colIndex].ctrl;
					ctrl.sendReleased();
					ctrl.selectPluginObject(
						iter.getAttribute("type"),XMLPaperMgr.getElmValue(iter));
					ctrl.sendCaptured();}
				break;
			default:break;}
			iter=iter.nextSibling;}
		this.colIndex++;},
	nextPage:function(tag){
		this.paperIndex++;this.pageNum++;
		if(this.xmlfiles.length==0){
			if(this.paperIndex>=this.paperCount){this.paperIndex=0;this.pageNum=1;}
			this.load(this.filename,this.paperIndex);
		}else{
			if(this.paperIndex>=this.paperCount){this.paperIndex=0;this.xmlIndex++;}
			if(this.xmlIndex>=this.xmlfiles.length){this.xmlIndex=0;this.pageNum=1;}
			this.load(this.xmlfiles[this.xmlIndex],this.paperIndex);
		}
	},
	prevPage:function(tag){
		this.paperIndex--;this.pageNum--;
		if(this.xmlfiles.length==0){
			if(this.paperIndex<0){this.paperIndex++;this.pageNum++;}
			this.load(this.filename,this.paperIndex);
		}else{
			if(this.paperIndex<0){this.paperIndex=-1;this.xmlIndex--;}
			if(this.xmlIndex<0){this.xmlIndex=this.xmlfiles.length-1;this.pageNum=0;}
			this.load(this.xmlfiles[this.xmlIndex],this.paperIndex);
		}
	},
	bindEditCtrl:function(){
		if(this.objects.length>0){
			this.paper.bindColumnEditCtrl(0,new PaperColumnCtrl("col1","記事１",this.objects[0]));}
		if(this.objects.length>1){
			this.paper.bindColumnEditCtrl(1,new PaperColumnCtrl("col2","記事２",this.objects[1]));}
		if(this.objects.length>2){
			this.paper.bindColumnEditCtrl(2,new PaperColumnCtrl("col3","記事３",this.objects[2]));}
		if(this.objects.length>3){
			this.paper.bindColumnEditCtrl(3,new PaperColumnCtrl("col4","記事４",this.objects[3]));}
		if(this.objects.length>4){
			this.paper.bindColumnEditCtrl(4,new PaperColumnCtrl("col5","記事５",this.objects[4]));}
	}
}
function createXMLHttpRequest2(){
	var XMLhttpObject=null;
	try{XMLhttpObject=new XMLHttpRequest();}
	catch(e){
		try{XMLhttpObject=new ActiveXObject("Msxml2.XMLHTTP");}
		catch(e){
			try{XMLhttpObject=new ActiveXObject("Microsoft.XMLHTTP");}
			catch(e){return null;}}}
	return XMLhttpObject;
}

