﻿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_stanhi = "stanhi"; // class name of the stanhis <div>
var j_display = "tophi";// class name of the tophis <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 stanhis = document.getElementsByClassName(j_stanhi); //div that stretches
	var toggles = document.getElementsByClassName(j_display); //spans where I click on

	showingmoo = getCookie("showingmoo");
	mooshowed = getCookie("mooshowed");
	if (showingmoo!=null) {
		currentstanhi = document.getElementById(showingmoo);
		if (mooshowed == "true") {
			tophi = showingmoo.replace(j_stanhi, j_display);
			j_oldtab = document.getElementById(tophi);
		}
	} else {
		document.cookie = "showingmoo = " + stanhis[0].id;
		document.cookie = "mooshowed = true";
	}

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

	ol_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 {
			currentstanhi.fx.hide();
		}
	} else {
		tophi = stanhis[0].id.replace(j_stanhi, j_display);
		j_oldtab = document.getElementById(tophi);
		j_oldtab.className = j_display + j_displayhover;
		ol_Accord.clearAndToggle(stanhis[0]);
	}
}