
const CGI = api.cgi
const skin = object{
    name: "Default Skin",
    pageTitle: {Default Page Title}
    out: api.io.output
}

var pathRoot = CGI.cgiRoot
skin.mainMenu = array [
    object {
        label: "Home",
        path: pathRoot
    },
    object {
        label: "Checkins",
        path: pathRoot+'manifest'
    },
    object {
        label: "Timeline",
        path: pathRoot+'timeline'
    },
    object {
        label: {Reports},
        path: pathRoot+'reports'
    },
    object {
        label: "Files",
        path: pathRoot+'dir'
    },
    object {
        label: 'Exception!',
        path: pathRoot+'exception'
    }
]
if( cgi.isLocalServer ){
    $skin.mainMenu.push object {
        label: 'ENV',
        path: pathRoot+'env'
    }
}

unset pathRoot

skin.outputMainMenu = proc(cgi){
    assert this.mainMenu
    var i, m
    const len = this.mainMenu.length()
    const out = api.io.output
    out({<div class='menu-main'>})
    for {i=0} {i < len} {i+=1} {
        m = this.mainMenu.(i)
        ('object' === typename m) || throw m
        out({<a class='menu-entry' href='}, m.path, {'>}, cgi.htmlEscape(m.label), {</a>})
    }
    out({</div>})
}

skin.outputOpener = proc(cgi){
    const out = api.io.output
    const F = cgi.getFossilInstance()
    cgi.setContentType('text/html; charset=utf-8')

    var title
    if('function' === typename this.pageTitle){
        title = this.pageTitle(cgi)
    }else if(this.pageTitle){
        title = this.pageTitle
    }else{
        title = cgi.request.ENV.PATH_INFO
    }
    assert title
    var pageTitle
    if(F){
        pageTitle = F.getProjectName() +': '+title
    }else{
        pageTitle = title
    }
    title = cgi.htmlEscape(title)
    pageTitle = cgi.htmlEscape(pageTitle)

    $out {<!DOCTYPE html><html><head>}
    var subPath = (cgi.request.ENV.PATH_INFO ? cgi.request.ENV.PATH_INFO.substr(1) : '')
    $out "<base href='" cgi.cgiRoot subPath "'/>"
    $out {<title>} pageTitle {</title>}
    if(true){
        $out '<link rel="stylesheet" href="' cgi.cgiRoot 'download/css/default.css" type="text/css" '\
            'media="screen" />'
    }else{
        $out {<style type='text/css'>}
        Fossil.file.passthrough(__FILE+".css")
        $out {</style>}
    }
    $out {</head><body>}

    $out {<div class='page-header'>}
    out({<div class='header-project-name'>}, cgi.getFossilInstance().getProjectName(), {</div>})
    if(title) {
        out({<div class='header-page-title'>}, title, {</div>})
    }
    this.outputMainMenu(cgi)
    $out {</div>}

}


skin.outputCloser = proc(cgi){
    $this.out {<footer class='footer'>}
    $this.out {This is the footer.}
    // BUG: "%1$0.4f".applyFormat(Fossil.runTimeMs().toDouble()/1000) is 0.0000 due to what appears to be a division bug
    $this.out ' Page generated using approximately ' "%1$0.3f".applyFormat(Fossil.runTimeMs().toDouble()/1000) ' seconds of CPU time.'
    // th1ish bug: CWAL_SCR_CANNOT_CONSUME: !+cgi.getVar('showSrc',0) (or (![-+]1), for that matter
    if(!(+cgi.getVar('showSrc',0)) && cgi.config.showPageSourceLinks){
        $this.out {<div><a href='?showSrc=1'>Source code for this page</a></div>}
    }    
    $this.out {</footer></body>}
}

skin /* this file's import() result must be the skin object */
