/* Author: Koty Yell @STAPLEGUN_

*/

$(document).ready(function() {
	$('.nivoSlider').nivoSlider({
		effect: 'fade',
		animSpeed: 250,
		pauseTime: 4000,
		controlNav: true,
		pauseOnHover: true,
		prevText: '',
		nextText: '',
		keyboardNav: true,
		directionNavHide:false
	});
	
	 $('#BPGallery').nivoSlider({
		effect: 'fade',
		controlNav: true,
		controlNavThumbs: true,
		controlNavThumbsSearch: /\.(jpe*g)$/i,
		controlNavThumbsReplace: '_thumb.$1',
		manualAdvance: true,
		prevText: '',
		nextText: '',
		keyboardNav: true,
		directionNavHide:false,
		animSpeed: 250
	});
	
	$('a.personName').hover(function() {
		$('[href=' + $(this).attr('href') + ']').toggleClass('active');
	});
	
	$('a.personBrick,a.personName').fancybox({
		showCloseButton:true,
		width:781
	});
	
	$('#peoplePage > #right').masonry({
		itemSelector: '.Brick',
		columnWidth: 92
	});
});

$(window).load(function() {
	if ( $('.nivo-controlNav').width() > $('.nivo-controlNavWrapper').width() ) {
		new Hovercraft( $('.nivo-controlNav') );
	}
});
    
function initPlaceholderSupport() {
    if( ! Modernizr.input.placeholder ) {
        $('[placeholder]').focus(function() {
          var input = $(this);
          if (input.val() == input.attr('placeholder')) {
            input.val('');
            input.removeClass('placeholder');
          }
        }).blur(function() {
          var input = $(this);
          if (input.val() == '' || input.val() == input.attr('placeholder')) {
            input.addClass('placeholder');
            input.val(input.attr('placeholder'));
          }
        }).blur();
    }
}

initPlaceholderSupport();
	
Hovercraft = function( target ) {
	this.target = target;
	this.maxSpeed=20;
	this.targetSpeed=10;
	this.acceleration=1;
	this.currentSpeed=0;
	this.intervalId=null;
	$( target ).parent().bind( {
		'mouseenter':$.proxy( this.mouseEnter, this ),
		'mousemove':$.proxy( this.mouseMove, this ),
		'mouseleave':$.proxy( this.mouseLeave, this )
	} );
}
	
Hovercraft.prototype.mouseMove = function( e ) {
	// compute new targetSpeed based on mouse position within hover area
	this.computeTargetSpeed( e );
};

Hovercraft.prototype.mouseEnter = function( e ) {
	// compute initial targetSpeed based on mouse position within hover area
	this.computeTargetSpeed( e );
	
	// set interval to do updates
	this.intervalId = setInterval( $.proxy( this.doUpdate, this ), 30 );
};

Hovercraft.prototype.mouseLeave = function( e ) {
	// kill interval for doing updates
	if( this.intervalId > 0 ) {
		clearInterval( this.intervalId );
		this.intervalId = -1;
	}
	// animate position of filmstrip to get an ease-out instead of just stopping abruptly?
	  this.currentSpeed = 0;
};

Hovercraft.prototype.doUpdate = function() {
	// if currentSpeed is less than targetSpeed, increase speed by acceleration
	if( this.currentSpeed < this.targetSpeed ) {
		this.currentSpeed = Math.min( this.currentSpeed + this.acceleration, this.targetSpeed );
	} else if( this.currentSpeed > this.targetSpeed ) {
		this.currentSpeed = this.targetSpeed;
	}
	
	var minPos = -this.target.width() + this.target.parent().width();
	var maxPos = 0;
	
	if( this.currentSpeed != 0 ) {
		var newPosition = this.limit( this.target.position().left + this.currentSpeed, minPos, maxPos );
		if( newPosition == minPos || newPosition == maxPos ) {
			this.currentSpeed = 0;
		}
		this.target.css( 'left', newPosition + 'px' ); 
	}
	
};

Hovercraft.prototype.limit = function( value, min, max ) {
	if( value > max ) {
		return max;
	}
	if( value < min ) {
		return min;
	}
	return value;
};

Hovercraft.prototype.computeTargetSpeed = function( e ) {			
	var xpos = e.pageX - this.target.parent().offset().left;
	var percentage = xpos/this.target.parent().width();
	this.targetSpeed = -((percentage - .5) * this.maxSpeed);
};
		

