/**
 * @fileoverview ZMarquee is a tool that will make possible to include marquee
 * effects in a W3C validated web.
 * 
 * @author Pau Picas Sans
 * @since 13-3-2007
 * @version 0.7
 */

/**
 * Namespace with the functions needed to convert specified objects to
 * marquees.
 * 
 * ZMarquee will convert all tags that have the class name ZMarquee or with
 * an ID included in the list (array) ZMarquee.ids.
 * 
 * @type object
 */
ZMarquee =  {
	/*
	 * Parametres configurables
	 */

	/**
	 * An array of tag ID's that will be converted to marquee tags.
	 * 
	 * @type array
	 */
	ids : [],
	
	/**
	 * @type int
	 */
	scrolldelay : 1,
	
	/**
	 * @type int
	 */
	scrollamount: 1,
	
	/*
	 * Funcions internes
	 */

	/**
	 * @private
	 */
	onLoad : function () {
		var elems, node;
		elems = this.getElementsByClassName('ZMarquee', document);
		
		for(var index = 0; index < elems.length; index++) {
			node = elems[index];
			this.replaceWithMarquee(node);
		}
		for(var index = 0; index < this.ids.length; index++) {
			node = document.getElementById(this.ids[index]);
			this.replaceWithMarquee(node);
		}
	},
	
	/**
	 * @private
	 */
	onMouseOverMarquee : function () {
		this.stop();
	},
	
	/**
	 * @private
	 */
	onMouseOutMarquee : function () {
		this.start();
	},
	
	/**
	 * @private
	 */
	replaceWithMarquee : function (node) {
		var inner = node.innerHTML;
		
		var div = document.createElement('div');
		node.parentNode.replaceChild(div, node);
		
		div.innerHTML = "<marquee scrollamount='" + this.scrollamount
			+ "' scrolldelay='" + this.scrolldelay
			+ "'>" + inner + "</marquee>";
		div.firstChild.start();
		div.firstChild.onmouseover = this.onMouseOverMarquee;
		div.firstChild.onmouseout = this.onMouseOutMarquee;
	},
	
	/**
	 * @private
	 */
	hasClassName: function(element, className) {
	    var elementClassName = element.className;
	    if (elementClassName.length == 0) return false;
	    if (elementClassName == className ||
	        elementClassName.match(new RegExp("(^|\\s)" + className + "(\\s|$)"))) {
	    	return true;
	    }
	    return false;
  	},
  	
  	/**
	 * @private
	 */
  	getElementsByClassName : function(className, parentElement) {
		var children = parentElement.getElementsByTagName('*');
	    var elements = [], child;
	    for (var i = 0, length = children.length; i < length; i++) {
	    	child = children[i];
			if (this.hasClassName(child, className)) {
		        elements.push(child);
			}
	    }
	    return elements;
	}
}

if (window.addEventListener) {
	window.addEventListener('load', function () {ZMarquee.onLoad()}, false);
}
else if(window.attachEvent) {
	window.attachEvent('onload', function() {ZMarquee.onLoad();});
}