var j_opacity = true; // opacity status
var j_delay = 400; // moo's animation delay time
var j_transition = fx.linear; // moo's transition type: fx.sinoidal, fx.linear, fx.cubic, fx.circ

var j_stretcher = "stretcher"; // class name of the stretchers <div>
var j_display = "headtab";// class name of the headtabs <li>
var j_displayhover = "-current"; // suffix of hover class

// global variable, used for cookie
var j_oldtab = "";

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function init(){
	var stretchers = document.getElementsByClassName(j_stretcher); //div that stretches
	var toggles = document.getElementsByClassName(j_display); //spans where I click on

	showingmoo = getCookie("showingmoo");
	mooshowed = getCookie("mooshowed");
	if (showingmoo!=null) {
		currentstretcher = document.getElementById(showingmoo);
		if (mooshowed == "true") {
			headtab = showingmoo.replace(j_stretcher, j_display);
			j_oldtab = document.getElementById(headtab);
		}
	} else {
		document.cookie = "showingmoo = " + stretchers[0].id;
		document.cookie = "mooshowed = true";
	}

	//accordion effect
	var ja_Accord = new fx.Accordion(
		toggles, stretchers, {opacity: j_opacity, duration: j_delay, transition: j_transition}
	);

	ja_Accord.showThisHideOpen = function(toShow) {
		this.elements.each(function(el, i) {
			if (el.offsetHeight > 0 && el != toShow) this.clearAndToggle(el); 
			if (el == toShow) document.cookie = "showingmoo = " + toShow.id;
		}.bind(this));
		if (toShow.offsetHeight > 0) document.cookie = "mooshowed = false";
		else document.cookie = "mooshowed = true";
		setTimeout(function(){this.clearAndToggle(toShow)  ;}.bind(this), this.options.delay);
	};
	
	if (showingmoo!=null) {
		if (mooshowed == "true") {
			if (j_oldtab) j_oldtab.className = j_display + j_displayhover;
		} else {
			currentstretcher.fx.hide();
		}
	} else {
		headtab = stretchers[0].id.replace(j_stretcher, j_display);
		j_oldtab = document.getElementById(headtab);
		j_oldtab.className = j_display + j_displayhover;
		ja_Accord.clearAndToggle(stretchers[0]);
	}
}