﻿function clktab(obj)
{
	var id=obj.substr(3,1);
	for(var i=0;i<10;i++)
	{
		if( document.getElementById("tab"+i))  document.getElementById("tab"+i).className="";
		if(document.getElementById("menu"+i)) document.getElementById("menu"+i).style.display="none";
	}
	if(document.getElementById("tab"+id)) document.getElementById("tab"+id).className="on";
	if(document.getElementById("menu"+id)) document.getElementById("menu"+id).style.display="block";
}


ESUNChart={};
/* 全局控制 */
ESUNChart.ini={
	default_has_line:false,
	map:[]/* 数字到字符映射 */
}

ESUNChart.CSS=function(obj,v){
	var _style=obj.style;
	if (_style[v])return _style[v];  
    if (obj.currentStyle) return obj.currentStyle[v]
    if (document.defaultView && document.defaultView.getComputedStyle){ 
            v = v.replace(/([A-Z])/g,"-$1").toLowerCase();
            var s = document.defaultView.getComputedStyle(obj,""); 
            return s && s.getPropertyValue(v); 
    }
    return null; 
}

ESUNChart.insertAfter=function(newElement,targetElement){
	var parent=targetElement.parentNode;
	if(parent.lastChild==targetElement){
		return parent.appendChild(newElement);
	}else{
		return parent.insertBefore(newElement,targetElement.nextSibling);
	};
}

/* 形状分组  */
SG=function(table,_x,_y,width,margin_bottom,fn_check){
	this.cloner=false;
	/* group style */
	this.ballKey="chartBall";
	this.ylKey="yl";
	this.isHiddenYL=false;
	
	this.defCSS="chartBall01";
	this.gYLCSS="yl01";
	this.gHCSS="chartBall05";
	this.gH3CSS="chartBall06";
	this.gVCSS="chartBall03";
	this.gV3CSS="chartBall04";
	this.gXCSS="chartBall07";
	this.gX3CSS="chartBall08";
	this.gXLCSS="hot";
	this.gXL3CSS="yhot";
	this.gXRCSS="hot";
	this.gXR3CSS="yhot";
	this.gTBCSS="chartBall02";
	/* group */
	this.gV=[];
	this.gV3=[];	
	this.gH=[];
	this.gH3=[];	
	this.gXL=[];
	this.gXL3=[];
	this.gXR=[];
	this.gXR3=[];
	this.gX=[];
	this.gX3=[];
	this.gYL=[];
	this.gAll=[];
	this.gTB=[];
	this.gCustom=[];
	
	var trs=document.getElementById(table).rows;	
	var rect={x:_x||0,y:_y||0,w:width||0,oh:margin_bottom||0};	
	var row_start=rect.y<0?(trs.length+rect.y):rect.y;
	var row_end=trs.length-rect.oh;
	var col_start=rect.x<0?(trs[row_start].cells.length+rect.x):rect.x;
	var col_end=col_start+rect.w;
	if(col_end>trs[row_start].cells.length)col_end=trs[row_start].cells.length;	
	if(rect.w<=0)col_end=trs[row_start].cells.length+rect.w;
	/* custom */
	this.check=fn_check||function(){};
	for(var i=row_start;i<row_end;i++){/* each and grouping */
		var tr=trs[i].cells;
		for(var j=col_start;j<col_end;j++){
			var td=tr[j];
			/* check外置接口可操纵当前td，并中断后续操作 */
			if(this.check(td,j,i)===false)continue;
			/* 特别号过滤 */	
			if(null!=td.getAttribute("tb")){this.gTB.push(td);}	
			/* return gYL */					
			if(td.className.indexOf(this.ballKey)==-1){/* not ball  */
				this.gYL.push(td);
				td._bgColor=ESUNChart.CSS(td,"backgroundColor");
				if(td._bgColor=="transparent")td._bgColor="#F3F2F2";
				td._color=ESUNChart.CSS(td,"color");
				td.value=td.innerHTML;
				continue;
			};
			/* return aAll */
			this.gAll.push(td);
			var n=0,x=j,y=i;
			/* grouping start */
			while((--y)>=row_start&&trs[y].cells[x].className.indexOf(this.ballKey)!=-1){n++};y=i;
			while((++y)<row_end&&trs[y].cells[x].className.indexOf(this.ballKey)!=-1)n++;
			if(n>0){if(n==2)this.gV3.push(td);this.gV.push(td)};
			/* V and V3 end */
			n=0,x=j,y=i;
			while((--x)>=col_start&&tr[x].className.indexOf(this.ballKey)!=-1){n++};x=j;
			while((++x)<col_end&&tr[x].className.indexOf(this.ballKey)!=-1)n++;
			if(n>0){if(n==1)this.gH3.push(td);this.gH.push(td)};
			/* H and H3 end */
			n=0,x=j,y=i;
			while((--y)>=row_start&&(--x)>=col_start&&trs[y].cells[x].className.indexOf(this.ballKey)!=-1){n++};x=j;y=i;
			while((++y)<row_end&&(++x)<col_end&&trs[y].cells[x].className.indexOf(this.ballKey)!=-1)n++;
			if(n>0){if(n>=2)this.gXL3.push(td);this.gXL.push(td)};
			/* XL and XL3 end */
			n=0,x=j,y=i;
			while((--y)>=row_start&&(++x)<col_end&&trs[y].cells[x].className.indexOf(this.ballKey)!=-1){n++};x=j;y=i;
			while((++y)<row_end&&(--x)>=col_start&&trs[y].cells[x].className.indexOf(this.ballKey)!=-1)n++;
			if(n>0){if(n>=2)this.gXR3.push(td);this.gXR.push(td)};
			/* XR and XR3 end */
			this.gX=this.gXL.concat(this.gXR);
			this.gX3=this.gXL3.concat(this.gXR3);
		};
	};
};

/* 分组的可见性控制 */
SG.prototype={
	show:function(type,yes){
		if(!this[type])return;
		var fLen=this[type].length;
		//var sTime=new Date();
		for(var i=0;i<fLen;i++){
			var _this=this[type][i];
			switch(type){
				case "gYL":{
					if(yes){
						if(_this.className.indexOf(this.gTBCSS)==-1)_this.style.color=_this._bgColor;
					}else{
						if(_this.className.indexOf(this.gTBCSS)==-1)_this.style.color=_this._color;
					}
					this.isHiddenYL=yes;					
					break;
				};
				case "gTB":{
					if(yes){
						_this.className+=" "+this.gTBCSS;
						_this.innerHTML=_this.getAttribute("tb");
					}else{
						_this.className=_this.className.replace(eval("/"+this.gTBCSS+" | "+this.gTBCSS+"$/"),'')
						_this.innerHTML=!this.isHiddenYL?_this.value:"<span style='visibility:hidden'>"+_this.value+"</span>";					
					}
					break;
				};
				default:{
					if(yes)_this.className+=" "+this[type+"CSS"];
					else _this.className=_this.className.replace(eval("/"+this[type+"CSS"]+" | "+this[type+"CSS"]+"$/"),'');					
				}
			}
		};//for end
		//var eTime=new Date();
		//alert(eTime-sTime)
	},
/* 对单元格设置点击响应，无依赖性，仅仅方便而挂在例程中，预置为样式切换,如果第二参数为函数，自动指向函数入口 */
	addClick:function(td,className_or_fn){
			td.style.cursor="pointer";
			if(typeof(className_or_fn)=="function"){return (td.onclick=className_or_fn)};
			td.onclick=function(){
				if(this.innerHTML==this.number){
					this.innerHTML="&nbsp;";
					this.className=this.className.replace(new RegExp(" ?"+className_or_fn),"");
				}else{
					this.innerHTML=this.number;
					this.className+=" "+className_or_fn;
				};
			};
	},
	/* 设置形状组的显隐 */
	set_on_off:function(on_off_name,groupName){
		var cks=on_off_name.split(",");
		var types=groupName.split(",");
		var _this=this;
		for(var i=0;i<cks.length;i++){
		(function(i){document.getElementById(cks[i]).onclick=function(){
			_this.show(types[i],this.checked)}})(i);
		};
	}	
};

/* 多分组形状对象  --------------------------------------------------------------------*/
oDFZ={
	vg:[],
	fg:[],
	table:false,
	add:function(x,y,w,mb){
		this.fg.push(new SG(this.table,x,y,w,mb));
		return this;
	},
	about:function(v){
		this.table=v;
		return this;
	},
	/* 绑定显隐复选框 */
	bind:function(on_off_name,groupName){
		var cks=on_off_name.split(",");
		var types=groupName.split(",");
		var _this=this;
		for(var i=0;i<cks.length;i++){
		(function(i){document.getElementById(cks[i]).onclick=function(){
			for(var j=0;j<_this.fg.length;j++)
			_this.fg[j].show(types[i],this.checked)
		}})(i);
		};
	}
}


/* 连线类  --------------------------------------------------------------------*/
JoinLine=function(color,size){
	this.color=color||"#000000";
	this.size=size||1;
	this.lines=[];
	this.tmpDom=null;
	this.visible=true;
};
JoinLine.prototype={
	show:function(yes){
		for(var i=0;i<this.lines.length;i++)
			this.lines[i].style.visibility=yes?"visible":"hidden";		
	},
	remove:function(){
		for(var i=0;i<this.lines.length;i++)
			this.lines[i].parentNode.removeChild(this.lines[i]);
		this.lines=[];		
	},
	join:function(objArray,hide,fn){
		this.remove();
		this.visible=hide?"visible":"hidden";
		this.tmpDom=document.createDocumentFragment();		
		for(var i=0;i<objArray.length-1;i++){
			var a=this.pos(objArray[i]);
			var b=this.pos(objArray[i+1]);
			/* 通过比对两个值来决策绘制与否 */
			if(fn&&fn(a,b)===false)continue;
			if(document.all){
				this.IELine(a.x,a.y,b.x,b.y)
			}else{
				this.FFLine(a.x,a.y,b.x,b.y)
			};
		};
		document.body.appendChild(this.tmpDom);		
	},
	 pos:function(obj){
		var pos={x:0,y:0},a=obj;
		for(;a;a=a.offsetParent){pos.x+=a.offsetLeft;pos.y+=a.offsetTop};
		pos.x+=parseInt(obj.offsetWidth/2);
		pos.y+=parseInt(obj.offsetHeight/2);
		return pos;
	},
	_oldDot:function (x,y,color,size){
		var dot=document.createElement("DIV");
		dot.style.cssText="position: absolute; left: "+x+"px; top: "+y+"px;background: "+color+";width:"+size+"px;height:"+size+"px;font-size:1px;overflow:hidden";
		dot.style.visibility=this.visible;
		this.lines.push(this.tmpDom.appendChild(dot));
	},
	_oldLine:function(x1,y1,x2,y2){
		var r=Math.floor(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
		var theta=Math.atan((x2-x1)/(y2-y1));
		if(((y2-y1)<0&&(x2-x1)>0)||((y2-y1)<0&&(x2-x1)<0))	theta=Math.PI+theta;
		var dx=Math.sin(theta),dy=Math.cos(theta),i=0;
		do{this.FFDot(x1+i*dx,y1+i*dy,this.color,this.size)}while(i++<r);
	},
	FFLine:function(x1,y1,x2,y2){
		var np=this.nPos(x1,y1,x2,y2,JoinLine.indent);//两端缩减函数（防止覆盖球）
		x1=np[0];y1=np[1];x2=np[2];	y2=np[3];
		var cvs=document.createElement("canvas");
		cvs.style.position="absolute";
		cvs.style.visibility=this.visible;
		cvs.width=Math.abs(x1-x2)||this.size;
		cvs.height=Math.abs(y1-y2);
		var newY=Math.min(y1,y2);
		var newX=Math.min(x1,x2);
		cvs.style.top=newY+"px";
		cvs.style.left=newX+"px";
		var FG=cvs.getContext("2d");
		FG.save();//缓存历史设置
		FG.strokeStyle=this.color;
		FG.lineWidth=this.size;
		//FG.globalAlpha=0.5;//透明度；	
		FG.beginPath(); 
		FG.moveTo(x1-newX,y1-newY);
		FG.lineTo(x2-newX,y2-newY);
		FG.closePath();
		FG.stroke();
		FG.restore();//恢复历史设置
		this.lines.push(cvs);
		this.tmpDom.appendChild(cvs);		
	},	
	IELine:function(x1,y1,x2,y2){
		var np=this.nPos(x1,y1,x2,y2,JoinLine.indent);//两端缩减函数（防止覆盖球）
		x1=np[0];y1=np[1];x2=np[2];	y2=np[3];		
		var line = document .createElement( "<esun:line></esun:line>" );
		line.from=x1+","+y1;
		line.to=x2+","+y2;
		line.strokeColor=this.color;
		line.strokeWeight=this.size+"px";
		line.style.cssText="position:absolute;z-index:999;top:0;left:0";
		line.style.visibility=this.visible;
		line.coordOrigin="0,0";
		this.lines.push(line);
		this.tmpDom.appendChild(line);
	},
	nPos:function(x1, y1, x2, y2, r){
		var a = x1 - x2, b = y1 - y2;
		var c = Math.round(Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)));
		var x3, y3, x4, y4;
		var _a = Math.round((a * r)/c);
		var _b = Math.round((b * r)/c);
		return [x2 + _a, y2 + _b, x1 - _a, y1 - _b]; 
	}
};

JoinLine.indent=8;

/* 过滤搜索连线操纵类 --------------------------------------------------------------------*/
LG=function(table,_x,_y,width,margin_bottom,fn_check){
	var rect={x:_x||0,y:_y||0,w:width||0,oh:margin_bottom||0};	
	var trs=document.getElementById(table).rows;
	var row_start=rect.y<0?(trs.length+rect.y):rect.y;
	var row_end=trs.length-rect.oh;
	var col_start=rect.x<0?(trs[row_start].cells.length+rect.x):rect.x;
	var col_end=col_start+rect.w;
	if(col_end>trs[row_start].cells.length)col_end=trs[row_start].cells.length;	
	if(rect.w==0)col_end=trs[row_start].cells.length;	
	this.g=[];
	//alert([row_start,row_end,col_start,col_end])
	for(var i=row_start;i<row_end;i++){/* each and grouping */
		var tr=trs[i].cells;
		for(var j=col_start;j<col_end;j++){
			var td=tr[j];
			/* 检测器返回绝对真时，单元格才被添加到组 */
			if(fn_check(td,j,i)===true)this.g.push(td);
		};
	};
	if(LG.autoDraw)this.draw();
};
LG.color='#E4A8A8';
LG.size=2;
LG.autoDraw=true;/* 默认自动绘线 */
LG.isShow=true;
LG.filter=function(){};
LG.prototype={
	draw:function(color,size,fn){
		this.line=new JoinLine(color||LG.color,size||LG.size);
		if(!fn)fn=LG.filter;
		this.line.join(this.g,LG.isShow,fn);
	},
	clear:function(){
		this.line.remove();
	},
	show:function(yes){this.line.show(yes)}
}

/* 批量绘线对象 -----------------------------------------------------------------------------------
设置表格；
设置开关；
设置检测函数；
添加块；x坐标从0开始
显示；
修改模式；
添加；
再显示；
*/
oZXZ={
	vg:[],
	lg:[],
	_vg:[],
	_lg:[],
	table:false,
	check:function(td){return !!(td.className.indexOf("chartBall")!=-1)},
	on_off:true,
	_on:true,/* 开关反作用 */
	novl:false,/* 忽略垂直线 */
	bind:function(tid,_on_off){
		this.table=tid;
		this.on_off=_on_off;
		return this;
	},
	color:function(c){
		LG.color=c;
		return this;
	},	
	newCheck:function(fn){
		this.check=fn;
		return this;
	},	
	draw:function(yes){
		if(!this.table)return;
		if(yes){
			var qL=this.vg.length;
			for(var i=0;i<qL;i++){
				var it=this.vg[i];
				LG.color=it.color;
				JoinLine.indent=it.indent;
				this.novl=it.novl;
				if(this.novl)LG.filter=function(a,b){return !(a.x==b.x)};
				//alert([it[0],it[1],it[2],it[3]])	
				//var t1=new Date();			
				this.lg.push(new LG(this.table,it[0],it[1],it[2],it[3],this.check));
				//var t2=new Date();
				//alert(t2-t1)
			}
		}
		if(this.on_off){
			var _this=this;
			document.getElementById(this.on_off).onclick=function(){
				var yes=_this._on?this.checked:!this.checked;
				_this.show(yes);
			};	
		}
		/* 转移与清空历史记录，等待下一次添加 */
		this._vg=this._vg.concat(this.vg);
		this.vg=[];
		this._lg=this._lg.concat(this.lg);
		this.lg=[];
		return this;
	},
	show:function(yes){
		/* 如果没有线则重绘一次 */
		if(this._lg.length==0)this.redraw();
		var qL=this._lg.length;
		for(var i=0;i<qL;i++){this._lg[i].show(yes)};
	},
	add:function(x,y,w,mb){
		this.vg.push([x,y,w,mb]);
		/* 记录本组缩进与颜色 */
		this.vg[this.vg.length-1].color=LG.color;
		this.vg[this.vg.length-1].indent=JoinLine.indent;
		this.vg[this.vg.length-1].novl=this.novl;
		return this;
	},
	clear:function(){
		for(var i=0;i<this._lg.length;i++)
			this._lg[i].clear();
		return this;
	},
	redraw:function(){
		this.clear();
		this.vg=this.vg.concat(this._vg);
		this._vg=[];
		this.draw(true);
	},
	newCheck:function(fn){
		this.check=fn;
		return this;
	},	
	setvl:function(v){
		this.novl=v;
		return this;
	},
	indent:function(v){
		JoinLine.indent=v;
		return this;
	}
}

/* 遗漏组对象 -----------------------------------------------------------------------------------*/
oYL={
	oSG:false,
	table:false,
	check:function(td,x,y){
		if(td&&td.className.indexOf("yl0")!=-1){
			this.gYL.push(td);
			td._bgColor=ESUNChart.CSS(td,"backgroundColor");
			if(td._bgColor=="transparent")td._bgColor="#F3F2F2";
			td._color=ESUNChart.CSS(td,"color");
			td.value=td.innerHTML;			
		}
		return false;
	},
	on_off:false,
	bind:function(tid,_on_off){
		this.table=tid;
		this.on_off=_on_off;
		return this;
	},
	newCheck:function(fn){
		this.check=fn;
		return this;
	},
	add:function(x,y,w,mb){
		this.oSG=new SG(this.table,x,y,w,mb,this.check);
		/* 闭包以维持多次添加遗漏区 */
		(function(kg,sg){
			document.getElementById(kg).onclick=function(){
				sg.show("gYL",this.checked);
		}})(this.on_off,this.oSG);
		return this;
	}
};


/* 预选组对象 -----------------------------------------------------------------------------------*/
/*
oPV={
	oSG:false,
	table:false,
	offset:0,
	map:[],
	css:"chartBall01",
	check:function(td,x,y){
		td.number=x-oPV.offset;
		if(ESUNChart.ini.map.length>0){//把自然数映射为码表
			td.number=ESUNChart.ini.map[td.number];
		}
		this.addClick(td,oPV.css);
		return false;
	},
	setMap:function(arr){ESUNChart.ini.map=arr||[];return this},
	bind:function(tid){
		this.table=tid;
		return this;
	},
	//x,y,width,margin-bottom,offsetNumber; table is need 
	
	add:function(x,y,w,mb,offsetNumber){
		this.offset=offsetNumber||0;
		this.oSG=new SG(this.table,x,y,w,mb,this.check);
		return this;
	},
	newCSS:function(v){
		this.css=v;
		return this;
	}
}
*/
/* 块排序 -------------------------------------------------------------------------------------- */
BSort=function(table,_x,_y,width,margin_bottom){
	this.map=[];
	this.w=0;
	this.h=0;
	this.newSort=[];
	this.init(table,_x,_y,width,margin_bottom);
};
BSort.prototype={
	init:function(table,_x,_y,width,margin_bottom){/* 支持top与left的负值 */
		var rect={x:_x||0,y:_y||0,w:width||0,oh:margin_bottom||0};
		var trs=document.getElementById(table).rows;
		var row_start=rect.y<0?(trs.length+rect.y):rect.y;
		var row_end=trs.length-rect.oh;
		var col_start=rect.x<0?(trs[row_start].cells.length+rect.x):rect.x;
		var col_end=col_start+rect.w;
		if(col_end>trs[row_start].cells.length-1)col_end=trs[row_start].cells.length-1;		
		if(rect.w==0)col_end=trs[row_start].cells.length;	
		/* to 2D array */
		var _x=_y=0;
		for(var i=row_start;i<row_end;i++){/* each and grouping */
			var tr=trs[i].cells;
			this.map[_y]=[];_x=0;
			for(var j=col_start;j<=col_end;j++){
				this.map[_y][_x]=tr[j];
				tr[j].index=_x++;
			}
			_y++;
		};
		this.w=_x;this.h=_y;
		var lastRow=[];
		/* get last row's cell array */
		for(var i=0;i<this.w;i++){lastRow.push(this.map[this.h-1][i])}
		/* last row sort of value */
		lastRow.sort(function(a,b){
			if(a.className.indexOf('chartBall')!=-1)return -1;
			if(b.className.indexOf('chartBall')!=-1)return -1;
			return parseInt(a.innerHTML)>parseInt(b.innerHTML)?-1:1;
		});
		var ball2back=[],ball=[];
		for(var i=0;i<lastRow.length;i++){
			if(lastRow[i].className.indexOf('chartBall')==-1)ball2back.push(lastRow[i]);
				else{ ball.push(lastRow[i])};
		}
		ball2back=ball2back.concat(ball)
		/* get new sort of index */
		for(var i=0;i<ball2back.length;i++){this.newSort.push(ball2back[i].index)};
	},
	sort:function(yes){
		var sort=yes?this.newSort:[];
		for(var i=0;i<this.h;i++)
		{
			/* get last cell */
			this.map.lastCell=this.map[i][this.w-1];
			//alert(this.map[0].length)
			for(var j=0;j<this.w;j++){
				var _this=this.map[i][j];
				var _j=sort.length>0?sort[j]:j;
				this.map.lastCell=ESUNChart.insertAfter(this.map[i][_j],this.map.lastCell);
			};
		};
	}
};

/* 添加所有的全局初始化动作
------------------------------------------------------------------------------------------*/
ESUNChart.init=function(){
	/* 复位复选框 */
	var inputs=document.getElementsByTagName("INPUT");
	for(var i=0;i<inputs.length;i++){
		var it=inputs[i];
		if(it.type.toLowerCase()=="checkbox")it.checked=false;
	}
	if(!ESUNChart.ini.default_has_line)return;
	var on_off=document.getElementById("has_line");
	if(!on_off)return;
	on_off.checked='checked';
};

function selnum(obj,num,clas)
{
	if (obj.className.indexOf("chartBall")!=-1)
	{
		obj.className="";
		obj.innerHTML="";
	}
	else
	{
	    obj.innerHTML=num;
		obj.className=clas;
	}
}