/*******************************************************************************
	mt_circle_menu.js

	v	:	1.01
	d	:	01/03
	s	:	win(ie5, IE6, NS6, MOZILLA)
	
*******************************************************************************
	requirements:
		-	mt_dhtml.js version 1.55
		-	xhtml 1.1 Strict DTD
		-	DOM compatible browser
	usage:
		-	called in init()
		-	window.oCircle = new circleMenu(sId, sULid, nRadiusX, nRadiusY, nLeft, nTop, nItems, nRotation)
		-	sID		=	object id
		-	sULid	=	UL id containing the menu items
		-	nRadiusX	=	initial horizontal radius
		-	nRadiusY	=	initial vertical radius
		-	nLeft		=	left position px
		-	nTop		=	top position px
		-	nItems		=	number of menu items to display - will duplicate  until circle is filled (recommended < 40)
		-	nRoration	=	rotation in Radians about the major axis to create offset ellipses
		
********************************************************************************/

function circleMenu(sId, sULid, nRadiusX, nRadiusY, nLeft, nTop, nItems, nRotation){
	gE(sULid).style.visibility = "hidden";
	this.id 		   = sId;
	this.nItems 	= nItems;
	this.nLeft 		= nLeft + 2 * nRadiusY;
	this.nTop 		= nTop + nRadiusY;
	this.nRadiusX 	= nRadiusX;
	this.nRadiusY 	= nRadiusY;
	this.nInc 		= 0;
	this.nLastInc	= 0;
	this.bHasMouse = 0;
	this.nRotation = nRotation;
	this.aTxt 		= [];
	this.aHrefs 	= [];
	this.aLinks 	= gE(sULid).childNodes;

	for(var i=0; i<this.aLinks.length; i++){
		if(this.aLinks[i].nodeType == 1){
			this.aHrefs[this.aHrefs.length] = this.aLinks[i].firstChild.getAttribute("href")
			this.aTxt[this.aTxt.length] = this.aLinks[i].firstChild.innerHTML
		}
	}
	var trunc = this.aTxt.length > 10;

	while(this.aTxt.length < this.nItems){
        this.aHrefs = this.aHrefs.concat(this.aHrefs);
        this.aTxt = this.aTxt.concat(this.aTxt);
	}

	if (trunc)
		{
		this.aTxt.splice(this.nItems);
		this.aHrefs.splice(this.nItems);
		}
	else
		{this.nItems = this.aTxt.length;}

	this.nTheta 	= Math.PI * 2 / this.nItems;

	for(i=0; i<this.nItems; i++){
		window[this + ".circleItem" + i] = o = new oDhtml("circleItem" + i)
		o.write("<a class=\"circleMenu\" href=\"" + this.aHrefs[i] + "\">" + this.aTxt[i] + "</a>")
		o.setVisible(1)
		o.setClass("circleMenu")
	}
	
	this.move()
	aDhtmlMove[aDhtmlMove.length] = this.id + ".mousemove()"
	this.spin()
}

circleMenu.prototype.mousemove = function(){
	if(mouseX > this.nLeft - this.nRadiusX + 50 && mouseX < this.nLeft + this.nRadiusX && mouseY > this.nTop - this.nRadiusY && mouseY < this.nTop + this.nRadiusY){
		if(!this.timer || !this.bHasMouse){
			this.bHasMouse = 1
			this.spin()
		}
	}else if(this.bHasMouse){
		this.bHasMouse = 0
	}
}

circleMenu.prototype.move = function(){
	for(var i=0; i<this.nItems; i++){
		var nX = this.nRadiusX * Math.cos((i + this.nInc)*this.nTheta)
		var nY = this.nRadiusY * Math.sin((i + this.nInc)*this.nTheta)
		var nXpos = nX * Math.cos(this.nRotation) - nY * Math.sin(this.nRotation) + this.nLeft - this.nRadiusX
		var nYpos = nX * Math.sin(this.nRotation) + nY * Math.cos(this.nRotation) + this.nTop
		window[this + ".circleItem" + i].moveTo(nXpos, nYpos)
		if(nXpos < this.nLeft - this.nRadiusX + 90){
			window[this + ".circleItem" + i].setVisible(0)
		}else{
			window[this + ".circleItem" + i].setVisible(1)
			window[this + ".circleItem" + i].setOpacity((nXpos - this.nLeft) / this.nRadiusX * 350 + this.nRadiusX)
		}
	}
}


circleMenu.prototype.spin = function(){
	this.nLastInc = (this.bHasMouse) ? ((Math.abs(mouseY - this.nTop) < 20) ? 0 :(mouseY - this.nTop) / this.nRadiusY / 1.5) : (this.nLastInc > 0) ? 0.1 : -0.1
	clearInterval(this.timer)
	this.nInc -= this.nLastInc
	this.move()
	this.timer = setInterval(this.id + ".spin(" + this.nLastInc + ")", 80)
}

circleMenu.prototype.stop = function(){
	clearInterval(this.timer)
	this.timer = null
}
