/* LICENSE

This work is licensed under the creative commons Attribution-Noncommercial-Share Alike 3.0 Unported License

http://creativecommons.org/licenses/by-nc-sa/3.0/

Attribution-Noncommercial-Share Alike 3.0 Unported
You are free:
    * to Share — to copy, distribute and transmit the work
    * to Remix — to adapt the work

Under the following conditions:
    * Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
    * Noncommercial. You may not use this work for commercial purposes.
    * Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.

    * For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.
    * Any of the above conditions can be waived if you get permission from the copyright holder.
    * Nothing in this license impairs or restricts the author's moral rights.

Rikkert Koppes (author)
http://www.rikkertkoppes.com/thoughts/toolbar

THIS LIBRARY IS STILL UNDER DEVELOPMENT

*/

if (!com) {var com={};}
if (!com.rikkertkoppes) {com.rikkertkoppes={};}
if (!com.rikkertkoppes.interfacing) {com.rikkertkoppes.interfacing={};}

com.rikkertkoppes.interfacing.Toolbar = function() {

	//helpers
	function addEventListener(obj,e,func,useCapture) {
		if (e=='DOMLoaded') {
			DOMLoadedFunctionPool.push(func);
			return;
		}
		if (obj.addEventListener) {
			if (e=='mousewheel') {obj.addEventListener('DOMMouseScroll',func,useCapture);}
			obj.addEventListener(e,func,useCapture);
		} else if (obj.attachEvent) {
			obj.attachEvent('on'+e,function() {
				return func.apply(obj);
			});
		}
	}
	function hasClassName(control, className) {
		return (control.className.length > 0 && (control.className == className || control.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)"))));
	}
	function addClassName(control, className) {
		if (!control || !className || hasClassName(control, className)) {return;}
		control.className += ((control.className==='')?"" : " ") + className;
	}
	function removeClassName(control, className) {
		control.className = control.className.replace(new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ').replace(/^\s+/, '').replace(/\s+$/, '');
	}
	
  function convertLegends(remove) {
		var i,legend,legends=[],legendElements = this.form.getElementsByTagName('legend');
    for (i=0; i<legendElements.length; i++) {legends.push(legendElements[i]);}
		for (i=0; (legend=legends[i]); i++) {
			var p = document.createElement('p');
			p.innerHTML = legend.innerHTML;
			addClassName(p,'rk-legend');
			legend.parentNode.insertBefore(p,legend.nextSibling);
      if (remove) {
        legend.parentNode.removeChild(legend);
      }
		}
	}
  
  
	function convertInputs() {
		var i,input,inputs = this.form.getElementsByTagName('input');
		var j,groupMembers,groupMember;
		
		function toolbarSpeedButtonChange() {
			(this.checked)?addClassName(this.nextSibling,'rk-checked'):removeClassName(this.nextSibling,'rk-checked');
			if (this.name) {
				groupMembers = this.form.elements[this.name];
				for (j=0; (groupMember=groupMembers[j]); j++) {
					(groupMember.checked)?addClassName(groupMember.nextSibling,'rk-checked'):removeClassName(groupMember.nextSibling,'rk-checked');
				}
			}
		}
	
		for (i=0; (input=inputs[i]); i++) {
			if (input.type=='radio'||input.type=='checkbox'||input.type=='button') {
				addClassName(input,'rk-speedbutton');
				addClassName(input.nextSibling,'rk-speedbutton');
				addEventListener(input,'change',toolbarSpeedButtonChange);
				toolbarSpeedButtonChange.call(input);
			}
		}
	}
	function convertLabels() {
		var i,label,labels = this.form.getElementsByTagName('label');
		var activeElement;
		for (i=0; (label=labels[i]); i++) {
			addEventListener(label,'mousedown',function() {
				addClassName(this,'rk-active');
				activeElement = this;
			});
			addEventListener(label,'mouseover',function() {
				addClassName(this,'rk-hover');
			});
			addEventListener(label,'mouseout',function() {
				removeClassName(this,'rk-hover');
			});
		}
		addEventListener(document,'mouseup',function() {
			if (activeElement) {
				removeClassName(activeElement,'rk-active');
				activeElement = false;
			}
		});
	}

	var Toolbar = function(formElement,options) {
		this.form = formElement;
    this.options = options||{};
		convertLegends.call(this,this.options.removeLegends||false);
		convertInputs.call(this);
		convertLabels.call(this);
		addClassName(formElement,'rk-toolbar');
		addEventListener(formElement,'click',function repaintToolbar() {
			//force redraw for safari
			this.style.display = 'none';
			this.style.display = '';
		});
	};
	
	return Toolbar;
}();
