var sburules = {
	"div.SBUPanel" : function(el) {
		if (typeof Site != "undefined")
			return;
		var link = el.getElementsByTagName("a")[0].getAttribute("href");
		el.style.cursor = "pointer";
		Event.observe(el, "click", function(ev) {
			document.location = link;
			Event.stop(ev);
		}.bindAsEventListener());
		Event.observe(el, "mouseover", function(ev) {
			Element.addClassName(el, "hover");
			Event.stop(ev);
		}.bindAsEventListener());
		Event.observe(el, "mouseout", function(ev) {
			Element.removeClassName(el, "hover");
			Event.stop(ev);
		}.bindAsEventListener());
	}
}

Behaviour.register(sburules);

Effect.Accordion = Class.create();
Effect.Accordion.prototype = {
	initialize: function(element, options) {
		if (Element.hasClassName(element, "is_accordion"))
			return false;
		else
			Element.addClassName(element, "is_accordion");
		this.container = $(element);
		this.container.style.height = "auto";
		this.options = Object.extend({
			paneltag: "div", // The tag of the panel containers
			headingtag: "h3", // The tag of the headings
			bodytag: "div", // The body
			initdisplay: 0 // Index of the panel to show first
		}, options || {});
		this.options.paneltag = this.options.paneltag.toUpperCase();
		this.options.headingtag = this.options.headingtag.toUpperCase();
		this.options.bodytag = this.options.bodytag.toUpperCase();
		this.panels = Array();
		this.headings = Array();
		this.bodies = Array();
		this.maxheight = 0;
		panels = this.container.childNodes;
		pl = panels.length;
		n = 0;
		for (i = 0; i < pl; i++) {
			if (panels[i].nodeName != this.options.paneltag)
				continue;
			this.panels[n] = panels[i];
			this.headings[n] = this.getPanelHeading(this.panels[n]);
			this.bodies[n] = this.getPanelBody(this.panels[n]);
			height = Element.getHeight(this.bodies[n]);
			if (height > this.maxheight)
				this.maxheight = height;
			n++;
		}
		// Set some heights
		bl = this.bodies.length;
		this.maxheight += 20;
		for (i = 0; i < bl; i++) {
			this.bodies[i].style.height = this.maxheight+"px";
		}
		// We should probably sort out some event listeners
		this.clickHandler = this.onClickHeading.bindAsEventListener(this);
		pl = this.panels.length;
		for (i = 0; i < pl; i++) {
			heading = this.headings[i];
			if (this.options.initdisplay != i) {
				Element.hide(this.bodies[i]);
			} else {
				Element.addClassName(heading, "active");
				this.curvisible = i;
			}
			Event.observe(heading, "click", this.clickHandler);
			Event.observe(heading, "mouseover", this.headingMouseOver.bindAsEventListener(this));
			Event.observe(heading, "mouseout", this.headingMouseOut.bindAsEventListener(this));
			heading.style.cursor = "pointer";
		}
		this.container.style.height = Element.getHeight(this.container)+"px";
	},
	headingMouseOver: function(ev) {
		el = Event.element(ev);
		clickedid = this.getHeadingNumber(el);
		heading = this.headings[clickedid];
		Element.addClassName(heading, "hover");
	},
	headingMouseOut: function(ev) {
		el = Event.element(ev);
		clickedid = this.getHeadingNumber(el);
		heading = this.headings[clickedid];
		Element.removeClassName(heading, "hover");
	},
	onClickHeading: function(ev) {
		el = Event.element(ev);
		clickedid = this.getHeadingNumber(el);
		panel = this.panels[clickedid];
		if (panel == this.panels[this.curvisible]) {
			Effect.toggle(this.bodies[this.curvisible], "blind", { duration: 0.5 });
			return;
		}
		newborn = this.bodies[clickedid];
		victim = this.bodies[this.curvisible];
		Element.addClassName(this.headings[clickedid], "active");
		Element.removeClassName(this.headings[this.curvisible], "active");
		this.curvisible = clickedid;
		new Effect.Parallel (
		[
			Effect.BlindUp(victim, { sync: true }),
			Effect.BlindDown(newborn, { sync: true })
		], {
			duration: 0.5
		});
	},
	getPanelHeading: function(el) {
		return el.getElementsByTagName(this.options.headingtag)[0];
	},
	getPanelBody: function(el) {
//		n = 0;
//		if (this.options.bodytag == this.options.headingtag)
//			n = 1;
		return el.getElementsByTagName(this.options.bodytag)[0];
	},
	/*
	getPanelBody: function(el) { // FIXME: Longer version to compensate for the body tag appearing in the heading tag. This sliently halts execution however. Fix if you need it.
		n = 0;
		if (this.options.bodytag == this.options.headingtag)
			n = 1;
		children = el.childNodes;
		cl = children.length;
		for (i = 0; i < cl; i++) {
			if (children[i].nodeName == this.options.bodytag) {
				if (n == 0)
					return children[i];
				else
					n = n-1;
			}
		}
	}
	*/
	getPanelNumber: function(el) {
		pl = this.panels.length;
		for (i = 0; i < pl; i++) {
			if (this.panels[i] == el)
				return i;
		}
		return "Not found";
	},
	getHeadingNumber: function(el) {
		while (el.nodeName != this.options.headingtag && el) {
			el = el.parentNode;
		}
		pl = this.headings.length;
		for (i = 0; i < pl; i++) {
			if (this.headings[i] == el)
				return i;
		}
		return "Not found";
	}
};
Effect.Accordion.Reset = function(el) {
	if (Element.hasClassName(el, "is_accordion"))
		Element.removeClassName(el, "is_accordion");
}

Effect.Notebook= Class.create();
Effect.Notebook.prototype = {
	initialize: function(element, options) {
		if (Element.hasClassName(element, "is_notebook"))
			return false;
		else
			Element.addClassName(element, "is_notebook");
		this.container = $(element);
		this.container.style.height = "auto";
		this.options = Object.extend({
			paneltag: "div", // The tag of the panel containers
			initdisplay: 0, // Index of the panel to show first
			tabBarClass: "tab_bar"
		}, options || {});
		this.options.paneltag = this.options.paneltag.toUpperCase();
		this.panels = Array();
		this.headings = Array();
		this.maxheight = 0;
		panels = this.container.childNodes;
		pl = panels.length;
		n = 0;
		for (i = 0; i < pl; i++) {
			if (panels[i].nodeName != this.options.paneltag)
				continue;
			this.panels[n] = panels[i];
			height = Element.getHeight(this.panels[n]);
			if (height > this.maxheight)
				this.maxheight = height;
			n++;
		}
		tabbar = this.container.getElementsByTagName("ul")[0]
		Element.addClassName(tabbar, "tab_bar");
		tabs = tabbar.getElementsByTagName("li");
		tabsl = tabs.length;
		for (i = 0; i < tabsl; i++) {
			this.headings[i] = tabs[i];
		}
		// Set some heights
		bl = this.panels.length;
		this.maxheight += 20;
//		for (i = 0; i < bl; i++) {
//			this.panels[i].style.height = this.maxheight+"px";
//		}
		// We should probably sort out some event listeners
		this.clickHandler = this.onClickHeading.bindAsEventListener(this);
		pl = this.panels.length;
		for (i = 0; i < pl; i++) {
			heading = this.headings[i];
			if (this.options.initdisplay != i) {
				Element.hide(this.panels[i]);
			} else {
				this.curvisible = i;
				Element.addClassName(heading, "active");
			}
			Event.observe(heading, "click", this.clickHandler);
			heading.style.cursor = "pointer";
		}
//		this.container.style.height = Element.getHeight(this.container)+"px";
	},
	onClickHeading: function(ev) {
		el = Event.element(ev);
		clickedid = this.getHeadingNumber(el);
		panel = this.panels[clickedid];
		newborn = this.panels[clickedid];
		victim = this.panels[this.curvisible];
		this.curvisible = clickedid;
		Element.hide(victim);
		Element.show(newborn);
		hl = this.headings.length;
		for (i = 0; i < hl; i++) {
			if (i == clickedid) Element.addClassName(this.headings[i], "active");
			else Element.removeClassName(this.headings[i], "active");
		}
	},
	getPanelNumber: function(el) {
		pl = this.panels.length;
		for (i = 0; i < pl; i++) {
			if (this.panels[i] == el)
				return i;
		}
		return "Not found";
	},
	getHeadingNumber: function(el) {
		while (el.nodeName != "LI" && el) {
			el = el.parentNode;
		}
		pl = this.headings.length;
		for (i = 0; i < pl; i++) {
			if (this.headings[i] == el)
				return i;
		}
		return "Not found";
	}
};
Effect.Notebook.Reset = function(el) {
	if (Element.hasClassName(el, "is_notebook"))
		Element.removeClassName(el, "is_notebook");
}
