// This Javascript file contains the following major functions:
// 1. Cookie support:  set, read, and delete cookies.
// 2. Menu choice:  display the brief or full "main menu".
// 3. Home page choice:  display the Short or Long home page.
//	3a. A "chooser" page allows the user to pick which home page
//	3b. A "redirector" page sends them to the correct home page.


// See bottom for installation plan.

// Initialize Menu choice
var menu_choice = true;
var menu_cookie = "remember_menu";
var mchoice = getCookie(menu_cookie);

// Initialize Home page choice
var home_chooser_name = "chooser";
var home_cookie = "remember_home";		// 3a
var home_redirector_name = "redirector";	// 3b
var joomla_base = "joomla/";
// ID numbers for the chooser, brief and full home pages.
var chooser_id_num = 187;
var chooser_item_num = 533;
var brief_id_num = 134;
var brief_item_num = 281;
var full_id_num = 9;
var full_item_num = 70;

////////////////////////////////////////////////////////////////////
// The central function -- this makes sure that the home page and
// menu choice take effect.
////////////////////////////////////////////////////////////////////

function doTheRightThing()
{
    var home_chooser = document.getElementById(home_chooser_name);
    if (home_chooser != null) {
	showChooser();
    }
    var home_redirector = document.getElementById(home_redirector_name);
    if (home_redirector != null) {
	redirectHome();
    }
    chooseMenu();
}

// alert("menu_choice: " + menu_choice);
////////////////////////////////////////////////////////////////////
// utility functions
////////////////////////////////////////////////////////////////////

// for debuggins
function showMe(s)
{
    var showid = "showme";
    var showme = document.getElementById(showid);
    showme.innerHTML = s;
    show(showid);
}

// Turn a joomla id# and item# into a URL and a clickable link

function makeURL(id, item)
{
    var url = "index.php";
    var fixedParams = '?option=com_content&task=view&';
    var varParams = "id=" + id + "&Itemid=" + item;
    return url + fixedParams + varParams;
}

function makeLink(id, item)
{
    var url = makeURL(id, item);
    return 'href="' + url + '"';
}

////////////////////////////////////////////////////////////////////
//	Cookie support
////////////////////////////////////////////////////////////////////

function setCookie(c_name,value,expiredays)
{
    if (expiredays == "never") {
	expiredays = 99999;
    }
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    var ck=c_name + "=" + escape(value) +
	((expiredays==null) ? "" : ";expires="+ exdate.toGMTString()) +
	"; path=/";
    document.cookie=ck;
// alert("set cookie: " + c_name + "= " + value);
// log("set cookie: " + c_name + "= " + value);
}

// Get the value of the specified cookie
// If found, return the (string) value of the cookie.
// If not found, return null
function getCookie(c_name)
{
    var ckval = null;
    if (document.cookie.length>0)
    {
      c_start = document.cookie.indexOf(c_name + "=");
      if (c_start != -1)
      { 
	c_start = c_start + c_name.length + 1; 
	c_end = document.cookie.indexOf(";", c_start);
	if (c_end == -1)
	{
	    c_end = document.cookie.length;
	}
	ckval = unescape(document.cookie.substring(c_start,c_end));
      }
    }
// alert("getCookie: " + c_name + ": " + ckval);
    return ckval;
}

function delCookie(c_name)
{
    setCookie(c_name, "", -1);
}

function hide(id)
{
// log("hide: " + id);
// alert("hide: " + id);
    var elt = document.getElementById(id);
    var cl = elt.className;
    var ix = cl.indexOf("hidden");
    if (!cl) {
	// class is empty, set it to hidden and we're done
// alert("empty -> hidden");
	cl = "hidden";
    } else if (cl.indexOf("hidden") == -1) {
	// Add hidden to the class
// alert("add hidden");
	cl = cl + " hidden";
    } else {
// alert("already hidden, index: " + ix);
	// hidden already there, don't do anything
    }
// alert("hide: id: " + id + ", old class: " + elt.className + ", new: " + cl);
    elt.className = cl;
}

function show(id)
{
// log("show: " + id);
// alert("show: " + id);
    var elt = document.getElementById(id);

    var cl = elt.className;
    var ix = cl.indexOf("hidden");
    if (ix == 0) {
	// it's just "hidden", replace with an empty string
	cl = "";
    } else if (ix != -1) {
	// other things precede: chop off the " hidden" at the end
	var ix2 = cl.indexOf(" hidden");
	cl = cl.substr(0, ix);
    } else {
	// not hidden, do nothing:
    }
// alert("show: id: " + id + ", old class: " + elt.className + ", new: " + cl);
    elt.className = cl;
    return false;
}

////////////////////////////////////////////////////////////////////
// Menu choice - display the brief or full "main menu"
// If the user has a menu cookie, display the menu he wants and hide the
// other one.
////////////////////////////////////////////////////////////////////

if (mchoice) {
    menu_choice = char2bool(mchoice);
}

function switchMenu(newval)
{
// alert("switchMenu: " + newval);
    if (newval === null) {
	newval = !menu_choice;
    }
    menu_choice = newval;
// alert("switchMenu, menu_choice: " + menu_choice);
    chooseMenu();
}

function useLongMenu()
{
// alert("useLongMenu");
    var v = hide("briefmenu");
if (v) { alert(v); y = v.foo; }
    v = show("fullmenu");
if (v) { alert(v); y = v.foo; }
}

function useShortMenu()
{
// alert("useShortMenu");
    var v  = hide("fullmenu");
if (v) { alert(v); y = v.foo; }
    v = show("briefmenu");
if (v) { alert(v); y = v.foo; }
}

function chooseMenu()
{
// log("chooseMenu, menu_choice: " + menu_choice);
    setCookie(menu_cookie, bool2char(menu_choice), "never");
    if (menu_choice) {
// log("long menu");
	useLongMenu();
    } else {
// log("short menu");
	useShortMenu();
    }
}

function bool2char(val)
{
    return val ? "true" : "false";
}

function char2bool(val)
{
    if (val == "true") {
	return true;
    } else if (val == "false") {
	return false;
    }
    // neither true nor false, return val but put up a popup
// alert("char2bool: unexpected value: " + val);
    return val;
}

/*********************************
function showlog()
{
    show("log");
}

function log(s)
{
    var logelt = document.getElementById("log");
    logelt.innerHTML += "<br/>" + s;
}
*********************************/

function switchHome(tf)
{
    home_choice = !(!tf);
    setCookie(home_cookie, bool2char(home_choice), "never")
    chooseHome();
}

/*********************************
function useLongHome()
{
    hide("briefhome");
    show("fullhome");
}

function useShortHome()
{
    hide("fullhome");
    show("briefhome");
}

function chooseHome()
{
    setCookie(home_cookie, char2bool(home_choice), "never");
    if (home_choice) {
	useLongHome();
    } else {
	useShortHome();
    }
}
*********************************/

// If we have are on the bivalent python home page, choose the right
// home page. If there's a cookie for the home page, use that.  Otherwise
// present the home page chooser.

function redirectHome()
{
    var hchoice = getCookie(home_cookie);
// alert("hchoice: " + hchoice);
    // Figure out which home page is wanted, then redirect to that page.
    var home_choice = char2bool(hchoice);
    var id;
    var item;
    /***********************************************
    if (hchoice === null) {
// alert("no cookie");
	// no cookie set -- go to the chooser page
	id =  chooser_id_num;
	item = chooser_item_num;
    }
    else
    ***********************************************/
    if (home_choice) {
// alert("home: true");
	// Cookie true: go to the full home page
	id = full_id_num
	item = full_item_num
    } else {
// alert("home: false");
	// Cookie false: go to the brief home page
	id = brief_id_num
	item = brief_item_num
    }
    var destURL = makeURL(id, item);
// alert("destURL: " + destURL);
    window.location = destURL;
}

function showChooser()
{
    var chooser_div = document.getElementById("chooser");
    if (chooser_div == null) {
	alert("Where's my chooser?");
	return; 	// Error - "chooser" div missing
    }
    chooser_div.innerHTML =
	'<div class="indented">' +
	'<a id="makebrief" onclick="switchHome(false);" ' +
	      makeLink(brief_id_num, brief_item_num) + 
	'>Brief Home Page (and remember it)</a><br />' +
	'<a id="makefull" onclick="switchHome(true);" ' +
	      makeLink(full_id_num, full_item_num) +
	'>Full Home Page (and remember it)</a><br />' +
	'<a ' + makeLink(brief_id_num, brief_item_num) + 
	'>Brief Home Page (just this once)</a> <br />' +
	'<a ' + makeLink(full_id_num, full_item_num) +
	'>Full Home Page (just this once)</a>' +
	'</div>';
    show("chooser");
}

////////////////////////////////////////////////////////////////////
// Installation instructions
////////////////////////////////////////////////////////////////////
// 1.	Put the Chooser into the lasfsinc site, pasting from and to
//	the _html_ edit buffer.  (You need to preserve the attributes,
//	especially the "id" attributes of the <div>).
// 2.	Add the Chooser page as the second item in the short & long menus.
//	Get the id and item numbers and update the front of this file.
// 3.   Copy up the template html file.
// 3.	Check this file to make sure it still works, then copy it up to the
//	LASFS website, then make sure the menu chooser still works.
// 4.	Add the Redirect page to the main and brief menus as the _first_
//	item in the menus.
// 5.	Test to make sure it all works.
// 6.	Unpublish the "Full" and "Brief" home page links in the main and
//	brief menus.


// After everything is loaded, "do the right thing".
