#!/mod/bin/jimsh

package require cgi
source /mod/webif/lib/setup
require settings.class

jqplugin enadis qtip2
jscss {} style.css
header

require epg.class spinner.class altrow epg_search totop system.class

set irinst [system pkginst ir]

set settings [settings]

if {[$settings chanchangenc]} {
	puts { <script type=text/javascript src=/js/chanchangenc.js></script> }
} else {
	jqplugin confirmAction
	puts { <script type=text/javascript src=/js/chanchange.js></script> }
}

if {[$settings service_style] eq "standard"} {
	set surl xservice
} else {
	set surl service
}

[spinner new {
	text "Loading EPG Information..."
	size "1.2em"
	style "margin: 1em;"
	}] start

require epg_popup

set now [clock seconds]
set stt [cgi_get stt $now]
set dst [expr $now - $stt]
if {$dst >= 0 && $dst < 1800} {
	set current 1
} elseif { $stt < [expr int ($now / 86400) * 86400] } {
	set stt $now
	set current 0
} else {
	set current 0
}

set favgroup [$settings channel_group]

set hours [cgi_get hours -]
if {$hours eq "-"} {
	set hours [expr 1.0 * [[settings] xepghours]]
}

if {$hours == 0} { set hours 4.0 }
if {$hours < 1} { set hours 1.0 }
if {$hours > 8} { set hours 8.0 }

set seconds $($hours * 3600)

set ostt $stt
set stt [expr int($stt / 1800.0) * 1800]

set ett $(int($stt + $seconds))
set offset 0
if {$current} {
	set offset $(($ostt - $stt) / 1800.0)
}

set chanpx 160
set eventpx 1000
set totalpx $($chanpx + $eventpx)
set contwidth $($totalpx + 50)

set hourpx $($eventpx / $hours)
set minpx $($hourpx / 60.0)
set secpx $($minpx / 60.0)

set start [clock milliseconds]
if {$::qepg} {
	set args [list -trange "$stt:$ett" -ljoin 1]
	if {$favgroup} {
		lappend args -fav $favgroup
	}
	set records [epg dbfetch dump {*}$args]
} else {
	require channelsort
	set records [channelsort [epg dbfetch dump -trange "$stt:$ett"]]
}
set got [clock milliseconds]

proc but {label time {active 1}} {
	puts -nonewline "<button style=\"margin-right: 2em\" tt=$time"
	if {$active} {
		puts -nonewline " class=\"nav\""
	} else {
		puts -nonewline " disabled class=\"nav ui-state-disabled\""
	}
	puts " active=$active>$label</button>"
}

puts "<!-- Hours $hours ($seconds) -->"

puts "<div id=timeline style=\"min-width: ${contwidth}px;\">"

######################################################################
# Time-jump bar

puts "<div class=timejump_outer>
<div class=timejump_label style=\"width: ${chanpx}px;\">
	Jump to:&nbsp;
</div>
<div class=timejump_bar>
"

set starthour [clock format $stt -format "%H"]
for {set i 0} {$i < 24} {incr i} {
	if {$i < 13} { set disp $i } else { set disp $($i - 12) }
	if {!$disp} { set disp 12 }

	set xclass ""

	if {$i == $starthour} {
		append xclass " tjstart"
	} elseif {$i > $starthour && $i < [expr $starthour + $hours]} {
		append xclass " tjother"
	}

	set tt [expr $starthour + $stt + 3600 * ($i - $starthour)]
	# Back to start of hour
	set tt $(3600 * int($tt / 3600))

	if {$xclass ne "" || $i == 0 || $i == 12} {
		append disp "<br>[clock format $tt -format %p]"
	}

	puts "<div tt=$tt class=\"timejump$xclass\">$disp</div>\n"
}

puts "</div>&nbsp;"
but "Now" $now
puts "</div>"

######################################################################
# Day-jump bar

puts "<div class=dayjump_outer>
<div class=dayjump_label style=\"width: ${chanpx}px;\"></div>
<div class=dayjump_bar>
"

set stt_time [clock format $stt -format "%H:%M"]
for {set i 0} {$i < 8} {incr i} {
	set day $($now + $i * 86400)
	if {!$i} {
		set s "Today"
	} else {
		set s [clock format $day -format "%A"]
	}
	set xclass ""
	if {[clock format $day -format "%j"] == \
	    [clock format $stt -format "%j"]} {
		append xclass " dayjump_current"
	}

	set tt [clock scan \
	    [clock format $day -format "%Y:%m:%d:$stt_time"] \
	    -format "%Y:%m:%d:%H:%M"]
	set alt [clock format $tt -format "%e %B"]

	puts "<div tt=$tt title=\"$alt\" class=\"dayjump$xclass\">$s</div>\n"
}

puts "</div>"
puts "<span class=\"footnote\">&nbsp;
    (Right-Click to select end hour)</span>"
puts "</div>"

######################################################################
# Time-line bar

# Displays the top border above the timeline.
puts "
    <div id=epgdate style=\"width: ${eventpx}px; margin-left: ${chanpx}px;\">
    </div>
<div class=xedate style=\"width: ${chanpx}px;\">
    [clock format $stt -format "%A %e %B"]&nbsp;&nbsp;</div>
"

for {set i 0} {$i < $hours * 2} {incr i} {
	puts "<div class=xetime id=xe$i
	    style=\"width: [expr $minpx * 30]px\">"
	puts "[clock format $($stt + 1800 * $i) -format "%H:%M"]</div>"
}

# Close timeline div
puts "</div>"

######################################################################
# Events

puts "
<script type=text/javascript>
var hours = $hours;
var stt = [expr $stt + 0];
var nowoffset = $offset;
</script>
"
jscss script.js
puts "
<div id=xepgnow></div>
<div id=xepg style=\"min-width: ${contwidth}px;\">
"

if {[$settings epgscroll]} {
	puts "<div id=xegrid style=\"height: 500px; overflow-y: auto;\">"
} else {
	puts "<div id=xegrid>"
}

set lim 0
set lcn 0
set bg "odd"

foreach e $records {
	set chnum [$e get channel_num]
	if {$chnum == 0} { continue }

	if {$chnum != $lcn} {
		puts "<div class=xechan style=\"width: ${chanpx}px;\">"

		if {$irinst} {
			puts "<a class=chanchange chan=$chnum href=#>"
		}
		puts "[$e channel_icon 30]&nbsp;"
		if {$irinst} { puts "</a>" }
		puts "$chnum
	    <a href=\"/epg/$surl.jim?service=[$e get service_id]\"
		title=\"[$e get channel_name]\">
	    [$e get channel_name]
	    </a></div>"

		set lcn $chnum
		set le 0
		set pos 0
		if {$bg eq "odd"} { set bg "even" } else { set bg "odd" }
	}

	if {[$e get event_id] eq ""} {
		# No data for this service and time period
		set px $(($ett - $stt) * $secpx)
		puts "<div class=xeprog style=\"width: ${px}px\">
		    <center><i>
			No EPG data for this time range...
		    </i></center></div>"
		continue
	}

	set es [$e get start]
	set ee [expr $es + [$e get duration]]

	if {$es < $le} continue

	if {$es < $stt} { set es $stt }
	if {$ee > $ett} { set ee $ett }

	set left [expr int(($es - $stt) * $secpx)]
	set right [expr int(($ee - $stt - 1) * $secpx)]
	if {$le == 0 && $es > $stt} {
		# EPG data carried over from previous day have been purged
		set pos $left
		puts "<div class=\"xeprog $bg\"
		    style=\"width: [expr $pos - 3]px;\" title=\"Unknown\">
		<i>Unknown</i></div>"
	}

	set px $($right - $left)
	if {$pos < $left} {
		set px $($px + $left - $pos - 1)
	} elseif {$pos > $left} {
		set px $($px - ($pos - $left - 1))
	}

	set le $ee

	incr pos $px

	$e process_sched

	set recopts 1
	if {[$e get series_crid] ne ""} { set recopts 2 }

	set perc [$e percent]
	set showing 0
	if {$perc > 0 && $perc < 100} { set showing 1 }

	set txt "[cgi_quote_html [$e get name]] <span>([\
	    clock format $es -format "%H:%M"] - [\
	    clock format $ee -format "%H:%M"])"
	if {$showing} { append txt " ($perc%)" }
	append txt "</span>"

	set htxt "[cgi_quote_html [$e get name]] ([\
	    clock format $es -format "%H:%M"] - [\
	    clock format $ee -format "%H:%M"])"
	if {$showing} { append hxt " ($perc%)" }

	set lbg $bg
	if {[$e scheduled]} { set lbg "blueshade" }

	puts "<div class=\"xeprog $lbg\"
	    style=\"width: [expr $px - 4]px;\" title=\"$htxt\">
	    <a class=event href=# xs=[$e get service_id]
		xe=[$e get event_id] sch=[$e get sched_type]
		rec=$recopts>"
	puts $txt
	puts "</a>"
	puts "</div>"
}
puts "<div id=keypos style=\"float: left; clear: left;\"></div>"
puts "</div>"
puts "</div>"

set earlier $($stt - 3600 * 4)

puts "

<div style=\"clear: both;\">
<small><button id=epgswitch>
	Switch to standard now/next display.
</button></small>
</div>

<a href=/settings/settings.jim>
<img border=0 height=14 src=/images/421_1_00_CH_Title_2R_Arrow.png>
Visit settings to change EPG options.
</a><br>
"

epg cleanup

set end [clock milliseconds]
puts "<font class=footnote>
    Retrieved in: [expr ($got - $start) / 1000.0] seconds.
    </font>"

footer

