/*
	CRIR - Checkbox & Radio Input Replacement
	Author: Chris Erwin (me[at]chriserwin.com)
	www.chriserwin.com/scripts/crir/

	Updated July 27, 2006.
	Jesse Gavin added the AddEvent function to initialize
	the script. He also converted the script to JSON format.
	
	Updated July 30, 2006.
	Added the ability to tab to elements and use the spacebar
	to check the input element. This bit of functionality was
	based on a tip from Adam Burmister.
*/

/** 
    Multi-Class Suppoer & Rewrite for MooTool by Corsair
*/

var CheckboxClass = {
    0:
        {0: 'blue_unchecked', 1: 'blue_checked'},
    1:
        {0: 'grey_unchecked', 1: 'grey_checked'},
    2: 
        {0: 'checkbox_unchecked3', 1: 'checkbox_checked3'}
};

var RadioClass = {
    0:
        {0: 'radio_unchecked', 1: 'radio_checked'},
    1: 
        {0: 'radio_unchecked2', 1: 'radio_checked2'},
    2: 
        {0: 'radio_unchecked3', 1: 'radio_checked3'}
};

crir = {
	init: function(where) {
        arrLabels = ($type(where)=='string') ? $$('#'+where+' label') : $$('label');
		//arrLabels = document.getElementsByTagName('label');
	
		searchLabels:
		for (var i=0; i<arrLabels.length; i++) {			
			// get the input element based on the for attribute of the label tag
			if (arrLabels[i].getAttributeNode('for') && arrLabels[i].getAttributeNode('for').value != '') {
				labelElementFor = arrLabels[i].getAttributeNode('for').value;				
				//inputElement = document.getElementById(labelElementFor);
				inputElement = $(labelElementFor);
			}
			else {				
				continue searchLabels;
			}	

			inputElementClass = inputElement.className;	
		
			// if the input is specified to be hidden intiate it
			if ($(inputElement).hasClass('crirHiddenJS'))
            {
                var matchtype = $type($(arrLabels[i].getProperty('for')).getProperty('class').match(/custom\[(\d+)\]/));
                var match = (matchtype != false) ? $(arrLabels[i].getProperty('for')).getProperty('class').match(/custom\[(\d+)\]/)[1] : 0;     

                //if (inputElementClass == 'crirHiddenJS') {
				inputElement.addClass('crirHidden');				
				inputElementType = inputElement.getProperty('type');	
				
				// add the appropriate event listener to the input element
				if (inputElementType == "checkbox") {
                    inputElement.onclick = crir.toggleCheckboxLabel;
				}
				else {
					inputElement.onclick = crir.toggleRadioLabel;
				}
                // set the initial label state
				if (inputElement.checked) {                
					if (inputElementType == 'checkbox') {                       
                        arrLabels[i].className = CheckboxClass[match][1];
                    }
					else {
                        arrLabels[i].className = RadioClass[match][1];
                    }
				}
				else {
					if (inputElementType == 'checkbox') {           
                        arrLabels[i].className = CheckboxClass[match][0];
                    }
					else { arrLabels[i].className = RadioClass[match][0]; }
				}
			}
			else if (inputElement.nodeName != 'SELECT' && inputElement.getAttributeNode('type').value == 'radio') { // this so even if a radio is not hidden but belongs to a group of hidden radios it will still work.
				arrLabels[i].onclick = crir.toggleRadioLabel;
				inputElement.onclick = crir.toggleRadioLabel;
			}
		}			
	},	
	
	findLabel: function (inputElementID) {
		//arrLabels = document.getElementsByTagName('label');
		arrLabels = $$('label');
	
		searchLoop:
		for (var i=0; i<arrLabels.length; i++) {
			if (arrLabels[i].getAttributeNode('for') && arrLabels[i].getAttributeNode('for').value == inputElementID) {				
				return arrLabels[i];
				break searchLoop;				
			}
		}		
	},	
	
	toggleCheckboxLabel: function () {
		//labelElement = crir.findLabel(this.getAttributeNode('id').value);
		labelElement = crir.findLabel(this.id);
	
		var matchtype = $type($(this.id).getProperty('class').match(/custom\[(\d+)\]/));
        var match = (matchtype != false) ? $(this.id).getProperty('class').match(/custom\[(\d+)\]/)[1] : 0;

        /*if(labelElement.className == 'checkbox_checked') {
        			labelElement.className = "checkbox_unchecked";
        		}
        		else {
        			labelElement.className = "checkbox_checked";
        		}*/  
                
        if(labelElement.className == CheckboxClass[match][1]) {
			labelElement.className = CheckboxClass[match][0];
		}
		else {
			labelElement.className = CheckboxClass[match][1];
		}
	},	
	
	toggleRadioLabel: function () {		  
		clickedLabelElement = crir.findLabel(this.id);

		clickedInputElement = this;
		clickedInputElementName = this.getProperty('name');
		//clickedInputElementName = clickedInputElement.getProperty('name');
		
		/**
		The original line: arrInputs = document.getElementsByTagName('input');
		Rewrote this using mootools because Fx gave an error that fucked by brain for the last 30 minutes... Works in IE7,Fx 2.0,Opera 9.2. Didn't test in other browsers, but probably will work.
		*/
        
		arrInputs = $$('input[type=radio]');		
	
        var matchtype = $type($(clickedInputElement.id).getProperty('class').match(/custom\[(\d+)\]/));
        var match = (matchtype != false) ? $(clickedInputElement.id).getProperty('class').match(/custom\[(\d+)\]/)[1] : 0;
    
		// uncheck (label class) all radios in the same group
		arrInputs.each(function(el) {
            if (el.getProperty('name') == clickedInputElementName && clickedInputElement.hasClass('crirHidden'))
            {                
                labelElement = crir.findLabel(el.id);                  
                labelElement.className = RadioClass[match][0];
            }
        });
	
		// if the radio clicked is hidden set the label to checked
		if (clickedInputElement.hasClass('crirHidden')) {
			clickedLabelElement.className = RadioClass[match][1];
		}
	},
	
	addEvent: function(element, eventType, doFunction, useCapture){
		if (element.addEventListener) 
		{
			element.addEventListener(eventType, doFunction, useCapture);
			return true;
		} else if (element.attachEvent) {
			var r = element.attachEvent('on' + eventType, doFunction);
			return r;
		} else {
			element['on' + eventType] = doFunction;
		}
	}
}

crir.addEvent(window, 'load', crir.init, false);