// /////////////////////////////////////////////////////////////////////////
//
// Name:  		throb.js
// Function:	Creates a subtle grow/shrink effect on elements
// Author: 		Alec Eriksson
// 
// Copyright: 	Copyright 2009 USRA Lunar & Planetary Institute
// 				All rights reserved
//
// Notes: 		Script only works if you're throbbing an image inside of 
// 				a link, inside of a div.  If the image isn't wrapped in an 
// 				<a> and a <div> it won't work. 
// 		
//				You need to pass the script the ID of the image
//
// 				The variable "mod" the pixel size of the horizontal growth,
//				while the script calculates the vertical growth based on the 
// 				aspect ratio of the image.  A mod of 10 is 5 pixels in all 
// 				directions, for example.
//
// /////////////////////////////////////////////////////////////////////////

function getDim(o) {
	// After getting the dimensions and calculating the aspect ratio
	// return these variables to the calling function as an array

	var x = y = a = 0;
	x = o.width
	y = o.height
	a = y / x

	return [x,y,a];
}

function throb(oo,amt) {
	var p = document.getElementById(oo).parentNode.parentNode
	var o = document.getElementById(oo)
	var mod = 8
	var dim = getDim(o)
	
	// Use the default modifier unless you supply one
	if (amt != undefined) { mod = amt }

	// Grow the element by the modifier
	// also adjusts the position so element remains centered
	o.style.width = dim[0] + mod +"px"
	o.style.height = dim[1] + (mod * dim[2]) +"px"
	
	o.style.marginLeft = (mod / 2) * -1 +"px"
	o.style.marginTop = ((mod * dim[2]) / 2) * -1 +"px"
	
	// Dump the original dimensions so it can be reset later
	// Array values are: Width, Height
	orig = new Array();
	orig = [dim[0],dim[1]]

	return orig
}

function unthrob(oo) {
	o = document.getElementById(oo)
	p = document.getElementById(oo).parentNode.parentNode
	o.style.width = orig[0] +"px"
	o.style.height = orig[1] +"px"
	o.style.marginLeft = 0 +"px"
	o.style.marginTop = 0 +"px"
}

$(function(){
	$('.throbbable').mouseover(function(){
 		throb($(this).attr("id"));
 	});
 	$('.throbbable').mouseout(function(){
 		unthrob($(this).attr("id"));
 	});
});
