/////////////////////////////////////////////////////////////////////
jQuery(document).ready(function() {
//CREATE RELATED PEOPLE UI ELEMENTS	
	relatedPeople();
	function relatedPeople() {
		jQuery('#relatedPeopleContent').append('<form id="relatedPeopleSort"><select><option value="all">Choose a Related Professional</option><option value="all">View All</option></select></form>');
		jQuery('#relatedPeopleContent').append('<div id="relatedPeopleList"><div class="topBlock"></div><div class="scrollTop"></div><div class="scrollBottom"></div><ul></ul></div>');
		jQuery('#peopleScroll li:first').clone().appendTo('#peopleScroll ul');
		jQuery('#peopleScroll li:first-child').attr('id' , 'active');
		//CREATE SELECT OPTIONS
		var titles = new Array();
		jQuery('#peopleScroll li').each(function() {
			var title = jQuery(this).find('.title').html();
			var value = jQuery(this).find('.title').attr('rel');
			if ( jQuery.inArray( title, titles ) == -1)
			{
				titles[titles.length] = title;
				jQuery('#relatedPeopleSort select').append('<option value="'+ value +'">'+ title +'</option>');
			}
		});
		
		//CREATE RELATED PEOPLE LIST
		jQuery('#peopleScroll li').each(function() {
			var name = jQuery(this).find('a').html();
			var href = jQuery(this).find('a').attr('href');
			var title = jQuery(this).find('.title').html();
			var className = jQuery(this).attr('class');
			var value = jQuery(this).find('p.title').attr('rel');
			jQuery('#relatedPeopleList ul').append('<li class="'+ className +'" rel="'+ value +'"><a href="'+ href +'">'+ name +',</a> '+ title +'</li>')
		});
	}
	
//FILTER BY TITLE 
	jQuery('#relatedPeopleSort select').change(function(){
		var selected = jQuery('#relatedPeopleSort select option:selected').attr('value');
		jQuery('#relatedPeopleList').css({'display' : 'block'});
		jQuery('#relatedPeopleContent').css({'height' : '295px'})
		jQuery('#relatedPeopleList ul').css('top' , '0');
		jQuery('#relatedPeopleList li').css('display' , 'none');
		jQuery('#relatedPeopleList li').each(function() {
			var title = jQuery(this).attr('rel');
			if(title === selected) {
				jQuery(this).css('display' , 'list-item');
			}
			else if (selected === 'all') {
				jQuery('#relatedPeopleList li').css('display' , 'list-item');
			}
		});
		jQuery('#relatedPeopleList li:last').css('display' , 'none');
	});	
	
//SHOW EMPLOYEE ON HOVER 	
	jQuery('#relatedPeopleList li').live('mouseenter', function() {
		var classRef = jQuery(this).attr('class');
		if(!jQuery('#peopleScroll ul').is(':animated')) {
			jQuery('#peopleScroll ul').css('margin-left' , '10px');
			jQuery('#peopleScroll li').css('display' , 'none').attr('id' , '');
			jQuery('#peopleScroll').find('.'+classRef).css('display' , 'block').attr('id', 'active');
		}
	});
	
//SCROLL UP AND DOWN THROUGH PROFESSIONALS 
	jQuery('#relatedPeopleList ul').css('top', '0px');
	function scrollList() {
		var startScroll,
		stopScroll,
		int; 
		(function() {
			var interval = 5,
				stopped = true;
				startScroll = function(scrollWhere, height) {
					if(stopped !== false) {
						var stopped = false;
						int = setInterval(function() { 
							var position = jQuery('#relatedPeopleList ul').position();
							var positionTop = position.top;
							var stopPosition = height-150; //150 is the size of the window
							if (scrollWhere == 'bottom') {
								if(-positionTop>=stopPosition) {
									return false 
								}
								else {
									scrollDown();
								}
							}
							else {
								if (-positionTop<=0) {
									return false;
								}
								else {
									scrollUp();
								}
							}	
						}, interval);
					}
			};
			stopScroll = function() {
				var stopped = true;
				clearInterval(int);
			};
		})();
		function scrollDown() {
			jQuery('#relatedPeopleList ul').css({'top' : '-=2px'});	
		}
		function scrollUp () {
			jQuery('#relatedPeopleList ul').css({'top' : '+=2px'});	
		}		
				
		jQuery('#relatedPeopleList .scrollBottom').live('mousedown' , function() {
			var scrollWhere = 'bottom';
			var height = jQuery('#relatedPeopleList ul').height();
			startScroll(scrollWhere, height);
		});
		jQuery('#relatedPeopleList .scrollTop').live('mousedown' , function() {
			var scrollWhere = 'top';
			var height = jQuery('#relatedPeopleList ul').height();
			startScroll(scrollWhere, height);
		});		
		jQuery('#relatedPeopleList .scrollBottom, #relatedPeopleList .scrollTop').live('mouseup' , function() {
			stopScroll();
		});						
	}
	scrollList();	
	
//AUTO SCROLL THROUGH IMAGES	
	function slidePeople() {
		var interval = 1200;
		var last = jQuery('#peopleScroll li:last').attr('id');
		if(last === 'active') {
			jQuery('#peopleScroll li#active').attr('id', '');
			jQuery('#peopleScroll ul').css({'margin-left' : '10px'});
			jQuery('#peopleScroll ul').animate({'margin-left' : '-=290px'}, interval, 
				function() {
					jQuery('#peopleScroll li:nth-child(2)').attr('id' , 'active');
				});		
		}
		else {
			jQuery('#peopleScroll li#active').attr('id', '').next().attr('id', 'active');
			jQuery('#peopleScroll ul').animate({'margin-left' : '-=290px'}, interval);		
		}
	}
	var startScroll,
	stopScroll,
	int; 
	(function() {
		var interval = 2000,
			stopped = true;
			startScroll = function() {
				if(stopped !== false && !jQuery('#peopleScroll ul').is(':animated')) {
					var stopped = false;
					int = setInterval(function() { 
						slidePeople();
					}, interval);
				}
		};
		stopScroll = function() {
			var stopped = true;
			clearInterval(int);
		};
	})();
	startScroll();
	jQuery('#relatedPeopleContent').mouseenter(function(){
		var stopScrolling = setInterval(function() {
			if(!jQuery('#peopleScroll ul').is(':animated')) {
				stopScroll();
				clearInterval(stopScrolling);
			}
		},10)
	});
	jQuery('#relatedPeopleContent').mouseleave(function() {		
		var position = jQuery('#peopleScroll #active').index();
		var currentMargin = -((position*290)-10); //290 = width of li
		jQuery('#peopleScroll li').css('display' , 'block');
		jQuery('#peopleScroll ul').css('margin-left' , currentMargin);
		startScroll();
	});
	jQuery(window).blur(function() {
		stopScroll();
	});	
});

/////////////////////////////////////////////////////////////////////

