var HOVER_SCALE = 1.5;
var PROXIMITY = 200.0;
 
$(function() {
    $(".icon_enlarge").each(function(i) {
        //private variables
        var dock = $(this);
        var dock_icons = dock.find("li a");
       
        //store the to and from sizes
        $.each(dock_icons, function() {
            var initHeight = parseInt($(this).height());
            var initWidth = parseInt($(this).width());
 
            $(this).data("initWidth", initWidth);
            $(this).data("initHeight", initHeight);
           
            $(this).data("newWidth", initWidth * HOVER_SCALE);
            $(this).data("newHeight", initHeight * HOVER_SCALE);
        });
       
        //event handlers
        dock.bind("mouseleave",function(event) {
            $.each(dock_icons, function() {
                $(this).animate({"height": $(this).data("initHeight") + "px", "width": $(this).data("initWidth") + "px"},"fast");
            });
        });
       
        dock.bind("mousemove", function(event) {
            $.each(dock_icons, function() {
                var newSize = calculateDockIconSize($(this), event.pageX);
               
                $(this).stop();
                $(this).width(newSize[0]);
                $(this).height(newSize[1]);
            });
        });
    });
});
 
/**
* Calculate the height of an icon based upon its position from the mouse
* @param icon
* @param mouse position (x only)
* @return new width/height
*/
function calculateDockIconSize(icon, mousePosX) {
    //get the distance in x from the mouse to the icon
    var initWidth = parseInt($(icon).data("initWidth"));
    var initHeight = parseInt($(icon).data("initHeight"));
    var newWidth = parseInt($(icon).data("newWidth"));
    var newHeight = parseInt($(icon).data("newHeight"));
   
    var xProximity = Math.abs(mousePosX - $(icon).offset().left - (newWidth/2.0));
   
    //if we need to vary the height because the mouse is within range
    if (xProximity<PROXIMITY) {
        //get the percentage height the icon needs to be
        var newRatio = ((PROXIMITY-xProximity)/PROXIMITY);
       
        var additionalWidth = newRatio * (newWidth-initWidth);
        var additionalHeight = newRatio * (newHeight-initHeight);
       
        //add on the additional percentage to the icon
        return [(initWidth + additionalWidth), (initHeight + additionalHeight)];
    } else {
        //otherwise, return the original icon size
        return [initWidth, initHeight];
    }
}
