/** Analysts JS

	Detaches the Analyst portion of the right bar menu when offscreen.

**/

var centerTarget = function(){

	var scrollContent = $('team_member_content');
	var scroller = new Fx.Scroll(scrollContent);
	var adjusted_y = $('target').getPosition(scrollContent).y - ( scrollContent.getSize().y - $('target').getSize().y ) / 2;
	
	if(adjusted_y < 0) {
		adjusted_y = 0; 
	}
	
	scroller.set(0,adjusted_y);
	
	return scroller;
	
}

var wakeScrollers = function(scroller){

	var analyst_links = $$('a.analyst_link');
	analyst_links.each(function(element){
		
		element_target = $(element.get('rel'));
		var adjusted_y = element_target.getPosition(this.element).y - ( this.element.getSize().y - element_target.getSize().y ) / 2;
		element.store('scroller',this).store('adjusted_y',adjusted_y).addEvent('click',scrollToAnalyst);
	
	},scroller);
	
}

var scrollToAnalyst = function(){

	this.retrieve('scroller').start(0,this.retrieve('adjusted_y'));
	
}

var detachedMenu = new Class({

	initialize: function(element,scrollWindow){
	
		this.menu = $(element).setStyle('top',0);
		this.container = $(scrollWindow);
	
		this.start_analyst_position = this.menu.getPosition(this.container).y;
		this.scroll_window_height = this.container.getSize().y
		this.analyst_list_height = this.menu.getSize().y
		// this.offset_adjust = ( this.scroll_window_height - this.analyst_list_height ) / 2;
		this.offset_adjust = (this.scroll_window_height - this.scroll_window_height);

		this.container.addEvent('scroll',this.adjust.bindWithEvent(this));
		this.detached = false;
		
	},
	adjust: function(event){
	
		//alert(this.offset_adjust);
		if(this.container.getScroll().y > this.start_analyst_position){
			
			if(this.detached == false){
			
				this.detached = true;
				//this.menu.setStyle('border-top','1px solid white');
			}	
			var offset = this.container.getScroll().y - this.start_analyst_position + this.offset_adjust;
			//this.menu.tween('top',offset);
			this.menu.setStyle('top',offset);
		
		}
		else{
		
			this.detached = false;
			//this.menu.setStyle('border-top','');
			//this.menu.tween('top',0);
			this.menu.setStyle('top',0);
			
		}
				
	}
});