document.cur_open_obj = null;
document.addEvent('click', function(){
	if (document.cur_open_jso) document.cur_open_jso.jsoClose(); 
});

window.addEvent('domready', function(){
	SI.Files.stylizeAll();
	
	$$('.jsb').each(function(e){
		var src = e.getProperty('src');
		if (!src) return;
		e.jsb = {}
		e.jsb.normal = src
		e.jsb.hover = src.replace(/\.([a-zA-Z]+)$/, "_h.$1");
		new Element('img', { 'src': e.jsb.hover, 'styles': { 'display': 'none' } }).inject($('body'));
		e.addEvent('mouseenter', function(){
			this.setProperty('src', this.jsb.hover);
		});
		e.addEvent('mouseleave', function(){
			this.setProperty('src', this.jsb.normal);
		});
	});
	$$('div.jss').each(function(e){
		var jss = {}
		e.jss = jss;
		jss.disabled = e.hasClass('jssdisabled');
		jss.options_open = false;
		jss.input = e.getElement('input');
		jss.content_div = e.getElement('div.jsscontent');
		jss.options_div = e.getElement('div.jssoptions');
		jss.cur_open_group = null;
		
		jss.jsoOpen = function() {
			if (document.cur_open_jso) document.cur_open_jso.jsoClose();
			document.cur_open_jso = this;
			this.options_open = true;
			this.options_div.setStyle('display', 'block');
		}
		jss.jsoClose = function() {
			if (this.cur_open_group) this.cur_open_group.closeGroup();
			if (document.cur_open_jso == this) document.cur_open_jso = null;
			this.options_open = false;
			this.options_div.setStyle('display', 'none');
		}
		e.addEvent('click', function(event){
			new Event(event).stop();
			if (this.jss.disabled) return;
			if (this.jss.options_open) this.jss.jsoClose();
			else this.jss.jsoOpen();
		});
		
		var func = function(jss, first) {
			var k = null;
			var group_name_div = null;
			for (var o = first; o; o = o.getNext()) {
				if (o.hasClass('jsskey')) k = o.get('html');
				else if (o.hasClass('jssgroupname')) group_name_div = o; 
				else if (o.hasClass('jssgroup')) {
					if (group_name_div) {
						var group_jss = { }
						group_name_div.group_jss = group_jss;
						group_jss.jss = jss;
						group_jss.group_div = o;
						group_jss.open = false;
						group_jss.openGroup = function() {
							if (this.jss.cur_open_group) this.jss.cur_open_group.closeGroup();
							this.jss.cur_open_group = this;
							this.open = true;
							this.group_div.setStyle('display', 'block');
						}
						group_jss.closeGroup = function() {
							if (this.jss.cur_open_group == this) this.jss.cur_open_group = null;
							this.open = false;
							this.group_div.setStyle('display', 'none');
						}
						group_name_div.addEvent('click', function(event){ 
							new Event(event).stop();
							if (this.group_jss.open) this.group_jss.closeGroup();
							else this.group_jss.openGroup();
						});
						func(jss, o.getElement('div'));
					}
				} else if (o.hasClass('jssvalue')) {
					if (jss.input.value == k) jss.content_div.set('html', o.get('html'));
					o.jsskey = k;
					o.addEvent('click', function(event){ 
						new Event(event).stop();
						jss.content_div.set('html', this.get('html'));
						jss.input.value = this.jsskey;
						jss.jsoClose();
					});
				}
			}
		}
		func(jss, jss.options_div.getElement('div'));
	});
});

function JsMenu(jsmroot) {
	jsmroot.jsm = {}
	jsmroot.jsm.open = null;
	jsmroot.jsoClose = function() {
		if (document.cur_open_jso == this) document.cur_open_jso = null;
		if (this.jsm.open) this.jsm.open.jsmClose();
	}
	jsmroot.getElements('.jsm').each(function(e){
		e.jsm = {}
		e.jsm.open = null;
		e.jsm.child = e.getNext('.jsmc');
		e.jsm.parent = e.getParent('.jsmc');
		if (e.jsm.parent) e.jsm.parent = e.jsm.parent.getPrevious('.jsm');
		if (!e.jsm.parent) e.jsm.parent = jsmroot;
		e.jsm.root = jsmroot;

		e.jsmOpen = function() {
			if (document.cur_open_jso != this.jsm.root) {
				if (document.cur_open_jso) document.cur_open_jso.jsoClose();
				document.cur_open_jso = this.jsm.root;
			}
			if (this.jsm.parent.jsm.open != this) {
				if (this.jsm.parent.jsm.open) this.jsm.parent.jsm.open.jsmClose();
				this.jsm.parent.jsm.open = this;
			}
			this.addClass('jsmopen');
			this.jsm.child.addClass('jsmcopen');
		}
		e.jsmClose = function() {
			if (this.jsm.parent.jsm.open == this) this.jsm.parent.jsm.open = null;
			if (this.jsm.open) this.jsm.open.jsmClose();
			if (document.cur_open_jso == this) document.cur_open_jso = null;
			this.removeClass('jsmopen');
			this.jsm.child.removeClass('jsmcopen');
		}
		e.addEvent('click', function(event){
			new Event(event).stop();
			if (this.hasClass('jsmopen')) this.jsmClose();
			else this.jsmOpen();
		});
	});
}
