/****** FUNCTIONS RELATED TO EVENTS *******/


/**
* @author Javier Millan
*
* Bind the plot hover and polot click events to control the highlighting
* and the visualization of point tags
*
* @param <placeholder> Placeholder of the flot
*
*/
//this variable is used to control the current alert component that has been
//computed
var controller = 0;

function bindAlertEvents(placeholder){

    //bind function, trigger here
    placeholder.bind("plotclick", function (event, pos, item) {

        if(item){
            //is an active cell
            if(item.series.now <= item.series.data[item.dataIndex][2]){

               //unhighlight the previous clicked cell
               if(last_alert_lighted.length>0)
                    unhighlight_gantt(data[last_alert_lighted[0]],data[last_alert_lighted[0]].data[last_alert_lighted[1]]);


               //highlight the point clicked and saves as the last highlighted
               highlight_gantt(item.series,item.datapoint);
               last_alert_lighted = [item.seriesIndex,item.dataIndex];


                //send the  events to other components
                var product_id = item.series.ident;
                var hv = item.series.hv;
                var epoch = item.series.data[item.dataIndex][2];

                if(item.series.events){
                    for(var i=0; i<item.series.events.length; i++){

                        switch (item.series.events[i]){

                            case 'EVENT_SET_PRODUCT_EPOCH':
                                triggerEventSetProductEpoch('alerts',epoch/1000,product_id,hv);
                                break;
                            case 'EVENT_SET_EPOCH':
                                triggerEventSetEpoch('alerts',epoch/1000);
                                break;
                            case 'EVENT_SET_NOW_EPOCH':
                                // PATCH -- in case we have an offset_start, should add it. 
                                // this happens in 7-day view
                                triggerEventSetNowEpoch('alerts',epoch/1000 + alerts_config.graph_config.offset_start);
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
    });
}

/**
* @author Javier Millan
*
*Events: response function for eventSetProductEpoch by alerts component
*
* @param <idElemTrigg> is the id of the element that has triggered the event
* @param <productId> id_product of the cell
* @param <higherValues> hv of the product of the cell
* @param <epoch> epoch 
*
*/
function alerts_eventSetProductEpoch(idElemTrigg,epoch,productId,higherValues){

        //avoid auto-event
        if(idElemTrigg != 'alerts'){
            
            //convert normal epoch in milliepoch
            epoch*=1000;

            //firtsly, find the correct plot
            var nplots = GLOBAL_ALERTS.length;
            var data_index = -1;
            var plot_index=0;
            var i = 0;

            for(i=0;i<nplots;i++){
                    data_index = find_uuid_hv_in_plot(GLOBAL_ALERTS[i].getData(),productId,higherValues);
                    if(data_index>-1){
                       plot_index = i;
                       break;
                    }
            }

            //invalid highlighting
            if(data_index==-1)
                    return -1;

            var data = GLOBAL_ALERTS[plot_index].getData();

            //find the index on the data serie
            if(data_index !=-1){
                
                var  eindex = getAlertItemIndex(data[data_index],epoch);

                if(eindex !=-1){

                    //highlight the current cell and unhighlight the last cell
                    highlight_gantt(data[data_index],data[data_index].data[eindex]);
                    if(last_alert_lighted.length>0){

                        if(!(last_alert_lighted[0] == data_index && last_alert_lighted[1] == eindex))
                            unhighlight_gantt(data[last_alert_lighted[0]],data[last_alert_lighted[0]].data[last_alert_lighted[1]]);

                    }
                    last_alert_lighted = [data_index,eindex];
                }
            }
        }

        return 0;
}


/**
* @author Javier Millan
*
*Events: response function for eventSetEpoch by alerts component
*
* @param <idElemTrigg> is the id of the element that has triggered the event
* @param <epoch> epoch
*
*/
function alerts_eventSetEpoch(idElemTrigg,epoch){

    //avoid auto-event
    if(idElemTrigg !='alerts'){

        //firtsly, find the correct plot
        var plot_index = controller;
        epoch*=1000;

        if(GLOBAL_ALERTS.length > 0){

            var data = GLOBAL_ALERTS[plot_index].getData();
            var data_index = 0;

            //only treatment for colors bar
            if(data.length>0 && data[0].div == 'colors_bar'){

                //epoch = epoch + data[data_index].alert_length;
                var  eindex = getAlertItemIndex(data[data_index],epoch);

                if(eindex !=-1){
                        highlight_gantt(data[data_index],data[data_index].data[eindex]);
                        if(last_alert_lighted.length>0)
                                unhighlight_gantt(data[last_alert_lighted[0]],data[last_alert_lighted[0]].data[last_alert_lighted[1]]);

                        last_alert_lighted = [data_index,eindex];
                }
            }
        }

        //plot control
        if(controller < GLOBAL_ALERTS.length -1)
            controller++;

        else
            controller = 0;
    }

    return 0;
}

/**
* @author Javier Millan
*
*Set the first value lighted
*
* @param <idElemTrigg> is the id of the element that has triggered the event
* @param <productId> id_product of the cell
* @param <higherValues> hv of the product of the cell
* @param <epoch> epoch
*
*/
function highlight_now(epoch){

    //firtsly, find the correct plot
    var nplots = GLOBAL_ALERTS.length;

    if(nplots>0){

        var data = GLOBAL_ALERTS[0].getData();
        //find the index on the data serie
        if(data.length>0){

            var  eindex = getAlertItemIndex(data[0],epoch);
            if(eindex !=-1){
                //highlight the current cell and unhighlight the last cell
                highlight_gantt(data[0],data[0].data[eindex]);
                if(last_alert_lighted.length>0)
                        unhighlight_gantt(data[last_alert_lighted[0]],data[last_alert_lighted[0]].data[last_alert_lighted[1]]);

                last_alert_lighted = [0,eindex];
            }
        }
    }

    return 0;
}



/**
* @author Javier Millan
*
*Helper to find the index of the product on a set of data
*
* @param <id_product> id_product of the cell
* @param <hv> hv of the product of the cell
* @param <data> array of data
*
*/
function getAlertProductIndex(id_product,hv,data){

	for(var i=0;i<data.length;i++){

		if((data[i].ident ==id_product) && (data[i].hv == hv))
			return i;
	}
	return -1;
}


/**
* @author Javier Millan
*
*Helper to find the item of the graph by epoch
*
* @param <product> flot product object
* @param <epoch> epoch
*
*/
function getAlertItemIndex(product,epoch){

	for(var i=0; i<product.data.length;i++){

		if(epoch>product.data[i][0] && epoch<=product.data[i][2])
			return i;
	}

	return -1;
}

/*
 * 
 * EVENTS FUNCTIONS
 * 
 */

/*
 * Main function that stablish relation between bind functions and the specific one
 * First unbind all possible events, and then bind
 */
function alerts_bindEvents(){
    //start with unbind all functions
    alerts_unbindFunctions();
    //now bind it
    alerts_bindFunctions();

}
/*
 * Simply unbind all events
 */
function alerts_unbindFunctions(){
    
    $(document).unbind('EVENT_SET_PRODUCT_EPOCH', function(event,idElemTrigg,epoch,productId,higherValues){alerts_eventSetProductEpoch(idElemTrigg,epoch,productId,higherValues)});
    $(document).unbind('EVENT_SET_EPOCH',  function(event,idElemTrigg,epoch){alerts_eventSetEpoch(idElemTrigg,epoch)});
    
}
/*
 * Simply bind all events
 */
function alerts_bindFunctions(){
    
    $(document).bind('EVENT_SET_PRODUCT_EPOCH', function(event,idElemTrigg,epoch,productId,higherValues){alerts_eventSetProductEpoch(idElemTrigg,epoch,productId,higherValues)});
    $(document).bind('EVENT_SET_EPOCH', function(event,idElemTrigg,epoch){alerts_eventSetEpoch(idElemTrigg,epoch)});
}
