/** 
 * @author Ricardo Bertolín
 * @desc Include general functions that can be used by events, such as trigger events   
 */

/**
 * TRIGGER FUNCTIONS
 * This are common everywhere just to trigger the events
 */

/**
 * trigger the EVENT_SET_EPOCH
 * @param {string} idElemTrigg is the id of the element that triggers the event
 * @param {int} epoch is the epoch to set
 */
function triggerEventSetEpoch(idElemTrigg,epoch){

    $(document).trigger('EVENT_SET_EPOCH', [ idElemTrigg,epoch ]);  
  
}

/**
 * trigger the EVENT_SET_PERIOD
 * @param {string} idElemTrigg is the id of the element that triggers the event
 * @param {int} startEpoch is the initial epoch
 * @param {int} endEpoch is the end epoch
 */
function triggerEventSetPeriod(idElemTrigg,startEpoch,endEpoch,idPoint){

    $(document).trigger('EVENT_SET_PERIOD', [ idElemTrigg, startEpoch, endEpoch,idPoint ]);
  
}

/**
 * trigger the EVENT_SET_ZOOM_OPTIONS
 * @param {string} idElemTrigg is the id of the element that triggers the event
 * @param {int} zoomLevel is the new zoom to stablish
 */
function triggerEventSetZoomOptions (idElemTrigg,zoomLevel){

    $(document).trigger('EVENT_SET_ZOOM_OPTIONS', [ idElemTrigg,zoomLevel ]);  
}

/**
 * trigger the EVENT_ANIMATION_PLAY
 * @param {string} idElemTrigg is the id of the element that triggers the event
 */
function triggerEventAnimationPlay( idElemTrigg ){

    $(document).trigger('EVENT_ANIMATION_PLAY',[ idElemTrigg ]);  
  
}

/**
 * trigger the EVENT_ANIMATION_PAUSE
 * @param {string} idElemTrigg is the id of the element that triggers the event
 */
function triggerEventAnimationPause( idElemTrigg ){
    
    $(document).trigger('EVENT_ANIMATION_PAUSE',[ idElemTrigg ]);  
  
}

/**
 * trigger the EVENT_SET_LAYER_OPTIONS
 * @param {string} idElemTrigg is the id of the element that triggers the event
 * @param {string} viewerLayerName is the layer name to modify
 * @param {array} options is a collection of options
 */
function triggerEventSetLayerOptions (idElemTrigg, viewerLayerName, options ){
    
    $(document).trigger('EVENT_SET_LAYER_OPTIONS',[ idElemTrigg, viewerLayerName, options ]);  
}

/**
 * trigger the EVENT_SET_COMPONENT_OPTIONS 
 * @param {string} idElemTrigg is the id of the element that triggers the event
 * @param {string} viewerComponentName is the viewer component name to modify
 * @param {array} options is a collection of options
 */
function triggerEventSetComponentOptions (idElemTrigg, viewerComponentName, options ){
    
    $(document).trigger('EVENT_SET_COMPONENT_OPTIONS',[ idElemTrigg, viewerComponentName, options ]);  
}

/**
 * trigger the EVENT_SET_PRODUCT_EPOCH
 * @param {string} idElemTrigg is the id of the element that triggers the event
 * @param {int} epoch is the epoch to set
 * @param {string} productId ID of the product that corresponds to the item clicked
 * @param {bool} higherValues is the higer_values of the product
 */
function triggerEventSetProductEpoch(idElemTrigg,epoch,productId,higherValues){

    $(document).trigger('EVENT_SET_PRODUCT_EPOCH', [ idElemTrigg,epoch,productId,higherValues ]); 

}

/**
 * trigger the EVENT_SET_NOW_EPOCH
 * @param {string} idElemTrigg is the id of the element that triggers the event
 * @param {int} nowEpoch is the epoch to set
 */
function triggerEventSetNowEpoch(idElemTrigg,nowEpoch){

    $(document).trigger('EVENT_SET_NOW_EPOCH', [ idElemTrigg,nowEpoch ]);

}

/**
 * trigger the EVENT_SET_PRODUCT_VALUES 
 * @param {string} idElemTrigg is the id of the element that triggers the event
 * @param {string} idProduct ID of the product that corresponds to the item clicked
 * @param {integer} value is the value to set
 * @param {integer} epoch is the epoch related to product, value and unit  
 */
function triggerEventSetProductValues(idElemTrigg,idProduct,value,epoch){

    $(document).trigger('EVENT_SET_PRODUCT_VALUES', [ idElemTrigg,idProduct,value,epoch ]);

}



/**
 * GENERAL FUNCTIONS
 */

/*
 * This function obtains information of a corresponding product from its id
 * @param {string} productId is the id of the product to search
 * @return {array} information about the product
 */
    function getProductInfoById(productId){

    var infoProduct;
    //do an ajax call to obtain values from db
         $.ajax({
            async: false,
            type: "POST",
            dataType: 'json',
            data: 'OPERATION=getProduct&PRODUCT_ID='+productId,
            url: '/php_includes/frontend/components/wicast/viewer_weekly_forecast/viewer_weekly_forecast.php' ,
            success: function( vars ){
                infoProduct=vars;
            }
        });
    
    return infoProduct;

    }
 
 /*
 * This function obtains information of a group product from id group and id product
 * @param {string} productId is the id of the product
 * @param {string} productGroup is the id of the group
 * @return {array} information about the group product
 */ 
    function getProductGroupInfoById(productId,productGroup){
        
    var infoGroupProduct;
    //do an ajax call to obtain values from db
         $.ajax({
            async: false,
            type: "POST",
            dataType: 'json',
            data: 'OPERATION=getGroupProduct&PRODUCT_ID='+productId+'&GROUP_ID='+productGroup,
            url: '/php_includes/frontend/components/wicast/viewer_weekly_forecast/viewer_weekly_forecast.php' ,
            success: function( vars ){
                infoGroupProduct=vars;
            }
        });
    
    return infoGroupProduct;
        
    }
    
    
/*
 * This function obtains vals and dirs for a given product.
 * It stores the result in the global cache valsAndDirsCache
 * @param {string} params are the params you want to pass to the call
 * @param {string} locale is the current locale to search for
 */ 
    function loadValsAndDirs(params,locale){

    var valsAndDirs;
    //do an ajax call to obtain values from db
         $.ajax({
            async: false,
            type: "GET",
            url: '/frontend.php/'+locale+'/query/values'+params,
            dataType: 'json',
            success: function( vars ){
                valsAndDirs=vars;
            }
        });
      
      return valsAndDirs;
      
    }



