/*
   JavaScript Utils
   ---------------------------------
   http://equivalence.co.uk

   Copyright (c) 2009, Gregg O'Malley. All rights reserved.
   Code licensed under the BSD License:
    
   V0.9
*/

function $A(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) return iterable.toArray();
  var length = iterable.length || 0, results = new Array(length);
  while (length--) results[length] = iterable[length];
  return results;
}

function bind() {
    if (arguments.length < 2 && arguments[0] == null) return this;
    var __method = arguments[1], args = $A(arguments), object = args.shift();
    return function() {
      return __method.apply(object, args.concat($A(arguments)));
    }
}

Hash = function() {
    var count_ = 0;
    var obj_ = {};

    return {
        add : function(key, value) {
            if(this.exists(key)) {
                this.remove(key)
            }
            
            obj_[key] = value;
            count_++;
        },
 
        exists : function(key) {
            return this.get(key) != undefined;
        },
       
        get : function(key) {
            var t = obj_[key];   
            return obj_[key];
        },
    
        count : function() {
            return count_;
        },
    
        remove : function(key) {
            // Check the key exists before we delete it
            //
            for(var i in obj_) {
                if(i == key) {
                    delete obj_[key];
                    count_--;
                    break;        
                }
            }
        },
        
        removeAll : function() {
            for(var i in obj_) {
                delete obj_[i];              
            }
            count_ = 0;
        }
    }
}

SnippetStore = function(storePath, callback) {
    var storePath_ = storePath;
    var data_ = new Hash();
    var callback_ = callback;
        
    return {
        load : function() {
            var ourSnippet = bind(this, this.snippetsLoaded);
            $.ajax({url: storePath_,
                    cache: false,
                    dataType: "json",
                    success: ourSnippet});
            //$.getJSON(storePath_, ourSnippet);
        },
     
        snippetsLoaded : function(func, data) {
            for(i in data)
            {
                data_.add(i, data[i]);
            }

            callback_();
        },

        get : function(id, args) {
            var item = data_.get(id);
            
            if(args == null) {
                return item;
            }
            
            // We need to perform some substitutions
            //
            for (val in args) {
                regExp = new RegExp("\\[\\[" + val + "\\]\\]", "ig");
                item = item.replace(regExp, args[val]);
            }
            
            return item;
        }
    }
}

function DependencyLoader(callback) {
    var callback_ = callback;
    var dependencies_ = new Array();
    var count_ = 0;
    
    this.dependencyLoaded = function dependencyLoaded(data) {
        if(count_ > 1) {
            count_--;
            return;
        }
        
        callback_();
    };
    
    DependencyLoader.prototype.add = function(file) {
        dependencies_.push(file);
        count_++;
    };
    
    DependencyLoader.prototype.load = function(callback) {
        callback_ = callback;
        for(var i in dependencies_) {
            $.getScript(dependencies_[i], this.dependencyLoaded)
        }
    };   
}