CH.ns(function() {with(CH) {
	containerSlider = Class.create({
		elements: {}, options: null, current_index: 1, element_count: 0, timer:null,
		
		initialize: function(options) {
			this.setOptions(options); // initialize the options
			
			this.elements.nav_container = $(this.options.nav_container);
			this.elements.elements_container = $(this.options.elements_container);
			
			// set event handelers for the nav buttons
			$A(this.elements.nav_container.getElementsByTagName('a')).each(
				function(button) {
					if(button.id == this.options.nav_next) {
						Event.observe(button, "click", this.next.bindAsEventListener(this));
					} else if(button.id == this.options.nav_previous) {
						Event.observe(button, "click", this.previous.bindAsEventListener(this));
					} else if(button.id.indexOf(this.options.nav_prefix) >= 0) {
						num = button.id.substring(button.id.indexOf(this.options.nav_prefix + "_") + this.options.nav_prefix.length + 1);
						Event.observe(button, "click", this.setElement.bindAsEventListener(this, num, true));
					}
					button.href = 'javascript:void(0)';
				}.bind(this)
			);
			
			$A(this.elements.elements_container.getElementsByClassName(this.options.elements_class)).each(
				function(img) {
					if(img.id.indexOf("_" + this.options.default_element) == -1) {
						Element.setOpacity(img, 0.0 );
						Element.setStyle(img, {display: 'none'});
					} else {
						this.current_index = this.options.default_element;
						this.setClassSelected(this.options.default_element);
					}
					Element.setStyle(img, {position: 'absolute'});
					this.element_count++;
				}.bind(this)
			);
			
			if(this.options.auto_rotate) {
				bindEndRotation = this.endRotation.bindAsEventListener(this);
				bindStartRotation = this.startRotation.bindAsEventListener(this);
				Event.observe(this.elements.nav_container, "mouseover", bindEndRotation);
				Event.observe(this.elements.nav_container, "mouseout", bindStartRotation);
				Event.observe(this.elements.elements_container, "mouseover", bindEndRotation);
				Event.observe(this.elements.elements_container, "mouseout", bindStartRotation);
				
				this.startRotation(); 
			}
		},
		
		setElement: function(e, num, fade) {
			if(num != this.current_index) {
				$(this.options.element_prefix + '_' + this.current_index).show();	
				
				var cur_element = this.options.element_prefix + '_' + this.current_index;
				var new_element = this.options.element_prefix + '_' + num;
	
				Element.setStyle(new_element, {display: ''});
				
				Element.setOpacity( cur_element, 1.0 );
				Element.setOpacity( new_element, 0.0 );
				
				if(fade) {
					new Effect.Opacity(cur_element, {duration:this.options.transition_time, from:1.0, to:0.0,  afterFinish: function() {Element.setStyle(cur_element, {display: 'none'});}.bind(this)});
					new Effect.Opacity(new_element, {duration:this.options.transition_time, from:0.0, to:1.0, afterFinish: function() {Element.setStyle(new_element, {display: ''});}.bind(this)});
				} else {
					Element.setOpacity( cur_element, 0 );
					Element.setOpacity( new_element, 1 );
					
					Element.setStyle(cur_element, {display: 'none'});
					Element.setStyle(new_element, {display: ''});
				}
				
				this.current_index = parseInt(num);
				this.setClassSelected(num);
			}
		},
		
		next: function(e, fade) {
			if(this.current_index == this.element_count) {
				this.setElement(e, 1, fade);
			} else {
				this.setElement(e, parseInt(this.current_index) + 1, fade);
			}
		},
		
		previous: function(e, fade) {
			if(this.current_index == 1) {
				this.setElement(e, this.element_count, fade);
			} else {
				this.setElement(e, parseInt(this.current_index) - 1, fade);
			}
		},
		
		setClassSelected: function(num) {
			$A(this.elements.nav_container.getElementsByTagName("a")).each(function(elm){Element.removeClassName(elm, 'selected');});
			$(this.options.nav_prefix + '_' + num).addClassName('selected');
		},
		
		startRotation: function() {
			this.timer = setTimeout(function() {
				this.next('e', true);
				this.startRotation();
			}.bind(this), this.options.rotate_interval);
		},
		
		endRotation: function() {
			clearTimeout(this.timer);
			this.timer = 0;
		},
		
		setOptions: function(options) {
			this.options = {
				nav_container: 'slider_nav_container', // name of the navigation container
				elements_class: 'slider_elm', // class name for element 
				nav_prefix: 'slider_nav', // prefix for the navigation buttons
				elements_container: 'slider_elements_container', // name of the elements container
				element_prefix: 'slider_element', // prefix for each element in the container
				default_element: 1, // default element button
				nav_next: 'nav_next', // id of the next button
				nav_previous: 'nav_previous', // if of the previous button
				auto_rotate: true,
				rotate_interval: 4000,
				transition_time: .5
			};
			Object.extend(this.options, options || {});
		}
	});
	
}});