/* Javascript March 1, 2010*/                             //////////////////////
 ////////////////////////////   N E W  L O O K	  ////////
//                          //////////////////////   	 //
//	BHGrid Version 0.29                   			     //
//  (c) Copyright 2006-2007 Howard Brown                // 
//      			                                   //
/////////////////////////////////////////////////////
function BHGridItem()
{
}

BHGridItem.prototype.setContent=function()
{
	if(arguments.length==1){
		var t=typeof(arguments[0]);
		if(t=='string'||t=='number'||t==__UF){
			this.col.div.innerHTML='&nbsp;';
			this.col.div.firstChild.data=arguments[0];
		}else this.col.div.appendChild(arguments[0]);
	}else{
		var t,col=this.cols[arguments[0]];
		t=typeof(arguments[1]);
		if(t=='string'||t=='number'||t==__UF){
			col.div.innerHTML='&nbsp;';
			col.div.firstChild.data=arguments[1];
		}else col.div.appendChild(arguments[1]);
	}
}

BHGridItem.prototype.textContent=function()
{
	 var e=this.cols[arguments.length?arguments[0]-0:0].div;
	 if(e.textContent)return e.textContent;
	 if(e.innerText)return e.innerText;
	 return e.innerHTML;
}

BHGridItem.prototype.nodeContent=function()
{
	return this.cols[arguments.length?arguments[0]-0:0].div;
}

BHGridItem.prototype.subItem=function(index,content)
{
	var div=this.cols[index].div;
	if(content&&content.nodeName)div.appendChild(content);
    else div.innerHTML=content;
}

BHGridItem.prototype.row=function()
{
	return this.col.row;
}

BHGridItem.prototype.move=function(index)
{
	var t,j,i,a=this.grid.headers;
	var nodes=a[this.col.col].div.childNodes;
	var grid=this.grid;
	if(index<0||index>grid.rows.length||index==this.col.row)return -1;
	i=(index-this.col.row)==1?(index+1):index;
	t=grid.rows[i];
	if(t)for(i=0;i<a.length;i++)a[i].div.insertBefore(this.cols[i].div,t.cols[i].div);
	else for(i=0;i<a.length;i++)a[i].div.appendChild(this.cols[i].div);
	a=grid.rows; a.splice(this.col.row,1);a.splice(index,0,this);
	for(i=0;i<a.length;i++){
	t=a[i].cols;for(j=0;j<t.length;j++)t[j].row=i;}
	if(grid.selectedRow!=-1){
	grid.sr(grid.selected,0);
	grid.sr(grid.selected,1);
	grid.selectedRow=grid.selected.cell.row;}
	return index;
}

BHGridItem.prototype.show=function()
{
	for(var i=0;i<this.cols.length;i++){
	this.cols[i].div.style.display='block';}
	this.hidden=false;
}

BHGridItem.prototype.hide=function()
{
	for(var i=0;i<this.cols.length;i++){
	this.cols[i].div.style.display='none';}
	this.hidden=true;
}

BHGridItem.prototype.remove=function()
{
	var i,j,g,h,r;
	g=this.grid;h=g.headers;r=false;
	if(g.selectedRow==this.col.row){
	g.selected=null;g.selectedRow=-1;r=true;}
	for(i=0;i<this.cols.length;i++){
	if(r && h[i].menu)h[i].menu.pairing.node.style.display='none';
	this.cols[i].div.parentNode.removeChild(this.cols[i].div);}
	g.rows.splice(this.col.row,1);
	for(i=this.col.row;i<g.rows.length;i++){
	for(j=0;j<this.cols.length;j++)g.rows[i].cols[j].row=i;}
	if(!g.rows.length)g.noteBar.show();
	return true;
}


function BHGridHeader()
{
	this.node=arguments[0];
}

BHGridHeader.prototype.show=function()
{
	this.node.style.display=arguments[0]===false?'none':'block';
}

function BHGridCellPopup()
{
	this.grid=arguments[0];this.div=bhUI.createElement();
	this.div.className=this.grid.skin+'GridCellPopup';
	this.div.style.left=this.div.style.top='0px';
	document.body.appendChild(this.div);
	this.div.onclick=this.onevent=bhUI.eventHandle(this.grid.Tx+".cellpopup.eventTrap");
}

BHGridCellPopup.prototype.eventTrap=function(evt)
{
	if(evt.type=='mousemove')
	{
		var mx,my,y,x,b=bhUI.getScroll();
		x=parseInt(this.div.style.left);y=parseInt(this.div.style.top);
		mx=evt.clientX+b.left;my=evt.clientY+b.top;
		if(mx>x&&mx<(x+this.div.offsetWidth)&&my>y&&my<(y+this.div.offsetHeight))return;
		this.div.style.visibility='hidden';
		document.removeEventListener(evt.type,this.onevent,false);
	}
	else
	{
		this.div.style.visibility='hidden';
		document.removeEventListener(evt.type,this.onevent,false);
		this.grid.eventClick({target:this.div.cell.div});
	}
}

BHGridCellPopup.prototype.show=function()
{
	document.addEventListener('mousemove',this.onevent,false);
	this.div.style.visibility='visible';
}

BHGridCellPopup.prototype.hide=function()
{
	clearTimeout(this.showTimer);this.div.style.visibility='hidden';
}

BHGridCellPopup.prototype.auto=function()
{
	var n=this.div,div=arguments[0]; 
	n.style.left=n.style.top='-1000px';n.innerHTML=div.innerHTML;
	if((n.offsetWidth+n.offsetHeight)>(div.parentNode.offsetWidth+div.offsetHeight))
	{
		var e=div,top=0,left=0;
		while(e.offsetParent){top+=e.offsetTop;left+=e.offsetLeft;e=e.offsetParent;}
		n.cell=div.cell;
		this.hide();
		n.style.top=top+'px';n.style.left=left+'px';
		this.showTimer=setTimeout(div.cell.parent.grid.Tx+".cellpopup.show()",1000);
	}
}


function BHGrid(bh)
{
	var vhs,vs,hs,n=12,h,w,div,i,z,v='px';
	bhUI.createObject('Grid',this);
	
	this.flags=(typeof(bh.flags)==__UF)?0x10:bh.flags;
	this.selected=null;
	this.selectedRow=-1;
	this.rows=new Array();
	this.headers=new Array;
	this.skin=bh.skin?bh.skin:'';
		
	this.cellpopup=new BHGridCellPopup(this);
	this.div=div=(typeof(bh.id)=='string'?bhUI.element(bh.id):bh.id);
	this.name=div.id;div.grid=this;
	div.style.position='relative';
	div.style.overflow='hidden';
	div.className=this.skin+'Grid';
	
	var wrap=div.addElement();
	wrap.style.position='relative';
	wrap.style.overflow='hidden';

	hs=wrap.addElement();
	hs.style.position='absolute';
	hs.style.top=hs.style.left='0px';
	
	var ctb=hs.addElement('TABLE');
	this.Header=new BHGridHeader(ctb);
	ctb.className=this.skin+'GridHeader';
	ctb.cellPadding=ctb.cellSpacing='0';ctb=ctb.addElement('TBODY');
	var ctr=ctb.addElement('TR');
	var td=ctr.addElement('TD');td.innerHTML='W';
	this.charHeight=ctb.parentNode.offsetHeight;ctr.removeChild(td);
	this.noteBar=hs.addElement();
	this.noteBar.className="NoteBar";
	this.noteBar.innerHTML='Empty';
	this.noteBar.hide=new Function("this.style.display='none'");
	this.noteBar.show=new Function("this.style.display='block'");
	this.noteBar.setContent=new Function("this.innerHTML=arguments[0]");
	var wrap2=hs.addElement();
	wrap2.style.position='relative';
	wrap2.style.overflow='hidden';
	vs=wrap2.addElement();
	vs.style.top=vs.style.left='0px';
	vs.style.position='absolute';
	var tb,tr,col,ctd,img,s,cw,d,e;
	tb=vs.addElement('TABLE');
	tb.className=this.skin+'GridContent';
	tb.cellPadding=tb.cellSpacing='0';
	tb=tb.addElement('TBODY');
	tr=tb.addElement('TR');
	tr.style.verticalAlign='top';
	s="/newlook/img/spacer.gif";
	this.tb=tb;
   	for(c=0;c<bh.columns.length;c++)
	{
		col=new Object();
		d=bh.columns[c];
		cw=d[1];	
		col.flags=d[2];
		ctd=ctr.addElement('TD');
		ctd.className=this.skin+'GridHeaderColumn';
		ctd.style.width=cw+v;
		img=ctd.addElement('IMG');
		img.src=s;
		img.style.width=cw+v;
		img.style.height='1px';
		img.style.display='block';
		e=ctd.addElement('SPAN');
		e.style.marginLeft='4px';
		e.innerHTML=d[0];
		
        td=tr.addElement('TD');
		cw=ctd.offsetWidth;
		td.style.textAlign=this.textAlign[d[2]&0x03];
		td.style.width=cw+v;
		img=td.addElement('IMG');
		img.src=s;
		img.style.width=cw+v;
		img.style.height='1px';
		col.div=td.addElement();
		col.div.id='column-'+ ((c==0)?'first':(c==bh.columns.length-1?'last':c));
		col.div.className=this.skin+'GridColumn'+(this.flags&0x10?'':'Disabled')+(this.flags&0x01?' ':' sepm');
	    col.div.style.position='relative';
		col.div.style.overflow='hidden';
		col.div.style.height='auto';
		col.div.style.width=cw+v;
		this.headers[c]=col;	
	}
	h=(div.offsetHeight-n-5);
	w=div.offsetWidth;
	wrap.style.height=h+v;
	wrap2.style.height=h+v;
	wrap.style.width=(w-n)+v;
   
	this.scroll=this.vhscroll=vhs=new BHScroll({skin:this.skin+'Grid'});
	e=vhs.vscroll.bar;
	div.appendChild(e);
	e.style.width=n+v;
	e.style.height=h+v;
	e.style.top='0px';
	e.style.left=(w-n-4)+v;
	e=vhs.hscroll.bar;
	div.appendChild(e);
	e.style.width=(w-n-5)+v;
	e.style.height=n+v;
	e.style.top=h+1+v;
	e.style.left='0px';
	vhs.height=(h-ctb.parentNode.offsetHeight);
	vhs.width=(w-n);
	vhs.vscroll.content=vs;
	vhs.hscroll.content=hs;
	
	wrap2.style.width=ctb.parentNode.offsetWidth+v;
	
	this.onmouseover=bhUI.eventHandle(this.Tx+".eventMouseOver");
    this.onmouseout=bhUI.eventHandle(this.Tx+".eventMouseOut");
    this.onclick=bhUI.eventHandle(this.Tx+".eventClick");
	this.ondragstart=new Function('evt',this.Tx+".eventDragStart(evt)");
	this.itemClick=bh.onitemclick?bh.onitemclick:__DF;

	this.clear();
	if(this.flags&0x20)this.Header.show(false);
	if(this.flags&0x100)vhs.show(false);
	if(this.flags&0x200)vhs.showHBar(false);
}

BHGrid.prototype.textAlign=['left','center','right'];

BHGrid.prototype.setEventListener=function(eventType,eventHandler)
{
	switch(eventType){
	case 'drop':
	this.onDrop=eventHandler;
	this.div.bhondrop=new Function("evt",this.Tx+".onDrop(evt)");
	break;
	}
}

BHGrid.prototype.getItemAt=function(x,y)
{
	var i,div,ox,oy,o2y,n,e;
	for(i=0;i<this.headers.length;i++){
	div=this.headers[i].div;e=div,ox=0,oy=0;
	while(e.offsetParent){oy+=e.offsetTop;
	ox+=e.offsetLeft;e=e.offsetParent;}
	if(x>=ox&&x<=(ox+div.offsetWidth)){
	var nodes=div.childNodes;
	for(i=0;i<nodes.length;i++){
	n=nodes[i];o2y=oy+n.offsetTop;
	if(y>=o2y&&y<=(o2y+n.offsetHeight)){
	return this.rows[n.cell.row];}}}}
	return null;
}

BHGrid.prototype.setMenu=function(col,menu)
{
	if(!menu.pairing.node){
	var img=document.createElement('IMG');
	img.className="GridMenuButton";
	img.style.position="absolute";
	img.src=bhUI.Theme.imgDir+"arrow/down.gif";
	menu.pair(img,0,0);}
	menu.pairing.node.style.zIndex=10;
	menu.pairing.node.style.display="none";
	this.headers[col].div.appendChild(menu.pairing.node);
	this.headers[col].menu=menu;
}

BHGrid.prototype.addItem=function(text)
{		
	var h,x,o_col,o_item=new BHGridItem();
	o_item.cols=new Array;
	o_item.grid=this;o_item.parent=this;
	
	this.noteBar.hide();
	
	if(arguments.length<2)h='auto';
	else{h=arguments[1];h=typeof(h)=='number'?(h==16?18:h)+'px':h;}
	var r=(this.rows.push(o_item)-1);
	for(c=0;c<this.headers.length;c++)
	{
		o_col=new Object();
		o_col.parent=o_item;
		o_col.row=r;
		o_col.col=c; 
		o_col.value='';
		o_col.tag=null;
		o_col.div=bhUI.createElement('DIV');
		o_col.div.cell=o_col;
		o_col.div.id="gridcell"
		o_col.div.className=this.skin+'GridCell';
		o_col.div.style.position='relative';
		o_col.div.style.height=h;
		o_col.div.style.width='auto';
		o_col.div.style.overflow='hidden';
		if(this.flags&0x10)
		{
			if(!(this.flags&0x40)){
			o_col.div.onmouseover=this.onmouseover;
        	o_col.div.onmouseout=this.onmouseout;}
			if(!(this.flags&0x80)){
			o_col.div.onclick=this.onclick;}
		}
	   	if(this.flags&0x08) 
		{
			o_col.div.bhondragstart=this.ondragstart;
		}
		this.headers[c].div.appendChild(o_col.div);
		o_item.cols[c]=o_col;
   	}
	o_item.col=o_item.cols[0];
	this.rows[r].cols[0].div.innerHTML=text?text:'';
	return o_item;
}

BHGrid.prototype.clear=function()
{
	this.cellpopup.hide();
	this.selected=null;this.selectedRow=-1;
	for(var c=0;c<this.headers.length; c++){
	var h=this.headers[c];
	if(h.menu){
	h.menu.pairing.node.style.display='none';
	h.div.innerHTML='';
	h.div.appendChild(h.menu.pairing.node);
	}else{
	h.div.innerHTML='';}/*
	var z=h.div.addElement();
	z.style.backgroundColor='#FFFFFF';
	z.setOpacity(0.10);
	z.style.position='absolute';
	z.style.top='0px';
	z.style.left=h.div.offsetWidth-2+'px';
	z.style.height='100%';
	z.style.width='2px';
	z.style.zIndex=1;*/
	}
	delete this.rows;this.rows=new Array();
	this.noteBar.show();
	this.vhscroll.set(1,1);
	this.redraw();}

BHGrid.prototype.redraw=function()
{
	this.vhscroll.redraw();
	var r,c,s,e,i=0,m=0;
	for(r=0;r<this.rows.length;r++){
	e=this.rows[r];if(e.hidden)continue;
	e.dr=i++;
	s=this.ca(10,e.cols[0].div.className,m);m^=1;
	if(s){for(c=0;c<e.cols.length;c++){
	e.cols[c].div.className=s;}}}
}

BHGrid.prototype.count=function()
{
	return this.rows.length;
}

BHGrid.prototype.item=function()
{
	return this.rows[arguments[0]];
}

BHGrid.prototype.Items=function()
{
	if(arguments.length==0)return this.rows;
	var itm=this.rows[arguments[0]];
	if(arguments.length==2&&typeof(itm)==__UF){
	itm=typeof(arguments[1])=='number'?this.addItem('',arguments[1]):this.addItem('');}
	return itm;
}

BHGrid.prototype.selectItem=function(index)
{
	if(!(this.flags&0x02)||this.selectedRow==index)return false;
	if(this.selected) this.sr(this.selected,0);
	this.selected=null;
	if(!this.rows[index])return false;
	this.selected=this.rows[index].cols[0].div;
	this.sr(this.selected,1);
	this.selectedRow=index; 
}

BHGrid.prototype.sr=function(node,mode)
{
	var c,l,s,i,r,div,e;
	if(this.flags&0x01){c=0;l=this.rows[0].cols.length;
	}else{c=node.cell.col;l=(c+1);}
	i=node.cell.row;r=this.rows[i];
	s=this.ca(mode?2:4,r.cols[c].div.className, (r.dr%2));
	for(i=c;i<l;i++){
	div=r.cols[i].div;div.className=s;
	if(mode){if(this.headers[i].menu){
	e=this.headers[i].menu.pairing.node;
	e.style.padding=parseInt((div.offsetHeight/2)-6)+'px';
	e.style.paddingLeft='4px';
	e.style.top=(div.offsetTop+2)+'px';
	e.style.left=(div.offsetLeft+(div.parentNode.offsetWidth-16))+'px';
	e.style.display='block';}
	}else{
	if(this.headers[i].menu)this.headers[i].menu.pairing.node.style.display='none';}
	}
}

BHGrid.prototype.ca=function(mode,s,d)
{
	var p=s,i,a=['Over','Selected','Odd'],f=0;
	for(i=0;i<a.length;i++){
	l=s.length-a[i].length;
	if(l>3&&s.substr(l)==a[i])s=s.substring(0,l);
	else a[i]='';}
	switch(mode){
	case 0:a=['',''];break
	case 1:a[0]='Over';break
	case 2:a[1]='Selected';break;
	case 3:a[0]='';break;
	case 4:a[1]='';break;
	}
	if(a[0] || a[1])s = s + a[1] + a[0];
	else s = s + (d ? 'Odd' : '');
	return s==p?'':s;
}

BHGrid.prototype.eventMouseOver=function(evt)
{
	var i,r,s,div=evt.target;
	while(!div.cell)div=div.parentNode;
	i=div.cell.row;r=this.rows[i];
	s=this.ca(1,div.className,(r.dr%2));
	if(s){if(this.flags&0x01){
	for(i=0;i<r.cols.length;i++)r.cols[i].div.className=s;
	}else div.className=s;}
	if(!(this.flags&0x04)){this.cellpopup.auto(div);}
}

BHGrid.prototype.eventMouseOut=function(evt)
{	
	var r,i,div=evt.target;
	while(!div.cell)div=div.parentNode;
	i=div.cell.row;r=this.rows[i];
	s=this.ca(3,div.className,(r.dr%2));
    if(s){if(this.flags&0x01){
	for(i=0;i<r.cols.length;i++)r.cols[i].div.className=s;
	}else div.className=s;}
}

BHGrid.prototype.eventClick=function(evt)
{
	var changed=false,node=evt.target;
	while(!node.cell && node.nodeName!='A')node=node.parentNode;
	if(node.nodeName=='A')return;
	if(this.selected!=node){
	if(this.flags&0x02){
	if(this.selected)this.sr(this.selected,0);
	this.sr(node,1);}changed=true;}
	this.selected=node;
	this.selectedRow=node.cell.row;
	this.itemClick(node.cell.row,node.cell.col,changed);
}

BHGrid.prototype.eventDragStart=function(evt)
{
	var node=evt.target;
	while(!node.cell && node.nodeName!='A')node=node.parentNode;
	if(node.nodeName=='A')return false;
	evt.bhdataTransfer.setData('cell',node.cell);
}


