/*******************************
JSTPL - Javascript Templating
by P. Mark Anderson
http://bordertownlabs.com 

USAGE:
Event.observe(window, "load", function() {
  Jstpl.load({
    header: {title:'John Cleese'},
    menu,
    footer
  })
})


*******************************/
var Jstpl = {
  // entry point
  load: function(sections, callbacks) {
    if (this.blank(sections)) {
      return
    }
    
    if (callbacks == null) {
      callbacks = {}
    }
    
    $A(sections).each(function(section) {
      section_name = ''
      if (typeof(section) == 'string') {
        section_name = section
        tpl_vars = null
        
      } else if (typeof(section) == 'object') {
        $H(section).each(function(section_vars) {
          section_name = section_vars[0]
          tpl_vars = section_vars[1]
        })
      } 

      Jstpl.ajax_update(section_name, section_name + ".tpl.html", tpl_vars, 
          "get", callbacks[section_name])
    })
  },

  ajax_get: function(url, complete, loading) {
    this.ajax_request('get', url, complete, loading)
  },

  ajax_post: function(url, complete, loading, params) {
    this.ajax_request('post', url, complete, loading, params)
  },

  ajax_request: function(meth, url, complete, loading, params) {
    if (complete == null) {complete = function() {} }
    if (loading == null) {loading = function() {} }
    if (params == null) {params = {} }

    new Ajax.Request(url, 
      {
        asynchronous:true, evalScripts:true, method:meth,
        onLoading:loading, onComplete:complete,
        parameters:params
      })
  },

  ajax_update: function(element, url, tpl_vars, meth, complete, loading, params) {
    if (!$(element)) {
      return
    }

    if (complete == null) {complete = function() {} }
    if (loading == null) {loading = function() {} }
    if (params == null) {params = {} }
    
    if (this.blank(meth)) {
      meth = 'get'
    }

    new Ajax.Swapper(element, url, 
      {
        asynchronous:true, evalScripts:true, method:meth, 
        onLoading:loading, onComplete:complete,
        parameters:params, template_variables:tpl_vars
      })
  },

  blank: function(v) {
		return (v == null || v == undefined || v == '' || v == 'null')
	}
	
}



/* Make sure Prototype is loaded first. */

//
// Ajax.Swapper extends Ajax.Updater
// Adds template variable swapping through options.template_variables.
//
Ajax.Swapper = Class.create();
Object.extend(Object.extend(Ajax.Swapper.prototype, Ajax.Updater.prototype), {

  updateContent: function() {
    var receiver = this.container[this.success() ? 'success' : 'failure'];
    var response = this.transport.responseText;

    // swap in template variables
    try {
      response = response.mixin(this.options.template_variables)  
    } catch (e) {
      alert(e)
    }
    
    if (!this.options.evalScripts) response = response.stripScripts();

    if (receiver = $(receiver)) {
      if (this.options.insertion)
        new this.options.insertion(receiver, response);
      else
        receiver.update(response);
    }

    if (this.success()) {
      if (this.onComplete)
        setTimeout(this.onComplete.bind(this), 10);
    }
  }

});



// "#{company} forever!".mixin({company: 'Unspace'}) // Unspace forever!
Object.extend(String.prototype, {
  mixin: function(obj) {
    if (obj == null) { return this; }
    return new Template(this).evaluate(obj);
  }
});



var JsUtil = {
  // true if str starts with start.
	starts_with: function(str, start) {
		return str.toString().match(new RegExp('^' + start)) != null;
	},

	// true if str ends with end.
	ends_with: function(str, end) {
		return str.toString().match(new RegExp(end + '$')) != null;
	}
	
}


