d = document;

/**************** Cookie section ****************/

var expire = new Date();
expire.setFullYear(expire.getFullYear() +1);
expire = expire.toGMTString();

var allcookies = document.cookie;

var position = allcookies.indexOf("section=");

if(position != -1){ /* if match is found, if not position is -1 */
var start = position + 8; /* start position of cookie value */
var end = allcookies.indexOf(";", start); /* end-point of named cookie value */
if (end == -1) end = allcookies.length; /* ... */
var cookie_value = allcookies.substring(start, end);
}
else{
cookie_value = undefined;
}

/*  Examples:
    if(cookie_value){do ...} // if there is a value for the cookie named section
    or:	 
	 or if (cookie_value == undefined){ do...} // if there is no value
*/

/*-------------------------------*/

if (typeof section == "undefined"){
section = undefined;
}

/* Examples:
   if(section){do ...} // if there is no section defined
   or:
   if(section == undefined){do...} // if there is no section defined
*/

/*
# If-block #1
#
# Arriving on a section page and if cookie does not exists:
#
# If document.cookie named 'section=' does not exist
# and if 'section' variable exists, set the cookie to
# the value of the defined section (tanzania or kenya).
# Inital state:
#         cookie =
#         section = x
#
# Modified state:
#         cookie = x
#         section = x
#
# This happens when arriving via direct navigation
# onto a section-specific page.
*/

if(cookie_value == undefined && section){
document.cookie = "section="+section+";expires="+expire;
}

/*
# If-block #2
#
# Arriving on a section page and if cookie exists:
#
# If document.cookie named 'section=' exists and if 'section'
# variable exists and if the strings are not
# the same, set cookie string to the value of
# the section variable.
# Initial state:
#        cookie = x
#       section = y
#
# Modified state:
#        cookie = y 
#       section = y
#
# This happens if arriving on a section-specific page and
# when the cookie value should be updated. The new cookie value
# is only used to hide the irellavant links on a non-section
# specific page if navigating from the current page
# or if coming back via search or direct navigation
# onto such page at a later stage.
*/

if(cookie_value && section && section != cookie_value){
document.cookie = "section="+section+";expires="+expire;
}

/*
# If-block #3
#
# Arriving on non-section specific pages
# without an existing cookie:
#
# If document.cookie named 'section=' does not exist and if
# 'section' variable is not defined, set the default cookie 
# to 'tanzania' AND the section variable to 'tanzania'.
# Initial state:
#        cookie =
#       section =
#
# Modified state:
#        cookie = x
#       section = x
#
# This happens when navigating straight to a non-section
# specific page. The cookie is thereafter read and used on subsequent
# non-section specific pages and left unmodified on
# any matching section-specific pages.
#
# This situation also occurs if cookies are blocked, in which
# case the section variable will be left undefined and
# both sections will be shown (see nested if (document.cookie) block.
# Otherwise Kenya links would not be shown in non-cookie browsers.
*/

if(cookie_value == undefined && section == undefined){
document.cookie = "section=tanzania;expires="+expire;

if (document.cookie != ""){
section = "tanzania";
}

}


/*
# If-block #4
#
# Arriving on non-section specific pages with an existing cookie:
#
# If document.cookie named 'section=' exists and if 'section'
# variable is not defined, set the 'section' variable to the
# value of the section cookie.
# Initial state:
#        cookie = x
#       section =
#
# Modified state:
#        cookie = x
#       section = x
#
# This only happens when arriving (by direct navigation
# or internal link) onto a non-section specific page 
# of the website after having previously visited
# any page of the website.
#
*/

if(cookie_value && section == undefined){
section = cookie_value;
}

/* Replace section image is called via onload of the section image itself
   if the section page (node) is not the same.
   The image replacement will not occur in no-cookie mode.
*/

function swap(section_image){
if (section == section_image){
complete_path = document.images[section_image+"_section"].src;
image_name = complete_path.substring (complete_path.lastIndexOf('/') + 1);
if (image_name == section_image+".png"){
document.images[section_image+"_section"].src="images/"+section_image+"3.png";
}
}
}

//if (section == "tanzania"){
//document.getElementById("tanzania_section").src='images/tanzania3.png';
//}


/**************************************************************/
/* Return true or false for old_browser or new_browser below */
/* if(old_browser) {..} runs code for IE5/6 only            */
/* if(new_browser) {..} runs code for all other browsers   */ 

var browser = navigator.appVersion;
var old_browser = browser.indexOf("MSIE 5") != -1 || browser.indexOf("MSIE 6") != -1;

var new_browser = new Boolean(1); /* assume new browser to be true */
if(old_browser == true ){        /* but if old browser is matched */
new_browser = false;            /* set new_browser to false      */
}
// new_browser = false; /* remove border images for testing */

/***************************************************/


/* Return upgrade browser notice, e.g. if(old_browser){upgrade_browser()} */

function upgrade_browser(){
d.write('<div style="padding:10px; margin: 0px 0px 10px 0px; border: 1px solid #c1ab18; background:#f6e46c;">'); 

/* Extract relevant browser for detailed error message */
browser_version = navigator.appVersion; /* only to test, if no-match */
if (browser.indexOf("MSIE 5") != -1){
browser_version = 'Microsoft Internet Explorer 5';
}
if (browser.indexOf("MSIE 6") != -1){
browser_version = 'Microsoft Internet Explorer 6';
}

d.write('<h3 style="margin:2px;">Please upgrade your browser</h3>');
d.write('Our website has detected that you are using');
d.write('<b>');
d.write(' '+browser_version+' ');
d.write('</b>');
d.write('which is an old, insecure and unstable browser version. ');
d.write('We recommend that you upgrade to ');
d.write('a more recent version of Explorer, or better, switch to ');
d.write('<a href="http://www.mozilla.com">Mozilla Firefox</a> or <a href="http://www.google.com/chrome">Google Chrome</a>.');
d.write('</div>');
}

/*
# Open and close multi-layer div's with positioned background edges
# for the text area, in the left, right and bottom margins.
# and thereafter in the header area.
#
# Descriptors for CSS ID, positions and image names
#   for content area in 'open_canvas_borders' function:
#
# ID  >  Position              >   Image name
# --------------------------------------------
# cml   content middle left     cml_d5d0b1.png
# cmr   content middle right    cmr_d5d0b1.png
# cbc   content bottom centre   cbc_d5d0b1.png
# cbl   content bottom left     cbl_d5d0b1.png
# cbr   content bottom right    cbr_d5d0b1.png
#
# ID descriptors for 'header_divs' function:
#
# hbc   header bottom centre    hbc_d5d0b1.png
# hml   header middle left      hml_d5d0b1.png
# hmr   header middle right     hmr_d5d0b1.png
# htc   header top centre       htc_d5d0b1.png
# htl   header top left         htl_d5d0b1.png
# htr   header top right        htr_d5d0b1.png
*/

function switch_theme(theme){

document.getElementById("hml").style.backgroundImage="url(images/hml_"+theme+".png)"
document.getElementById("hmr").style.backgroundImage="url(images/hmr_"+theme+".png)"
document.getElementById("htc").style.backgroundImage="url(images/htc_"+theme+".png)"
document.getElementById("htl").style.backgroundImage="url(images/htl_"+theme+".png)"
document.getElementById("htr").style.backgroundImage="url(images/htr_"+theme+".png)"
document.getElementById("hbc").style.backgroundImage="url(images/hbc_"+theme+".png)"

document.getElementById("cml").style.backgroundImage="url(images/cml_"+theme+".png)"
document.getElementById("cmr").style.backgroundImage="url(images/cmr_"+theme+".png)"
document.getElementById("cbc").style.backgroundImage="url(images/cbc_"+theme+".png)"
document.getElementById("cbl").style.backgroundImage="url(images/cbl_"+theme+".png)"
document.getElementById("cbr").style.backgroundImage="url(images/cbr_"+theme+".png)"

/* fails on IE
document.body.style.background="#"+theme+";"; 
*/

document.getElementById("viewport").style.backgroundImage="url(images/"+theme+".jpg)"
document.cookie = "display="+theme+";expires="+expire;

}

default_theme = "d3c1af";

/*********** theme cookie ************/

var theme_position = allcookies.indexOf("display=");

if(theme_position != -1){ /* if match is found, if not theme_position is -1 */
var theme_start = theme_position + 8; /* start theme_position of cookie value */
var theme_end = allcookies.indexOf(";", theme_start); /* end-point of named cookie value */
if (theme_end == -1) theme_end = allcookies.length; /* ... */
var theme_cookie_value = allcookies.substring(theme_start, theme_end);
}
else{
theme_cookie_value = undefined;
}

if (theme_cookie_value != undefined){
default_theme = theme_cookie_value;
}

/**************************************/



/* For edit procedure: filename (thisfile) */

filename = window.location.href;
thisfile = filename.substring (filename.lastIndexOf('/') +1);

/* identify photos.html display and if there is a search parameter */
var filename_without_query = thisfile.substring(0,11)
if (filename_without_query == 'photos.html' && location.search.length != 0){
lightbox_clickthrough = true;
}
else{
lightbox_clickthrough = false;
}


/* identify photos.html display if there is no search parameter */
var filename_without_query = thisfile.substring(0,11)
if (thisfile == 'photos.html'){
lightbox_index = true;
}
else{
lightbox_index = false;
}



/* initialise onload events */

function init(){
//switch_theme(default_theme)

if (typeof header_image == 'undefined'){
set_header('zebra_field.jpg');
//alert(typeof header_image)
}
else {
set_header(header_image);

/* optionally align header image left or right (default is center) */
if (typeof align_header != 'undefined'){
document.getElementById("header_img").style.backgroundPosition=align_header;

}

}
document.getElementById("viewport").style.backgroundImage="url(images/"+default_theme+".jpg)"

if(lightbox_clickthrough == false){
document.getElementById("content").style.backgroundColor='#f4f0e0';
document.getElementById("content").style.backgroundImage="url(images/f4f0e0_effect.jpg)"
}

/* add pages with gmaps here */
if (thisfile == "tanzania.html" || thisfile == "kenya.html" || thisfile == "arusha.html" || thisfile == "gombe_stream.html" || thisfile == "lake_manyara_national_park.html" || thisfile == "mahale_mts_national_park.html" || thisfile == "mikumi_national_park.html" || thisfile == "ngorongoro_conservation_area.html" || thisfile == "ruaha_national_park.html" || thisfile == "rubondo_island_national_park.html" || thisfile == "saadani_game_reserve.html" || thisfile == "selous_game_reserve.html" || thisfile == "serengeti_national_park.html" || thisfile == "tarangire_national_park.html" || thisfile == "zanzibar_island.html" || thisfile == "the_aberdares_national_park.html" || thisfile == "amboseli_national_park.html" || thisfile == "masai_mara_national_reserve.html" || thisfile == "mt_kenya_national_park.html" || thisfile == "nairobi_national_park.html" || thisfile == "tsavo_national_park.html" || thisfile == "lamu_island.html"){
load();
}

if (lightbox_clickthrough == true){
document.getElementById("header_img").style.backgroundImage='url(images/d3c1af.jpg)';
document.getElementById("header_img").style.backgroundRepeat='repeat';
//document.getElementById("content").style.backgroundImage='url(images/d3c1af.jpg)';

}


}

/* replace header image onload and onresize */
window.onload = init;
window.onresize = init;


function open_canvas_borders(){
if(lightbox_clickthrough == false){
d.write('<div id="cml" style="background:url(images/cml_'+default_theme+'.png);background-position: 0% -284px;background-repeat:repeat-y;">'); /* can re-use 'hml' insted, offset 284px */
d.write('<div id="cmr" style="background:url(images/cmr_'+default_theme+'.png);background-position: 100% -284px;background-repeat:repeat-y;">'); /* can re-use 'hmr' instead, offset 284px */
d.write('<div id="cbc" style="background:url(images/cbc_'+default_theme+'.png);background-position: 16px 100%;background-repeat:repeat-x;">');
d.write('<div id="cbl" style="background:url(images/cbl_'+default_theme+'.png);background-position: 0% 100%;background-repeat:no-repeat;">');
d.write('<div id="cbr" style="background:url(images/cbr_'+default_theme+'.png);background-position: 100% 100%;background-repeat:no-repeat;">');
}
}

function close_canvas_borders(){
if(lightbox_clickthrough == false){
document.write('</div></div></div></div></div>');
}
}


/***********************************************************/

/* set a standard header_height unless 'header_height = '...px' is
   already defined in <head> section variable */

if (typeof header_height == "undefined"){
var header_height = '300px';
}
if (typeof header_height != "undefined"){
}



function header_divs(){
if(lightbox_clickthrough == false){

d.write('<div id="hbc" style="height:'+header_height+';background:url(images/hbc_'+default_theme+'.png);background-position: 100% 100%;background-repeat:repeat-x;">');
d.write('<div id="hml" style="height:'+header_height+';background:url(images/hml_'+default_theme+'.png);background-position: 0% 16px;background-repeat:repeat-y;">');
d.write('<div id="hmr" style="height:'+header_height+';background:url(images/hmr_'+default_theme+'.png);background-position: 100% 16px;background-repeat:repeat-y;">');
d.write('<div id="htc" style="height:'+header_height+';background:url(images/htc_'+default_theme+'.png);background-position: 16px 0%;background-repeat:repeat-x;">');
d.write('<div id="htl" style="height:'+header_height+';background:url(images/htl_'+default_theme+'.png);background-position: 0% 0%;background-repeat:no-repeat;">');
d.write('<div id="htr" style="height:'+header_height+';background:url(images/htr_'+default_theme+'.png);background-position: 100% 0px;background-repeat:no-repeat;">');

/* d.write('write on header img...'); */

d.write('</div>');
d.write('</div>');
d.write('</div>');
d.write('</div>');
d.write('</div>');
d.write('</div>');
}

}


/* Run by 'init', onload and onresize */
function set_header(img){ 

var scroll_width = document.getElementById("header_img").scrollWidth;

/* If header_img is wider than 750px return the 1000px image. */
if (scroll_width > 750){
img = '1000x300/' + img;
}

/* if header_img div is less than or equal to 750px and
   if wider than 574px return the 750px image. */
if (scroll_width <= 750 && scroll_width > 574){
img = '750x300/' + img;
}

/* if header_img is less than or equal to 574px return the 574px image. */
if (scroll_width <= 574){
img = '574x300/' + img;
}

document.getElementById("header_img").style.backgroundImage="url(images/"+img+")";
//document.getElementById("header_img").style.display=none;


/* alert(img);
alert(scroll_width)
var body_width = document.getElementById("body").scrollWidth;
alert(body_width);
alert("images/"+img+"");
*/

}





function edit_page(){
/* output the edit link */
if(filename.indexOf("/edit/") != -1 && location.search == ''){

/* when index is accessed without index.html */
if (thisfile == ""){
thisfile = "index.html";
}

d.write("<span id=\"edit\">");
d.write("<a href=\"edit.cgi?page="+thisfile+"\">Edit "+thisfile+"</a> | ");
d.write("<a href=\"edit.cgi?page=publish.pages\">Publish</a> &nbsp; ");
d.write("</span>");
}
}

function skype(voip){
ttip = voip;
d.write('<a href="skype:'+voip+'?chat" onmousedown="/*document.images.skypecall.src=\'images/skyperequest.png\'*/" onmouseup="/*document.images.skypecall.src=\'http://mystatus.skype.com/smallicon/'+voip+'\'*/">'+voip+'</a> ');
//d.write('<a href="skype:'+voip+'?chat" onmousedown="document.images.skypecall.src=\'images/skyperequest.png\'" onmouseup="document.images.skypecall.src=\'http://mystatus.skype.com/smallicon/'+voip+'\'">')
voip = voip.replace(/\./g,'%2E');
d.write('<img src="http://mystatus.skype.com/smallicon/'+voip+'" style="border:none;vertical-align:middle;" width="16" height="16" title="Skype '+ttip+'" name="skypecall"></a>')
//d.write('<br>');
//setTimeout('document.location.reload()',3000000);
}

function email(email){
x = '@idream'; y = 'ofafrica'; q = '.com'; domain = x + y + q;
d.write('<a href="mailto:'+email+domain+'">'+email+domain+'<\/a>');
}

function print_header(){
document.write('<hr noshad">');
document.write('<table cellpadding="10" border="0"><tr><td valign="top">');
document.write('<img src="images/400x300/'+header_image+'">');
document.write('</td><td align="left" valign="top">');
document.write('<img src="images/logo.png" width="127" height="118" style="margin-left:0px"><br>');
document.write('<br>');

document.write('P.O. Box 2281<br>');
document.write('Arusha<br>');
document.write('Tanzania<br><br>');

document.write('Christopher Samwel Sululu<br>');
document.write('Email: ');
email('christopher');
document.write('<br>');
document.write('Skype: ');
skype('i.dream.of.africa');
document.write('<br><br>');

document.write('Office tel/fax: +255 27 254 5017<br>');
document.write('Mobile phone: +255 754 461 594<br><br>');
document.write('</td></tr></table>');
document.write('<hr style="noshade">');
}



function active_theme(show){
for(var i = 0; i <myThemes.length; i++){
if(myThemes[i] == show){
document.getElementById(myThemes[i]).style.border="#cbbfa6 solid 1px";
document.getElementById(myThemes[i]).style.BackgroundImage="url(images/sample_shade.png)";

}
else{
document.getElementById(myThemes[i]).style.border="#f7efde solid 1px";
}
}
}

/* Simply add themes here */
var myThemes = new Array(
"d3c1af",
"c8bdae",
"f3bd76");


function theme_selector(){

for(var i = 0; i <myThemes.length; i++){
if(myThemes[i] == default_theme){
d.write('<a href="javascript:void(0);" onclick=switch_theme("'+myThemes[i]+'");active_theme("'+myThemes[i]+'");>');
d.write('<img id='+myThemes[i]+' src="images/'+myThemes[i]+'_sample.png" style="border: 1px solid #cbbfa6;"></a> ');
}
else{
d.write('<a href="javascript:void(0);" onclick=switch_theme("'+myThemes[i]+'");active_theme("'+myThemes[i]+'");>');
d.write('<img id='+myThemes[i]+' src="images/'+myThemes[i]+'_sample.png" style="border:1px solid #f7efde;"></a> ');
}
}
}

