//Event.observe(window, 'load', accordion_init);
document.observe('dom:loaded', accordion_init);

function accordion_init() {
    if ($('rhc_accordion')) {
        $$('.accordion_segment').each(function(segment) {
            segment.segment = new accordion_segment(segment);
            if (segment.hasClassName('expand')) segment.segment.toggle();
        });
        $('rhc_accordion').style.visibility = 'visible';
    }
}

var accordion_segments = [];

function toggle_segment(title_bar){
	get_segment(title_bar.up('.accordion_segment').id).toggle();
}

function get_segment(element_id){
	return accordion_segments.find(function(segment){
		return (segment.element.id==element_id);
	});
}

function animate_segments(){
	for(var i=0;i<accordion_segments.length;i++){
		if(accordion_segments[i].animating) accordion_segments[i].animate();
	}
}

window.setInterval(animate_segments,1);

function accordion_segment(element){
		this.state = 'collapse';
		this.speed = 10;
		this.element = element;
		this.element.id = 'accordion_segment_'+accordion_segments.length;
		this.current_height = 1;
		this.animating = false;
		this.content_element = this.element.down('.accordion_segment_content');
		this.original_height = this.content_element.offsetHeight;
		this.content_element.style.height = '1px';
		this.content_element.style.display = 'none';
		this.content_element.style.marginLeft = '0px';
		this.content_element.style.position = 'relative';
		this.content_element.style.visibility = 'visible';
		
		//this.element.addClassName('collapse');
		accordion_segments.push(this);

		var title_bar = this.element.down('.accordion_segment_titlebar');
		title_bar.style.cursor = "pointer";
		title_bar.observe('click',function(){toggle_segment(this);},false);

		this.toggle = function(override) {
		    if (this.animating && !override) return;
		    collapse_segments(this.element.id);
		    this.state = (this.state == 'collapse') ? 'expand' : 'collapse';
		    this.start_timer();

		    if (this.state == 'collapse') {
		        
		        this.element.removeClassName('expand');
		        this.element.addClassName('collapse');

		        title_bar.down('.numberImage').style.color = '#fff';
		        title_bar.down('.numberImage').style.opacity = '.50';
		        title_bar.down('img').src = './app_images/bluearrow.gif';
		        title_bar.down('img').style.width = '4px';
		        title_bar.down('img').style.height = '10px';
		    } else {
		        this.element.removeClassName('collapse');
		        this.element.addClassName('expand');

		        title_bar.down('.numberImage').style.color = '#001b34';
		        title_bar.down('.numberImage').style.opacity = '100';
		        title_bar.down('img').src = './app_images/bluearrow_down.gif';
		        title_bar.down('img').style.width = '10px';
		        title_bar.down('img').style.height = '4px';
		        title_bar.down('img').style.marginTop = '-5px';
		    }
		}
		this.animate = function(){
			// TODO: USE sin TO ADD EASING
			this.increment = this.speed;

			if(this.state=='collapse'){
				if(this.current_height>1){
					this.current_height = Math.max(1,this.current_height-this.increment);
				}
			}else{
				if(this.current_height<this.original_height){
					this.current_height = Math.min(this.original_height,this.current_height+this.increment);
				}
			}

			this.content_element.style.height = this.current_height + 'px';
			this.set_opacity();
			if((this.state=='collapse'&&this.current_height==1)||(this.state=='expand'&&this.current_height==this.original_height)) this.stop_timer();
		}

		this.set_opacity = function(){
			var opacity = this.current_height / this.original_height;
			this.content_element.setOpacity(opacity);
		}

		this.start_timer = function(){
			this.animating = true;
			this.content_element.style.display = 'block';
		}
		this.stop_timer = function(){
			this.animating = false;
			if(this.current_height==1) this.content_element.style.display = 'none';
		}
}

function collapse_segments(exclude_element_id){
	for(var i=0;i<accordion_segments.length;i++){
		if(accordion_segments[i].state=='expand'&&accordion_segments[i].element.id!=exclude_element_id){
			accordion_segments[i].toggle(true);
		}
	}
}