#!/mod/bin/jimsh

source /mod/var/mongoose/lib/setup
require altrow rsv.class progressbar epg.class

puts {
<div id=dialogue></div>
<script type=text/javascript>

$.tablesorter.addParser({
	id: 'programme',
	is: function () { return false; },
	format: function(s) {
		return s.toLowerCase().replace(/---/, 'zzz');
	},
	type: 'text'
});

$.tablesorter.addParser({
	id: 'date',
	is: function () { return false; },
	format: function(s) {
		var d = new Date(s.substring(0, s.length - 4));
		return d.getTime();
	},
	type: 'numeric'
});

$(document).ready(function() {
	$('table.tablesorter').tablesorter({
	    headers: {
		1: { sorter: false },
		3: { sorter: 'programme' },
		4: { sorter: 'date' },
		5: { sorter: 'date' },
		5: { sorter: false },
		6: { sorter: false },
		7: { sorter: false }
	    }
	});
	$('table.tablesorter thead th').filter('[class!=header]')
	    .addClass('headerplain');
});

$(document).ready(function() {
	function docancel()
	{
		var table = $('#dialogue').attr('table');
		var slot = $('#dialogue').attr('slot');

		if (confirm('Really remove scheduled event?'))
		{
			$.get('/cgi-bin/sched_cancel.jim?slot=' + slot +
			    '&table=' + table, function() {
				window.location.reload(true);
			});
			$('#dialogue').dialog('close');
		}
	}

	var $buttons1 = {
	    "Close" : function() {$(this).dialog('close');}
	};
	var $buttons2 = $.extend(
	    {"Cancel Event": function() { docancel() }},
	    $buttons1);

	var $dialog = $('#dialogue').dialog({
		title: "Schedule Details",
		modal: false, autoOpen: false,
		height: 500, width: 700,
		show: 'scale', hide: 'fade',
		draggable: true, resizable: true,
		buttons: $buttons2,
		close: function(e,u) { $('#dialogue').empty().html(
		    '<img src="/img/loading.gif" alt="loading">'); }
	});

	function schedpopup(e, o)
	{
		e.preventDefault();
		var slot = o.attr('slot');
		var table = o.attr('table');
		$('#dialogue').attr('slot', slot).attr('table', table);

		var url = '/cgi-bin/sched_info.jim?slot=' + slot +
		    '&table=' + table;
		$('#dialogue').load(url);
		$dialog.dialog('open');
	}
	$('a.schedule').click(function(e) { schedpopup(e, $(this)) });

	$('table.tablesorter tbody tr').hover(
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); });
});
</script>
}

proc eventheader {{table TBL_RESERVATION} note} {
	puts {
		<fieldset style="display: inline; padding: 1em">
		<legend>
	}
	puts "<h3>$note</h3>"
	puts {
		</legend>
		<table class="borders tablesorter">
		<thead>
		<tr>
	}

	if {$table eq "pending" } { puts "<th>Action</th>" }

	puts {
			<th></th>
			<th>&nbsp;</th><th>Channel</th>
			<th>Programme</th>
			<th>Start Time</th>
			<th>Duration</th>
			<th>Mode</th>
		</tr>
		</thead>
		<tbody>
	}
}

proc eventrow {event {table TBL_RESERVATION}} {
	set name [$event name]

	altrow

	set rsvicon [$event icon]
	set RKIcon [$event RKIcon]
	if {[$event get ucRecKind] == 4} {
		set series 1
	} else {
		set series 0
	}

	if {$rsvicon ne ""} {
		set rsvicon "<img src='/images/$rsvicon' height=20>"
		if {[$event get ersvtype] == 3} {
			if {[$event padded]} {
				set padding "<- [expr [$event get ulPreOffset] / 60], [expr [$event get ulPostOffset] / 60] ->"
				append rsvicon \
				    "<img src=/img/pad.png height=20
				     title=\"$padding\" alt=\"$padding\">"
			} else {
				append rsvicon \
				    "<img src=/img/ar.png height=20>"
			}
		}
	}
	if {$RKIcon ne ""} {
		set RKIcon "<img src='/images/$RKIcon' height=20>"
	}

	if {$table eq "pending"} {
		switch [$event get action] {
		    0 { set icon "add" }
		    1 { set icon "close" }
		    2 { set icon "ar" }
		    3 { set icon "pad" }
		}
		puts "<td align=center>
		    <img class=va width=30 src=/img/$icon.png></td>"
	}

	puts "<td>[$event get ulslot]</td>"

	if {[$event get usLcn] ne ""} {
		puts "
		<td>
			[epg channelicon [$event channel_name] 50]
		</td>
		<td nowrap>[$event get usLcn]<br>
			[$event channel_name]</td>
		"
	} else {
		puts "<td>&nbsp;<br><br></td><td>&nbsp;</td>"
	}

	puts "<td nowrap>
		<a slot=[$event get ulslot] table=$table
		href=# class=schedule>$name</a>
		</td>"

	set s [$event get nsttime]
	set d [$event get nduration]
	set e $($s + $d)
	set n [clock seconds]

	set ds "[clock format $s -format "%a %d %b %Y"]<br>
	    [clock format $s -format "%H:%M %Z"]"

	if { $n > $e && [$event get ersvtype] <= 3} {
		puts "<td nowrap class=va
			alt=\"$ds\" title=\"$ds\">
			--- -- --- ----<br>--:--"
		puts "</td><td>--:--:--</td>"
	} else {
		puts "<td nowrap class=\"va"
		if {$n > $e} { puts " blood" }
		puts "\">$ds"
		if {$d > 0 && $n > $s && $n < $e} {
			puts "<br>"
			set perc [expr [expr $n - $s] * 100 / $d]
			puts "<img class=va
			    src=/images/745_1_11_Video_1REC.png>"
			puts [progressbar $perc]
		}
		puts "</td><td>"
		puts "[clock format [$event get nduration] -format %T]</td>"
	}

	puts "<td nowrap>$rsvicon $RKIcon</td><td>"
	if {[$event get aulEventToRecordInfo] != ""} {
		puts "<img src=/img/lightning.png
		    alt=\"Accepted by Humax\"
		    title=\"Accepted by Humax\">"
	}
	puts "</td>";
	puts "</tr>"
}

proc eventfooter {} {
	puts "</tbody></table></fieldset>"
}

set events [rsv list pending]
if {[llength $events] > 0} {
	eventheader pending "Pending Scheduled Events"
	foreach event $events {eventrow $event pending}
	eventfooter
	puts {
		<small>
		<button onclick="window.location='/cgi-bin/db.jim?db=rsvp.db&tab=pending';">
		Raw database view
		</button>
		</small>
	}
}

set events [rsv list]
eventheader "live" "Scheduled Events"
foreach event $events {eventrow $event}
eventfooter

puts {
	<br>
	<small>
	<button onclick="window.location='/backup.shtml';">
		Backup/Restore Scheduled Recordings/Events
	</button>
	<button onclick="window.location='/cgi-bin/db.jim?db=rsv.db&tab=TBL_RESERVATION';">
		Raw database view
	</button>
	<script type=text/javascript>
		$('button').button();
	</script>
}