/***************************************************************
*  JavaScript DHTML foldout menu for TYPO3
*
*  Copyright notice
*
*  (c) 2004-2006 Peter Klein
*  All rights reserved
*
*  Released under GNU/GPL (see license file in tslib/)
*  This copyright notice MUST APPEAR in all copies of this script
*
***************************************************************/

function initFoldMenu() {
	// ------------- Config Stuff -----------------------------------------------
	this.autoFold = 0;				// Autofold the unfolded item when clicking on a new item?
	this.foldCurrent = 0;			// Allow currrent unfolded item to be folded? (Only active if "autoFold = 1")
	this.startFolded = 0;			// Should menu start with all items folded (Only active if "autoFold = 0")
	this.enableCookie = 0;			// Save current menu state in a cookie?
	this.cookieLifetime = 0;		// Lifetime of persistant cookie, in days. If set to 0 then a session cookie is set.
	this.direction = 0;				// What direction should the menu fold/unfold?
	this.imagePath = "fileadmin/";	// Path to image files.
	this.prefix = "foldoutMenu";	// Prefix of menu

	// ------------- Main function ----------------------------------------------
	this.foldMenu = function(curid) {
		if (!document.getElementById && !document.all) return; // Old Browsers
		this.displayMode = (this.direction)?"inline":"block";
		if (this.enableCookie) {
			this.foldout_expand = this.parseString(this.prefix+'_expand')
			this.foldout_collapse = this.parseString(this.prefix+'_collapse')
		}

var el = this.getElById(this.prefix+'-'+curid);
this.fold(el,curid,(el.style.display==this.displayMode && this.foldCurrent)?0:1);
if (this.enableCookie) {
				this.saveString(this.prefix+'_expand',this.foldout_expand)
				this.saveString(this.prefix+'_collapse',this.foldout_collapse)
			}
	}

	this.fold = function(el,uid,mode) {
		el.style.display = (mode)?this.displayMode:"none";
		this.foldout_expand[uid] = mode;
		this.foldout_collapse[uid] = !mode;
	}



	this.getElById = function(idVal) {
		return (document.getElementById)?document.getElementById(idVal):(document.all)?document.all[idVal]:null;
	}

	this.setMenu = function(uids) {
		this.foldout_expand = this.parseString(this.prefix+'_expand')
		this.foldout_collapse = this.parseString(this.prefix+'_collapse')
		this.displayMode = (this.direction)?"inline":"block";
		var uid = uids.split(",");
		for (var i=0; i<uid.length; i++) {
			var el = this.getElById(this.prefix+"-"+uid[i]);
			if (this.foldout_expand[uid[i]] == 1 && this.enableCookie) this.fold(el,uid[i],1);
			else if (el.style.display!=this.displayMode) {
				this.fold(el,uid[i],(!this.startFolded && !this.autoFold)?1:0)
//				this.fold(el,uid[i],(!this.startFolded)?1:0)
//				this.fold(el,uid[i],0)
}
		}
	}

	// ------------- Cookie functions --------------------------------------
	this.setCookie = function(name, value, lifespan, access_path) {
		var CookieString = name + "=" + escape(value);
		if (lifespan>0) {
			var today = expiredate = new Date();
			expiredate.setTime(today.getTime() + 1000*60*60*24*lifespan);
			CookieString += "; expires=" + expiredate.toGMTString();
		}
		CookieString += "; PATH="+((access_path != null)?access_path:"");
		document.cookie = CookieString;
	}

	this.getCookie = function(name) {
		var search = name + "=";
		var CookieString = document.cookie;
		var result = null;
		if (CookieString.length > 0) {
			var offset = CookieString.indexOf(search);
			if (offset != -1) {
				offset += search.length;
				end = CookieString.indexOf(";", offset)
				if (end == -1) end = CookieString.length;
				result = unescape(CookieString.substring(offset, end));
			}
		}
		return result;
	}

	this.deleteCookie = function(name, path) {
		this.setCookie(name,"Deleted", -1, path)
	}

    // ------------- Cookie parse functions --------------------------------
	this.parseString = function(cookieName) {
		var cookieString = this.getCookie(cookieName);
		var uids = new Array();
		if (cookieString) {
			var uid = cookieString.split('|');
			for (var i=0; i<uid.length-1; i++) {
				uids[uid[i]] = 1;
			}
		}
		return uids;
	}

	this.saveString = function(cookieName,uids) {
		var cookieString = '';
		for (var i=0; i<uids.length; i++) {
			if (uids[i] == 1) cookieString += i + '|';
		}
		this.setCookie(cookieName, cookieString,this.cookieLifetime,'/');
	}

}
