<jsp:useBean id="historyEnd" scope="session" class="java.util.Date" />
<jsp:setProperty name="historyEnd" property="*"/>
<c:if test="${!empty param.now}">
    <% historyEnd.setTime(System.currentTimeMillis()); %>
</c:if>
<c:if test="${!empty param.length}">
    <c:set var="historyLength" value="${param.length*60*1000}" scope="session"/>
</c:if>
<c:if test="${empty sessionScope.historyLength}">
    <c:set var="historyLength" value="${12*60*60*1000}" scope="session"/>
</c:if>
<%
long len = -Long.parseLong(pageContext.getAttribute("historyLength", PageContext.SESSION_SCOPE)+"");
pageContext.setAttribute("historyItems", serviceBean.getHistory(historyEnd.getTime(), historyEnd.getTime()+len*3/2));
Map printerEvents = new TreeMap();
pageContext.setAttribute("printerHistory", printerEvents);
pageContext.setAttribute("start", new Long(historyEnd.getTime()+len));
Set watches = getWatches(serviceBean);
%>
<!--partition events-->
<%
int historySize = 0;
%>
<c:forEach var="historyItem" items="${historyItems}">
    <c:set var="serviceItem" value="${historyItem.item}"/>
    <%
    ServiceItem item = (ServiceItem )pageContext.getAttribute("serviceItem");
    URI uri = getURI(item.attributeSets);
    if(watches.contains(uri)) {
        Vector v = (Vector )printerEvents.get(uri);
        if(v==null) {
                v=new Vector();
                printerEvents.put(uri, v);
                }
        AdminBean.HistoryItem historyItem = (AdminBean.HistoryItem )pageContext.getAttribute("historyItem");
        v.add(0, historyItem);  //add in reverse order
        historySize++;
        if(v.size()>1) {
           AdminBean.HistoryItem hi = (AdminBean.HistoryItem )v.get(v.size()-2);
           if(hi.getTime()<=historyEnd.getTime()+len) {
                v.remove(v.size()-1);
                historySize--;
           }
        }
    }
    %>
</c:forEach>
<table width="100%" border=0>
<tr>
<td>
<form action="selector.jsp">
<input type=hidden name="now" value="true">
<input type=hidden name="view" value="history">
<input type=submit value="Now">
</form>
</td>
<td align=left>
<form action="selector.jsp">
<input type=hidden name="view" value="history">
<select name="month">
<c:forTokens items="0,1,2,3,4,5,6,7,8,9,10,11" delims="," var="value">
<option value="<c:out value='${value}'/>"
<c:if test='${value==historyEnd.month}'>SELECTED</c:if>
><c:out value='${value+1}'/></option>
</c:forTokens>
</select> /
<select name="date">
<c:forTokens items="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31" delims="," var="value">
<option value="<c:out value='${value}'/>"
<c:if test='${value==historyEnd.date}'>SELECTED</c:if>
><c:out value='${value}'/></option>
</c:forTokens>
</select> 
<select name="hours">
<c:forTokens items="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23" delims="," var="value">
<option value="<c:out value='${value}'/>"
<c:if test='${value==historyEnd.hours}'>SELECTED</c:if>
><c:out value='${value}'/></option>
</c:forTokens>
</select>:
<select name="minutes">
<c:forTokens items="00,10,20,30,40,50" delims="," var="value">
<option value="<c:out value='${value}'/>"
<c:if test='${value==historyEnd.minutes-historyEnd.minutes%10}'>SELECTED</c:if>
><c:out value='${value}'/></option>
</c:forTokens>
</select>
<input type=submit value="Date">
</form>
</td>
<td align=right>
<%
pageContext.setAttribute("lengthParams", new Object[] {
	new String[] {"10", "10 Mins"},
	new String[] {"30", "Half Hour"},
	new String[] {"60", "1 Hour"},
	new String[] {"180", "3 Hours"},
	new String[] {"720", "12 Hours"},
	new String[] {"1440", "1 Day"},
	new String[] {"2880", "2 Days"},
	new String[] {"7200", "5 Days"}
	});
%>
<form action="selector.jsp">
<input type=hidden name="view" value="history">
<select name="length">
<c:forEach var="lparam" items="${lengthParams}">
<option value="<c:out value='${lparam[0]}'/>"
<c:if test='${lparam[0]==sessionScope.historyLength/(60*1000)}'>SELECTED</c:if>
>
<c:out value='${lparam[1]}'/>
</option>
</c:forEach>
</select>
<input type=submit value="Length">
</form>
</td>
</tr>
<c:forEach var="printer" items="${printerHistory}">
	<tr>
<td nowrap width="0%">
	<c:forTokens items="${printer.key.host}" delims="." end="0" var="host">
	<c:out value="${host}"/>
	</c:forTokens>
</td>
	<td width="100%" colspan=2>
		<table height="12" width="100%" border=0 cellspacing=0><tr>
		<c:set var="last" value="${historyEnd.time}"/>
                <c:forEach var="historyItem" items="${printer.value}">

                        <c:choose>
                        <c:when test="${historyItem.type==3}">
<%
AdminBean.HistoryItem item = (AdminBean.HistoryItem )pageContext.getAttribute("historyItem");
pageContext.setAttribute("entries", item.getItem().attributeSets);

%>
                            <%@include file="include/status_message.jsp"%>
                            <c:set var="color" value="${color}"/>
                            <c:set var="title" value="${errors}"/>
			</c:when>
                        <c:when test="${historyItem.type==1}">
                            <c:set var="color" value="gray"/>
                            <c:set var="title" value="online"/>
                        </c:when>
                        <c:when test="${historyItem.type==2}">
                            <c:set var="color" value="red"/>
                            <c:set var="title" value="out of service"/>
                        </c:when>
			</c:choose>
			<c:set var="width" value='${100.0*(last-historyItem.time)/historyLength}'/>
                        <c:if test="${(width>=1.0) || (width>0.1 && color=='green') || (width>0.01 && color=='yellow') || (width>0.001 && color=='red')}">
                            <c:if test="${width<1.0}"><c:set var="width" value="1.0"/></c:if>
                            <td bgcolor="<c:out value='${color}'/>"
                            width="<c:out value='${width}'/>%"
                            ><span title="<c:out value='${title}'/>" class="popup"><table border=0 cellspacing=0 width="100%" height="100%"><tr><td></td></tr></table></span></td>
                            <c:set var="last" value="${last-width*historyLength/100}"/>
                        </c:if>
        </c:forEach>
	<c:set var="width" value='${100*(last-start)/historyLength}'/>
        <c:if test="${width>1.0 && last>start}">
		<td bgcolor="black" width="<c:out value='${100*(last-start)/historyLength}'/>%">
		<span title="no info available" class="popup"><table border=0 cellspacing=0 witdh="100%" height="100%"><tr><td></td></tr></table></span>
		</td>
        </c:if>
		</tr></table>
	</td>
	</tr>
</c:forEach>
<table>
<%=historySize+" entries"%>
