// JavaScript Document

var associates = Array();

document.observe('dom:loaded', function(){
	$$('#associatesBar .asc_details').each(function(e){
		associates.push(e);
	});
	
	associates.pointer = 0;
	
	associates.current = function() {
		return associates[associates.pointer];
	};
	
	associates.next = function() { 
		if (associates.length > 0)
		{
			associates.pointer += 1;  
			if (associates.pointer >= associates.length)
			{
				associates.pointer = 0;
			}
			return associates.current();
		} else {
			return false;
		}
	};
	
	associates.prev = function() {
		if (associates.length > 0)
		{
			associates.pointer -= 1;  
			if (associates.pointer < 0)
			{
				associates.pointer = associates.length - 1;
			}
			return associates.current();
		} else {
			return false;
		}
	};
	
	associates.goto = function(i) {
		if (associates.length > 0)
		{
			associates.pointer = i;  
			if (associates.pointer < 0)
			{
				associates.pointer = 0;
			} else if (associates.pointer >= associates.length) {
				associates.pointer = associates.length - 1;
			}
			return associates.current();
		} else {
			return false;
		}
	}
	
	$('asc_next').clickFX = function(){
		if (associates.length > 1)
		{
			var c = associates.current();
			
			new Effect.Opacity(c.identify(), {
				from: 1,
				to: 0,
				duration: 0.5,
				beforeSetup: function(){
					$('asc_next').stopObserving('click', $('asc_next').clickFX);
					$('asc_prev').stopObserving('click', $('asc_prev').clickFX);
					
					var n = associates.next();
					n.show();
					
					new Effect.Opacity(n.identify(), {
						from: 0,
						to: 1,
						duration: 0.5,
						afterFinish: function(){
							c.hide();
							$('asc_next').observe('click', $('asc_next').clickFX);
							$('asc_prev').observe('click', $('asc_prev').clickFX);
						}
					});
				}
			});
		}
	};
	$('asc_next').observe('click', $('asc_next').clickFX);
	
	$('asc_prev').clickFX = function(){
		if (associates.length > 1)
		{
			var c = associates.current();
			
			new Effect.Opacity(c.identify(), {
				from: 1,
				to: 0,
				duration: 0.5,
				beforeSetup: function(){
					$('asc_next').stopObserving('click', $('asc_next').clickFX);
					$('asc_prev').stopObserving('click', $('asc_prev').clickFX);
					
					var p = associates.prev();
					p.show();
					
					new Effect.Opacity(p.identify(), {
						from: 0,
						to: 1,
						duration: 0.5,
						afterFinish: function(){
							c.hide();
							$('asc_next').observe('click', $('asc_next').clickFX);
							$('asc_prev').observe('click', $('asc_prev').clickFX);
						}
					});
				}
			});
		}
	};
	$('asc_prev').observe('click', $('asc_prev').clickFX);
});
