Accordion = function(id)
{
	var targets = [];
	var isExclusive = false;
	
	this.initialize = function(_exclusive)
	{
		isExclusive = (_exclusive) ?_exclusive :false;
		var wrapper = document.getElementById(id);
		if(wrapper==null) return;
		//console.log(wrapper);
		var elements = wrapper.getElementsByTagName("div");
		//console.log(elements);
		for(var i=0; i<elements.length; i++)
		{
			var elm = elements[i];
			if(elm.nodeType==1)
			{
				//console.log(elm);
				var header = elm.getElementsByTagName("h4")[0];
				//console.log(header);
				var p = elm.getElementsByTagName("p")[0];
				//console.log(p);
				var ae = new Accordion.Element(header, p);
				targets.push(ae);
			}
		}
		this.update(-1);
		if(isExclusive)
		{
			for(var i=0; i<targets.length; i++)
			{
				var target = targets[i];
				target.link(i, this);
			}
			this.update(0);
		}
	}
	this.update = function(n)
	{
		for(var i=0; i<targets.length; i++)
		{
			var target = targets[i];
			var visible = target.visible();
			if(i!==n)
			{
				//ターゲットではない
				//->閉じる
				target.hideTarget();
			} else if(i==n&&!visible){
				//ターゲットであり、かつ閉じている
				//->開く
				target.showTarget();
			} else {
				//ターゲットであり、かつ開いている
				//->閉じる
				target.hideTarget();
			}
		}
	}
	this.exclusive = function(b)
	{
		if(!b) return isExclusive;
		if(typeof(b)==boolean) isExclusive = b;
	}
}
Accordion.Element = function(header, target)
{
	var Style = net.curoko.Style;
	
	var Event = net.curoko.Event;
	var _self = this;
	var id;
	var mediator = null;
	var isVisible = true;
	var timer;
	var baseHeight = target.offsetHeight;
	var damping = 0.5;
	var attraction = 0.2;
	
	this.link = function(n, m)
	{
		id = n;
		mediator = m;
	}
	this.toggle = function()
	{
		//console.log("toggled.");
		if(mediator!=null)
		{
			mediator.update(id);
			
		} else {
			if(isVisible)
			{
				this.hideTarget();
			} else {
				this.showTarget();
			}
		}
	}
	this.visible = function(b)
	{
		if(!b) return isVisible;
		if(typeof(b)==boolean) isVisible = b;
	}
	this.hideTarget = function()
	{
		//console.log("hide");
		header.style.cursor = "pointer";
		slide(0);
		header.className = "close";
		isVisible = false;
	}
	this.showTarget = function()
	{
		//console.log("show");
		header.style.cursor = "pointer";
		slide(baseHeight);
		header.className = "open";
		isVisible = true;
	}
	var slide = function(destination)
	{
		//console.log(getHeight());
		var integrator = new Integrator(target.offsetHeight, damping, attraction);
		//console.log(destination);
		integrator.target(destination);
		if(timer) clearInterval(timer);
		timer = setInterval(function()
		{
			integrator.update();
			target.style.height = integrator.value+"px";
			//console.log(integrator.value);
			if(Math.abs(integrator.value-destination)<1)
			{
				target.style.height = destination;
				clearInterval(timer);	
			}
		}, 33);
	}
	target.style.overflow = "hidden";
	header.className = "open";
	Event.addListener(header, "click", "toggle", _self);
}

