// MyUtil - JavaScript utility for MyLinear
var MyUtil = {
	save : function(value) {
		document.cookie = "my_linear_key_index_t=" + escape(value);
	},
	retr : function() {
		var val = document.cookie.match('my_linear_key_index_t=(.*?)(;|$)');
		if (val) {
			return unescape(val[1]);
		} else {
			return "";
		}
	},
	open : function(t) {
		var v = MyUtil.retr() + ' ' + t;
		MyUtil.save(v);
	},
	close : function(t) {
		var v = MyUtil.retr().replace(new RegExp("^"+t+"\\b\\s*|\\s*\\b"+t+"\\b",'g'), '');
		MyUtil.save(v);
	},
	is_open : function(t) {
		var v = MyUtil.retr();
		var eq = v && v.match('^'+t+'\\b|\\b'+t+'\\b|\\b'+t+'$|^'+t+'$');
		return eq;
	},
	load : function() {
		// set up the left nav favorites
		var lnav = document.getElementById('lnavfav');
		if (lnav) {
			lnav.onclick = function() {
				// cycle through the favorites
				for (var t = 3; t < 6; t++) {
					var tab = document.getElementById('tab'+t);
					if (tab) {
						if (HasClass(tab, 'plus')) {
							tab.toggleui();
						}
					}
					MyUtil.open(t);
				}
			}
		}
		var val = MyUtil.retr();
		for (var t = 0; t < 6; t++) {
			var tab = document.getElementById('tab'+t);
			if (tab && tab.onclick) {
				tab.toggleui = tab.onclick;
				if (MyUtil.is_open(t)) tab.toggleui();
				tab.onclick = function() {
					this.toggleui();
					var n = this.id.match('tab(\\d)');
					if (HasClass(this, 'plus')) {
						MyUtil.close(n[1]);
					} else {
						MyUtil.open(n[1]);
					}
					return false;
				}
			}
		}
	},
	start : function() {
		var oldonload = window.onload;
		
		if (typeof window.onload != 'function') {
			window.onload = MyUtil.load;
		} else {
			window.onload = function() {
				try {
					oldonload();
				} catch (e) {
					// do nothing
				}
				MyUtil.load();
			}
		}
	}
}

MyUtil.start();
