/* render date */
TP.render_date = function(v, td, item) {
    if(v == 0) { return 'never'; }
    return TP.date_format(v);
}

/* just add title */
TP.add_title  = function(v, td, item, row, col, store, view) {
    return "<div title='"+v+"'>"+v+"<\/div>";
}

/* render icon image */
TP.render_icon = function(v, td, item, row, col, store, view) {
    return "<img src='[% logo_path_prefix %]"+v+"' border='0' width='20' height='20' alt='"+item.data.icon_image_alt+"' title='"+item.data.icon_image_alt+"'>";
}

/* render log icon */
TP.render_icon_log = function(v, td, item) {
    return "<div style=\"width:20px;height:20px;background-image:url(../themes/[% theme %]/images/"+v+");background-position:center center;background-repeat:no-repeat;\">&nbsp;<\/div>";
}

/* render site status icon */
TP.render_icon_site = function(v, td, item) {
    title="";
    if(item.data.runtime=="") {
        title="title=\""+item.data.version+"\"";
    };
    return "<div class=\"clickable\" "+title+" onclick=\"TP.toggleBackend(this, \'"+item.data.id+"\')\" style=\"width:20px;height:20px;background-image:url(../plugins/panorama/images/"+v+");background-position:center center;background-repeat:no-repeat;\">&nbsp;<\/div>";
}

/* render enabled / disabled switch */
TP.render_enabled_switch = function(v, td, item) {
    if(v==1) {
        return "On";
    };
    return "Off";
}

/* render yes / no */
TP.render_yes_no = function(v, td, item) {
    if(v==1) {
        return "Yes";
    };
    return "No";
}

/* render On / Off */
TP.render_on_off = function(v, td, item) {
    if(v==1) {
        return "On";
    };
    return "Off";
}

/* render host status */
TP.render_host_status = function(v, td, item) {
    var state;
    if(item.data.has_been_checked==0) {
        state = 'Pending';
    }
    else if(v==0) { state = 'Up';          }
    else if(v==1) { state = 'Unreachable'; }
    else if(v==2) { state = 'Down';        }

    td.tdCls = state.toUpperCase();
    return state;
}

/* render service status */
TP.render_service_status = function(v, td, item) {
    var state;
    if(item.data.has_been_checked==0) {
        state = 'Pending';
    }
    else if(v==0) { state = 'Ok';       }
    else if(v==1) { state = 'Warning';  }
    else if(v==2) { state = 'Critical'; }
    else if(v==3) { state = 'Unknown';  }

    td.tdCls = state.toUpperCase();
    return state;
}

/* render status totals */
TP.render_statuscount = function(v, td, item, row, col, store, view) {
    if(v > 0) {
        td.tdCls = item.data.state.toUpperCase();
    }
    return v;
}

/* render hostname in service grid */
TP.render_service_host = function(v, td, item, row, col, store, view) {
    if(row == 0 || view.lastHost == undefined || view.lastHost != v) {
        view.lastHost = v;
        var hstate = 'Up';
        if(item.data.host_state==1) { hstate = 'Unreachable'; }
        if(item.data.host_state==2) { hstate = 'Down';        }
        td.tdCls = 'BG_'+hstate.toUpperCase();
        return TP.render_clickable_host(v, td, item, row, col, store, view);
    }
    return '';
}

/* render last check date */
TP.render_last_check = function(v, td, item) {
    if(item.data.last_check) {
        return TP.date_format(item.data.last_check);
    } else {
        return 'never';
    }
}

/* render duration */
TP.render_duration = function(v, td, item) {
    var d   = new Date();
    var now = Math.floor(d.getTime() / 1000);
    if(item.data.last_state_change) {
        return TP.duration(now - item.data.last_state_change);
    } else {
        var peer_key      = item.data.peer_key;
        var program_start = initial_backends[peer_key].program_start;
        return TP.duration(now - program_start)+'+';
    }
}

/* render current attempt */
TP.render_attempt = function(v, td, item) {
    var ret = item.data.current_attempt + '/' + item.data.max_check_attempts;
    [%+ IF show_notification_number %]
    if(item.data.current_notification_number > 0) {
       ret = ret + ' #'+ item.data.current_notification_number;
    }
    if(item.data.first_notification_delay > 0) {
        var first_remaining = TP.calculate_first_notification_delay_remaining(item.data);
        if(first_remaining >= 0) {
            ret = ret + ' ~'+first_remaining+'min';
        }
    }
    [% END +%]
    return ret;
}

/* render check type */
TP.render_check_type = function(v, td, item) {
    if(v==0) { return "Active" }
    return "Passive";
}

/* render value of systat */
TP.render_systat_value = function(v, td, item, row, col, store, view) {
    if(item.data.cat == 'CPU') {
        return v + '%';
    }
    if(item.data.cat == 'Memory') {
        return v + 'MB';
    }
    return v;
}

/* render graph of systat */
TP.render_systat_graph = function(v, td, item, row, col, store, view) {
    if(item.data.warn == '') {
        return '';
    }
    var val = item.data.value;
    if(val > item.data.max) { val = item.data.max; }
    var perc   = Math.floor(val/item.data.max*100);
    var status = 'ok';
    if(item.data.value > item.data.warn) { status = 'warn'; }
    if(item.data.value > item.data.crit) { status = 'crit'; }
    td.tdCls = 'systat_graph';
    return "<div><div style='width:"+perc+"%; height: 15px;' class='systat_"+status+"'>&nbsp;<\/div><\/div>";
}

/* render plugin output */
TP.render_plugin_output = function(v, td, item) {
    var type = 'Host';
    if(item.data.description != undefined) { type = 'Service'; }
    if(item.data.has_been_checked == 0) {
        if(item.data.active_checks_enabled == 0) {
            return(type+' is not scheduled to be checked...');
        } else {
            return(type+' check scheduled for '+TP.date_format(item.data.next_check));
        }
    }
    return v;
}

/* render host icons in service grid */
TP.render_host_service_icons = function(v, td, item, row, col, store, view) {
    var d = item.data;
    if(row == 0 || view.lastHostIcon == undefined || view.lastHostIcon != d.host_display_name) {
        view.lastHostIcon = d.host_display_name;
    } else {
        return '';
    }
    var data = {
        notifications_enabled:    d.host_notifications_enabled,
        check_type:               d.host_check_type,
        active_checks_enabled:    d.host_active_checks_enabled,
        accept_passive_checks:    d.host_accept_passive_checks,
        is_flapping:              d.host_is_flapping,
        acknowledged:             d.host_acknowledged,
        comments:                 d.host_comments,
        scheduled_downtime_depth: d.host_scheduled_downtime_depth,
        action_url_expanded:      d.host_action_url_expanded,
        notes_url_expanded:       d.host_notes_url_expanded,
        icon_image_expanded:      d.host_icon_image_expanded,
        icon_image_alt:           d.host_icon_image_alt,
        custom_variable_names:    d.host_custom_variable_names,
        custom_variable_values:   d.host_custom_variable_values
    };
    return TP.render_host_icons(v, td, item, row, col, store, view, data);
}

/* render host icons */
TP.render_host_icons = function(v, td, item, row, col, store, view, data) {
    var icons = '';
    var d     = item.data;
    if(data != undefined) {
        d = data;
    }
    if(d.notifications_enabled == 0)   { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/ndisabled.gif" alt="Notifications for this host have been disabled" title="Notifications for this host have been disabled" border="0" height="20" width="20">'; }

    [%+ IF strict_passive_mode +%]
        if(d.check_type == 0 && d.active_checks_enabled == 0) { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/disabled.gif" alt="Checks of this host have been disabled" title="Checks of this host have been disabled" border="0" height="20" width="20">'; }
        if(d.check_type == 1 && d.accept_passive_checks == 0) { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/disabled.gif" alt="Checks of this host have been disabled" title="Checks of this host have been disabled" border="0" height="20" width="20">'; }
        if(d.check_type == 1 && d.accept_passive_checks == 1) { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/passiveonly.gif" border="0" width="20" height="20" alt="Active checks of the host have been disabled - only passive checks are being accepted" title="This host is checked passive">'; }
    [%+ ELSE +%]
        if(d.active_checks_enabled == 0 && d.accept_passive_checks == 0) { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/disabled.gif" alt="Checks of this host have been disabled" title="Checks of this host have been disabled" border="0" height="20" width="20">'; }
        else if(d.active_checks_enabled == 0) { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/passiveonly.gif" border="0" width="20" height="20" alt="Active checks of the host have been disabled - only passive checks are being accepted" title="Active checks of the host have been disabled - only passive checks are being accepted">'; }
    [%+ END +%]

    if(d.is_flapping)                  { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/flapping.gif" alt="This host is flapping between states" border="0" height="20" width="20">'; }
    if(d.acknowledged)                 { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/ack.gif" alt="This host problem has been acknowledged" border="0" height="20" width="20">'; }
    if(d.comments.length > 0)          { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/comment.gif" alt="This host has '+d.comments.length+' comments associated with it" border="0" height="20" width="20">'; }
    if(d.scheduled_downtime_depth > 0) { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/downtime.gif" alt="This host is currently in a period of scheduled downtime" border="0" height="20" width="20">'; }
    if(d.action_url_expanded )         { icons += "<a href='"+d.action_url_expanded+"' target='_blank'><img src='[% url_prefix %]thruk/themes/[% theme %]/images/"+action_icon(d, '[% host_action_icon %]')+"' border='0' width='20' height='20' alt='Perform Extra Host Actions' title='Perform Extra Host Actions'><\/a>"; }
    if(d.notes_url_expanded )          { icons += "<a href='"+d.notes_url_expanded+"' target='_blank'><img src='[% url_prefix %]thruk/themes/[% theme %]/images/notes.gif' border='0' width='20' height='20' alt='View Extra Host Notes' title='View Extra Host Notes'><\/a>"; }
    if(d.icon_image_expanded )         { icons += "<img src='[% logo_path_prefix %]"+d.icon_image_expanded+"' border='0' width='20' height='20' alt='"+d.icon_image_alt+"' title='"+d.icon_image_alt+"'>"; }
    return icons;
}

/* render service icons */
TP.render_service_icons = function(v, td, item, row, col, store, view) {
    var icons = '';
    var d     = item.data;
    if(d.notifications_enabled == 0)   { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/ndisabled.gif" alt="Notifications for this service have been disabled" title="Notifications for this service have been disabled" border="0" height="20" width="20">'; }

    [%+ IF strict_passive_mode +%]
        if(d.check_type == 0 && d.active_checks_enabled == 0) { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/disabled.gif" alt="Checks of this service have been disabled" title="Checks of this service have been disabled" border="0" height="20" width="20">'; }
        if(d.check_type == 1 && d.accept_passive_checks == 0) { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/disabled.gif" alt="Checks of this service have been disabled" title="Checks of this service have been disabled" border="0" height="20" width="20">'; }
        if(d.check_type == 1 && d.accept_passive_checks == 1) { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/passiveonly.gif" border="0" width="20" height="20" alt="Active checks of the service have been disabled - only passive checks are being accepted" title="This service is checked passive">'; }
    [%+ ELSE +%]
        if(d.active_checks_enabled == 0 && d.accept_passive_checks == 0) { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/disabled.gif" alt="Checks of this service have been disabled" title="Checks of this service have been disabled" border="0" height="20" width="20">'; }
        else if(d.active_checks_enabled == 0) { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/passiveonly.gif" border="0" width="20" height="20" alt="Active checks of the service have been disabled - only passive checks are being accepted" title="Active checks of the service have been disabled - only passive checks are being accepted">'; }
    [%+ END +%]

    if(d.is_flapping)                  { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/flapping.gif" alt="This service is flapping between states" border="0" height="20" width="20">'; }
    if(d.acknowledged)                 { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/ack.gif" alt="This service problem has been acknowledged" border="0" height="20" width="20">'; }
    if(d.comments.length > 0)          { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/comment.gif" alt="This service has '+d.comments.length+' comments associated with it" border="0" height="20" width="20">'; }
    if(d.scheduled_downtime_depth > 0) { icons += '<img src="[% url_prefix %]thruk/themes/[% theme %]/images/downtime.gif" alt="This service is currently in a period of scheduled downtime" border="0" height="20" width="20">'; }
    if(d.action_url_expanded )         { icons += "<a href='"+d.action_url_expanded+"' target='_blank'><img src='[% url_prefix %]thruk/themes/[% theme %]/images/"+action_icon(d, '[% service_action_icon %]')+"' border='0' width='20' height='20' alt='Perform Extra Service Actions' title='Perform Extra Service Actions'><\/a>"; }
    if(d.notes_url_expanded )          { icons += "<a href='"+d.notes_url_expanded+"' target='_blank'><img src='[% url_prefix %]thruk/themes/[% theme %]/images/notes.gif' border='0' width='20' height='20' alt='View Extra Service Notes' title='View Extra Service Notes'><\/a>"; }
    if(d.icon_image_expanded )         { icons += "<img src='[% logo_path_prefix %]"+d.icon_image_expanded+"' border='0' width='20' height='20' alt='"+d.icon_image_alt+"' title='"+d.icon_image_alt+"'>"; }
    return icons;
}

/* make host clickable */
TP.render_clickable_host = function(v, td, item, row, col, store, view) {
    td.tdCls += ' clickable';
    var host  = item.data.host_name ? item.data.host_name : item.data.name;
    return "<div class='clickable' onClick=\"TP.add_panlet({type:'TP.PanletHost', conf: { xdata: { host: '"+host+"'}}})\">"+v+"<\/div>";
}

/* make service description clickable */
TP.render_clickable_service = function(v, td, item, row, col, store, view) {
    td.tdCls = 'clickable';
    return "<div class='clickable' onClick=\"TP.add_panlet({type:'TP.PanletService', conf: { xdata: { host: '"+item.data.host_name+"', service: '"+item.data.description+"'}}})\">"+v+"<\/div>";
}

/* render url */
TP.render_url = function(v, td, item, row, col, store, view) {
    return "<a href='"+v+"' target='_blank'><img src='[% url_prefix %]thruk/themes/[% theme %]/images/action.gif' border='0' width='20' height='20' alt='Perform Extra Service Actions' title='Perform Extra Service Actions'><\/a>";
}

/* render gearman queues */
TP.render_gearman_queue = function(v, td, item, row, col, store, view) {
    td.tdCls = '';
    if(item.data.worker * 10 <= item.data.waiting) {
        td.tdCls = 'BG_WARNING';
    }
    if(item.data.worker * 20 <= item.data.waiting) {
        td.tdCls = 'BG_DOWN';
    }
    if(item.data.worker == 0 && item.data.waiting > 0) {
        td.tdCls = 'BG_DOWN';
    }
    if(item.data.waiting == 0) {
        td.tdCls = '';
    }
    return v;
}

/* create performance bar */
TP.render_perfbar = function(v, td, item, row, col, store, view) {
    var r =  perf_table(false, item.data.state, item.data.plugin_output, item.data.perf_data, item.data.check_command, "");
    if(r == false) { return ""; }
    td.tdCls = "less_padding";
    return r;
}

/* format timestamp */
TP.date_format = function(t, f) {
    var d = new Date(t*1000);
    if(f != undefined) {
        return Ext.Date.format(d, f);
    }
    if(Ext.Date.format(new Date(), "Y-m-d") == Ext.Date.format(d, "Y-m-d")) {
        return Ext.Date.format(d, "H:i:s");
    }
    return Ext.Date.format(d, "Y-m-d H:i:s");
}

/* format duration */
TP.duration = function(duration) {
    var minus = '';
    if(duration < 0) {
        duration = duration * -1;
        minus    = '-';
    }

    var days    = 0;
    var hours   = 0;
    var minutes = 0;
    var seconds = 0;
    if(duration >= 86400) {
        days     = Math.floor(duration/86400);
        duration = duration%86400;
    }
    if(duration >= 3600) {
        hours    = Math.floor(duration/3600);
        duration = duration%3600;
    }
    if(duration >= 60) {
        minutes  = Math.floor(duration/60);
        duration = duration%60;
    }
    seconds = duration;

    return(""+minus+days+"d "+hours+"h "+minutes+"m "+seconds+"s");
}

/* calculate remaining time till next notification */
TP.calculate_first_notification_delay_remaining = function(obj) {
    if(obj.state == 0) { return -1; }

    var first_problem_time = -1;
    if(obj.last_time_ok != undefined) {
        first_problem_time = obj.last_time_ok;
        if((obj.last_time_warning < first_problem_time) && (obj.last_time_warning > obj.last_time_ok)) {
            first_problem_time = obj.last_time_warning;
        }
        if((obj.last_time_unknown < first_problem_time) && (obj.last_time_unknown > obj.last_time_ok)) {
            first_problem_time = obj.last_time_unknown;
        }
        if((obj.last_time_critical < first_problem_time) && (obj.last_time_critical > obj.last_time_ok)) {
            first_problem_time = obj.last_time_critical;
        }
    }
    else if(obj.last_time_up != undefined) {
        first_problem_time = obj.last_time_up;
        if((obj.last_time_down < first_problem_time) && (obj.last_time_down > obj.last_time_up)) {
            first_problem_time = obj.last_time_down;
        }
        if((obj.last_time_unreachable < first_problem_time) && (obj.last_time_unreachable > obj.last_time_up)) {
            irst_problem_time = obj.last_time_unreachable;
        }
    }
    if(first_problem_time == 0) { return -1; }
    var d = new Date;
    var t = Math.floor(d.getTime() / 1000);
    var remaining_min = Math.floor((t - first_problem_time) / 60);
    if(remaining_min > obj.first_notification_delay) {
        return -1;
    }

    return(obj.first_notification_delay - remaining_min);
}

/* return action info icon */
function action_icon(o, action_icon) {
    for(var nr in o.custom_variable_names) {
        if(o.custom_variable_names[nr] == 'ACTION_ICON') {
            return o.custom_variable_values[nr];
        }
    }
    return action_icon;
}
