(function($){
    $.fn.resizeOnApproach = function(settings){
        var config = {
            'elementDefault': 12,
            'elementClosest': 30,
            'triggerDistance': 25,
			'setWidthAndHeight':false
        };
        
        if (settings) 
            $.extend(config, settings);
        
        var setWidthAndHeight=config.setWidthAndHeight;
        var expandIcon = this.not(".active");
        var imgSize = config.elementDefault;
        var imgMax = config.elementClosest;
        var trigger = config.triggerDistance
        
        var max = imgMax - imgSize;
        
        var factor = max / trigger;
        var resized = false;
        $(document).ready(function(){
            expandIcon.each(function(e){
                //this.style.offsetWidth = imgSize + 'px';

				//console.log(this.offsetHeight)
				this.style.fontSize = imgSize + 'px';
				//console.log(this.offsetHeight)
				if (setWidthAndHeight) {
				this.style.height = imgSize + 'px';
				}
				repositionItems(e, expandIcon, trigger, imgSize, max, factor);
            });

			

        });

        $(document).mousemove(function(e){
            repositionItems(e, expandIcon, trigger, imgSize, max, factor);
        });
	    
    }

	function repositionItems(e, expandIcon, trigger, imgSize, max, factor){
		var mouseX = e.pageX;
	    var mouseY = e.pageY;
	
	    expandIcon.each(function(){
	        //how far away the top left corner of the element is from the corner of the window
	        var pos = $(this).offset();
	        //calculate the distance from the mouse poiter to the centre of the square. Sum takes into account that the image position is taken from corner
	        //var dist = distToSqEdge(affaire.offsetWidth, pos.left + (affaire.offsetWidth / 2), pos.top + (affaire.offsetHeight / 2), mouseX, mouseY);
			var dist = distToSqEdge(this.offsetHeight, this.offsetWidth, pos.top, pos.left , mouseY, mouseX);
	        //set the distance to zero if inside the square

	        if (dist < trigger) {
				//if(this.)
	            if (dist < 0) {
	                dist = 0;
	            }
	            resized = true;
	            var size = imgSize +
	            (max - (dist * factor));
	            this.style.fontSize = size + 'px';
				/*if (setWidthAndHeight) {
									this.style.fontSize = size + 'px';
								}*/

				//distributeInSpace(this, 5, 100);var i = 0;
				var i=0;
				var numberOfElements=0;
				$(".menu a").each(function(index) {
				    i += $(this).height();
					numberOfElements++;
				});
				spaceLeft = (90-i);
				topPadding = Math.floor(spaceLeft / (numberOfElements-1));
				//$(".menu a").css("padding-top" , topPadding + 'px');
				$(".menu a:not('.last')").each(function(index) {
				    $(this).css("margin-bottom" , topPadding + 'px');
				});


	        }
	        else {
	            this.style.width = imgSize + 'px';
				this.style.fontSize = imgSize + 'px';/*
								if (setWidthAndHeight) {
								       this.style.fontSize = imgSize + 'px';
								}*/
				//distributeInSpace(this, 5, 100);var i = 0;
				var i=0;
				var numberOfElements=0;
				$(".menu a").each(function(index) {
				    i += $(this).height();
					numberOfElements++;
				});
				spaceLeft = (90-i);
				topPadding = Math.floor(spaceLeft / (numberOfElements-1));
				//$(".menu a").css("padding-top" , topPadding + 'px');
				$(".menu a:not('.last')").each(function(index) {
				    $(this).css("margin-bottom" , topPadding + 'px');
				});
	        }

	    });

	}
})(jQuery)




//returns the distance from the edge of the square of the given width and centre C to the
//point P. If the distance is negative, the mouse in within the square
function distToSqEdge(sqWidth, sqHeight, cx, cy, px, py){
    //length of line from point to centre
    var pl = Math.sqrt((cx - px) * (cx - px) +
    (cy - py) *
    (cy - py));
    
    //the x and y length of the line
    vx = px - cx;
    vy = py-30 - cy;
    
    //determine the unit vector to the side the line intersects the square
    var Xx = 0;
    var Xy = 0;
    if (vx > vy) {
        if (vx > -vy) {
            Xx = 1;
        }
        else {
            Xy = 1;
        }
    }
    else {
        if (vx > -vy) {
            Xy = -1;
        }
        else {
            Xx = -1;
        }
    }
    
    // determine the unit vector of line to mouse point
    vlength = Math.sqrt((vx * vx) + (vy * vy));
	
    vux = vx / vlength;
    vuy = vy / vlength;
    
    cosA = vux * Xx + vuy * Xy;
    
    //distance from centre to the edge of the square
    centreToSqEdge = Math.abs((0.5 * sqWidth) / cosA);
    //centreToSqEdge = Math.abs((0.5 * sqHeight) / cosA);
    
    mouseToSquareEdge = vlength - centreToSqEdge;
	//console.log(vlength);
    return mouseToSquareEdge;
    
}

