Issue with variable inside a function

I have a for loop that is adding button functionality to 16 buttons on the stage. It is targeting the button fine with no problem using the var i as the button number. The issue is inside the button function I have another nested button function for the on state of the button which I also use the var i to specify the url number I want associated with the particular button but it is always outputting 17. I don’t even understand how it is getting that number. Here is the code.


function gotoURL(movieNum) {
	getURL("javascript:flashURL("+movieNum+");");
}
// ----
for (i=0; i<=16; i++) {
	this["b"+i].onRelease = function() {
		// --  bring selected button to the top deptht         
		_root.x += 2;
		this.swapDepths(_root.x);
		// -- defines last selected button and then delete it         
		var lastBtn:MovieClip;
		this._parent.lastBtn.gotoAndPlay(11);
		trace("Button Event One");
		this.gotoAndPlay(2);
		delete this._parent.lastBtn.onPress;
		this._parent.lastBtn = this;
		updateAfterEvent();
		// -- If the user clicks the on state then it goes to some URL         
		this.onPress = function() {


			_root.gotoURL(i);             
			trace (i);


			trace("Button Event two");
			updateAfterEvent();
		};
	};
}