/**
------------
Highlight.js
------------
	Written by: Greg Militello <gmilitello@dyrectmedia.com>
				thinkof.net
	

------------
License information (BSD lisence based):
------------
		Copyright (c) 2006, Greg Militello
		All rights reserved.

		Redistribution and use in source and binary forms, with or without modification, 
		are permitted provided that the following conditions are met:

		    * Redistributions of source code must retain the above copyright notice, 
				this list of conditions and the following disclaimer.
		    * Redistributions in binary form must reproduce the above copyright notice, 
				this list of conditions and the following disclaimer in the 
				documentation and/or other materials provided with the distribution.
		    * Neither the name of the thinkof.net nor the names of its contributors 
				may be used to endorse or promote products derived from this software 
				without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
		IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
		INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
		BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
		OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
		WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
		ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
		POSSIBILITY OF SUCH DAMAGE.

------------
Sample usage:
------------

	<ul id="fruit">
		<li>banana</li>
		<li>orange</li>
		<li>grape</li>
	</ul>
	<script language="javascript">
		new HighlightAlternate.list('fruit');
	</script>

	<!-- List with 3 style classes -->
	<ol id="vegi">
		<li>carrot</li>
		<li>pepper</li>
		<li>onion</li>
		<li>green been</li>
		<li>celery</li>
		<li>lettuce</li>
	</ol>
	<script language="javascript">
		new HighlightAlternate.list('vegi', {modClass: new Array ('vegiZero', 'vegiOne', 'vegiTwo'), hoverClass: 'vegiHover'});
	</script>

	<!-- Table example, note the required <tbody/> tag-->
	<table id="numbers">
		<thead>
			<tr>
				<td>Number/Numero</td>
				<td>English</td>
				<td>Espanol</td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>1</td>
				<td>One</td>
				<td>Uno</td>
			</tr>
			<tr>
				<td>2</td>
				<td>Two</td>
				<td>Dos</td>
			</tr>
			<tr>
				<td>3</td>
				<td>Three</td>
				<td>Tres</td>
			</tr>
			<tr>
				<td>4</td>
				<td>Four</td>
				<td>Quatro</td>
			</tr>
			<tr>
				<td>5</td>
				<td>Five</td>
				<td>Cinco</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<td>Number/Numero</td>
				<td>English</td>
				<td>Espanol</td>
			</tr>
		</tfoot>
	</table>
	<script language="javascript">
		new HighlightAlternate.table('numbers', {modClass: new Array ('numbersZero', 'numbersOne', 'numbersTwo'), hoverClass: 'numbersHover'});
	</script>
*/
var HighlightAlternate = {}

//
// OBJECT Highlight.Base
// Creates a set of functions for the rest of the notify LIB to inherit from.
// Defines a method for option switching 
//		(Code adapted from [http://script.aculo.us/]'s Effect.Base)
//
HighlightAlternate.Base = function() {};
HighlightAlternate.Base.prototype = {
	//
	// PUBLIC METHOD Notify.Base.getVersion()
	// Returns the version of this object
	//
	getVersion: function () {
		return ('v0.2.0');
	},
	
	//
	// PRIVATE METHOD Notify.Base.setOptions()
	// Parse options into usable values
	//
	setOptions: function(options) {
		this.options = Object.extend({
		}, options || {});
	},
	//
	// PRIVATE METHOD Notify.Base.start()
	// Actions to kickoff to once this has started
	//
	start: function(options) {
		this.setOptions(options || {});
	},
	//
	// PRIVATE METHOD HighlightAlternate.list.getElementsByElementType()
	//
	getElementsByElementType: function(wrapperId, ElementType) {
		return $A($(wrapperId).getElementsByTagName(ElementType));
	},
	//
	// PRIVATE METHOD HighlightAlternate.list.highlight()
	//
	highlight: function(element, index) {
		var modIndexVal = index % this.options.modClass.length;
		element.className += " " + this.options.modClass[modIndexVal];
		if (this.options.hoverClass != '') {
			this.setMouseEvents(element);
		}
	},
	//
	// PRIVATE METHOD HighlightAlternate.list.highlight()
	//
	setMouseEvents: function(el) {
		//Event.observe(el, 'mouseover', function () { this.className += " highlightHover"; return false; });
		//Event.observe(el, 'mouseout', function () { this.className = this.className.replace(" highlightHover", ""); return false; });
		/*
		Event.observe(el, 'mouseover', (function(event) {
		        Element.addClassName(el, 'highlightHover');
		}
		));
		*/
		var mouseoverEventFunction = function(event) {
				if (!Element.hasClassName(el, this.options.hoverClass)) {
					Element.addClassName(el, this.options.hoverClass);
				} 
			}.bind(this);
		Event.observe(el, 'mouseover', (mouseoverEventFunction));
		
		/*
		Event.observe(el, 'mouseout', (function(event) {
		        Element.removeClassName(el, 'highlightHover');
		}
		));
		*/
		//var hoverClass = this.options.hoverClass;
		var mouseoutEventFunction = function(event) {
				if (Element.hasClassName(el, this.options.hoverClass)) {
					Element.removeClassName(el, this.options.hoverClass);
				} 
			}.bind(this);
		Event.observe(el, 'mouseout', (mouseoutEventFunction));
		/*
		Event.observe(el, 'click', (function(event) {
		        if (Element.hasClassName(el, 'highlightClick')) {
					Element.removeClassName(el, 'highlightClick');
				} else {
					Element.addClassName(el, 'highlightClick');
				}
		}
		));
		*/
		var clickClass = this.options.clickClass;
		var clickEventFunction = function(event) {
				if (Element.hasClassName(el, this.options.clickClass)) {
					Element.removeClassName(el, this.options.clickClass);
				} else {
					Element.addClassName(el, this.options.clickClass);
				}   
			}.bind(this);;
		Event.observe(el, 'click', (clickEventFunction));
	}
	
}

//
// OBJECT HighlightAlternate.list
//
HighlightAlternate.list = Class.create();
HighlightAlternate.list.prototype = Object.extend(new HighlightAlternate.Base(), {
	//
	// PRIVATE METHOD HighlightAlternate.list.initialize()
	// Class setup
	//
	initialize: function(containerId) {
		
		this.containerId = containerId;
		this.elementName = 'li';
	    var options = Object.extend({
			modClass: new Array ('highlightZero', 'highlightOne'),
			hoverClass: 'highlightHover',
			clickClass: 'highlightClick'
		}, arguments[1] || {});
		this.start(options);
		this.render();
	},
	//
	// PRIVATE METHOD HighlightAlternate.list.render()
	// Do the work
	//
	render: function() {
		this.highlightElements = this.getElementsByElementType(this.containerId, this.elementName);
		this.highlightElements.each(
				function (el, index) {
					this.highlight(el, index);
				}.bind(this)
			);
	}
});

//
// OBJECT HighlightAlternate.tableCols
//
HighlightAlternate.tableCells = Class.create();
HighlightAlternate.tableCells.prototype = Object.extend(new HighlightAlternate.Base(), {
	//
	// PRIVATE METHOD HighlightAlternate.table.initialize()
	// Class setup
	//
	initialize: function(containerId) {
		this.containerId = containerId;
		this.elementWrapper = 'tbody';
		this.elementName = 'td';
		var options = Object.extend({
			modClass: new Array ('highlightZero', 'highlightOne'),
			hoverClass: 'highlightHover',
			clickClass: 'highlightClick'
		}, arguments[1] || {});
		this.start(options);
		this.render();
	},
	//
	// PRIVATE METHOD HighlightAlternate.table.render()
	// Do the work
	//
	render: function() {
		var wrapElements = this.getElementsByElementType(this.containerId, this.elementWrapper);
		wrapElements.each(function (wrapEl) {
			var els = this.getElementsByElementType(wrapEl, this.elementName)
				els.each(function (el, index) {
					this.highlight(el, index);
					}.bind(this)						
				);
			}.bind(this)
			);
		//this.highlight(this.highlightElements);
	}
});

//
// OBJECT HighlightAlternate.table
// Highlight rows
//
HighlightAlternate.table = Class.create();
HighlightAlternate.table.prototype = Object.extend(new HighlightAlternate.Base(), {
	//
	// PRIVATE METHOD HighlightAlternate.table.initialize()
	// Class setup
	//
	initialize: function(containerId) {
		this.containerId = containerId;
		this.elementWrapper = 'tbody';
		this.elementName = 'td';
		var options = Object.extend({
			modClass: new Array ('highlightZero', 'highlightOne'),
			hoverClass: 'highlightHover',
			clickClass: 'highlightClick'
		}, arguments[1] || {});
		this.start(options);
		this.render();
	},
	//
	// PRIVATE METHOD HighlightAlternate.table.render()
	// Do the work
	//
	render: function() {
		var wrapElements = this.getElementsByElementType(this.containerId, this.elementWrapper);
		wrapElements.each(function (wrapEl) {
			var trs = this.getElementsByElementType(wrapEl, 'tr')
				trs.each(function (tr, index) {
						this.highlight(tr, index);	
					}.bind(this)
				);
			}.bind(this)
			);
	}
});
