// JavaScript Document


var FancyTable = new Class({

initialize: function() {
	var tag;
	$$('table.fancy').each(function(tab) {
		$(tab).getChildren().each(function(section) {
			tag = section.get("tag");
			
			$(section).getChildren('tr').each(function(row) {
				row.getChildren(':last-child').setStyle('border-right','none');
				row.getChildren(':first-child').setStyle('border-left','none');
				row.getChildren().addClass('fancy');
			});

			section.getChildren('tr:first-child').getChildren().each(function(cell) {
				cell.setStyle('border-top','none');
			});

			if (tag == 'thead') {
				section.getChildren('tr:last-child').getChildren().each(function(cell) {
					cell.addClass('img');
				});
			}
			if (tag == 'tbody') {
				section.getChildren(':odd').addClass('odd');
			}
			if (tag == 'tfoot') {
				section.getChildren('tr:first-child').getChildren().each(function(cell) {
					cell.addClass('img');
				});
			}

		});
		$(tab).removeProperty('border');
		$(tab).removeProperty('cellpadding');
		$(tab).setStyle('border','none');
		$(tab).setStyle('padding','0px');
		

		wrapper = new Element('div');
		wrapper.setProperty("align",tab.getProperty("align"));
		wrapper.inject(tab,"before");

		sideBackL = new Element('div');
		sideBackL.setStyles({
			"height" : tab.getCoordinates().height,
			"background-image" : "url(/theme/wsm/fancytable/sidebar-grey-l.gif)",
			"background-position" : "top left",
			"background-repeat" : "repeat-y",
			"width" : "3px",
			"float" : "left"
		});

		sideBackR = new Element('div');
		sideBackR.setStyles({
			"height" : tab.getCoordinates().height,
			"background-image" : "url(/theme/wsm/fancytable/sidebar-grey-r.gif)",
			"background-position" : "top right",
			"background-repeat" : "repeat-y",
			"width" : "3px",
			"float" : "left"
		});
		
		if (tab.getElement('thead')) {
			sideHeadL = new Element('div');
			sideHeadL.setStyles({
				"height" : tab.getElement('thead').getCoordinates().height,
				"background-image" : "url(/theme/wsm/fancytable/sidebar-blue-l.gif)",
				"background-position" : "bottom left",
				"background-repeat" : "no-repeat",
				"width" : "3px"
			});
			sideHeadR = new Element('div');
			sideHeadR.setStyles({
				"height" : tab.getElement('thead').getCoordinates().height,
				"background-image" : "url(/theme/wsm/fancytable/sidebar-blue-r.gif)",
				"background-position" : "bottom right",
				"background-repeat" : "no-repeat",
				"width" : "3px"
			});
			sideHeadL.inject(sideBackL,"top");
			sideHeadR.inject(sideBackR,"top");
		}

		if ((tab.getElement('tbody')) && (tab.getElement('tbody').getCoordinates().height > 0)) {
			sideBodyL= new Element('div');
			sideBodyL.setStyles({
				"height" : tab.getElement('tbody').getCoordinates().height,
				"width" : "3px",
				"overflow" : "hidden"
			});
			sideBodyR = new Element('div');
			sideBodyR.setStyles({
				"height" : tab.getElement('tbody').getCoordinates().height,
				"width" : "3px",
				"overflow" : "hidden"
			});
			sideBodyL.inject(sideBackL,"bottom");
			sideBodyR.inject(sideBackR,"bottom");
		}

		if (tab.getElement('tfoot')) {
			sideFootL = new Element('div');
			sideFootL.setStyles({
				"height" : tab.getElement('tfoot').getCoordinates().height,
				"background-image" : "url(/theme/wsm/fancytable/sidebar-navy-l.gif)",
				"background-position" : "top left",
				"background-repeat" : "no-repeat",
				"width" : "3px"
			});
			sideFootR = new Element('div');
			sideFootR.setStyles({
				"height" : tab.getElement('tfoot').getCoordinates().height,
				"background-image" : "url(/theme/wsm/fancytable/sidebar-navy-r.gif)",
				"background-position" : "top right",
				"background-repeat" : "no-repeat",
				"width" : "3px"
			});
			sideFootL.inject(sideBackL,"bottom");
			sideFootR.inject(sideBackR,"bottom");
		}

		middlecenter = new Element('div');
		middlecenter.setStyle("float","left");
		middlecenter.setStyle("width",tab.getCoordinates().width);
		tab.setProperty("width","100%");
		tab.inject(middlecenter,"bottom");

		middle = new Element('div');
		middlecenter.inject(middle,"inside");

		sideBackL.inject(middle,"top");
		sideBackR.inject(middle,"bottom");

		middle.inject(wrapper,"inside");
		middle.setStyle("width",middlecenter.getCoordinates().width+6);
		
		headL = new Element('div');
		if (tab.getElement('thead')) {
			headL.setStyle("background-image","url(/theme/wsm/fancytable/header-blue-r.gif)");
		} else {
			headL.setStyle("background-image","url(/theme/wsm/fancytable/header-grey-r.gif)");
		}
		headL.setStyles({
			"width" : middlecenter.getCoordinates().width+6,
			"background-position" : "top right",
			"background-repeat" : "no-repeat",
			"height" : "5px",
			"clear" : "both"
		});
		
		headR = new Element('div');
		if (tab.getElement('thead')) {
			headR.setStyle("background-image","url(/theme/wsm/fancytable/header-blue-l.gif)");
		} else {
			headR.setStyle("background-image","url(/theme/wsm/fancytable/header-grey-l.gif)");
		}
		headR.setStyles({
			"background-position" : "top left",
			"background-repeat" : "no-repeat",
			"overflow" : "hidden",
			"height" : "5px"
		});

		headR.inject(headL,"inside");
		headL.inject(wrapper,"top");

		footL = new Element('div');
		if (tab.getElement('tfoot')) {
			footL.setStyle("background-image","url(/theme/wsm/fancytable/footer-navy-r.gif)");
		} else {
			footL.setStyle("background-image","url(/theme/wsm/fancytable/footer-grey-r.gif)");
		}
		footL.setStyles({
			"width" : middlecenter.getCoordinates().width+6,
			"background-position" : "top right",
			"background-repeat" : "no-repeat",
			"height" : "5px",
			"clear" : "both"
		});
		
		footR = new Element('div');
		if (tab.getElement('tfoot')) {
			footR.setStyle("background-image","url(/theme/wsm/fancytable/footer-navy-l.gif)");
		} else {
			footR.setStyle("background-image","url(/theme/wsm/fancytable/footer-grey-l.gif)");
		}
		footR.setStyles({
			"background-position" : "top left",
			"background-repeat" : "no-repeat",
			"overflow" : "hidden",
			"height" : "5px"
		});

		footR.inject(footL,"inside");
		footL.inject(wrapper,"bottom");
	});
}

});
