CH.ns(function() {with(CH) {
	Dropdown = Class.create({
		initialize: function(options) {
			
			this.setOptions(options); //initialize the default options

			this.dropdown = $(this.options.dropdownContainer);

			if(this.options.hitAreaClass == null)
				this.hitArea = this.dropdown.select('a')[0];
			else
				this.hitArea = this.dropdown.select('.' + this.options.hitAreaClass)[0];

			this.list = this.dropdown.select('.' + this.options.dropdownListClass)[0];

			this.out = false;
			
			this.list.hide();

			if(this.options.closeButtonClass != null){
				var close_button = this.list.select('.' + this.options.closeButtonClass)[0];
				Event.observe(close_button, 'click', this.hideList.bindAsEventListener(this));
			}

			Event.observe(this.hitArea, 'click', function(){ this.out = true; }.bind(this));			
			Event.observe(this.hitArea, this.options.mouseBehavior, this.showListDelay.bindAsEventListener(this));
			Event.observe(this.dropdown, "mouseout", this.hideListDelay.bindAsEventListener(this));	
		},
	
		// hit area should be a className and should follow the format "hitAreaPrefix_dropdownContainer"
		getHitArea: function() {
			return $$("." + this.options.dropdownHitAreaPrefix + "_" + this.dropdown.id)[0];
		},
		
		// list should be an id and should follow the format "listPrefix_dropdownContainer"
		getList: function() {
			return $(this.options.dropdownListPrefix + "_" + this.dropdown.id);
		},

		showListDelay: function(e){
			this.out = false;
			setTimeout(function(){ this.showList() }.bind(this), this.options.delayTime);
		},
		
		//shows list when mouseBehavior on hitarea
		showList: function(target) {
			if(!this.out){
				this.dropdown.addClassName(this.options.selectedClass);
				this.list.show();
			}
		},

		hideListDelay: function(e){
			this.out = true;
			var event = e;
			setTimeout(function(){ this.hideListCheck(event); }.bind(this), this.options.delayTime);
		},

		hideListCheck: function(event){

			if(this.out){

				if(!Element.descendantOf(event.relatedTarget, this.dropdown)){
					this.hideList();
				}
			}

		},
		
		hideList: function(event, force) {
			this.list.hide();
			this.dropdown.removeClassName(this.options.selectedClass);			
		},
		
		setOptions: function (options) {
		
			this.options = {
				dropdownContainer: 'dropdown',
				mouseBehavior: 'mouseover',
				HitAreaClass: null,
				dropdownListClass: 'dropdown',
				selectedClass: 'selected_hover',
				delayTime: 350,
				closeButtonClass: null
			};
			Object.extend(this.options, options || {});
		}
	});
	
}});