//*** This code is copyright 2003-2004 by Gavin Kistner, gavin@refinery.com
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
//*** Reuse or modification is free provided you abide by the terms of that license.
//*** (Including the first two lines above in your source code mostly satisfies the conditions.)

// See http://phrogz.net/tmp/scrolltable/scrolltable.html for more information.

// Requires AttachEvent() -- see http://phrogz.net/JS/AttachEvent_js.txt
// Requires AddClass() and KillClass() -- see http://phrogz.net/js/AddClassKillClass_js.txt

// v1.5  20040909  Changed styles and wrappers to prevent z-index overlap problem.

AttachEvent(window,'load',function(){
	var realTables = document.getElementsByTagName('table');
	window.scrollTableList=[];
	for (var i=0,len=realTables.length;i<len;i++) if (HasClass(realTables[i],'scrolltable')) scrollTableList.push(realTables[i]);
	for (var i=0,len=scrollTableList.length;i<len;i++){
		var table=scrollTableList[i];
		var thead=table.scrollhead=null;
		var tfoot=table.scrollfoot=null;
		
		if (table.thead=thead=FirstChild(table,'thead')){
			var tWrapper = table.parentNode.insertBefore(document.createElement('div'),table);
			tWrapper.style.overflow='hidden';
			tWrapper.style.marginRight='16px';
			var t = table.scrollhead = tWrapper.appendChild(table.cloneNode(true));
			AddClass(t,tWrapper.className='scrollhead');
			t.style.marginTop='0';
		}

		if (table.tfoot=tfoot=FirstChild(table,'tfoot')){
			var tWrapper = table.parentNode.insertBefore(document.createElement('div'),table.nextSibling);
			tWrapper.style.overflow='hidden';
			tWrapper.style.marginRight='16px';
			var t = table.scrollfoot = tWrapper.appendChild(table.cloneNode(true));
			AddClass(t,tWrapper.className='scrollfoot');
			t.style.marginTop=t.style.marginBottom='0';
		}
		
		if (table.scrollhead || table.scrollfoot){
			AddClass(table,'scrollbody');
			table.style.marginTop=table.style.marginBottom='0';
			if (table.thead) table.thead.style.visibility='hidden';
			if (table.tfoot) table.tfoot.style.visibility='hidden';

			var div = table.scrollwrap = table.parentNode.insertBefore(document.createElement('div'),table);
			div.appendChild(table);
			div.className='scrollwrap';
			div.style.overflow='auto';
			try{ div.style.overflowX='hidden'; }catch(e){}
		}
	}
	
	FixOffsets();
	AttachEvent(window,'resize',FixOffsets,false);
	setTimeout(FixOffsets,10);
	
	function FixOffsets(){
		for (var i=0,len=scrollTableList.length;i<len;i++){
			var table=scrollTableList[i];
			var headHeight = table.scrollhead ? table.thead.offsetHeight*1 : 0;
			var footHeight = table.scrollfoot ? table.tfoot.offsetHeight*1 : 0;

			if (table.scrollhead){
				table.style.marginTop='-'+headHeight+'px';
				table.scrollhead.parentNode.style.height = headHeight+'px';
			}
			if (table.scrollfoot){
				table.style.marginBottom='-'+footHeight+'px';
				for (var bodyHeight=0,j=0,len2=table.scrollfoot.tBodies.length;j<len2;j++) bodyHeight+=table.scrollfoot.tBodies[j].offsetHeight*1;
				table.scrollfoot.style.marginTop='-'+(table.scrollfoot.offsetHeight - footHeight)+'px';
				table.scrollfoot.parentNode.scrollTop = table.scrollfoot.parentNode.scrollHeight + Math.random();
				table.scrollfoot.parentNode.style.height = footHeight+'px';
			}
		}		
	}
	function MyAttachEvent(obj,evt,fnc){
		if (!obj.myEvents) obj.myEvents={};
		if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
		var evts = obj.myEvents[evt];
		evts[evts.length]=fnc;
	}
	function MyFireEvent(obj,evt){
		if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
		var evts = obj.myEvents[evt];
		for (var i=0,len=evts.length;i<len;i++) evts[i]();
}


	function FirstChild(el,tagName){
		var els = el.getElementsByTagName(tagName);
		return els && els.length>0 ? els[0] : null;
	}

},false);
