    
document.observe('dom:loaded', function() {
	if (!$('distributors')) { return false; }

	var all_distributors = $$('div.distributor-group');
	all_distributors.invoke('hide');
	all_distributors.first().show();

	function showDistributorsInState(event) {
		var state_value = $F(Event.element(event));
		var message_container = $('distributor_locator_messages');

		all_distributors.invoke('hide');

		if (state_value.blank()) {
			message_container.innerHTML = "Select a state from the list above.";
			return;
		}	

		var state_el = $(state_value);

		if (state_el) {	
			state_el.show();
			message_container.innerHTML = "";

		}	else {
   		message_container.innerHTML = "There are no distributors available for the selected state.";
		}
	}

	$('state_list').observe('change', showDistributorsInState);

});


// ---------------------------------------------------------------------------
// Test tool client-side scoring

var Quiz = Class.create({
	initialize: function(element) {
		this.element = $(element);
		if (!this.element) { return; }
		
		this.form = this.element.down('form');
		this.score = this.element.down('.score');
		this.score.down("button.try_again").observe('click', this.clearAnswers.bindAsEventListener(this));
		this.element.down("button.check").observe('click', this.scoreQuiz.bindAsEventListener(this));
		this.answer_fields = this.element.select("input[type=radio]").pluck('name').uniq();
		this.total = this.answer_fields.size();
	},
	
	scoreQuiz: function(e){
		if(e){ e.stop(); }
		var results = $H(this.form.serialize(true));
		var currentScore = this.answer_fields.inject(0, function(sum, field_name){
			var element = $(field_name);
			if(results.get(field_name) == '1'){
				element.addClassName("correct");
				return sum + 1;
			} else {
				element.addClassName("wrong");
				return sum;
			}
		});
		if (this.total == currentScore){
			this.score.removeClassName('fail').addClassName('pass');
		} else {
			this.score.removeClassName('pass').addClassName('fail');
		}
		// this.score.down(".right_answers").update(currentScore);
		// this.score.down(".percent").update((currentScore/this.total * 100).round());
		this.element.down(".check").hide();
		this.element.select("input").invoke('disable');
		this.score.show();
	},
	
	clearAnswers: function(e){
		if(e) { e.stop(); }
		this.form.reset();
		this.element.select("li.Question").each(function(element){ element.removeClassName("wrong").removeClassName("correct"); });
		this.score.hide();
		this.element.select("input").invoke('enable');
		this.element.down(".check").show();
	}
});

document.observe('dom:loaded', function(){
	$$('div.Quiz').each(function(element){ var quiz = new Quiz(element); quiz.clearAnswers(); });
});



REDKEN.addFeature('favorites-link', {
	setupElements : function() {

		var FavoritesLink = Class.create({
			initialize : function(anchor) {
				this.node = $(anchor);
				if (!this.node) { return; }
				this.node.observe('click', this.clickHandler.bindAsEventListener(this));
			},

			clickHandler : function(e){
				e.stop();
				e.element().blur();

				this.processFavorites();
			},
			
			processFavorites : function() {
				new Ajax.Request(this.node.href, {
					'method' : ((this.node.id == 'favorites-link') ? 'post' : 'delete'),
					'onSuccess' : this.successHandler.bind(this)
				});
			},

			successHandler : function(transport) {
				this.node.replace(transport.responseText);
				if (typeof cmCreateConversionEventTag === "function") {
					cmCreateConversionEventTag("Save to Favorites", 1);
				}
			}
		});

		$$('#favorites-link-remove, #favorites-link').each(function(anchor){
			new FavoritesLink(anchor);
		});
	}
});

// ---------------------------------------------------------------------------
// Professional homepage sidecolumn tabset

var HomeTabs = Class.create({
	initialize : function(tabset){
		this.node = $(tabset);
		if (!this.node) { return; }
		this.tabsets = this.node.up().select('.Tab');
		this.tabs = this.node.select('LI');
		if (this.tabsets.length < 2 || this.tabs.length < 2) { return; }
		this.tabsets.invoke('addClassName','Activated');
		this.tabs[0].addClassName('Active');
		this.node.setStyle({ display : 'block' });
		this.tabsets[1].hide();
		this.tabs.each(function(tab){ tab.observe('click', this.switchTabs.bindAsEventListener(this, tab.id)); }, this);
	},
	switchTabs : function(e, click_id){
		var actives = click_id.match(/stylist/) ? [1,0] : [0,1];
		this.tabs[actives[0]].addClassName('Active');
		this.tabs[actives[1]].removeClassName('Active');
		this.tabsets[actives[0]].setStyle({ display : 'block' });
		this.tabsets[actives[1]].hide();
	}
});

document.observe('dom:loaded', function(){
	new HomeTabs('homepage-tabset');
});
