if (!CD3) var CD3 = {};
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);
		
		// create replace elements
		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);
		
		// insert container and all elements in it
		select.insert({before: this.container.insert(this.link.insert(this.linkspan)).insert(this.hidden).insert(this.div.insert(this.ul))});

		// add options
		if ($A(select.options).each(this.addOption.bind(this)).length > 6)
			this.div.addClassName('scrolled');
		
		// delete old select
		Element.remove(select);
		
		// add events
		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))
		));
	},
	select: function(option){
		this.linkspan.innerHTML = option.text;
		this.hidden.value		= option.value != null ? option.value : option.text;
		this.hide();
	}
});

CD3.Behaviors({
	// normal page behaviors
	'input[type=text]': {
		focus:	function() { if (this.value == this.title)	this.value = '';			},
		blur:	function() { if (!this.value)				this.value = this.title;	}
	},
	'#submitbtn': {
		mouseover:	function() { this.style.backgroundPosition='left bottom'; },
		mouseout:	function() { this.style.backgroundPosition='left top'; }
	},
	// rooms menu
	'#rooms .big_star a:click': function(e){
		if (this.hasClassName('select')) {
			this.removeClassName('select');
			this.up().next('div').blindUp({duration: .5});
		} else {
			$('rooms').select('.select').each(function(a){
				a.removeClassName('select');
				a.up().next('div').blindUp({duration: .5});
			});
			this.addClassName('select');
			this.up().next('div').blindDown({duration: .5});

			$$('#images .center_ie').each(function(div, i){
				div.fade({
					afterFinish: function(effect){
						effect.element.innerHTML = '<img src="' + this.getAttribute('image' + i) + '" alt=""  />';
						effect.element.appear();
					}.bind(this)
				});
			}.bind(this));
		}
	},
	// index
	// request form
	'select': CD3.Select,
	'#request_form': function(form){
		var calendar = new CD3.Calendar('calendar', {
			min_date:	new Date(),
			onClick:	function (yr, mo, dy) {
				var data = [dy, mo, yr];
				$$('#request_form .trav')[this.opener].select('.select').each(function(span, index){
					var value = data[index];
					span.down('input').setValue(value);
					span.down('.drop span').innerHTML = value;
				});
			}
		});
		
		form.select('.cal').invoke('observe', 'click', function(){
			document.stopObserving('click');
			var pos = this.cumulativeOffset();
			calendar.show({top: pos[1], left: pos[0] + 22});
			calendar.opener = this.hasClassName('java_calendar') ? 0 : 1;
		});
	}
});