
Function.prototype.bind = function(object) {
  var __method = this;
  return function() {
    return __method.apply(object, arguments);
  }
}

Function.prototype.asynchronize = function() {
  var f = this;
  return function() {
    var resultcb = function() {};
    var failcb = function(e) { throw e };
    if (typeof arguments[arguments.length - 1] == 'function') {
      resultcb = Array.prototype.pop.apply(arguments);
      if (typeof arguments[arguments.length - 1] == 'function') {
        failcb = Array.prototype.pop.apply(arguments);
      }
    }
    try {
      var result = f.apply(this, arguments);
      setTimeout(function() { resultcb(result) }, 0);
    }
    catch (e) {
      setTimeout(function() { failcb(e) }, 0);
    }
  };
};

var jAccord = function(togglers, elements, options, callback) {
	var fn ;
	var ja = this ;
	this.callback = '' ;
	this.callback = callback ;
	jAccord.prototype.el = elements ;
	jAccord.prototype.tog = togglers ;
	jAccord.prototype.opt = options ;
	fn = this.initialize.bind(this) ;
	fn.asynchronize()(function(ret) {
		if (ret == 'done' && ja.callback != null) {
			ja.callback() ;
		}
	}) ;
}

jAccord.prototype = {
	
	initialize: function() {
		var ja = this ;
		
		this.now = [] ;
		this.h = [] ;
		
		this.tog.each(function(i) {
			$(this).click(function() {
				ja.showThisHideOpen(i) ;
			}) ;
			ja.h[i] = $(ja.el[i]).height() ;
			ja.hideThis(i) ;
		}) ;
		return 'done' ;
	},
	
	hideThis: function(i) {
		this.now[i] = 0 ;
		$(this.el[i]).slideUp(this.opt,
			function(i) {
//				$(this).height(0) ;
			}) ;
	},
	
	showThis: function(i) {
		var test ;
		this.now[i] = 1 ;
		$(this.el[i]).slideDown(this.opt,
			function() {
				test = $(this).height() ;
//				alert($(this).height()) ;
			}) ;
		
	},
	
	showThisHideOpen: function(iToShow) {
		var ja = this ;
		$(this.el).each(function(i) {
			if (ja.now[i] == 1) {
				ja.hideThis(i) ;
			}
		}) ;
		this.showThis(iToShow) ;
	}
} ;
