/* NTControl_slider class */

function NTControl_slider(symbol, addr)
{
    // call base when not typed dynamically (see change_class_type()) 
    // otherwise, the base object is already constructed
    if(!this._dynamically_typed)
        NTControl(symbol, addr)

    this._gui_id0 = get_unique_id(); // slider image
    this._range = this._electrodes.length * 2 - 2;

    // define RAM type
    pcm_read_ptr(this._symbol + "->data");
    pcm_define_symbol(this._symbol + "->data", pcm.LastVariable_vValue, "nt_control_slider_data");

    debug_print("The Slider Control has been Created.", false);
}

NTControl_slider.prototype.GetRange = function()
{
    return this._range;
}

NTControl_slider.prototype.GetPosition = function()
{
   if(!pcm_read_var(this._symbol + "->data.position", 1))
        return 0;
        
    return pcm.LastVariable_vValue;
}

NTControl_slider.prototype.GetGuiTag = function()
{
    var str = "";
    str = "<center><img src=\"images/slider.png\" width=120px height=120px></center>";
    return str;
}

NTControl_slider.prototype.GetGuiTagsLabels = function()
{
    var str = "";
    str = "<th>position</th><th>range</th><th>touch</th><th>movement</th><th>direction</th><th>invalid touch</th>";
    return str;
}

NTControl_slider.prototype.GetGuiTags = function()
{
  
  var str = "";
  str = "<td><span id=" + this._gui_id0 + "pos>?</span></td>"+
        "<td><span id=" +  this._gui_id0 + "range>?</span></td>"+
        "<td><img id=" + this._gui_id0 + "Touch src=\"images/icn_yes_hidden.png\" style=\"height:24px;width:24px;\"></td>"+
        "<td><img id=" + this._gui_id0 + "Movement src=\"images/icn_arrow_move_hidden.png\" style=\"height:24px;width:24px;\">"+
        "</td><td><img id=" + this._gui_id0 + "Direction src=\"images/icn_dir_right.png\" style=\"height:24px;width:24px;\"></td>"+
        "</td><td><img id=" + this._gui_id0 + "Invalid src=\"images/icn_warning_hidden.png\" style=\"height:24px;width:24px;\"></td>";
  return str;
}

NTControl_slider.prototype.UpdateGui = function()
{
    var pos = this.GetPosition();
    var rng = this.GetRange();
    var flags = this.GetFlags();

    var kobj = document.getElementById(this._gui_id0+"pos");
    if(kobj)
        kobj.innerHTML = pos.toString(10);

    kobj = document.getElementById(this._gui_id0+"range");    
    if(kobj)
        kobj.innerHTML = rng.toString(10);

    kobj = document.getElementById(this._gui_id0+"Touch");    
    if(kobj)
    {
        kobj.src = (flags & (1<<19)) ? "images/icn_yes.png" : "images/icn_yes_hidden.png"; 
    }

    kobj = document.getElementById(this._gui_id0+"Movement");    
    if(kobj)
    {
        kobj.src = (flags & (1<<18)) ? "images/icn_arrow_move.png" : "images/icn_arrow_move_hidden.png"; 
    }

    kobj = document.getElementById(this._gui_id0+"Direction");    
    if(kobj)
    {
        kobj.src = (flags & (1<<17)) ? "images/icn_dir_right.png" : "images/icn_dir_left.png"; 
    }

    kobj = document.getElementById(this._gui_id0+"Invalid");    
    if(kobj)
    {
        kobj.src = (flags & (1<<16)) ? "images/icn_warning.png" : "images/icn_warning_hidden.png"; 
    }       
}

NTControl_slider.prototype.DefinePositionVariable = function(vname)
{
    return pcm_define_variable(vname, this._symbol + "->data.position");
}

NTControl_slider.prototype.OnLinkClicked = function()
{
    var pname = this._name + "_position";
    var ok = this.DefinePositionVariable(pname);

    // scope variables
    var vars = [ 
        {"variable":pname, "visible":true, "y_block":0 } 
    ]; 

    // up to 7 electrode signals
    var el_count = this._electrodes.length;
    if(el_count > 7)
        el_count = 7;
        
    for(var e=0; e<el_count; e++)
    {
        var ename = this._name + "_electrode" + e + "_signal";
        ok = ok && this._electrodes[e].DefineSignalVariable(ename);
        
        var edef = { "variable":ename, "visible":true, "y_block":1 }; 
        vars.push(edef);
    }

    // scope Y-blocks
    var yblocks = [
        { "laxis_label":"position", "join_class":0, "laxis_min":0, "laxis_max":this.GetRange(), "laxis_min_auto":false, "laxis_max_auto":false },
        { "laxis_label":"signal", "join_class":1, "laxis_min_auto":true, "laxis_max_auto":true },
    ];

    // scope definition
    var def = {};
    def["var_info"] = vars;
    def["yblock_info"] = yblocks;
    def["scope_period"] = 0.025;
    def["auto_delete"] = true;
    def["href"] = DOC_PATH+"group__slider.html#details";

    var item = this._name + " Details";
    ok = ok && pcm.DefineScope(item, JSON.stringify(def));
    
    if(ok)
        pcm.SelectItem(item, "scope");
    else
        alert("Could not create graph with selected signals"); 
}

