/**
* @author Ricardo Bertolín
* General functions used in the base layer component
*/

/*
 * This function changes the base layer: visually in the control animation and throw corresponding event
 * @param td_sel {object} is the cell selected
 * @param name_baseLayer {string} is the name of the new base layer
 * @param url_baseLayer {string} is the url of the new base layer
 */
function selectBaseLayer(td_sel,name_baseLayer,url_baseLayer){
    //first check: if the cell has the selected class means its the selected and do nothing
        if (!$(td_sel).hasClass("opacity-04"))
            return;
    //call function to change the visual cell
    change_selected_cell(td_sel,'layer_table','opacity-1','opacity-04');
    Extroptions = ({"layer_name":name_baseLayer,"layer_url":url_baseLayer});//compose the options to be send
    triggerEventSetLayerOptions("layer_grid",getCurrentBaseLayer(),Extroptions);//finally trigger event  
        
}

/*
 * This function changes only visually the selected cell into a grid.
 * It simply adds a select class into the selected element and mantains the non_selected in others
 * @param {object} td_sel is the cell object selected
 * @param {string} element_id is the id of the cell selected
 * @param {string} selected_class is the class we want to imput as selected. It may vary 
 * @param {string} non_selected_class is the class we want to imput as not selected. It may vary
 */

function change_selected_cell(td_sel,element_id,selected_class,non_selected_class){
    
    var lgth, cell;
    //first get the length of the grid we want to do the changes. Its the number of cells it contains!
    lgth=document.getElementById(element_id).tBodies[0].rows[0].cells.length;
    //loop over the cells to see if they have the selected_class and remove
        for (var i=0; i<lgth; i++){          
           //get the cell
           cell=document.getElementById(element_id).tBodies[0].rows[0].cells[i];
           //now check if has the selected class, remove and add the non_selected
                if ($(cell).hasClass(selected_class)){
                    $(cell).removeClass(selected_class);
                    $(cell).addClass(non_selected_class);
                }                   
        }
    //at this point all cells have the non_selected_class
    //now we have to add the selected class to the new one and remove the non_selected
    $(td_sel).addClass(selected_class);
    $(td_sel).removeClass(non_selected_class); 
}


