﻿function MenuManager() {
	this.categorys = [
		{ CssId : 'news', IsFold : true },
		{ CssId : 'account', IsFold : true },
		{ CssId : 'startupGuide', IsFold : true },
		{ CssId : 'indepthGuide', IsFold : true },
		{ CssId : 'community', IsFold : true },
		{ CssId : 'myshop', IsFold : true },
		{ CssId : 'downloads', IsFold : true },
		{ CssId : 'support', IsFold : true }
	];
	
	// separator = ':'
	// fold = '0', upfold = '1'
	if (arguments.length == 1) {
		var foldSet = arguments[0].split(':');
		if (foldSet.length == this.categorys.length) {
			for (var i = 0; i < this.categorys.length; i++) {
				this.categorys[i].IsFold = (foldSet[i] == '0') ? true : false;
			}
		}
	}
	
	this.settingSeparator = '_';
	this.FOLD = 'f';
	this.UNFOLD = 'u';
	this.settingName = 'trickster_menu_folding_set';
	this.settingDomain = 'tricksteronline.com';
	this.settingExpired = new Date(9999, (12-1), 31);
	
	this.GetSetting = function() {
		var setting = '';
		for (var i = 0; i < this.categorys.length; i++) {
			setting += ((this.categorys[i].IsFold) ? this.FOLD : this.UNFOLD) + this.settingSeparator;
		}
		if (setting.length > 1) {
			setting = setting.substring(0, setting.length - 1);
		}
		return setting;
	};
	this.SaveSetting = function() {
		var setting = this.GetSetting();
		this.DeleteSettingCookie();
		document.cookie = this.settingName + '=' + escape(setting) +
			';path=/;domain='+ this.settingDomain +			
			';expires='+ this.settingExpired.toGMTString();
	};	
	this.DeleteSettingCookie = function() {
		if (this.GetSettingCookie()) {
			document.cookie = this.settingName + '=' +
				';path=/;domain=' + this.settingDomain +
				';expires=Thu, 01-Jan-1970 00:00:01 GMT';
		}
	};	
	this.GetSettingCookie = function() {
		var start = document.cookie.indexOf(this.settingName + "=");
		var len = start + this.settingName.length + 1;
		if ((!start) && (this.settingName != document.cookie.substring(0, this.settingName.length))) {
			return null;
		}
		if (start == -1) {
			return null;
		}
		var end = document.cookie.indexOf(';', len);
		if (end == -1) {
			end = document.cookie.length;
		}
		return unescape(document.cookie.substring(len, end));
	};
	this.LoadSetting = function() {
		var settingLoaded = this.GetSettingCookie();
		if (settingLoaded) {
			var foldSettingList = settingLoaded.split(this.settingSeparator);
			if (foldSettingList.length == this.categorys.length) {
				for (var i = 0; i < foldSettingList.length; i++) {
					try {
						this.categorys[i].IsFold = (foldSettingList[i] == this.FOLD);
					}
					catch (e) { }
				}
			}
		}
	};
	this.ApplyFolding = function() {
		this.LoadSetting();
		for (var i = 0; i < this.categorys.length; i++) {
			if (this.categorys[i].IsFold) {
				this.Fold(i);
			}
			else {
				this.Unfold(i);
			}
		}
	};
	this.Fold = function(categoryId) {
		var category = this.categorys[categoryId];
		var categoryObj = document.getElementById(category.CssId);
		categoryObj.getElementsByTagName('ul')[0].style.display = 'none';
		categoryObj.getElementsByTagName('div')[0].className = 'fold';
		this.categorys[categoryId].IsFold = true;
	};
	this.Unfold = function(categoryId) {
		var category = this.categorys[categoryId];
		var categoryObj = document.getElementById(category.CssId);
		categoryObj.getElementsByTagName('ul')[0].style.display = 'block';
		categoryObj.getElementsByTagName('div')[0].className = '';
		this.categorys[categoryId].IsFold = false;
	};	
	this.ChangeFolding = function(categoryId) {
		if (this.categorys[categoryId].IsFold) {
			this.Unfold(categoryId);
		}
		else {
			this.Fold(categoryId);
		}
		this.SaveSetting();
	};
}