/*
THIS LICENSE HEADER MUST REMAIN INTACT FOR LEGAL USE!
Pagefusion; Pagefusion Core Platform
Description: A powerful, easy-to-use, and highly customizable content management system.
Copyright (C), 2008 Pagefusion.com, All Rights Reserved.
Author: Chris Davis, chris@pagefusion.com
Support portal: www.pagefusion.com

Filename: /components/grid/javascript/grid.js
Description: Handles row highlighting.

This program 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 2
of the License, or (at your option) any later version.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

// define methods for data structures
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/, "");
} // function

Array.prototype.indexOf = function(item, start) { 
	for (var i=(start || 0); i<this.length; i++) { 
		if (this[i] == item) { 
			return i; 
		} // if
	} // for
	return -1; 
} // function

function getElementsByClass(searchClass, node, tagName) {
	var	classElements =	new	Array();
	if (node == null)
		node = document;
	if (tagName ==	null)
		tagName	= '*';
	
	var	els	= node.getElementsByTagName(tagName);
	var	elsLen = els.length;
	
	for	(i = 0,	j =	0; i < elsLen; i++) {
		if (hasClass(els[i], searchClass)) {
			classElements[j] = els[i];
			j++;
		} // if
	} // for
	return classElements;
} // function

function getClasses(element) {
	return element.className.trim().split(/\s+/);
} // function

function hasClass(element, className) {
	return getClasses(element).indexOf(className) != -1;
} // function

function toggleCheck(element) {
	var oCells = element.getElementsByTagName('td'); // collection of tds within tr
	for (var i=0;i<oCells.length;i++) {
		var children = oCells[i].childNodes;// childNodes collection of each td
		for (var j=0;j<children.length;j++) {
			if((children[j].nodeType == 1) && (children[j].getAttribute('type') == 'checkbox')) { // if childNode is a tag and if the type is checkbox
				if (oCells[i].getElementsByTagName('input')[0].checked == false) { // switches checked/unchecked
					oCells[i].getElementsByTagName('input')[0].checked = true;
				} else {
					oCells[i].getElementsByTagName('input')[0].checked = false;
				} // if
			} // if
		} // for
	} // for
} // function

function addClass(element, className) {
    var classes = getClasses(element);
    
    if (classes.indexOf(className) == -1) {
        classes.push(className);
        element.className = classes.join(' ');
    } // if
} // function

function removeClass(element, className) {
    var classes = getClasses(element);
    var index = classes.indexOf(className);
    
    if (index != -1) {
        classes.splice(index, 1);
        element.className = classes.join(' ');
    } // if
} // function

function toggleClass(element, className) {
	var str = element.className;
    if (str.search(/selected/) > 1) {
		removeClass(element, 'selected');
		//toggleCheck(element);
	} else {
		addClass(element, 'selected');
		//toggleCheck(element);
	} // if	
} // function

function initRowHighlighting() {
	if (!document.getElementsByTagName) { return; }

	var tables = getElementsByClass('grid', document, 'table');
	
	for (var i=0; i<tables.length; i++)	{
		var table = tables[i];
		// make sure to use <th> tags for header row
		attachRowMouseEvents(table.getElementsByTagName('tr'));
	} // for
} // function

function attachRowMouseEvents(rows) {
	for (var i=0; i<rows.length; i++) {
		var row = rows[i];
		if (i%2 == 1) { // alternate
			row.onmouseover = function() { addClass(this, 'highlight'); } // function
			row.onmouseout = function() { removeClass(this, 'highlight'); } // function
			row.onmousedown = function() { toggleClass(this, 'selected'); } // function
		} else {
			row.onmouseover = function() { addClass(this, 'highlightAlt'); } // function
			row.onmouseout = function() { removeClass(this, 'highlightAlt'); } // function
			row.onmousedown = function() { toggleClass(this, 'selected'); } // function
		} // if
	} // for
} // function
