/*
 *	Gallery menu and miscellaneous common utility functions
 *	-------------------------------------------------------
 *	Copyright (c) 2009-2011 Wabbit Web Works
 *	All Rights Reserved
 *
 *	This programme is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public Licence as published by
 *	the Free Software Foundation, either version 3 of the Licence, or
 *	(at your option) any later version.
 *
 *	This programme is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public Licence for more details.
 *
 *	You should have received a copy of the GNU General Public Licence
 *	along with this programme.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 *	gallery_add_menu()
 *	------------------
 *	Attach a menu to a specific element in the DOM.  This function
 *	will call itself recursively to attach all sub-menus.
 */
function gallery_add_menu(el,menu)
{
    if (!menu)
	return;

    var list = document.createElement('ul');

    for (var i = 0; i < menu.length; i++) {
	var curr = menu[i];

	if (!curr) {
	    alert('Menu item ' + i + ' is NULL');
	    continue;
	}

	var item = document.createElement('li');

	if (curr.menu) {
	    var newel = document.createElement('a');
	    newel.setAttribute('href','#');
	    newel.innerHTML = curr.name;
	    newel.onclick = function() { return false; };
	    newel.style.cursor = 'default';

	    item.appendChild(newel);
	    gallery_add_menu(item,curr.menu);
	}
	else {
	    var newel = document.createElement('a');
	    newel.setAttribute('href',curr.uri);
	    newel.innerHTML = curr.name;
	    newel.onclick = function() { return true; };

	    item.appendChild(newel);
	}
	list.appendChild(item);
    }
    el.appendChild(list);
}

/*
 *	========================
 *	UTILITY FUNCTIONS FOLLOW
 *	========================
 */

var wabbit_utilities = {
    start_stack: function() {},
    ran_onload: false,
    org_onload: window.onload
}

/*
 *	xmlrpc_get_data()
 *	-----------------
 *	Send a URI GET request to the server.
 *	Execute the provided callback function when the server responds.
 */
function xmlrpc_get_data(uri,callback)
{
    var req;

    try {
	if (typeof ActiveXObject != 'undefined') {
	    req = new ActiveXObject('Microsoft.XMLHTTP');
	}
	else if (window['XMLHttpRequest']) {
	    req = new XMLHttpRequest();
	}
    }
    catch (err) {
	alert('XMLHTTP: ' + err);
    }

    if (!req)
	return;

    req.onreadystatechange = function() {
	if (req.readyState == 4) {	// loaded
	    if (req.status == 200) {	// HTTP OK
		req.onreadystatechange = function() {};
		callback(req.responseText);
	    }
	    else if (req.status != 0) {
		alert(
		    "There was a problem retrieving the requested file:\n" +
		    "uri = '" + uri + "'\n" +
		    "status = '" + req.status + "'\n" +
		    "statusText = '" + req.statusText + "'"
		);
	    }
	}
    }

    // var date = new Date();
    // uri += '?cb=' + date.getTime();

    try {
	req.open('GET',uri,true);
	req.send(null);
    }
    catch (err) {
	alert('GET request: ' + err);
    }
}

/*
 *	add_load_event()
 *	----------------
 *	Execute the provided callback function when the DOM is ready.
 *	Callback functions will be executed in the order in which they
 *	are added
 */
function add_load_event(callback)
{
    var curr_stack = wabbit_utilities.start_stack;

    wabbit_utilities.start_stack = function () {
	curr_stack();
	callback();
    }
}

/*
 *	add_unload_event()
 *	------------------
 *	Execute the provided callback function when the user leaves the
 *	current page.  Callback functions will be executed in the order
 *	in which they are added
 */
function add_unload_event(callback)
{
    var old_onunload = window.onunload;

    if (typeof window.onunload != 'function') {
	window.onunload = callback;
    }
    else {
	window.onunload = function() {
	    if (old_onunload)
		old_onunload();
	    callback();
	}
    }
}

if (document.addEventListener) {
    document.addEventListener('DOMContentLoaded', function() {
	if (!wabbit_utilities.ran_onload) {
	    wabbit_utilities.ran_onload = true;
	    wabbit_utilities.start_stack();
	}
    }, false);
}
else if (document.all && !window.opera) {
    document.write('<scr' + 'ipt id="DOMReady" defer="true" ' + 'src="//:"></scr' + 'ipt>');  
    document.getElementById('DOMReady').onreadystatechange = function(){
	if (this.readyState == 'complete' && !wabbit_utilities.ran_onload){
	    wabbit_utilities.ran_onload = true;
	    wabbit_utilities.start_stack();
	}
    }
}

window.onload = function() {
    if (typeof(wabbit_utilities.org_onload) == 'function') {
	wabbit_utilities.org_onload();
    }
    if (!wabbit_utilities.ran_onload) {
	wabbit_utilities.ran_onload = true;
	wabbit_utilities.start_stack();
    }
}

/*
 *	========================
 *	END OF UTILITY FUNCTIONS
 *	========================
 */

/*
 *	load the gallery menu when the page has loaded
 */
add_load_event(function() {
    var el = document.getElementById('gallery');
    if (!el) {
	alert("No 'gallery' ID defined");
	return;
    }

    var el0 = el.firstChild;
    el0.style.cursor = 'default';
    el0.onclick = function() { return false; };

    xmlrpc_get_data('/data/gallery_menu.json',function(json) {
	try {
	    gallery_add_menu(el,eval(json));
	}
	catch (err) {
	    alert('eval: ' + err);
	}
    });
});

