/* Author: Jared Cornwall

*/
jQuery(document).ready(function($) {
	
// Masthead animation scripts		
	//stop anchor link
	$('a[href=#top]').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow'); // scroll to top
		return false;
	});
	
	var offset = $("#masthead").offset();
	var overMasthead = false;
	
	// rollover link in logo tab to move masthead in
	$('a[href=#top]').mouseenter(function() {
		overMasthead = true;
		$("#masthead").stop().animate({ marginTop: $(window).scrollTop() - offset.top });
    });
	// we are over masthead, don't move it
	$('#masthead').mouseenter(function() {
		overMasthead = true;
		//move it just incase it's barely showing
		$(this).stop().animate({ marginTop: $(window).scrollTop() - offset.top });
	});
	// Move masthead to top of page 
	$('#masthead').mouseleave(function() {
		overMasthead = false;
		$("#masthead").stop().removeClass('fixed').animate({ marginTop: 0 });
    });
	// Move masthead with page scroll we are over it
    $(window).scroll(function() {
        if (overMasthead == true) {
			$("#masthead").stop().animate({ marginTop: $(window).scrollTop() - offset.top }, 0).addClass('fixed');
			//addClass fixed makes things behave better in Firefox
        };
    });


// Animate portfolio excerpt rollovers
// code form stackoverlow via Caspar Kleijne
// http://stackoverflow.com/questions/3627042/jquery-animation-for-a-hover-with-mouse-direction	
	$(".projects-list-item").bind("mouseenter mouseleave",function(e) {

		/** the width and height of the current div **/
		var w = $(this).width();
		var h = $(this).height();

		/** calculate the x and y to get an angle to the center of the div from that x and y. **/
		/** gets the x value relative to the center of the DIV and "normalize" it **/
		var x = (e.pageX - this.offsetLeft - (w/2)) * ( w > h ? (h/w) : 1 );
		var y = (e.pageY - this.offsetTop  - (h/2)) * ( h > w ? (w/h) : 1 );

		/** the angle and the direction from where the mouse came in/went out clockwise (TRBL=0123);**/
		/** first calculate the angle of the point, 
		 add 180 deg to get rid of the negative values
		 divide by 90 to get the quadrant
		 add 3 and do a modulo by 4  to shift the quadrants to a proper clockwise TRBL (top/right/bottom/left) **/
		var direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180 ) / 90 ) + 3 )  % 4;
		
		/** target item to animate **/
		var animateTarget = $(this).find('div');
		
		/** do your animations here **/ 
		switch(direction) {
			case 0:
			 	/** TOP **/
				if (e.type == "mouseenter") {
					animateTarget.css({'top':'-190px','left':'0'}).animate({top:'0'}, 250); // set start position
				} else if (e.type == "mouseleave") {
					animateTarget.animate({top:'-190px'}, 250); // set start position
				};
			break;
			case 1:
			 	/** RIGHT **/
				if (e.type == "mouseenter") {
					animateTarget.css({'top':'0','left':'320px'}).animate({left:'0'}, 250); // set start position
				} else if (e.type == "mouseleave") {
					animateTarget.animate({left:'320px'}, 250); // set start position
				};
			break;
			case 2:
			 	/** BOTTOM **/
				if (e.type == "mouseenter") {
					animateTarget.css({'top':'190px','left':'0'}).animate({top:'0'}, 250); // set start position
				} else if (e.type == "mouseleave") {
					animateTarget.animate({top:'190px'}, 250); // set start position
				};
			break;
			case 3:
			 	/** LEFT **/
				if (e.type == "mouseenter") {
					animateTarget.css({'top':'0','left':'-320px'}).animate({left:'0'}, 250); // set start position
				} else if (e.type == "mouseleave") {
					animateTarget.animate({left:'-320px'}, 250); // set start position
				};
			break;
		}
	});

	
// Follow Project Pagination with the Keyboard
	$(window).keyup(function(e) {
		if (e.keyCode === 39) { // Right arrow
			gotoLink('li.prev a');
		}
		else if (e.keyCode === 37) { // Left arrow
			gotoLink('li.next a');
		}
		else if (e.keyCode === 36) { // Home key
			gotoLink('li.list a');
		};
	});
	// goto link location if defined
	function gotoLink (target) {
		var locationLink = $(target).attr('href');
		if (locationLink.length != 0) {
			window.location=locationLink;
		};
	}

});


























