/*
  Circle.js
  Copyright 2010, Kipp Software Corporation
*/

function Circle(div, callback) {
    this.div = div;
    this.div.className = "circle";
    this.callback = callback;
    this.knob = document.createElement("div");
    this.knob.className = "knob";
    this.div.appendChild(this.knob);
    var circle = this;    
    this.knob.onmousedown = function(e) { circle.startCapture(); };
    this.mousemove = function(e) { if (document.circle) document.circle.moveMouse(e); };
    this.mouseup = function() { if (document.circle) document.circle.stopCapture(); };
    this.mousewheel = function(e) { circle.moveWheel(e); }
    if (window.addEventListener) {
        window.addEventListener('DOMMouseScroll', this.mousewheel, false);
    } else if (document.attachEvent) {
	window.onmousewheel = document.onmousewheel = this.mousewheel;
    }
    this.setTheta(0);
}

Circle.prototype.startCapture = function() {
    document.circle = this;
    if (document.addEventListener) {
	document.addEventListener("mousemove", this.mousemove, true);
	document.addEventListener("mouseup", this.mouseup, true);
    } else if (document.attachEvent) {
	document.attachEvent("onmousemove", this.mousemove);
	document.attachEvent("onmouseup", this.mouseup);
    }
};

Circle.prototype.stopCapture = function() {
    if (document.removeEventListener) {
	document.circle = null;
	document.removeEventListener("mousemove", this.mousemove, true);
	document.removeEventListener("mouseup", this.mouseup, true);
    } else if (document.detachEvent) {
	document.circle = null;
	document.detachEvent("onmousemove", this.mousemove);
	document.detachEvent("onmouseup", this.mouseup);
    }
};

Circle.prototype.moveMouse = function(e) {
    var e = e ? e : (event ? event : null);
    var cx = e.clientX - this.div.offsetLeft - this.div.offsetWidth / 2;
    var cy = e.clientY - this.div.offsetTop - this.div.offsetHeight / 2;
    this.setTheta(Math.atan2(cx,-cy));
};

Circle.prototype.moveWheel = function(e) {
    var e = e ? e : (event ? event : null);    
    var delta = e.detail | -e.wheelDelta;
    this.debug(delta);
    this.setTheta(this.theta + (delta > 0 ? 1 : -1) * Math.PI / 24);
    if (e.preventDefault) {
	e.preventDefault();
    } else {
	e.returnValue = false;
    }
};

Circle.prototype.setTheta = function(theta) {
    this.theta = theta;
    var radius = 2.25;
    while (this.theta < 0) {
	this.theta += 2 * Math.PI;
    }
    while (this.theta >= 2 * Math.PI) {
	this.theta -= 2 * Math.PI;
    }
    this.debug("theta " + this.theta);
    var posX = this.div.clientLeft + this.div.offsetWidth / radius * Math.cos(this.theta - Math.PI / 2) + this.div.offsetWidth / 2;
    var posY = this.div.clientTop + this.div.offsetHeight / radius * Math.sin(this.theta - Math.PI / 2) + this.div.offsetHeight / 2;
    posX -= this.knob.offsetWidth / 2;
    posY -= this.knob.offsetHeight /2;
    this.knob.style.left = posX;
    this.knob.style.top = posY;
    this.callback(this.theta);
};

Circle.prototype.debug = function(message) {
    var debug = document.getElementById("debug");
    debug.innerHTML = message;
};
