/*--------------------------------------------------------------------------
 * Copyright (c) 2006, drk<drk7jp@gmail.com>(http://www.drk7.jp/)
 *
 * License :
 *   Articstic License 2.0
 *
 * xml2json.js :
 *   This library is a utility for XML2JSON service(http://www.drk7.jp/MT/archives/001011.html)
 *
 * Description : 
 *   XML2JSON provides easy way to use XML2JSON service.
 *   By using AjaxPages Template-Engine, you can customize your design.
 *   Original code is http://inforno.net/articles/2006/02/20/xml2json-with-the-javascript-template-engine
 *
 * See also :
 *   Original XML2JSON : http://inforno.net/articles/2006/02/20/xml2json-with-the-javascript-template-engine
 *   JS Template AjaxPages : http://ajax-pages.sourceforge.net/doc/js_docs_out/index.html
 *
 *--------------------------------------------------------------------------*/

var XML2JSON = Class.create();
Object.extend(XML2JSON, {
	id : 0,
	instance : {},
	proxy : 'http://app.drk7.jp/xml2json/'
});
Object.extend(XML2JSON.prototype, {
	initialize : function(url, options, params) {
		if(!url) return;
		var klass = XML2JSON;

		this.url = url;
		this.options = Object.extend({
			onload    : this.onload.bind(this),
			charset   : 'UTF-8',
			container : null,
			template  : null,
			id        : null
		}, options || {});
		if(options.id!=null) {
			this.id = options.id;
		} else {
			this.id = klass.id++;
		}
		this.params  = params ? $H(params) : null;
		klass.instance[this.id] = this;
		this.sendRequest();
	},

	sendRequest : function() {
		var script = document.createElement('script');
		script.charset = this.options.charset;
		var src = [XML2JSON.proxy, '&var=XML2JSON.instance[',this.id,'].options&url=', escape(this.url)];
		if(this.params) src.push('?'+this.params.toQueryString());
		script.src = src.join("");
		document.body.appendChild(script);
	},

	onload : function(data) {
		var options = this.options;
		if(!options.template || !options.container) return ;
		$(options.container).innerHTML = options.template(data);
	}
});




