﻿var DateStyler={DSP:null,Table:null,ColIndex:-1,HeaderRowIndex:-1,StandardDateStyle:false,DateCookieRegex:/\bDS_Standard=\d/i,DateRegex:/(\d{2,4})[-\/年](\d{1,2})[-\/月](\d{1,2})[日]?/,Dates:[],Init:function(table,headerrowindex,colindex)
{with(this)
{var c=document.cookie,i=c.search(DateCookieRegex);StandardDateStyle=(i==-1)?false:(c.charAt(c.indexOf("=",i)+1)=="1");if(!table||headerrowindex==null||colindex==null)
return;Table=table;HeaderRowIndex=headerrowindex;ColIndex=colindex;if(Table.rows.length>HeaderRowIndex&&Table.rows[HeaderRowIndex].cells.length>ColIndex)
{DSP=document.createElement("label");with(this.DSP)
{className="DateStyler";innerHTML=this.StandardDateStyle?"(精简)":"(标准)";title="选择日期格式";onclick=function(e)
{DateStyler.SetStandardDateStyle(!DateStyler.StandardDateStyle);DateStyler.DSP.innerHTML=DateStyler.StandardDateStyle?"(精简)":"(标准)";DateStyler.Refresh();};}
Table.rows[HeaderRowIndex].cells[ColIndex].appendChild(DSP);}
Refresh();}},Refresh:function()
{with(DateStyler)
{var inited=Dates.length>0;for(var i=0;i<Table.rows.length;i++)
{var r=Table.rows[i];if(r.cells.length<=ColIndex)
continue;if(i>HeaderRowIndex)
{r.onmouseover=DateStyler.RowFocus;r.onmouseout=DateStyler.RowBlur;}
if(!StandardDateStyle)
{var d=r.cells[ColIndex].innerHTML;if(DateRegex.test(d))
{r.cells[ColIndex].innerHTML=_convertDate(d.replace(DateRegex,"$2/$3/$1"));if(!inited)
Dates[i]=d;}}
else
{if(Dates[i])
r.cells[ColIndex].innerHTML=Dates[i];}}}},_convertDate:function(d)
{var dt=new Date(Date.parse(d));var today=new Date();var y1=dt.getFullYear(),m1=dt.getMonth()+1,d1=dt.getDate();var y2=today.getFullYear(),m2=today.getMonth()+1,d2=today.getDate();var d=Math.floor((today-dt)/1000);var s;
if (d <= 60)
	s = "一分钟前";
else if (d < 3600)
	s = Math.floor(d / 60) + "分钟前";
else if (d < 3600 * 12)
	s = Math.floor(d / 3600) + "小时前";
else if (d < (3600 * 24))
	s = "昨天";
else if (d < (3600 * 24 * 7))
	s = Math.floor(d / (3600 * 24)) + "天前";
else if ((y1 == y2 && m1 == m2 - 1) || (y1 == y2 - 1 && m1 == 12 && m2 == 1))
	s = "上个月";
else if (d < 3600 * 24 * 180)
	s = ((y2 - y1) * 12 + m2 - m1) + "个月前";
else
	s = dt.getFullYear() + "年" + (dt.getMonth() + 1) + "月" + dt.getDate() + "日 " + dt.getHours() + ":" + dt.getMinutes();

return s;},SetStandardDateStyle:function(standardstyle)
{with(this)
{StandardDateStyle=standardstyle;document.cookie="DS_Standard="+(standardstyle?"1":"0")+";expires="+new Date(new Date().valueOf()+6622560000000).toUTCString();}},RowFocus:function(e)
{var t=$E_T(e);while(t.tagName!="TR")
t=t.parentNode;t.style.backgroundColor="#fafaff";},RowBlur:function(e)
{var t=$E_T(e);while(t.tagName!="TR")
t=t.parentNode;t.style.backgroundColor="white";}}