//Copyright 2007 Roman Ardern-Corris
//Licenced to 3legs.com


/*
    ligoGrid
*/

extend(ligoDrillGrid, ligoGrid);

//#####################################################################################################################

function ligoDrillGrid (paramsObj) {
    this.pathFields = [];       //field in the dataset that will be displayed in the cookie crumb,
                                //it is an array, so that if one of them is null, we will use the next
    this.crumbDiv = null;       //The div where crumb controls will go
    this.crumbDelimiter = ' / ';    //String to separate crumb items
    this.crumbItemMaxLength = 0;    //the max number of chars a crumb item can be
    
    this._path = [['Top','parent_block_id=' + -1]];   //path to where we are in the drill down

    ligoDrillGrid.superclass.call(this, paramsObj);

}

//#####################################################################################################################

ligoDrillGrid.prototype.onRowDblClick = function (row_num) {
    //store the clicked row details and 
    var current_row_json = this.Dataset.getVerboseCurrentRow();
    var current_row = this.Dataset.getVerboseCurrentRowObject();
    
    var crumb_string;
    for (var i=0; i<this.pathFields.length; i++) {
        crumb_string = current_row.data[this.pathFields[i]];
        if (crumb_string) {
            //trim to be this.crumbItemMaxLength chars long
            if (this.crumbItemMaxLength > 0) {
                crumb_string = crumb_string.substr(0, this.crumbItemMaxLength);
            }
            break;
        }
    }
    
    if (crumb_string != null) {
        this._path.push([crumb_string, current_row_json, current_row]);
        
        //basically perform a refresh with our dataset as the source, this will use this dataset as the master
        this.Dataset.refresh(this.Dataset);
    }
    else {
        alert("Error: unable to get crumb string");
    }
        
}

//#####################################################################################################################

ligoDrillGrid.prototype.render = function () {
    this.render_crumb_trail();
    
    //Do the normal ligoGrid.render()
    ligoDrillGrid.superproto.render.call(this);
}

//#####################################################################################################################

ligoDrillGrid.prototype.render_crumb_trail = function () {
    var div_crumb = $(this.crumbDiv);
    
    var html = [];
    for (var i=0; i<this._path.length; i++) {
        var link = 'javascript:' + this.instanceName + '.gotoPathItem(' + i + ')';
        var html_string = this._path[i][0];
        if (i != this._path.length - 1) {   //do not have a link on the last item in the path
            html_string = '<a href="' + link + '">' + html_string + '</a>';
        }
        html.push(html_string);
    }
    
    div_crumb.innerHTML = html.join(this.crumbDelimiter);

}

//#####################################################################################################################

ligoDrillGrid.prototype.gotoPathItem = function (path_index) {
    
    var master_row_string = this._path[path_index][1];
    
    //remove all path items after the path_index
    this._path.splice(path_index + 1);
        
    //this is needed because the controller does not expect the master row string when doing the top level
    var dont_append_masterparam = false;
    if (path_index == 0) {
        dont_append_masterparam = 1;
    }
    this.Dataset.refreshUsingMasterRow(master_row_string, dont_append_masterparam);
    
}

//#####################################################################################################################

ligoDrillGrid.prototype.currentNode = function () {
    //returns the name of the last item in this_path
    
    return(this._path[this._path.length -1][0]);
    
}


//#####################################################################################################################

ligoDrillGrid.prototype.parentRowObject = function () {
    //returns the name of the last item in this_path
    
    var row = this._path[this._path.length -1][2];
    
    return( row );
    
}

//#####################################################################################################################
