var enabletabpersistence = 1;
var tabcontentIDs = new Object();

function expandcontent(linkobj) {
	var ulid = linkobj.parentNode.parentNode.id;
	var ullist = document.getElementById(ulid).getElementsByTagName("li");
	for (var i = 0; i < ullist.length; i++) ullist[i].className = "";
	linkobj.parentNode.className = "selected";
	myAccordion.showThisHideOpen(document.getElementById(linkobj.getAttribute("rel")));
	saveselectedtabcontentid(ulid, linkobj.getAttribute("rel"));
}

function savetabcontentids(ulid, relattribute) {
	if (typeof tabcontentIDs[ulid] == "undefined") tabcontentIDs[ulid] = new Array();
	tabcontentIDs[ulid][tabcontentIDs[ulid].length] = relattribute;
}

function saveselectedtabcontentid(ulid, selectedtabid) {
	if (enabletabpersistence == 1) setCookie(ulid, selectedtabid);
}

function getullistlinkbyId(ulid, tabcontentid) {
	var ullist = document.getElementById(ulid).getElementsByTagName("li");
	for (var i = 0; i < ullist.length; i++) {
		if (ullist[i].getElementsByTagName("a")[0].getAttribute("rel") == tabcontentid) {
			return ullist[i].getElementsByTagName("a")[0];
			break;
		}
	}
}

function initializetabcontent() {
	for (var i = 0; i < arguments.length; i++) {
		if (enabletabpersistence == 0 && getCookie(arguments[i]) != "") setCookie(arguments[i], "");
		var clickedontab = getCookie(arguments[i]);
		var ulobj = document.getElementById(arguments[i]);
		var ulist = ulobj.getElementsByTagName("li");
		for (var x = 0; x < ulist.length; x++) {
			var ulistlink = ulist[x].getElementsByTagName("a")[0];
			if (ulistlink.getAttribute("rel")) {
				savetabcontentids(arguments[i], ulistlink.getAttribute("rel"));
				ulistlink.onclick = function() {
					expandcontent(this);
					return false;
				}
				if (ulist[x].className == "selected" && clickedontab == "") expandcontent(ulistlink);
			}
		}
		if (clickedontab != "") {
			var culistlink = getullistlinkbyId(arguments[i], clickedontab);
			if (typeof culistlink != "undefined") {
				expandcontent(culistlink);
			}
			else {
				expandcontent(ulist[0].getElementsByTagName("a")[0]);
			}
		}
	}
}

function getCookie(Name) { 
	var re = new RegExp(Name+"=[^;]+", "i");
	if (document.cookie.match(re)) return document.cookie.match(re)[0].split("=")[1];
	return "";
}

function setCookie(name, value) {
	document.cookie = name + "=" + value;
}

var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
}

Object.extend = function(destination, source) {
	for (property in source) destination[property] = source[property];
	return destination;
}

Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		return __method.apply(object, arguments);
	}
}

function $() {
	if (arguments.length == 1) return get$(arguments[0]);
	var elements = [];
	$c(arguments).each(function(el){
		elements.push(get$(el));
	});
	return elements;

	function get$(el){
		if (typeof el == 'string') el = document.getElementById(el);
		return el;
	}
}

if (!window.Element) var Element = new Object();

Object.extend(Element, {
	hasClassName: function(element, className) {
		element = $(element);
		if (!element) return;
		var hasClass = false;
		element.className.split(' ').each(function(cn) {
			if (cn == className) hasClass = true;
		});
		return hasClass;
	},
});

document.getElementsByClassName = function(className) {
	var children = document.getElementsByTagName('*') || document.all;
	var elements = [];
	$c(children).each(function(child) {
		if (Element.hasClassName(child, className)) elements.push(child);
	});  
	return elements;
}

function $c(array) {
	var nArray = [];
	for (i = 0; el = array[i]; i++) nArray.push(el);
	return nArray;
}

var fx = new Object();
fx.Base = function(){};
fx.Base.prototype = {
	setOptions: function(options) {
	this.options = {
		duration: 500	// User settable
	}
	Object.extend(this.options, options || {});
	},

	go: function() {
		this.startTime = (new Date).getTime();
		this.timer = setInterval (this.step.bind(this), 13);
	},

	step: function() {
		var time  = (new Date).getTime();
		if (time >= this.options.duration + this.startTime) {
			this.now = this.to;
			clearInterval (this.timer);
			this.timer = null;
		}
		else {
			var Tpos = (time - this.startTime) / (this.options.duration);
			this.now = Tpos * (this.to - this.from) + this.from;
		}
		this.increase();
	},

	custom: function(from, to) {
		if (this.timer != null) return;
		this.from = from;
		this.to = to;
		this.go();
	},

	hide: function() {
		this.now = 0;
		this.increase();
	},

	clearTimer: function() {
		clearInterval(this.timer);
		this.timer = null;
	}
}

fx.Layout = Class.create();
fx.Layout.prototype = Object.extend(new fx.Base(), {
	initialize: function(el, options) {
		this.el = $(el);
		this.el.style.overflow = "hidden";
		this.el.iniWidth = this.el.offsetWidth;
		this.el.iniHeight = this.el.offsetHeight;
		this.setOptions(options);
	}
});

fx.Height = Class.create();
Object.extend(Object.extend(fx.Height.prototype, fx.Layout.prototype), {	
	increase: function() {
		this.el.style.height = this.now + "px";
	},

	toggle: function() {
		if (this.el.offsetHeight > 0) this.custom(this.el.offsetHeight, 0);
		else this.custom(0, this.el.scrollHeight);
	}
});

fx.Combo = Class.create();
fx.Combo.prototype = {
	initialize: function(el) {
		this.el = $(el);
		this.el.h = new fx.Height(el);
	},
	
	toggle: function() { this.checkExec('toggle'); },
	hide: function(){ this.checkExec('hide'); },
	clearTimer: function(){ this.checkExec('clearTimer'); },
	
	checkExec: function(func){
		if (this.el.o) this.el.o[func]();
		if (this.el.h) this.el.h[func]();
		if (this.el.w) this.el.w[func]();
	},
}

fx.Accordion = Class.create();
fx.Accordion.prototype = {
	initialize: function(togglers, elements) {
		this.elements = elements;
		elements.each(function(el, i) {
			if (el.offsetHeight > 0) el.style.height = '1%';
			el.fx = new fx.Combo(el);
			el.fx.hide();
		});

		togglers.each(function(tog, i) {
			tog.onclick = function() {
				this.showThisHideOpen(elements[i]);
			}.bind(this);
		}.bind(this));
	},

	showThisHideOpen: function(toShow){
		if (toShow.offsetHeight == 0) setTimeout(function() {this.clearAndToggle(toShow);}.bind(this), 100); // Delay:100 User settable
		this.elements.each(function(el, i) {
			if (el.offsetHeight > 0 && el != toShow) this.clearAndToggle(el);
		}.bind(this));
	},

	clearAndToggle: function(el){
		el.fx.clearTimer();
		el.fx.toggle();
	}
}

Array.prototype.each = function(func) {
	for (var i = 0; ob = this[i]; i++) func(ob, i);
}
