/*
 * rfengine.js
 *
 * JavaScript engine for www.redfelineninja.org.uk
 *
 * Copyright (C) 2006 Daniel Thompson <daniel@redfelineninja.org.uk> 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

var _RFEngine_lookup = new Object
_RFEngine_lookup['About'] = 'about/index.html'
_RFEngine_lookup['Art and Photos'] = 'art/index.html'
_RFEngine_lookup['Software'] = 'software/index.html'
_RFEngine_lookup['Woodcraft'] = 'woodcraft/index.html'
_RFEngine_lookup['Writings'] = 'writings/index.html'

function _RFEngine_sidebar()
{
	var v = this.view

	v.write('<div class="sidebar">')
	v.write('<h2><a href="'+this.root+'index.html">Home</a></h2>')

  v.write('<h2><a href="'+this.root+'about/index.html">About</a></h2>')
	if (this.id[0] == 'About') {
		v.write('\
			<ul>\
			<li><a href="'+this.root+'about/cv.html">Curriculum Vitae</a></li>\
			</ul>')
	}

	v.write('<h2><a href="'+this.root+'art/index.html">Art and Photos</a></h2>')
	if (this.id[0] == 'Art and Photos') {
		v.write('\
			<ul>\
			<li><a href="'+this.root+'witherwye/index.html">Family Life</a></li>\
			<li><a href="'+this.root+'art/infant_inside.html">Infant Inside</a></li>\
			</ul>')
	}

	v.write('<h2><a href="'+this.root+'software/index.html">Software</a></h2>')
	if (this.id[0] == 'Software') {
		v.write('\
			<ul>\
			<li><a href="risk_arena.html">The Risk Arena</a></li>\
			<li><a href="rfbackup.html">Red Feline Backup</a></li>\
			<li><a href="rftidy.html">Red Feline Tidy</a></li>\
			<li><a href="pdc.html">PDC</a></li>\
			<li><a href="prc-eclipse.html">prc-eclipse</a></li>\
			<li><a href="pydar.html">Pydar</a></li>\
			<li><a href="snippets.html">Snippets</a></li>\
			<li><a href="http://tzones.sf.net">TZones</a></li>\
			</ul>')
	}
	
	v.write('<h2><a href="'+this.root+'woodcraft/index.html">Woodcraft</a></h2>')	
	if (this.id[0] == 'Woodcraft') {
		v.write('\
			<ul>\
			<li><a href="'+this.root+'woodcraft/games/index.html">Co-operative Games</a></li>\
			<li><a href="'+this.root+'woodcraft/craft_x3/craft_x3.html">Craft, Craft, Craft</a></li>\
			<li><a href="'+this.root+'woodcraft/dexitow/index.html">DexiTow bike trailer</a></li>\
			<li><a href="'+this.root+'woodcraft/street_youth_hostel.html">Street YHA</a></li>\
			<li><a href="'+this.root+'woodcraft/tiley_lamp.html">Tiley lamp HOWTO</a></li>\
			</ul>')
	}

	v.write('<h2><a href="'+this.root+'writings/index.html">Writings</a></h2>')
	if (this.id[0] == 'Writings') {
		v.write('\
			<ul>\
			<li><a href="'+this.root+'writings/howto.html">Everything HOWTO</a></li>\
			<li><a href="'+this.root+'writings/safe_hex.html">Safe Hex</a></li>\
			</ul>')
	}

	v.write('\
<a href="http://www.fsf.org/register_form?referrer=1529">\
<img class="nofloat" align="center" src="'+this.root+'images/fsf_member_1529-88x31.png" alt="[FSF Associate Member #1529]" title="Click here to join the Free Software Foundation" width="88" height="31" /></a>')
	v.write('</div>')
}

function _RFEngine_open_content()
{
	if (this.has_sidebar)
		this.view.write('<div class="content">\n')
	else
		this.view.write('<div class="purecontent">\n')
}

function _RFEngine_close_content()
{
	this.view.write('</div>');
}

function _RFEngine_header()
{
	var v = this.view;

	v.write('<div class="header">')
	v.write('<h1>')
	// help folks who use offline copies (including me) find the original again
	v.write('<a href="http://redfelineninja.org.uk">redfelineninja.org.uk</a>')

	for (i in this.id) {
		var id = this.id[i]

		v.write(" &gt; ")
		if (i == 0)
			v.write('<a href="'+this.root+this.lookup[id]+'">')
		v.write(id)
		if (i == 0)
			v.write('</a>')
	}

	v.write('</h1>')
	v.write('</div>')

	if (this.has_sidebar)
		this._sidebar()
	this._open_content()
}

function _RFEngine_footer()
{
	var v = this.view
	this._close_content()

	v.write('\
<div class="footer">\
  Copyright &copy; 1999-2007\
  <a href="mailto:webmaster@redfelineninja.org.uk">\
    Daniel Thompson\
  </a>\
</div>')

}

function RFEngine(view, id, has_sidebar)
{
	// initialize static properties
	this.lookup = _RFEngine_lookup

	// initialize dynamic properties
	this.view = view
	this.id = id
	this.has_sidebar = has_sidebar

	// infer path to document root (unless we have an explicit override).
	// the hack_to_get_nav_right is possibly the nastiest hack I've ever
	// seen. basically JS generates an error when checking for the
	// definedness of an object but *not* when checking for the
	// definedness of an object property.
	if (window.hack_to_get_nav_right)
	        this.root = window.hack_to_get_nav_right
	else if (id.length == 0)
		this.root = ''
	else
		this.root = '../'

	// private methods
	this._open_content = _RFEngine_open_content
	this._close_content = _RFEngine_close_content
	this._sidebar = _RFEngine_sidebar

	// public methods
	this.header = _RFEngine_header
	this.footer = _RFEngine_footer

	// add the icon
	document.write('\
<link rel="icon" href="'+this.root+'images/white_cheetah-16x16.png" type="image/png">')
}

/* The border settings are inserted by the JavaScript in order to
 * achieve graceful degradation when JavaScript is not available.
 */
document.write('\
<style>\n\
<!--\n\
body { border: 1px solid #990000; }\n\
-->\n\
</style>\n')



