/**
 * CMenu (Classic Menu)
 *
 * Modified version for Intellogy to support Ajax calls
 *
 * @version 1.0
 * @copyright Creative Pulse 2008
 * @link http://www.creativepulse.eu/goto/cmenu
 */

function CMenu(params)
{
	if (typeof params.iname != 'string') {
		alert('CMenu initialization error: Instance name is missing');
		return;
	}

	this.iname = params.iname;

	// look for IE6
	var ver = navigator.userAgent.toLowerCase().match(/msie (\d(.\d*)?)/);
	var ie6 = ver && ver[1] && ver[1] < 7;
	
	this.hide_timeout = typeof params.hide_timeout != 'number' ? 500 : params.hide_timeout;

	this.mi_layers = typeof params.mi_layers != 'number' ? 1 : params.mi_layers;
	if (this.mi_layers < 1)
		this.mi_layers = 1;

	this.mi_empty_normal_class = typeof params.mi_empty_normal_class != 'string' ? '' : params.mi_empty_normal_class;
	this.mi_empty_hover_class = typeof params.mi_empty_hover_class != 'string' ? '' : params.mi_empty_hover_class;
	this.mi_full_normal_class = typeof params.mi_full_normal_class != 'string' ? '' : params.mi_full_normal_class;
	this.mi_full_hover_class = typeof params.mi_full_hover_class != 'string' ? '' : params.mi_full_hover_class;

	this.rounded_corners = typeof params.rounded_corners != 'boolean' ? true : params.rounded_corners;

	this.transparency = typeof params.transparency != 'boolean' ? true : params.transparency;
	if (this.transparency) {
		this.transparency_ie6 = typeof params.transparency_ie6 != 'boolean' ? false : params.transparency_ie6;
		if (!this.transparency_ie6 && ie6)
			this.transparency = false;
	}

	if (this.transparency) {
		this.min_opacity = typeof params.min_opacity != 'number' ? 0 : params.min_opacity;
		if (this.min_opacity < 0)
			this.min_opacity = 0;
		else if (this.min_opacity > 100)
			this.min_opacity = 100;
	
		this.max_opacity = typeof params.max_opacity != 'number' ? 0 : params.max_opacity;
		if (this.max_opacity < 0)
			this.max_opacity = 0;
		else if (this.max_opacity > 100)
			this.max_opacity = 100;
	}

	this.panel_offset1_x = typeof params.panel_offset1_x != 'number' ? 0 : params.panel_offset1_x;
	this.panel_offset1_y = typeof params.panel_offset1_y != 'number' ? 0 : params.panel_offset1_y;
	this.panel_offset2_x = typeof params.panel_offset2_x != 'number' ? 0 : params.panel_offset2_x;
	this.panel_offset2_y = typeof params.panel_offset2_y != 'number' ? 0 : params.panel_offset2_y;
	if (!this.rounded_corners) {
		this.panel_offset2_x = 0;
		this.panel_offset2_y = 0;
	}

	this.separator_class = typeof params.separator_class != 'string' ? '' : params.separator_class;
	this.panel_class = typeof params.panel_class != 'string' ? '' : params.panel_class;
	this.panel_tl_class = typeof params.panel_tl_class != 'string' ? '' : params.panel_tl_class;
	this.panel_tc_class = typeof params.panel_tc_class != 'string' ? '' : params.panel_tc_class;
	this.panel_tr_class = typeof params.panel_tr_class != 'string' ? '' : params.panel_tr_class;
	this.panel_ml_class = typeof params.panel_ml_class != 'string' ? '' : params.panel_ml_class;
	this.panel_mr_class = typeof params.panel_mr_class != 'string' ? '' : params.panel_mr_class;
	this.panel_bl_class = typeof params.panel_bl_class != 'string' ? '' : params.panel_bl_class;
	this.panel_bc_class = typeof params.panel_bc_class != 'string' ? '' : params.panel_bc_class;
	this.panel_br_class = typeof params.panel_br_class != 'string' ? '' : params.panel_br_class;

	// custom handlers
	this.ch_create = typeof params.ch_create == 'undefined' ? null : params.ch_create;
	this.ch_mouseover = typeof params.ch_mouseover == 'undefined' ? null : params.ch_mouseover;
	this.ch_mouseout = typeof params.ch_mouseout == 'undefined' ? null : params.ch_mouseout;
	this.ch_panel_show = typeof params.ch_panel_show == 'undefined' ? null : params.ch_panel_show;
	this.ch_panel_hide = typeof params.ch_panel_hide == 'undefined' ? null : params.ch_panel_hide;
	this.ch_state_change = typeof params.ch_state_change == 'undefined' ? null : params.ch_state_change;

	this.animate = typeof params.animate != 'undefined' || typeof params.anim_step != 'undefined' || typeof params.anim_interval != 'undefined';
	if (typeof params.animate == 'boolean' && !params.animate)
		this.animate = false;

	if (this.animate) {
		this.anim_step = typeof params.anim_step != 'number' ? 30 : params.anim_step;
	
		if (this.anim_step <= 0 || this.anim_step > 100)
			this.anim_step = 1;

		this.anim_interval = typeof params.anim_interval != 'number' ? 100 : params.anim_interval;
	}

	this.side = typeof params.side != 'string' ? '' : params.side.replace(/^\s+|\s+$/g, '').toLowerCase();
	if (this.side != 'left' && this.side != 'right' && this.side != 'up' && this.side != 'down')
		this.side = 'right';

	this.orientation = typeof params.orientation != 'string' ? '' : params.orientation.replace(/^\s+|\s+$/g, '').toLowerCase();
	if ((this.side == 'left' || this.side == 'right') && this.orientation != 'up' && this.orientation != 'down')
		this.orientation = 'down';
	else if ((this.side == 'up' || this.side == 'down') && this.orientation != 'left' && this.orientation != 'right')
		this.orientation = 'right';

	this.direction = this.side.charAt(0) + this.orientation.charAt(0);
	this.last_inst_id = 0;
	this.menuitems = new Array();
	this.registry = new Array();
	this.timer_hide = 0;
	this.vis_mi = null;
	this.wdg_base = document.getElementsByTagName('body')[0];
}

CMenu.prototype.registry_add = function (mi)
{
	this.registry.push(mi);
	return this.registry.length - 1;
}

CMenu.prototype.add_item = function (params)
{
	params.base = this;
	params.parent = this;
	params.iname = typeof params.id != 'string' ? '' : params.id;
	var mi = new CMenuItem(params, true);
	this.menuitems.push(mi);
	return mi;
}

CMenu.prototype.run = function ()
{
	for (var i = 0, len = this.menuitems.length; i < len; i++)
		this.menuitems[i].run();
}

CMenu.prototype.handle_mouseover = function (reg_id, e)
{
	if (this.timer_hide > 0) {
		clearTimeout(this.timer_hide);
		this.timer_hide = 0;
	}

	var mi = this.registry[reg_id];
	
	if (this.vis_mi) {
		var panels_new = new Array();
		mi.query_panels(panels_new, true);
		var panels_old = new Array();
		this.vis_mi.query_panels(panels_old, false);

		while (true) {
			var mi_old = panels_old.shift();
			var mi_new = panels_new.shift();

			if (!mi_old)
				break;
			
			if (!mi_new || mi_old != mi_new) {
				mi_old.hide_now();
				while (mi_old = panels_old.shift())
					mi_old.hide_now();

				break;
			}
		}
	}
	this.vis_mi = mi;

	mi.handle_mouseover(e);
}

CMenu.prototype.handle_mouseout = function (reg_id, e)
{
	if (this.timer_hide == 0)
		this.timer_hide = setTimeout('document["' + this.iname + '"].hide_process(' + reg_id + ')', this.hide_timeout);

	var mi = this.registry[reg_id];
	mi.handle_mouseout(e);
}

CMenu.prototype.handle_click = function (reg_id)
{
	var mi = this.registry[reg_id];
	mi.handle_click();
}

CMenu.prototype.hide_process = function (reg_id)
{
	var mi = this.registry[reg_id];
	mi.hide_process();
}


function CMenuItem(params, is_header)
{
	this.is_header = is_header;
	this.base = params.base;
	this.parent = params.parent;
	this.iname = typeof params.iname != 'string' ? '' : params.iname;
	this.link = typeof params.link != 'string' ? '' : params.link;
	this.caption = typeof params.caption != 'string' ? '' : params.caption;
	this.selection = typeof params.selection != 'number' ? 0 : params.selection;

	this.type = typeof params.type != 'string' ? '' : params.type;
	if (this.type != 'text' && this.type != 'link' && this.type != 'link_new' && this.type != 'link_popup' && this.type != 'separator') {
		// set default type
		if (this.link != '' && this.caption != '')
			this.type = 'link';
		else if (this.caption != '')
			this.type = 'text';
		else
			this.type = 'separator';
	}

	this.panel_inner = null;
	this.panel_outer = null;
	this.panel_x = 0;
	this.panel_y = 0;
	this.panel_outer_w = 0;
	this.panel_outer_h = 0;
	this.panel_inner_w = 0;
	this.panel_inner_h = 0;
	this.panel_ch_x = 0;
	this.panel_ch_y = 0;
	this.panel_ch_min_w = 0;
	this.anim_progress = 0;
	this.anim_speed = 0;
	this.anim_timer = 0;
	this.menuitems = new Array();
	this.state_mouseover = false;
	this.state_panelopen = false;

	if (this.iname == '') {
		this.wdg = document.createElement('div');

		if (typeof params.id != 'undefined')
			this.wdg.id = params.id;


		var container = this.parent.panel_inner;
		container.appendChild(this.wdg);
		container = this.wdg;
		for (var i = 2; i <= this.base.mi_layers; i++) {
			obj = document.createElement('div');
			container.appendChild(obj);
			obj.className = 'l' + i;
			container = obj;
		}
//	this.panel_inner.appendChild(mi.wdg);

		if (this.type == 'separator') {
			if (this.base.separator_class != '')
				this.wdg.className = this.base.separator_class;
		}
		else {
//			if (this.caption != '')
//				this.last_child(this.wdg).innerHTML = this.caption;
			if (this.caption != '') {
				var obj = document.createElement('div');
				obj.innerHTML = this.caption;
				obj.className = 'text';
				container.appendChild(obj);
			}

			if (this.type == 'link' || this.type == 'link_new' || this.type == 'link_popup')
				this.wdg.style.cursor = 'pointer';

			this.h_state_change();
		}
	}
	else {
		this.wdg = document.getElementById(this.iname);
	}

	this.reg_id = this.base.registry_add(this);

	this.wdg.setAttribute('base_iname', this.base.iname);
	this.wdg.setAttribute('reg_id', this.reg_id);
	this.wdg.onmouseover = function (e) { document[this.getAttribute('base_iname')].handle_mouseover(this.getAttribute('reg_id'), e); }
	this.wdg.onmouseout = function (e) { document[this.getAttribute('base_iname')].handle_mouseout(this.getAttribute('reg_id'), e); }
	this.wdg.onclick = function () { document[this.getAttribute('base_iname')].handle_click(this.getAttribute('reg_id')); }

	if (this.base.ch_create != null)
		this.base.ch_create(this);
}

CMenuItem.prototype.last_child = function(obj)
{
	while (obj.firstChild)
		obj = obj.firstChild;

	return obj;
}

CMenuItem.prototype.isMouseEnterOrLeave = function (element, e)
{
	if (e.type != 'mouseout' && e.type != 'mouseover')
		return false;

	var relatedTarget = e.relatedTarget || (e.type == 'mouseout' ? e.toElement : e.fromElement);

	while (relatedTarget && relatedTarget != element)
		relatedTarget = relatedTarget.parentNode;

	return relatedTarget != element;
}

CMenuItem.prototype.add_item = function (params)
{
	if (!this.panel_outer) {
		// create sub-menu panel
		this.panel_outer = document.createElement('div');
		this.base.wdg_base.appendChild(this.panel_outer);
		this.panel_outer.style.overflow = 'hidden';

		if (this.base.rounded_corners) {
			var tbl = document.createElement('table');
			this.panel_outer.appendChild(tbl);
			tbl.setAttribute('cellSpacing', 0);
			tbl.setAttribute('cellPadding', 0);
			
				var tbd = document.createElement('tbody');
				tbl.appendChild(tbd);
			
					// top row
					var tr = document.createElement('tr');
					tbd.appendChild(tr);
			
						var td_tl = document.createElement('td');
						tr.appendChild(td_tl);
						if (this.base.panel_tl_class != '')
							td_tl.className = this.base.panel_tl_class;

						var td_tc = document.createElement('td');
						tr.appendChild(td_tc);
						if (this.base.panel_tc_class != '')
							td_tc.className = this.base.panel_tc_class;

						var td_tr = document.createElement('td');
						tr.appendChild(td_tr);
						if (this.base.panel_tr_class != '')
							td_tr.className = this.base.panel_tr_class;

					// middle row
					var tr = document.createElement('tr');
					tbd.appendChild(tr);
			
						var td_ml = document.createElement('td');
						tr.appendChild(td_ml);
						if (this.base.panel_ml_class != '')
							td_ml.className = this.base.panel_ml_class;

						var td_mc = document.createElement('td');
						tr.appendChild(td_mc);

						var td_mr = document.createElement('td');
						tr.appendChild(td_mr);
						if (this.base.panel_mr_class != '')
							td_mr.className = this.base.panel_mr_class;

					// bottom row
					var tr = document.createElement('tr');
					tbd.appendChild(tr);
			
						var td_bl = document.createElement('td');
						tr.appendChild(td_bl);
						if (this.base.panel_bl_class != '')
							td_bl.className = this.base.panel_bl_class;

						var td_bc = document.createElement('td');
						tr.appendChild(td_bc);
						if (this.base.panel_bc_class != '')
							td_bc.className = this.base.panel_bc_class;

						var td_br = document.createElement('td');
						tr.appendChild(td_br);
						if (this.base.panel_br_class != '')
							td_br.className = this.base.panel_br_class;

			this.panel_inner = document.createElement('div');
			td_mc.appendChild(this.panel_inner);
		}
		else {
			this.panel_inner = document.createElement('div');
			this.panel_outer.appendChild(this.panel_inner);
		}

		if (this.base.panel_class != '')
			this.panel_inner.className = this.base.panel_class;
	}

	params.base = this.base;
	params.parent = this;
	var mi = new CMenuItem(params, false);
	this.menuitems.push(mi);

	this.h_state_change();

	return mi;
}

CMenuItem.prototype.run = function ()
{
	if (this.panel_outer) {
		this.panel_outer.style.position = 'absolute';
		this.panel_outer.style.visibility = 'hidden';
		this.panel_outer.style.left = '0px';
		this.panel_outer.style.top = '0px';
		this.panel_outer.style.display = 'block';
		this.panel_outer_w = this.panel_outer.offsetWidth;
		this.panel_outer_h = this.panel_outer.offsetHeight;
		this.panel_inner_w = this.panel_inner.offsetWidth;
		this.panel_inner_h = this.panel_inner.offsetHeight;
		this.panel_outer.style.display = 'none';
		this.panel_outer.style.visibility = 'visible';
	}

	// run submenus
	for (var i = 0, len = this.menuitems.length; i < len; i++)
		this.menuitems[i].run();
}

CMenuItem.prototype.query_panels = function (ar, show)
{
	if (show && this.anim_speed < 0)
		this.anim_speed = this.base.anim_step;

	if (this.panel_outer)
		ar.unshift(this);

	if (!this.is_header)
		this.parent.query_panels(ar, show);
}

CMenuItem.prototype.handle_timer = function ()
{
	this.anim_timer = 0;

	this.anim_progress += this.anim_speed;
	var progress = this.anim_speed > 0 ? Math.ceil(this.anim_progress) : Math.floor(this.anim_progress);

	if (progress <= 0) {
		this.panel_hide();
		this.anim_speed = 0;
	}
	else {
		this.panel_show();

		if (progress > 100)
			progress = 100;

		this.animateit(progress);

		if (progress < 100)
			this.anim_timer = setTimeout('document["' + this.base.iname + '"].registry[' + this.reg_id + '].handle_timer()', this.base.anim_interval);
		else
			this.anim_speed = 0;
	}
}

CMenuItem.prototype.animateit = function (progress)
{
	if (this.base.transparency) {
		var opacity = Math.round((progress / 100) * (this.base.max_opacity - this.base.min_opacity) + this.base.min_opacity);
		this.panel_outer.style.opacity = opacity / 100;
		this.panel_outer.style.filter = 'alpha(opacity=' + opacity + ')';
	}

	var h = Math.round(this.panel_outer_h * progress / 100);

	switch (this.base.direction) {
		case 'dl':
		case 'dr':
		case 'ld':
		case 'rd':
			this.panel_outer.style.height = h + 'px';
			break;

		case 'ul':
		case 'ur':
		case 'lu':
		case 'ru':
			this.panel_outer.style.top = (this.panel_y + this.panel_outer_h - h) + 'px';
			this.panel_outer.style.height = h + 'px';
			this.panel_outer.scrollTop = this.panel_outer_h - h;
			break;
	}
}

CMenuItem.prototype.hide_process = function ()
{
	if (this.panel_outer) {
		if (this.base.animate && this.anim_progress > 1) {
			if (this.anim_speed == 0)
				this.anim_progress = 100 - this.base.anim_step;

			this.anim_speed = -this.base.anim_step;

			if (this.anim_timer == 0)
				this.anim_timer = setTimeout('document["' + this.base.iname + '"].registry[' + this.reg_id + '].handle_timer()', this.base.anim_interval);
		}
		else {
			this.panel_hide();
		}
	}

	if (!this.is_header)
		this.parent.hide_process();
}

CMenuItem.prototype.hide_now = function ()
{
	if (this.anim_timer > 0) {
		clearTimeout(this.anim_timer);
		this.anim_timer = 0;
	}

	this.anim_speed = 0;
	this.anim_progress = 0;

	if (this.panel_outer)
		this.panel_hide();
}

CMenuItem.prototype.handle_mouseover = function (e)
{
	if (!this.isMouseEnterOrLeave(this.wdg, e || window.event))
		return;

	if (this.base.ch_mouseover != null)
		this.base.ch_mouseover(this);

	if (this.panel_outer) {

		if (this.panel_ch_x == 0 && this.panel_ch_y == 0) {
			var x = 0, y = 0, obj = this.wdg;
			while (obj) {
				x += obj.offsetLeft;
				y += obj.offsetTop;
				obj = obj.offsetParent;
			}
	
			switch (this.base.direction) {
				case 'ul':
					if (this.is_header) {
						x += this.wdg.offsetWidth - this.panel_outer_w;
						y -= this.panel_outer_h;
					}
					else {
						x -= this.panel_outer_w;
						y += this.wdg.offsetHeight - this.panel_outer_h;
					}
					break;
	
				case 'ur':
					if (this.is_header) {
						y -= this.panel_outer_h;
					}
					else {
						x += this.wdg.offsetWidth;
						y += this.wdg.offsetHeight - this.panel_outer_h;
					}
					break;
	
				case 'dl':
					if (this.is_header) {
						x += this.wdg.offsetWidth - this.panel_outer_w;
						y += this.wdg.offsetHeight;
					}
					else {
						x -= this.panel_outer_w;
					}
					break;
	
				case 'dr':
					if (this.is_header)
						y += this.wdg.offsetHeight;
					else
						x += this.wdg.offsetWidth;
					break;
	
				case 'lu':
					x -= this.panel_outer_w;
					y += this.wdg.offsetHeight - this.panel_outer_h;
					break;
	
				case 'ld':
					x -= this.panel_outer_w;
					break;
	
				case 'ru':
					x += this.wdg.offsetWidth;
					y += this.wdg.offsetHeight - this.panel_outer_h;
					break;
	
				case 'rd':
					x += this.wdg.offsetWidth;
					break;
			}
	
			this.panel_x = x;
			this.panel_y = y;
	
			if (this.is_header) {
				this.panel_x += this.base.panel_offset1_x;
				this.panel_y += this.base.panel_offset1_y;
			}
			else {
				this.panel_x += this.base.panel_offset2_x;
				this.panel_y += this.base.panel_offset2_y;
			}
		}
		else {
			this.panel_x = this.panel_ch_x;
			this.panel_y = this.panel_ch_y;
		}

		if (this.panel_ch_min_w > 0 && this.panel_outer_w < this.panel_ch_min_w)
			this.panel_inner.style.width = (this.panel_inner_w + this.panel_ch_min_w - this.panel_outer_w) + 'px';

		this.panel_outer.style.left = this.panel_x + 'px';
		this.panel_outer.style.top = this.panel_y + 'px';
		this.panel_show();

		if (this.base.animate && this.anim_progress < 100) {
			if (this.anim_speed == 0) {
				this.anim_progress = 1;
				this.animateit(1);
			}

			this.anim_speed = this.base.anim_step;

			if (this.anim_timer == 0)
				this.anim_timer = setTimeout('document["' + this.base.iname + '"].registry[' + this.reg_id + '].handle_timer()', this.base.anim_interval);
		}
	}

	this.state_mouseover = true;
	this.h_state_change();
}

CMenuItem.prototype.handle_mouseout = function (e)
{
	if (!this.isMouseEnterOrLeave(this.wdg, e || window.event))
		return;

	if (this.base.ch_mouseout != null)
		this.base.ch_mouseout(this);

	this.state_mouseover = false;
	this.h_state_change();
}

CMenuItem.prototype.handle_click = function ()
{
	if (this.link != '') {
		switch (this.type) {
			case 'link':
				if ((this.link[0]=='/') && $.cms.config.enabled.ajax) {
					if (!$.cms.ajax.go(this.link)) return;
				}
				window.location = this.link;
				break;

			case 'link_new':
				window.open(this.link, '_blank');
				break;

			case 'link_popup':
				window.open(this.link, '', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=550');
				break;
		}
	}
}

CMenuItem.prototype.panel_show = function ()
{
	if (this.panel_outer.style.display != 'block') {
		if (this.base.ch_panel_show != null)
			this.base.ch_panel_show(this);

		this.panel_outer.style.height = '1px';
		this.panel_outer.style.display = 'block';
		this.state_panelopen = true;
		this.h_state_change();
	}
}

CMenuItem.prototype.panel_hide = function ()
{
	if (this.panel_outer.style.display != 'none') {
		if (this.base.ch_panel_hide != null)
			this.base.ch_panel_hide(this);

		this.panel_outer.style.display = 'none';
		this.state_panelopen = false;
		this.h_state_change();
	}
}

CMenuItem.prototype.h_state_change = function ()
{
	if (this.base.ch_state_change != null)
		this.base.ch_state_change(this);

	if (!this.is_header) {
		if (this.type != 'separator') {
			var class_name = 'mi'
				+ (this.panel_outer ? '_full' : '_empty')
				+ (this.state_mouseover || this.state_panelopen ? '_hover' : '_normal')
				+ '_class';
			this.wdg.className = this.base[class_name];
		}
	}
}



/** Template_js **/
function addEvent(obj, event, func)
{
	if (obj.addEventListener)
		obj.addEventListener(event, func, false);
	else if (obj.attachEvent)
		obj.attachEvent('on' + event, func);
}

function template_get_wdg(wdg_center)
{
	return {
		left: wdg_center.previousSibling.nodeType == 1 ? wdg_center.previousSibling : wdg_center.previousSibling.previousSibling,
		center: wdg_center,
		right: wdg_center.nextSibling.nodeType == 1 ? wdg_center.nextSibling : wdg_center.nextSibling.nextSibling
	}
}

function template_ch_create(mi)
{
	if (mi.is_header) {
		var wdg = template_get_wdg(mi.wdg);
		wdg.left.style.cursor = 'pointer';
		wdg.center.style.cursor = 'pointer';
		wdg.right.style.cursor = 'pointer';
	}
}

function template_ch_mouseover(mi)
{
	if (document.page_loaded && mi.is_header) {
		var wdg = template_get_wdg(mi.wdg);

		mi.panel_ch_x = 0;
		mi.panel_ch_y = wdg.center.offsetHeight;
		var obj = wdg.left;
		mi.panel_ch_x += obj.offsetLeft;
		obj = obj.offsetParent;
		while (obj && obj.tagName != 'BODY') {
			mi.panel_ch_x += obj.offsetLeft;
			mi.panel_ch_y += obj.offsetTop;
			obj = obj.offsetParent;
		}
		mi.panel_ch_min_w = wdg.left.offsetWidth + wdg.center.offsetWidth + wdg.right.offsetWidth;

		if (typeof mi.td_center_class == 'undefined') {
			mi.td_left_class = wdg.left.className;
			mi.td_center_class = wdg.center.className;
			mi.td_right_class = wdg.right.className;
		}

		if (wdg.left.className == 'cmenu_hd_norm_left_round')
			wdg.left.className = 'cmenu_hd_sel_left_round';
		else if (wdg.left.className == 'cmenu_hd_norm_left_corner')
			wdg.left.className = 'cmenu_hd_sel_left_corner';

		wdg.center.className = 'cmenu_hd_sel_body';

		if (wdg.right.className == 'cmenu_hd_norm_right_round')
			wdg.right.className = 'cmenu_hd_sel_right_round';
		else if (wdg.right.className == 'cmenu_hd_norm_right_corner')
			wdg.right.className = 'cmenu_hd_sel_right_corner';
	}
}

function template_menu_close(mi)
{
	if (document.page_loaded) {
		var wdg = template_get_wdg(mi.wdg);
		wdg.left.className = mi.td_left_class;
		wdg.center.className = mi.td_center_class;
		wdg.right.className = mi.td_right_class;
	}
}

function template_ch_mouseout(mi)
{
	if (mi.is_header && mi.menuitems.length == 0)
		template_menu_close(mi);
}

function template_ch_panel_hide(mi)
{
	if (mi.is_header)
		template_menu_close(mi);
}

function page_init()
{
	document.page_loaded = true;

	if (document['cmenu_items']) {
		document['mc'] = new CMenu({
			iname: 'mc',

			side: 'down',
			orientation: 'right',

			animate: true,
			anim_step: 10,
			anim_interval: 40,

			transparency: true,
			transparency_ie6: false,

			min_opacity: 0,
			max_opacity: 100,

			ch_create: template_ch_create,
			ch_mouseover: template_ch_mouseover,
			ch_mouseout: template_ch_mouseout,
			ch_panel_hide: template_ch_panel_hide,

			panel_offset1_x: 0,
			panel_offset1_y: 0,
			panel_offset2_x: 5,
			panel_offset2_y: 0,

			separator_class: 'cmenu_separator',
			panel_class: 'cmenu_panel',
			panel_tl_class: 'cmenu_tl',
			panel_tc_class: 'cmenu_tc',
			panel_tr_class: 'cmenu_tr',
			panel_ml_class: 'cmenu_ml',
			panel_mr_class: 'cmenu_mr',
			panel_bl_class: 'cmenu_bl',
			panel_bc_class: 'cmenu_bc',
			panel_br_class: 'cmenu_br',

			mi_empty_normal_class: 'cmenu_mi_empty_normal',
			mi_empty_hover_class: 'cmenu_mi_empty_hover',
			mi_full_normal_class: 'cmenu_mi_full_normal',
			mi_full_hover_class: 'cmenu_mi_full_hover'

		});
	
		var len = document['cmenu_items'].length;
		if (len > 0) {
			for (var i = 0; i < len; i++) {
				var item = document['cmenu_items'][i];
		
				if (typeof item.parent_id == 'undefined')
					this[item.id] = document['mc'].add_item(item);
				else
					this[item.id] = this[item.parent_id].add_item(item);
			}
		
			document['mc'].run();
		}
	}
}

addEvent(window, 'load', page_init);
