Event.observe(window,'load',function(){
	var rhc_accordion = $('rhc_accordion');
	if(rhc_accordion){
		rhc_accordion.select('.accordion_segment').each(function(segment){
			segment.segment = new accordion_segment(segment);
			if(segment.hasClassName('rhc_accordion_expand')) segment.segment.toggle();
		});
		rhc_accordion.style.visibility = 'visible';

		//hover on titles
		$$('.accordion_segment_titlebar').each( function(e) {
		Event.observe(e, 'mouseover', function() {
		    Element.addClassName(e, 'accordion_segment_titlebar_hover');
		});
		Event.observe(e, 'mouseout', function() {
			Element.removeClassName(e, 'accordion_segment_titlebar_hover');
		});

		rhc_accordion.remove_rhp = function(rhp_id){
			// REMOVES ANY PROMOS WITH THIS RHP ID IN ANY SEGMENT. TO REMOVE IT
			// FROM ONLY A PARTICULAR SEGMENT, CALL THE remove_rhp METHOD BELONGING
			// TO THAT SEGMENT.
			rhc_accordion.select('.accordion_segment').each(function(segment){
				segment.segment.remove_rhp(rhp_id);
			});
		}
	});
	}
	var rhc = $$('td#right')[0];
	if(rhc) rhc.style.backgroundImage = 'none';
});

var accordion_segments = [];

function toggle_segment(title_bar,invoke_site_promo_tag){
	get_segment(title_bar.up('.accordion_segment').id).toggle(false,true);
}

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');

	// REMEMBER HEIGHT
	this.original_height = this.content_element.offsetHeight;

	// REMEMBER HEIGHTS OF ITEMS
	var rhp_items = this.content_element.select('div.rhp_item');
	rhp_items.each(function(rhp_item){
		rhp_item.original_height = rhp_item.clientHeight;
	});

	this.content_element.style.height = '1px';
	this.content_element.style.display = 'none';
	this.content_element.style.marginLeft = '0px';

	accordion_segments.push(this);

	var title_bar = this.element.down('.accordion_segment_titlebar');
	title_bar.observe('click',function(){toggle_segment(this,true);},false);

	this.toggle = function(override,invoke_site_promo_tag){
		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('rhc_accordion_expand');
			this.element.addClassName('rhc_accordion_collapse');
		}else{
			this.element.removeClassName('rhc_accordion_collapse');
			this.element.addClassName('rhc_accordion_expand');
			if(invoke_site_promo_tag){
				// ASSUMING WE SHOULD STRIP OUT URL PARAMS FOR REPORTING PURPOSES (WE WOULD OTHERWISE USE
				// EITHER '?' OR '&'
				var tag = window.location.pathname + '?cm_sp=Toughbook Site Promotions-_-Right Hand Category-_-' + cm_sanitize(this.caption());
				cmCreateManualLinkClickTag(tag);
			}
		}
	}
	this.caption = function(){
		var title_bar = this.element.down('.accordion_segment_titlebar');
		return (title_bar.innerText||title_bar.textContent);
	}
	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';
	}
	this.remove_rhp = function(rhp_id){
		var rhp = this.content_element.down('div[rhp_id=\''+rhp_id+'\']');
		if(rhp){
			var horizontal_rule_height = 1;
			this.original_height -= rhp.original_height + horizontal_rule_height;
			rhp.style.display = 'none';
			var hr = rhp.next('div.horizontal_rule');
			if(hr) hr.style.display = 'none';
			// IF SEGMENT CURRENTLY OPEN, RESIZE
			if(this.state=='expand'){
				// REDUCE HEIGHT
				this.current_height = this.original_height - 1;
				this.start_timer();
			}
		}
	}

}

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);
		}
	}
}