var roundmenus = new Array();

function findRoundmenu(extID) {
	var roundmenu = null;
	for (var i=0; i<roundmenus.length; i++)
		if (roundmenus[i].extID == extID) {
			roundmenu = roundmenus[i];
			break;
		}
	return roundmenu;
}

function getRoundmenu(extID) {
	var roundmenu = findRoundmenu(extID);
	if (roundmenu == null) {
		roundmenu = new Roundmenu(extID);
		roundmenu.id = roundmenus.length;
		roundmenus[roundmenu.id] = roundmenu;
	}
	return roundmenu;
}

function Roundmenu(extID) {
	this.id = -1;
	this.extID = extID;
    this.subColor = '#999999';

	this.tree = null;
	this.setTree = function (tree) {
		this.tree = tree;
		tree.treeview[this.id] = this;
	}
	this.root = null;
	this.setRoot = function (root) {
		this.root = root;
	}

	this.getVisibleItems = function () {
		var result = new Array();
		for (var i=0; i<this.tree.items.length; i++) {
			var root = this.tree.items[i];
			if (root!=null && root.parent==null) {
				result[result.length] = root;
				if (this.tree.treeview[this.id].activeItem == root.id)
					for (var j=0; j<root.items.length; j++)
						result[result.length] = root.items[j];
			}
		}
		return result;
	}

	this.doc = null;
	this.picsSource = null;
	this.draw = function () {

		var source = '';
		var items = this.tree.items;
		for (var i=0; i<items.length; i++) {
			if (items[i].parent == null) {
				source += '<DIV id="Roundmenu_' + items[i].id + '" class="clTop">';
				source += this.drawRoot(items[i]);
				source += '</DIV>';
			}
			else {
				source += '<DIV id="Roundmenu_' + items[i].id + '" class="clSub">';
				source += this.drawSub(items[i]);
				source += '</DIV>';
			}
		}
		source += '<DIV id="Roundmenu_pressed" class="clTop">';
		source += '<IMG src="'+this.picsSource+'pressed.gif" align="absmiddle" border="0" width="16" height="16" >';
		source += '</DIV>';
		this.doc.writeln(source);

		for (var i=0; i<items.length; i++)
			items[i].obj = getObject('Roundmenu_' + items[i].id, this.doc);
		this.objPressed = getObject('Roundmenu_pressed', this.doc);

		roundmenuSelect(this.id, this.activeItem);
	}

	this.drawRoot = function(item) {
		var source = '';
		source += '<a href="javascript: roundmenuSelect(' + this.id + ', ' + item.id + ')" class="clMain">';
		source += '<IMG src="'+this.picsSource+'off.gif" align="absmiddle" border="0" width="16" height="16" ' +
			'onMouseOver="javascript: cache.get(\''+this.picsSource+'pressed.gif\', this);"' +
			'onMouseOut="javascript: cache.get(\''+this.picsSource+'off.gif\', this)"' +
			'>';
		source += '&nbsp;' + item.text;
		source += '</a>';
		return source;
	}

	this.drawSub = function (item) {
		var source = '';
		source += '<a href="javascript: roundmenuSelect(' + this.id + ', ' + item.id + ')" class="clSub">';
		source += '<IMG src="/images/blank.gif" align="left" border="0" width="2" height="2">';
		source += '<li style="color:#CC0000;"><font color="'+this.subColor+'">' + item.text + '</font>';
		source += '</a>';
		return source;
	}

	this.activeItem = null;

	this.marginLeft = 0;
	this.marginTop = 317;
	this.deltaAngle = Math.PI/15; // Math.PI/12;
	this.startAngle = Math.PI/2 - Math.PI/10; //Math.PI/2 - Math.PI/10;
	this.radius = 134;

	cache.put(this.picsSource+'off.gif', 'off');
	cache.put(this.picsSource+'on.gif', 'on');
	cache.put(this.picsSource+'pressed.gif', 'pressed');
}

function roundmenuSelect(menuID, rootID) {
    //debugger;
	//alert('menuID: ' + menuID + ', rootID:' + rootID);
	var roundmenu = roundmenus[menuID];
	var activeID1 = roundmenu.tree.treeview[menuID].activeItem;
	var activeID2 = rootID;
	if (activeID2!=null && roundmenu.tree.items[activeID2].parent==null)
		roundmenu.tree.treeview[menuID].activeItem = rootID;

	var items;
	if (activeID1!=null) {
		for (var i=0; i<roundmenu.tree.items.length; i++)
			hideObject(roundmenu.tree.items[i].obj);
	}
	items = roundmenu.getVisibleItems();
	var x0 = roundmenu.marginLeft;
	var y0 = roundmenu.marginTop;
	var x = 0;
	var y = 0;
	var r = roundmenu.radius;
	var da = roundmenu.deltaAngle;
	var a = roundmenu.startAngle;
	for (var i=0; i<items.length; i++) {
		items[i].obj = getObject('Roundmenu_' + items[i].id, roundmenu.doc);
		x = x0 + Math.round(r * Math.cos(a));
		y = y0 - Math.round(r * Math.sin(a));
		//a -= da;
		a = Math.asin(Math.sin(a) - getObjectHeight(items[i].obj)/r);
		//alert(i + "-" + items[i].text + " = " + x + " : " + y);
		moveTo(items[i].obj, x, y);
		showObject(items[i].obj);
	}

	if (rootID == null) {
		hideObject(roundmenu.objPressed);
	}
	else {
		var item = roundmenu.tree.items[rootID];

		if (item.parent == null) {
			moveTo(roundmenu.objPressed, getObjectLeft(item.obj), getObjectTop(item.obj));
			showObject(roundmenu.objPressed);
		}
		if ((activeID1 == null)) {
			/*window.parent.location.href = '990000081?content=' + item.url +
				'&' + roundmenu.prms +
				'&' + 'activeItem=' + item.id;*/
			window.parent.frames['left'].location.href = '990000080?' + roundmenu.prms +
				'&' + 'activeItem=' + item.id;
			window.parent.frames['right'].location.href = item.url + '?' + roundmenu.prms;
		}
		else {
			window.parent.frames['right'].location.href = item.url;
		}
	}
}

function decodeURL(url) {
	if (url==null || url=='')
		url = "javascript: //";
	return url;
}

