var CD3 = {};
CD3.Behaviors = function(rules){
if (Prototype.Browser.IE)
Event.observe(window, 'load', CD3.Behaviors.assign.curry(rules));
else
document.observe('dom:loaded', CD3.Behaviors.assign.curry(rules));
};
Object.extend(CD3.Behaviors, {
cache: [],
assign: function(rules){
for (var selector in rules) {
var observer = rules[selector];
selector.split(',').each(function(sel) {
var parts = sel.split(/:(?=[a-z]+$)/), css = parts[0], event = parts[1];
$$(css).each(function(element) {
if (event) {
CD3.Behaviors.observe(element, event, observer);
} else if (Object.isFunction(observer)){
if (observer.prototype.initialize)
new observer(element);
else
observer(element);
} else {
for(var e in observer)
CD3.Behaviors.observe(element, e, observer[e]);
}
});
});
}
},
observe: function(element, event, observer){
var behavior = CD3.Behaviors;
if (!Object.isFunction(observer))
observer = behavior.delegate(observer);
$(element).observe(event, observer);
behavior.cache.push([element, event, observer]);
},
delegate: function(rules){
return function(e){
var element = $(e.element());
for (var selector in rules)
if (element.match(selector))
return rules[selector].call(this, e);
}
},
unload: function(){
CD3.Behaviors.cache.each(function(c) {
Event.stopObserving.apply(Event, c);
});
CD3.Behaviors.cache = [];
}
});
Event.observe(window, 'unload', CD3.Behaviors.unload);
CD3.Radio = Class.create({
initialize: function(radio){
this.radio = $(radio).hide();
this.name = this.radio.getAttribute('name') || this.radio.identify();
this.button = new Element('a', {className:'radio', href:'javascript:;'}).update(' ');
this.radio.insert({before: this.button});
if (this.radio.className)
this.button.addClassName(radio.className);
this.button.observe('click', this.toggle.bind(this));
this.refresh();
if (!CD3.Radio._elements[this.name])
CD3.Radio._elements[this.name] = [];
CD3.Radio._elements[this.name].push(this);
},
toggle: function(){
this.radio.checked = !this.radio.checked;
CD3.Radio._elements[this.name].invoke('refresh');
},
refresh: function(){
this.button[this.radio.checked ? 'addClassName' : 'removeClassName']('selected');
}
});
CD3.Radio._elements = {};
CD3.Dropdown = Class.create({
initialize: function (container) {
this.container = $(container);
this.link = this.container.down('a.drop')
this.div = this.container.down('div').hide();
this.ul = this.container.down('ul');
this.bindEvents();
},
bindEvents: function(){
this.link.observe('click', this.toggle.bind(this)); 
this.clickObserver = this.close.bind(this);
},
toggle: function(){
this[this.div.visible() ? 'hide' : 'show']();
},
show: function(){ 
Effect.BlindDown(this.div, {duration: 0.2});
document.observe('click', this.clickObserver);
},
hide: function(){
Effect.BlindUp(this.div, {duration: 0.1});
document.stopObserving('click', this.clickObserver);
},
close: function(){
if (this.div.visible()) this.hide();
}
});
CD3.Select = Class.create(CD3.Dropdown, {
initialize: function(select){ 
select = $(select);
this.container = new Element('span', {className:'dropper'});
this.link = new Element('a', {href: 'javascript:;', className: 'drop'})
this.linkspan = new Element('span').update(select.selectedIndex > -1 ? select.options[select.selectedIndex || 0].text : '');
this.hidden = new Element('input', {type: 'hidden', name: select.name, value: select.getValue()});
this.div = new Element('div').hide();
this.ul = new Element('ul');
if (select.className)
this.container.addClassName(select.className);
select.insert({
before: this.container
.insert(this.link.insert(this.linkspan))
.insert(this.hidden)
.insert(this.div.insert(this.ul))
});
var options = Object.extend({
onChange: null,
topBottom: false
}, arguments[1] || {});
if (options.topBottom)
this.ul.insert({
before: new Element('span', {'class': 'top'}).insert(new Element('span')),
after: new Element('span', {'class': 'bottom'}).insert(new Element('span'))
});
this.onChange = options.onChange;
if ($A(select.options).each(this.addOption.bind(this)).length > 6)
this.div.addClassName('scrolled');
Element.remove(select);
this.bindEvents();
},
addOption: function (option){
this.ul.insert(new Element('li').insert(
new Element('a', {href: 'javascript:;'}).update(option.text).observe('click', this.select.bind(this, option))
));
},
removeOptions: function(){
this.ul.select('li').each(function(li){
li.down('a').stopObserving('click');
li.remove();
});
},
select: function(option){
this.linkspan.innerHTML = option.text;
this.hidden.value = option.value != null ? option.value : option.text;
this.hide();
if (this.onChange)
this.onChange.call(this, option.value);
}
});