plugin_service.js Var Declarations

Same variables were declared several times throughout a single function
block inside FOR loops. This would create duplicate declarations at
run-time due to hoisting. Moved declarations to the top of the function.