/* Copyright(c) http://www.noma-soft.com/ */
var Paper_mode=0;
var Paper_showMode=0;
var Paper_editMode=1;

PaperSerializer=function(){this.namePad="^";this.fieldPad="|";}
PaperSerializer.prototype={
	serialize:function(obj){
		var output="";var is_first=1;
		for(key in obj){
			if((key)&&(obj[key])){
				if(is_first==1){is_first=0;output=key;output+=this.namePad;
					if((this.namePad.indexOf(this.fieldPad)==-1)){output+=obj[key];}
				}else{output+=this.fieldPad;output+=key;output+=this.namePad;
					if((this.namePad.indexOf(this.fieldPad)==-1)){output+=obj[key];}}}}
		return output;},
	deserialize:function(obj,str){
		if(str!=""){
			var fields=str.split(this.fieldPad);
			for(var idx=0;idx<fields.length;idx++){var elm=fields[idx].split(this.namePad); obj[elm[0]]=elm[1];}}}
}

PaperGroupSetSerializer=function(){}
PaperGroupSetSerializer.prototype={
	serialize:function(obj){
		var str="[";
		for(var idx=0;idx<obj.length;idx++){
			if(idx!=0){str+=",";}
			str+="{";str+=obj[idx];str+="}";}
		str+="]";return str;},
	deserialize:function(obj,text){
		var c;var nest=0;var lastIdx=0;
		for(var idx=0;idx<text.length;idx++){
			c=text.charAt(idx);
			if(c=='{'){if(nest==0){lastIdx=idx+1;}nest++;}
			else if(c=='}'){nest--;if(nest==0){obj.push(text.substring(lastIdx,idx));}}
			else if(c=='"'){idx++;while(idx<text.length){c=text.charAt(idx);if(c=='\\'){idx++;}else if(c=='"'){break;}idx++}}}}
}

Paper=function(){
	this.styleSerializer=new PaperSerializer;
	this.setting={obj1_width:320,obj1_height:240,obj2_width:240,obj2_height:160,obj3_width:240,obj3_height:160,obj4_width:240,obj4_height:160,obj5_width:240,obj5_height:160}
	this.config={
		paper:{tagId:"paper",type:"boxes_y",width:960,height:1400,
			margin:{right:10,left:10,top:10,bottom:10},
			xbackground:{color:"#eeeeee"}},
		header:{tagId:"pp_header",type:"boxes_px",height:20,
			children:[
			{tagId:"header_left",type:"text",weight:10,font:{align:"left"}},
			{tagId:"header_center",type:"text",weight:20,font:{align:"center"}},
			{tagId:"header_right",type:"text",weight:10,font:{align:"right"}}
			]},
		inner:{tagId:"pp_inner",type:"boxes_y",weight:10,
			padding:{left:2,top:2,right:2,bottom:2},
			border:{width:1,color:"green"},
			children:[
			{tagId:"row1",type:"boxes_nx",weight:10.5},
			{tagId:"y1pd",type:"box",height:4},
			{tagId:"row2",type:"boxes_px",weight:10},
			{tagId:"y2pd",type:"box",height:4},
			{tagId:"row3",type:"boxes_px",weight:10},
			{tagId:"y3pd",type:"box",height:4},
			{tagId:"ads",type:"text",height:240}
			]},
		footer:{tagId:"pp_footer",type:"boxes_px",height:20,
			children:[
			{tagId:"footer_left",type:"text",weight:10,font:{align:"left"}},
			{tagId:"footer_center",type:"text",weight:20,font:{align:"center"}},
			{tagId:"footer_right",type:"text",weight:10,font:{align:"right"}}]},
		title:{tagId:"pp_title",type:"boxes_y",weight:15,
			children:[
			{tagId:"title_label",type:"text_vr",weight:10,
				padding:{left:10,top:10,right:10,bottom:10},
				border:{width:10,color:"black"},
				font:{size:46}},
			{tagId:"title_sub",type:"text",height:35,
				margin:{left:0,top:1,right:0,bottom:1},
				border:{width:1,color:"black"},
				font:{align:"center"}}]},
		col1:{tagId:"col1",type:"boxes_y",weight:85,
			border:{width:1,color:"black"},
			children:[
			{tagId:"col1_title",type:"text",height:65,
				border:{width:5,color:"black"},padding:{top:4,bottom:4},
				font:{size:40,color:"white",align:"center"},background:{color:"gray"}},
			{tagId:"col1_body",type:"contents",weight:10,
				padding:{left:4,top:4,right:4,bottom:4},
				font:{size:18}},
			{tagId:"col1_meta",type:"boxes_px",height:15,
				background:{color:"#cccccc"},
				children:[
				{tagId:"col1_meta_left",type:"text",weight:10,font:{size:12,align:"left"}},
				{tagId:"col1_meta_center",type:"text",weight:20,font:{size:12,align:"center"}},
				{tagId:"col1_meta_right",type:"text",weight:10,font:{size:12,align:"right"}}]}]},
		col2:{tagId:"col2",type:"boxes_y",weight:52,
			border:{width:1,color:"black"},
			children:[
			{tagId:"col2_title",type:"text",height:48,
				border:{width:3,color:"black"},padding:{top:3,bottom:3},
				font:{size:32,color:"white",align:"center"},background:{color:"gray"}},
			{tagId:"col2_body",type:"contents",weight:10,
				padding:{left:4,top:4,right:4,bottom:4},
				font:{size:18}},
			{tagId:"col2_meta",type:"boxes_px",height:15,
				background:{color:"#cccccc"},
				children:[
				{tagId:"col2_meta_left",type:"text",weight:10,font:{size:12,align:"left"}},
				{tagId:"col2_meta_center",type:"text",weight:20,font:{size:12,align:"center"}},
				{tagId:"col2_meta_right",type:"text",weight:10,font:{size:12,align:"right"}}]}]},
		col3:{tagId:"col3",type:"boxes_y",weight:48,
			border:{width:1,color:"black"},
			children:[
			{tagId:"col3_title",type:"text",height:48,
				border:{width:3,color:"black"},padding:{top:3,bottom:3},
				font:{size:32,color:"white",align:"center"},background:{color:"gray"}},
			{tagId:"col3_body",type:"contents",weight:10,
				padding:{left:4,top:4,right:4,bottom:4},
				font:{size:18}},
			{tagId:"col3_meta",type:"boxes_px",height:15,
				background:{color:"#cccccc"},
				children:[
				{tagId:"col3_meta_left",type:"text",weight:10,font:{size:12,align:"left"}},
				{tagId:"col3_meta_center",type:"text",weight:20,font:{size:12,align:"center"}},
				{tagId:"col3_meta_right",type:"text",weight:10,font:{size:12,align:"right"}}]}]},
		col4:{tagId:"col4",type:"boxes_y",weight:48,
			border: {width:1,color:"black"},
			children:[
			{tagId:"col4_title",type:"text",height:48,
				border:{width:3,color:"black"},padding:{top:3,bottom:3},
				font:{size:32,color:"white",align:"center"},background:{color:"gray"}},
			{tagId:"col4_body",type:"contents",weight:10,
				padding:{left:4,top:4,right:4,bottom:4},
				font:{size:18}},
			{tagId:"col4_meta",type:"boxes_px",height:15,
				background:{color:"#cccccc"},
				children:[
				{tagId:"col4_meta_left",type:"text",weight:10,font:{size:12,align:"left"}},
				{tagId:"col4_meta_center",type:"text",weight:20,font:{size:12,align:"center"}},
				{tagId:"col4_meta_right",type:"text",weight:10,font:{size:12,align:"right"}}]}]},
		col5:{tagId:"col5",type:"boxes_y",weight:52,
			border:{width:1,color:"black"},
			children:[
			{tagId:"col5_title",type:"text",height:48,
				border:{width:3,color:"black"},padding:{top:3,bottom:3},
				font:{size:32,color:"white",align:"center"},background:{color:"gray"}},
			{tagId:"col5_body",type:"contents",weight: 10,
				padding:{left:4,top:4,right:4,bottom:4},
				font:{size:18}},
			{tagId:"col5_meta",type:"boxes_px",height:15,
				background:{color:"#cccccc"},
				children:[
				{tagId:"col5_meta_left",type:"text",weight:10,font:{size:12,align:"left"}},
				{tagId:"col5_meta_center",type:"text",weight:20,font:{size:12,align:"center"}},
				{tagId:"col5_meta_right",type:"text",weight:10,font:{size:12,align:"right"}}]}]},
		x1pd:{tagId:"x1pd",type:"box",width:4},
		x2pd:{tagId:"x2pd",type:"box",width:4},
		x3pd:{tagId:"x3pd",type:"box",width:4}
	}
	if(Paper_mode==Paper_editMode){
		this.setEditCtrlConfig("pp_title",this.config.title);
		this.setEditCtrlConfig("col1",this.config.col1);
		this.setEditCtrlConfig("col2",this.config.col2);
		this.setEditCtrlConfig("col3",this.config.col3);
		this.setEditCtrlConfig("col4",this.config.col4);
		this.setEditCtrlConfig("col5",this.config.col5);
	}
	this.objects=new Array;
	this.box = NmBoxFactory.createBox(this.config.paper);
	this.header={};
	this.header.box=NmBoxFactory.createBox(this.config.header);
	this.inner={};
	this.inner.box=NmBoxFactory.createBox(this.config.inner);
	this.footer={};
	this.footer.box=NmBoxFactory.createBox(this.config.footer);
	this.title={};
	this.title.box=NmBoxFactory.createBox(this.config.title);
	this.columns=[{},{},{},{},{}];
	this.columns[0].box=NmBoxFactory.createBox(this.config.col1);
	Paper.initColumnSetting_(this.columns[0]);
	this.columns[1].box=NmBoxFactory.createBox(this.config.col2);
	Paper.initColumnSetting_(this.columns[1]);
	this.columns[2].box=NmBoxFactory.createBox(this.config.col3);
	Paper.initColumnSetting_(this.columns[2]);
	this.columns[3].box=NmBoxFactory.createBox(this.config.col4);
	Paper.initColumnSetting_(this.columns[3]);
	this.columns[4].box=NmBoxFactory.createBox(this.config.col5);
	Paper.initColumnSetting_(this.columns[4]);
	this.box.appendChild(this.header.box);
	this.box.appendChild(this.inner.box);
	this.box.appendChild(this.footer.box);
	var inner_children=this.inner.box.children;
	this.rows=[{},{},{}];
	this.rows[0].box=inner_children[0];
	this.rows[1].box=inner_children[2];
	this.rows[2].box=inner_children[4];
	this.pads=[{},{},{}];
	var rows=this.rows;
	var box;
	box=rows[0].box;
	box.appendChild(this.title.box);
	box.appendChild(NmBoxFactory.createBox(this.config.x1pd));
	box.appendChild(this.columns[0].box);
	box=rows[1].box;
	box.appendChild(this.columns[1].box);
	box.appendChild(NmBoxFactory.createBox(this.config.x2pd));
	box.appendChild(this.columns[2].box);
	box=rows[2].box;
	box.appendChild(this.columns[3].box);
	box.appendChild(NmBoxFactory.createBox(this.config.x3pd));
	box.appendChild(this.columns[4].box);
}
Paper.initColumnSetting_=function(column){
	column.setting=new Object;
	column.setting.author="";
	column.setting.blog="";
	column.setting.date="";
}
Paper.prototype={
	setEditCtrlConfig:function(target,config){
		var subConfig;var children;
		if(config.children){children=config.children;}else{children=null;}
		subConfig=new Object;subConfig.tagId=target;subConfig.type=config.type;
		if(children!=null){subConfig.children=children;}
		config.type="boxes_l";config.tagId=target+"_dummy";
		config.selectedIndex=1;config.children=new Array;config.children.push(subConfig);
		subConfig=new Object;subConfig.tagId=target+"_ctrl";subConfig.type="contents";
		config.children.push(subConfig);},
	setRatio:function(setting){
		var box1;var box2;
		box1=NmBoxMgr.instances["row1"];
		if(setting['y1_ratio']){box1.weight=parseInt(setting['y1_ratio']);}else{box1.weight=10.5;}
		box1=NmBoxMgr.instances["row2"];
		if(setting['y2_ratio']){box1.weight=parseInt(setting['y2_ratio']);}else{box1.weight=10;}
		box1=NmBoxMgr.instances["row3"];
		if(setting['y3_ratio']){box1.weight=parseInt(setting['y3_ratio']);}else{box1.weight=10;}
		box1=NmBoxMgr.instances["pp_title"];box2=NmBoxMgr.instances["col1"];
		if(setting['x1_ratio']){box1.weight=parseInt(setting['x1_ratio']);}else{box1.weight=15;}
		box2.weight=100-box1.weight;
		box1=NmBoxMgr.instances["col2"];box2=NmBoxMgr.instances["col3"];
		if(setting['x2_ratio']){box1.weight=parseInt(setting['x2_ratio']);}else{box1.weight=52;}
		box2.weight=100-box1.weight;
		box1=NmBoxMgr.instances["col4"];box2=NmBoxMgr.instances["col5"];
		if(setting['x3_ratio']){box1.weight=parseInt(setting['x3_ratio']);}else{box1.weight=48;}
		box2.weight=100-box1.weight;
		if(Paper_mode==Paper_editMode){
			box1=NmBoxMgr.instances["pp_title_dummy"];box2=NmBoxMgr.instances["col1_dummy"];
			if(setting['x1_ratio']){box1.weight=parseInt(setting['x1_ratio']);}else{box1.weight=15;}
			box2.weight=100-box1.weight;
			box1=NmBoxMgr.instances["col2_dummy"];box2=NmBoxMgr.instances["col3_dummy"];
			if(setting['x2_ratio']){box1.weight=parseInt(setting['x2_ratio']);}else{box1.weight=52;}
			box2.weight=100-box1.weight;
			box1=NmBoxMgr.instances["col4_dummy"];box2=NmBoxMgr.instances["col5_dummy"];
			if(setting['x3_ratio']){box1.weight=parseInt(setting['x3_ratio']);}else{box1.weight=48;}
			box2.weight=100-box1.weight;
		}},
	loadStyle:function(style){this.styleSerializer.deserialize(this.setting,style);},
	loadColumnStyle:function(colNum,style){
		this.styleSerializer.deserialize(this.columns[colNum].setting,style);
		this.getColumn(colNum,"meta_left").innerText=this.columns[colNum].setting.author;
		this.getColumn(colNum,"meta_center").innerText=this.columns[colNum].setting.blog;
		this.getColumn(colNum,"meta_right").innerText=this.columns[colNum].setting.date;},
	storeStyle:function(){return this.styleSerializer.serialize(this.setting);},
	storeColumnStyle:function(colNum){return this.styleSerializer.serialize(this.columns[colNum].setting);},
	setColumnAuthor:function(colNum,author){
		this.columns[colNum].setting.author=author;this.getColumn(colNum,"meta_left").innerText=author;},
	setColumnBlog:function(colNum,blog){
		this.columns[colNum].setting.blog=blog;
		this.getColumn(colNum,"meta_center").innerText=blog;},
	setColumnDate:function(colNum,date){
		this.columns[colNum].setting.date=date;this.getColumn(colNum,"meta_right").innerText=date;},
	setStyle:function(setting){
		this.setRatio(setting);
		this.setBoxStyle("paper","",setting);
		this.setBoxStyle("pp_header","header",setting);
		this.setTextStyle("header_left","header",setting);
		this.setTextStyle("header_center","header",setting);
		this.setTextStyle("header_right","header",setting);
		this.setBoxStyle("pp_inner","inner",setting);
		this.setBoxStyle("pp_footer","footer",setting);
		this.setTextStyle("footer_left","footer",setting);
		this.setTextStyle("footer_center","footer",setting);
		this.setTextStyle("footer_right","footer",setting);
		this.setBoxStyle("pp_title","title",setting);
		this.setTextBoxStyle("title_label","title_label",setting);
		this.setTextBoxStyle("title_sub","title_sub",setting);
		this.setBoxStyle("col1","col1",setting);
		this.setTextBoxStyle("col1_title","col1_title",setting);
		this.setTextBoxStyle("col1_body","col1_body",setting);
		this.setTextBoxStyle("col1_meta","col1_meta",setting);
		this.setBoxStyle("col2","col2",setting);
		this.setTextBoxStyle("col2_title","col2_title",setting);
		this.setTextBoxStyle("col2_body","col2_body",setting);
		this.setTextBoxStyle("col2_meta","col2_meta",setting);
		this.setBoxStyle("col3","col3",setting);
		this.setTextBoxStyle("col3_title","col3_title",setting);
		this.setTextBoxStyle("col3_body","col3_body",setting);
		this.setTextBoxStyle("col3_meta","col3_meta",setting);
		this.setBoxStyle("col4","col4",setting);
		this.setTextBoxStyle("col4_title","col4_title",setting);
		this.setTextBoxStyle("col4_body","col4_body",setting);
		this.setTextBoxStyle("col4_meta","col4_meta",setting);
		this.setBoxStyle("col5","col5",setting);
		this.setTextBoxStyle("col5_title","col5_title",setting);
		this.setTextBoxStyle("col5_body","col5_body",setting);
		this.setTextBoxStyle("col5_meta","col5_meta",setting);
		this.setBoxStyle("x1pd","x1pd",setting);
		this.setBoxStyle("x2pd","x2pd",setting);
		this.setBoxStyle("x3pd","x3pd",setting);
		this.setBoxStyle("y1pd","y1pd",setting);
		this.setBoxStyle("y2pd","y2pd",setting);
		this.setBoxStyle("y3pd","y3pd",setting);
		this.setBoxStyle("row1","row1",setting);
		this.setBoxStyle("row2","row2",setting);
		this.setBoxStyle("row3","row3",setting);
		this.setObjectStyle("col1_body","obj1",setting);
		this.setObjectStyle("col2_body","obj2",setting);
		this.setObjectStyle("col3_body","obj3",setting);
		this.setObjectStyle("col4_body","obj4",setting);
		this.setObjectStyle("col5_body","obj5",setting);
	},
	getRayoutData:function(){return this.styleSerializer.serialize(this.setting);},
	setRayoutData:function(data){
		this.styleSerializer.deserialize(this.setting,data);
		this.setStyle(this.setting);},
	setTextBoxStyle:function(target,source,setting){
		this.setTextStyle(target,source,setting);this.setBoxStyle(target,source,setting);},
	setTextStyle:function(target,source,setting){
		var box=NmBoxMgr.instances[target];
		if(!box)return;
		var prefix="";var attrName;
		if(source!=""){prefix=source+"_";}
		attrName=prefix+"fontSize";
		if(setting[attrName]){if(!box.font){box.font=new Object;}box.font.size=parseInt(setting[attrName]);}
		attrName=prefix+"fontFamily";
		if(setting[attrName]){if(!box.font){box.font=new Object;}box.font.family=setting[attrName];}
		attrName=prefix+"fontColor";
		if(setting[attrName]){if(!box.font){box.font=new Object;}box.font.color=setting[attrName];}},
	setBoxStyle:function(target,source,setting){
		var box=NmBoxMgr.instances[target];
		if(!box)return;
		var prefix="";var attrName;
		if(source!=""){prefix=source+"_";}
		attrName=prefix+"float";
		if(setting[attrName]){
			if((box.type=="boxes_px")||(box.type=="boxes_nx")){
				if(setting[attrName]=="left"){box.type="boxes_px";}else{box.type="boxes_nx";}}}
		attrName=prefix+"width";
		if(setting[attrName]){box.width=parseInt(setting[attrName]);}
		attrName=prefix+"height";
		if(setting[attrName]){box.height=parseInt(setting[attrName]);}
		attrName=prefix+"bgColor";
		if(setting[attrName]){if(!box.background){box.background=new Object;}box.background.color=setting[attrName];}
		attrName=prefix+"bgImage";
		if(setting[attrName]){if(!box.background){box.background=new Object;}box.background.image=setting[attrName];}
		attrName=prefix+"xBgColor";
		if(setting[attrName]){if(!box.xbackground){box.xbackground=new Object;}box.xbackground.color=setting[attrName];}		attrName=prefix+"xBgImage";
		if(setting[attrName]){if(!box.xbackground){box.xbackground=new Object;}box.xbackground.image=setting[attrName];}		if(setting[prefix+"mgnL"]||setting[prefix+"mgnT"]||setting[prefix+"mgnR"]||setting[prefix+"mgnB"]){
			box.margin=new NmRect;
			attrName=prefix+"mgnL";if(setting[attrName]){box.margin.left=parseInt(setting[attrName]);}
			attrName=prefix+"mgnT";if(setting[attrName]){box.margin.top=parseInt(setting[attrName]);}
			attrName=prefix+"mgnR";if(setting[attrName]){box.margin.right=parseInt(setting[attrName]);}
			attrName=prefix+"mgnB";if(setting[attrName]){box.margin.bottom=parseInt(setting[attrName]);}}
		if(setting[prefix+"padL"]||setting[prefix+"padT"]||setting[prefix+"padR"]||setting[prefix+"padB"]){
			box.padding=new NmRect;
			attrName=prefix+"padL";if(setting[attrName]){box.padding.left=parseInt(setting[attrName]);}
			attrName=prefix+"padT";if(setting[attrName]){box.padding.top=parseInt(setting[attrName]);}
			attrName=prefix+"padR";if(setting[attrName]){box.padding.right=parseInt(setting[attrName]);}
			attrName=prefix+"padB";if(setting[attrName]){box.padding.bottom=parseInt(setting[attrName]);}}
		attrName=prefix+"bdrWidth";
		if(setting[attrName]){if(!box.border){box.border=new Object;}box.border.width=parseInt(setting[attrName]);}
		attrName=prefix+"bdrColor";
		if(setting[attrName]){if(!box.border){box.border=new Object;}box.border.color=setting[attrName];}},
	setObjectStyle:function(target,source,setting){
		var box=NmBoxMgr.instances[target];
		if(!box)return;
		if(!box.object)return;
		var prefix="";var attrName;
		if(source!=""){prefix=source+"_";}
		if(setting[prefix+"width"]){box.object.width=parseInt(setting[prefix+"width"]);}
		if(setting[prefix+"height"]){box.object.height=parseInt(setting[prefix+"height"]);}
		if(setting[prefix+"align"]){box.object.align=setting[prefix+"align"];}},
	init:function(){this.box.init();},
	write:function(){document.write(this.box.write());
		if(Paper_mode==Paper_showMode){
			for(var index=0;index<this.objects.length;index++){document.write(this.objects[index].writeCtrl());}}},
	writeStyle:function(){document.write(this.box.writeStyle());},
	modify:function(){this.box.modify();},
	bindObject:function(colNum,object){
		var box=NmBoxMgr.instances["col"+(colNum+1)+"_body"];
		if(box){box.object=object;box.objectAlign="right";object.paper=this;object.columnIndex=colNum;
			object.width=parseInt(this.setting["obj"+(colNum+1)+"_width"]);
			object.height=parseInt(this.setting["obj"+(colNum+1)+"_height"]);
			this.objects.push(object);}},
	bindTitleEditCtrl:function(editCtrl){var box=NmBoxMgr.instances["title_ctrl"];
		if(box){box.object=editCtrl;}
		editCtrl.paper=this;},
	bindColumnEditCtrl:function(colNum,editCtrl){var box=NmBoxMgr.instances["col"+(colNum+1)+"_ctrl"];
		if(box){box.object=editCtrl;}
		editCtrl.paper=this;editCtrl.columnIndex=colNum;},
	setObjSetting:function(idx,name,v){this.setting["obj"+idx+"_"+target]=v;},
	getObjSetting:function(idx,name){return this.setting["obj"+idx+"_"+name];},
	getHeader:function(target){return NmBoxMgr.instances["header"+"_"+target];},
	getFooter:function(target){return NmBoxMgr.instances["footer"+"_"+target];},
	getTitle:function(target){return NmBoxMgr.instances["title"+"_"+target];},
	getColumn:function(colNum,target){return NmBoxMgr.instances["col"+(colNum+1)+"_"+target];},
	getAds:function(target){return NmBoxMgr.instances["ads"];}
}
