/*
	name			: ClassBehaviours, the javascript framework based on class-name parsing
	update			: 9.11.9
	author			: Maurice van Creij
	dependencies	: jquery.classbehaviours.js
	info			: http://www.classbehaviours.com/

    This file is part of jQuery.classBehaviours.

    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2008  Maurice van Creij

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.
*/

	// create the jQuery object if it doesn't already exist
	if(typeof(jQuery)=='undefined') jQuery = function(){};

	// create the root classbehaviours object if it doesn't already exist
	if(typeof(jQuery.classBehaviours)=='undefined') jQuery.classBehaviours = function(){};

	// create the handlers child object if it doesn't already exist
	if(typeof(jQuery.classBehaviours.handlers)=='undefined') jQuery.classBehaviours.handlers = function(){}

	// rebuild a table into sections
	jQuery.classBehaviours.handlers.sectionedTable = {
		// properties
		name: 'sectionedTable',
		// methods
		start: function(node){
			// fix the dimensions of the table
			this.cellDimensions(node);
			// clone the table
			this.cloneTables(node);
			// set the events
			node.getElementsByTagName('ARTICLE')[0].onscroll = this.scrolled;
		},
		cloneTables: function(node){
			sourceNode = node.getElementsByTagName('ARTICLE')[0];
			// copy the table, strip it from unwanted sections,copy it to the fixed header
			tableNode = sourceNode.getElementsByTagName('TABLE')[0].cloneNode(true);
			tableNode.getElementsByTagName('TBODY')[0].parentNode.removeChild(tableNode.getElementsByTagName('TBODY')[0]);
			tableNode.getElementsByTagName('TFOOT')[0].parentNode.removeChild(tableNode.getElementsByTagName('TFOOT')[0]);
			node.getElementsByTagName('HEADER')[0].getElementsByTagName('DIV')[0].appendChild(tableNode);
			// copy the stripped header to the scrolled header
			tableNode = node.getElementsByTagName('HEADER')[0].getElementsByTagName('DIV')[0].getElementsByTagName('TABLE')[0].cloneNode(true);
			node.getElementsByTagName('HEADER')[0].getElementsByTagName('DIV')[1].appendChild(tableNode);
			// copy the table, strip it from unwanted sections,copy it to the fixed footer
			tableNode = sourceNode.getElementsByTagName('TABLE')[0].cloneNode(true);
			tableNode.getElementsByTagName('TBODY')[0].parentNode.removeChild(tableNode.getElementsByTagName('TBODY')[0]);
			tableNode.getElementsByTagName('THEAD')[0].parentNode.removeChild(tableNode.getElementsByTagName('THEAD')[0]);
			node.getElementsByTagName('FOOTER')[0].getElementsByTagName('DIV')[0].appendChild(tableNode);
			// copy it to the scrolled footer
			tableNode = node.getElementsByTagName('FOOTER')[0].getElementsByTagName('DIV')[0].getElementsByTagName('TABLE')[0].cloneNode(true);
			node.getElementsByTagName('FOOTER')[0].getElementsByTagName('DIV')[1].appendChild(tableNode);
			// copy it to the aside
			tableNode = sourceNode.getElementsByTagName('TABLE')[0].cloneNode(true);
			node.getElementsByTagName('ASIDE')[0].appendChild(tableNode);
			// change the source container's style
			sourceNode.className += ' scripted';
		},
		cellDimensions: function(node){
			// find the table node
			tableNode = node.getElementsByTagName('ARTICLE')[0].getElementsByTagName('TABLE')[0];
			tableHead = tableNode.getElementsByTagName('THEAD')[0];
			tableBody = tableNode.getElementsByTagName('TBODY')[0];
			tableFoot = tableNode.getElementsByTagName('TFOOT')[0];
			// measure and fix the header dimensions
			headContents = tableHead.getElementsByTagName('SPAN');
			bodyContents = tableBody.getElementsByTagName('SPAN');
			footContents = tableFoot.getElementsByTagName('SPAN');
			if(headContents[0].offsetWidth>0){
				// measure and fix the header dimensions
				for(var a=0; a<headContents.length; a++){
					headContents[a].style.width = (headContents[a].offsetWidth) + 'px';
					headContents[a].style.height = (headContents[a].offsetHeight) + 'px';
				}
				// measure and fix the body dimensions (but maybe not all of them)
				bodyContentsLength = (bodyContents.length>64) ? 64 : bodyContents.length ;
				for(var a=0; a<bodyContentsLength; a++){
					bodyContents[a].style.width = (bodyContents[a].offsetWidth) + 'px';
					bodyContents[a].style.height = (bodyContents[a].offsetHeight) + 'px';
				}
				// measure and fix the footer dimensions
				for(var a=0; a<footContents.length; a++){
					footContents[a].style.width = (footContents[a].offsetWidth) + 'px';
					footContents[a].style.height = (footContents[a].offsetHeight) + 'px';
				}
			}
		},
		// events
		scrolled: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var stb = jQuery.classBehaviours.handlers.sectionedTable;
			// get the scroll position
			leftOffset = objNode.scrollLeft;
			topOffset = objNode.scrollTop;
			// adjust the header column
			objNode.parentNode.getElementsByTagName('ASIDE')[0].getElementsByTagName('TABLE')[0].style.marginTop = (-1 * topOffset) + 'px';
			// adjust the header rows
			objNode.parentNode.getElementsByTagName('HEADER')[0].getElementsByTagName('DIV')[1].getElementsByTagName('TABLE')[0].style.marginLeft = (-1 * leftOffset) + 'px';
			objNode.parentNode.getElementsByTagName('FOOTER')[0].getElementsByTagName('DIV')[1].getElementsByTagName('TABLE')[0].style.marginLeft = (-1 * leftOffset) + 'px';

		}
	}

	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.sectionedTable = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.sectionedTable.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".sectionedTable").sectionedTable();
			}
		);
	}
