// JavaScript Document

var increment = 540;

//Event.observe(window, "load", function() {
//	initPeopleScroller();
//});

function initPeopleScroller(scrolling){
	
	if (!scrolling) {
	if ($('scrollLeft') && $('scrollRight')) {
		$('scrollLeft').update('<a onclick="scrollPeopleLeft(); return false;" class="scrollerright"></a>');
		$('scrollRight').update('<a onclick="scrollPeopleRight(); return false;" class="scrollerleftnull"></a>');
/*
		// observe scroller left/right
		$('scrollLeft').observe('mouseover', function(e) {
			$('scrollLeft').update('<a onclick="scrollPeopleLeft(); return false;"><img src="/img/buttons/arrow-right-RO.png" /></a>');
		});
		$('scrollLeft').observe('mouseout', function(e) {
			$('scrollLeft').update('<a onclick="scrollPeopleLeft(); return false;"><img src="/img/buttons/arrow-right.png" /></a>');
		});
		$('scrollRight').observe('mouseover', function(e) {
			$('scrollRight').update('<a onclick="scrollPeopleRight(); return false;"><img src="/img/buttons/arrow-left-RO.png" /></a>');
		});
		$('scrollRight').observe('mouseout', function(e) {
			$('scrollRight').update('<a onclick="scrollPeopleRight(); return false;"><img src="/img/buttons/arrow-left.png" /></a>');
		});
*/
	}
	}
	
	var biogs = $$('.person');
	var titles = $$('.person h2')
	if (!scrolling) {
	titles.invoke('hide');
	biogs.invoke('hide');
	biogs[0].show();

	// make 1st list item active
	$('peopleScroller').firstDescendant().addClassName('active');
	}
	
	// collapse list
	if ($('peopleScrollerContainer')) {
		$('peopleScrollerContainer','peopleScroller').invoke('addClassName', 'active');
	}
	
	// attach onclick to navList links
	var peopleScroller = $('peopleScroller');
	var scrollerLinks = peopleScroller.getElementsByTagName("li");
	for (var i=0; i<=scrollerLinks.length-1; i++) {
		$(scrollerLinks[i]).observe('click', function(e) {
			var person = this.firstDescendant().href.split('#')[1];
			$('peopleReel').showFlashImage(person);
			// hide everyones descriptions
			$$('.person').invoke('hide');
			// show active description
			$(person+"Biog").show();
			// change active classname
			$('peopleScroller').select('li.active').invoke('removeClassName', 'active');
			$(person+"Thumb").addClassName('active');
		});
	}
}

function disablePeopleScroller(){
	$('scrollLeft').update('<img src="/img/buttons/arrow-left.png" />');
	$('scrollRight').update('<img src="/img/buttons/arrow-right.png" />');
}

function scrollPeopleLeft() {
	scroll('left');
}

function scrollPeopleRight() {
	scroll('right');
}

function scroll(direction){
	//disablePeopleScroller();
	currentLocation = $('peopleScroller').getStyle('left');
	//ie issue
	if(currentLocation == null){
		currentLocation = '0px';
	}
	currentLocation = currentLocation.replace("px","");
	//console.log(currentLocation);
	
	if((direction == 'left' && currentLocation > -1080) && (currentLocation==0 || currentLocation==-540 || currentLocation==-1080) ){
		newLocation = parseInt(currentLocation) - parseInt(increment);	
		newLocation = checkLocation(newLocation);
		//alert(newLocation);
		//console.log('newLocation='+newLocation);
		move(newLocation);
		
		var left = $('scrollLeft').down('a');
		var right = $('scrollRight').down('a');
		
		if (newLocation <= -1080) {
			
			left.removeClassName('scrollerright');
			left.addClassName('scrollerrightnull');
		} else {
			
			left.removeClassName('scrollerrightnull');
			left.addClassName('scrollerright');
		}
		
		if (newLocation <= -540) {
			right.removeClassName('scrollerleftnull');
			right.addClassName('scrollerleft');
		} 
		
		left = null;
		right = null;
		
	} else if((direction == 'right' && currentLocation <= -540) && (currentLocation==0 || currentLocation==-540 || currentLocation==-1080) ){
		newLocation = parseInt(currentLocation) + parseInt(increment);
		newLocation = checkLocation(newLocation);
		//console.log('newLocation='+newLocation);
		move(newLocation);
		//alert(newLocation);
	
		if (newLocation <= -1080) {
			$('scrollLeft').down('a').removeClassName('scrollerright');
			$('scrollLeft').down('a').addClassName('scrollerrightnull');
		} else {
			$('scrollLeft').down('a').removeClassName('scrollerrightnull');
			$('scrollLeft').down('a').addClassName('scrollerright');
		}
	
		if (newLocation >= 0) {
			$('scrollRight').down('a').removeClassName('scrollerleft');
			$('scrollRight').down('a').addClassName('scrollerleftnull');
		} 
		
	} else {
		//alert('no!');
		if((direction == 'left') && currentLocation <= -1080) {
			$('scrollLeft').down('a').removeClassName('scrollerright');
			$('scrollLeft').down('a').addClassName('scrollerrightnull');
		}
		if((direction == 'right') && currentLocation >= -540) {
			$('scrollRight').down('a').removeClassName('scrollerleft');
			$('scrollRight').down('a').addClassName('scrollerleftnull');
		}
	}
	initPeopleScroller(1);
}

function checkLocation(location){
	if(location > 0-increment){
		return 0;
	}
	return location;
}

function move(newLocation){
	moveMe = $('peopleScroller');
	new Effect.Move(moveMe, { x: newLocation, y: 0, mode: 'absolute' } ); 
}

function jumpstart() {
	if (window.location.href.split('#')[1]) {
	var person = window.location.href.split('#')[1];
	if (person=="robin-wight") { person = "robinWight"; }
	$('peopleReel').showFlashImage(person);
	// hide everyones descriptions
	$$('.person').invoke('hide');
	// show active description
	$(person+"Biog").show();
	// change active classname
	$('peopleScroller').select('li.active').invoke('removeClassName', 'active');
	$(person+"Thumb").addClassName('active');
	}
}		
