/*
	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.
*/

var breadcrumbs = new YAHOO.util.Element('breadcrumbs'); 

crir = {
	userAgent: '',
	isSafari: false,
	init: function() {
		
		this.userAgent = navigator.userAgent.toLowerCase();
		this.isSafari = ((this.userAgent.indexOf('safari')!=-1)&&(this.userAgent.indexOf('mac')!=-1))?true:false;
		
		if (this.isSafari) {
			alert('This section of Women\'s Link Worldwide is only supported in Internet explorer v6+ and Firefox, please use a different browser (sorry!)');
		}
		
		if (! this.isSafari) { // the script doesn't work in safari.
			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);
				}
				else {				
					continue searchLabels;
				}	
								
				inputElementClass = inputElement.className;	
			
				// if the input is specified to be hidden intiate it
				if (inputElementClass == 'crirHiddenJS') {
					inputElement.className = 'crirHidden';
					
					inputElementType = inputElement.getAttributeNode('type').value;	
					
					// 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') { 
							inputElement.checked = false;
							arrLabels[i].className = 'checkbox_unchecked'}
						else { arrLabels[i].className = 'radio_checked' }
					}
					else {
						if (inputElementType == 'checkbox') { arrLabels[i].className = 'checkbox_unchecked'}
						else { arrLabels[i].className = 'radio_unchecked' }
					}
				}
				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');
	
		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);
		
		var breadcrumbs = document.getElementById("breadcrumbs");
		var breadcrumbsContent = breadcrumbs.innerHTML;
		var currentFilter = labelElement.innerHTML;
		
		if(labelElement.className == 'checkbox_checked') {
			labelElement.className = "checkbox_unchecked";

			breadcrumbs.innerHTML = breadcrumbsContent.replace(currentFilter + ' + ', '');
									
			toggleFilter(this);
			returnToResults();

		}
		else {
			labelElement.className = "checkbox_checked";			
			breadcrumbs.innerHTML = breadcrumbs.innerHTML + labelElement.innerHTML + " + ";
			toggleFilter(this);
			returnToResults();

		}
	},	

	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);