/*
   Copyright (c) 2009 Paul Warren. All rights reserved.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.


   License: GPL
   
   funcStorage.js       v. 1.0

   The latest version is available at
   http://www.paulwarren.ca
   or http://www.paulwarren.net

   Created 2009.12.01
*/

// Base Class
var funcStorage = new function(){
   this.funcs = new Array();
   this.vars  = new Array();
   
   // Set a variable
   this.set = function( dest, val ) {
      this.vars[dest] = val;
   }
   // Del a variable
   this.del = function( dest ) {
      delete this.vars[dest];
   }
   
   // Create or overwrite the function.
   this.create = function( dest, func ) {
      this.funcs[dest] = new Array();
      this.funcs[dest].push(func);
         
      if ( (typeof funcStorage[dest] == 'undefined' ) || (typeof funcStorage[dest] == null) ) {
         funcStorage[dest] = function() {
            for (k in this.funcs[dest]) {
               this.funcs[dest][k]();
            }
         }
      }
   }
   
   // Extend function to allow function appending
   this.extend = function( dest, func ) {
      if ( (typeof this.funcs[dest] == 'undefined' )  || (typeof this.funcs[dest] == null) ) {
         this.funcs[dest] = new Array();
      }
      this.funcs[dest].push(func);
         
      if ( (typeof funcStorage[dest] == 'undefined' ) || (typeof funcStorage[dest] == null) ) {
         funcStorage[dest] = function() {
            for (k in this.funcs[dest]) {
               this.funcs[dest][k]();
            }
         }
      }
   }

   // Clear the function from the system. Different from destroy. Clear keeps the calling function, where destroy removes every trace of it.
   this.clear = function ( dest ) {
      this.funcs[dest] = null;
      delete this.funcs[dest];
   }

   // Remove the function from the system.
   this.destroy = function ( dest ) {
      this.funcs[dest] = null;
      funcStorage[dest] = null;
      delete this.funcs[dest];
      delete funcStorage[dest];
   }
   
   // Call the function. Initialize the function if it doesn't exist.
   this.call = function( dest ) { 
      if ( (typeof funcStorage[dest] == 'undefined' ) || (typeof funcStorage[dest] == null) ) {
         funcStorage[dest] = function() {
            for (k in this.funcs[dest]) {
               this.funcs[dest][k]();
            }
         }
      }
      return function() {
         funcStorage[dest](); 
      }
   };
}