/* 
 This file was generated by Dashcode.  
 You may edit this file to customize your widget or web page 
 according to the license.txt file included in the project.
 */

var knobMessages = {
    'volume': '/Synthesizer1/MasterVolume',
    'mix1': '/Mixer1/Ch1Level',
    'mix2': '/Mixer1/Ch2Level',
    'mix3': '/Mixer1/Ch3Level',
    'tune1': '/AnalogOscil1/CoarseTune',
    'tune2': '/AnalogOscil2/CoarseTune',
    'tune3': '/WavetableOscil3/CoarseTune',
};
var knobValues = {
    'volume': 0.5,
    'mix1': 0.5,
    'mix2': 0.5,
    'mix3': 0.5,
    'tune1': 0.5,
    'tune2': 0.5,
    'tune3': 0.5,
};
var knobOffsetXs = {};

//
// Function: load()
// Called by HTML body element's onload event when the web application is ready to start
//
function load()
{
    dashcode.setupParts();
    for (var knobName in knobValues) {
        drawKnob(knobName);
    }
}

function drawKnob(knobName)
{
    knob = document.getElementById(knobName);
    value = knobValues[knobName];
    
    ctx = knob.getContext("2d");
    w = knob.width;
    h = knob.height;
    
    ctx.fillStyle = "rgb(0,0,0)";
    ctx.fillRect (0, 0, w, h);
    
    if (knobName == 'volume') 
        ctx.strokeStyle = "rgb(255,255,255)";
    else
        ctx.strokeStyle = "rgb(255,255,0)";
    
    // Draw arcs
    
    ctx.beginPath();
    ctx.lineWidth = w/5;
    var x          = w/2;               // x coordinate
    var y          = h/2;               // y coordinate
    var radius     = w/2-ctx.lineWidth/2;                    // Arc radius
    var startAngle = Math.PI*.625;                     // Starting point on circle
    var endAngle   = Math.PI*.645+(Math.PI*1.73*value); // End point on circle
    ctx.arc(x,y,radius,startAngle,endAngle, false);
    ctx.stroke();
    
    ctx.beginPath();
    ctx.lineWidth = w/50;
    var x          = w/2;               // x coordinate
    var y          = h/2;               // y coordinate
    var radius     = w/2-ctx.lineWidth/2;                    // Arc radius
    var startAngle = Math.PI*.625;                     // Starting point on circle
    var endAngle   = Math.PI*.625+Math.PI*1.75; // End point on circle
    ctx.arc(x,y,radius,startAngle,endAngle, false);
    ctx.strokeStyle = "rgb(127,127,127)";
    ctx.stroke();
}

function sendMessage(event)
{
    window.location.href="osc:"+document.getElementById('message').value;
}

function receiveOSC()
{
    var foo = "Got this message:<br>";
    for (var i=0; i < arguments.length; i++) {
        foo += arguments[i] + " ";
    }
    document.getElementById('text').innerHTML = foo;
}

function buttonClick(event)
{
    window.location.href="osc:/button?s="+this.element.parentNode.innerText;
}

function knobSet(knobName, value)
{
    value = Math.min(1.0, Math.max(0.0, value));
    knobValues[knobName] = value;
    drawKnob(knobName);
    window.location.href="osc:"+knobMessages[knobName]+"?f="+value;
}

function knobStart(event)
{
    var knobName = event.target.id;
    var knob = event.target;
    var value = knobValues[knobName];
    var valuex = value * knob.height;
    try { touchx = event.targetTouches[0].clientX; }
    catch (ex) { touchx = event.clientX; }
    knobOffsetXs[knobName] = touchx - valuex;
}


function knobMove(event)
{
    var knobName = event.target.id;
    var knob = event.target;
    var touchx;
    try { touchx = event.targetTouches[0].clientX; }
    catch (ex) { touchx = event.clientX; }
    var valuex = touchx - knobOffsetXs[knobName];
    var value = valuex / knob.height;
    knobSet(knobName, value);
}


function knobEnd(event)
{
    var knobName = event.target.id;
    drawKnob(knobName);
}


function preventScrolling(event)
{
    event.preventDefault();
}
