//window.loadFirebugConsole();

if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}

function debug(text)
{
	//alert(text);
	console.log(text);
}

function ajaxUpdateAdd(event) { 
	debug("ajaxUpdateAdd started");
	myARKiveAJAXListAddStarted(); 
	new Ajax.Updater('myarkive_scrapbooks_list_add','/myarkive/scrapbooks/ajax_view/0/?action=add', 
	{asynchronous:true, evalScripts:true, onComplete:myARKiveAJAXListAddComplete, onFailure:myARKiveAJAXListAddFailure, onSuccess:myARKiveAJAXListAddSuccess, requestHeaders:['X-Update', 'myarkive_scrapbooks_list_add']}
	);
	Event.stop(event);
	debug("ajaxUpdateAdd finished");
}

function ajaxUpdateEdit(event) { 
	myARKiveAJAXListEditStarted(); 
	new Ajax.Updater('myarkive_scrapbooks_list_edit','/myarkive/scrapbooks/ajax_view/0/?action=edit', 
	{asynchronous:true, evalScripts:true, onComplete:myARKiveAJAXListEditComplete, onFailure:myARKiveAJAXListEditFailure, onSuccess:myARKiveAJAXListEditSuccess, requestHeaders:['X-Update', 'myarkive_scrapbooks_list_edit']}
	);
	Event.stop(event);
}


function myARKiveAJAXAddSuccess (request, json)
{
	debug('myARKiveAJAXAddSuccess started');
	$('myarkive_scrapbooks_list_add').style.display = 'none';
	$('myarkive_scrapbook_add_button').innerHTML = 'Saved to scrapbook';
	//$('li_MyARKive_add').removeChild($('myarkive_scrapbook_add_cancel'));
debug('myARKiveAJAXAddSuccess finished');
}

function myARKiveAJAXListAddCancel (event)
{
	debug('myARKiveAJAXListAddCancel started');
	// we do both events here in case the cancel button was not pressed on the other menu item
	// (in practise this will be the lower of the two scrapbook options becuase the upper
	// result window will prevent the lower link being clicked)
	Event.observe('myarkive_scrapbook_add_button', 'click', ajaxUpdateAdd, false);
	Event.observe('myarkive_scrapbook_edit_button', 'click', ajaxUpdateEdit, false);
	$('myarkive_scrapbooks_list_add').style.display = 'none';
	$('myarkive_scrapbook_add_button').innerHTML = 'Add to scrapbook';
	//$('myarkive_scrapbook_edit_button').href = '/myarkive/scrapbooks/manage';
	//$('myarkive_scrapbook_edit_button').onclick = function() {event.returnValue = false; return false;};
	Event.stopObserving('myarkive_scrapbook_add_button', 'click', myARKiveAJAXListAddCancel, false);
	//Event.stopObserving('myarkive_scrapbook_add_cancel', 'click', myARKiveAJAXListAddCancel, false);
	Event.stop(event);
	//$('li_MyARKive_add').removeChild($('myarkive_scrapbook_add_cancel'));
	
	debug('myARKiveAJAXListAddCancel finished');
}

function myARKiveAJAXListAddStarted()
{
	debug('myARKiveAJAXListAddStarted started');
	$('myarkive_scrapbook_add_button').innerHTML = 'Add to scrapbook (loading)';
	$('myarkive_scrapbooks_list_edit').style.display = 'none';
	$('myarkive_scrapbook_edit_button').innerHTML = 'Go to scrapbook';
//TODO: incl. tidy up	$('li_MyARKive_edit').removeChild($('myarkive_scrapbook_edit_loading'));
	Event.stopObserving('myarkive_scrapbook_add_button', 'click', ajaxUpdateAdd, false);
	/*
	var txt = document.createTextNode("(loading)");
		var aNode = document.createElement("a");
		aNode.id="myarkive_scrapbook_add_loading";
		aNode.setAttribute("href", "#");
		aNode.appendChild(txt);
		
		$('li_MyARKive_add').appendChild(aNode);*/
	debug('myARKiveAJAXListAddStarted finished');
}

function myARKiveAJAXListAddSuccess (request, json)
{
	
}

function myARKiveAJAXListAddFailure (request, json)
{
	debug('myARKiveAJAXListAddFailure started');
	//alert('test');
	$('myarkive_scrapbooks_list_add').style.display = 'none'; // should already be hidden but just in case
	$('myarkive_scrapbooks_list_add').innerHTML = 'scrapbooks go here';
	$('myarkive_scrapbook_add_button').innerHTML = "<a href=\"javascript:alert('Could not load list of scrapbooks. The MyARKive service may be undergoing essential maintainance. Please try again later.')\">ERROR - not saved</a>";
	debug('myARKiveAJAXListAddFailure finished');
}

function myARKiveAJAXListAddComplete (request, json)
{
	debug('myARKiveAJAXListAddComplete started');
	debug($('myarkive_scrapbooks_list_add').innerHTML);
	
	stringResult = new String($("myarkive_scrapbooks_list_add").innerHTML);
	debug (stringResult);
	
	//$('li_MyARKive_add').removeChild($('myarkive_scrapbook_add_loading'));
	
	if (stringResult.search("Loading login page...") != -1)
	{
		debug("need to redirect to login page...");
		$('myarkive_scrapbooks_list_add').style.display = 'none'; // should already be the case
		$('myarkive_scrapbook_add_button').innerHTML = 'Loading login page...';
		//window.location = '/myarkive/users/login?redirect='+thisPageRelativeURL;
		debug("redirecting to " + $('myarkive_scrapbook_add_button').href);
		window.location = $('myarkive_scrapbook_add_button').href;
	} else if (stringResult.search("You have no scrapbooks") != -1)
	{
		debug("user has no scrapbooks...");
		$('myarkive_scrapbooks_list_add').style.display = 'none'; // should already be the case
		$('myarkive_scrapbook_add_button').innerHTML = 'You have no scrapbooks';
	} else if (stringResult.search("Sorry, MyARKive is unavailable") != -1)
	{
		debug("myARKive is offline...");
		$('myarkive_scrapbooks_list_add').style.display = 'none'; // should already be the case
		$('myarkive_scrapbook_add_button').innerHTML = 'Sorry, MyARKive is offline';
	} else
	{
		debug("no need to redirect to login page...");
		$('myarkive_scrapbooks_list_add').style.display = 'block';
		$('myarkive_scrapbook_add_button').innerHTML = 'Add to scrapbook (cancel)';
		/*var txt = document.createTextNode("(cancel)");
		var aNode = document.createElement("a");
		aNode.id="myarkive_scrapbook_add_cancel";
		aNode.setAttribute("style", "width:30px !important; clear:both;");
		aNode.setAttribute("href", "#");
		aNode.appendChild(txt);
		
		$('li_MyARKive_add').firstChild.setAttribute("style", "width:130px !important; clear:both;");
		$('li_MyARKive_add').appendChild(aNode);*/
	}
	
	Event.observe('myarkive_scrapbook_add_button', 'click', myARKiveAJAXListAddCancel, false);
	//Event.observe('myarkive_scrapbook_add_cancel', 'click', myARKiveAJAXListAddCancel, false);
	debug('myARKiveAJAXListAddComplete finished');
}

function myARKiveAJAXListEditCancel (event)
{
	// we do both events here in case the cancel button was not pressed on the other menu item
	// (in practise this will be the lower of the two scrapbook options becuase the upper
	// result window will prevent the lower link being clicked)
	Event.observe('myarkive_scrapbook_edit_button', 'click', ajaxUpdateEdit, false);
	Event.observe('myarkive_scrapbook_add_button', 'click', ajaxUpdateAdd, false);
	$('myarkive_scrapbooks_list_edit').style.display = 'none';
	$('myarkive_scrapbook_edit_button').innerHTML = 'Go to scrapbook';
	//$('myarkive_scrapbook_edit_button').href = '/myarkive/scrapbooks/manage';
	//$('myarkive_scrapbook_edit_button').onclick = function() {event.returnValue = false; return false;};
	Event.stopObserving('myarkive_scrapbook_edit_button', 'click', myARKiveAJAXListEditCancel, false);
	//Event.stopObserving('myarkive_scrapbook_edit_cancel', 'click', myARKiveAJAXListEditCancel, false);
	Event.stop(event);
	//$('li_MyARKive_edit').removeChild($('myarkive_scrapbook_edit_cancel'));
}

function myARKiveAJAXListEditStarted()
{
	$('myarkive_scrapbook_edit_button').innerHTML = 'Go to scrapbook (loading)';
	$('myarkive_scrapbooks_list_add').style.display = 'none';
	$('myarkive_scrapbook_add_button').innerHTML = 'Add to scrapbook';
	Event.stopObserving('myarkive_scrapbook_edit_button', 'click', ajaxUpdateEdit, false);
	/*
	var txt = document.createTextNode("(loading)");
		var aNode = document.createElement("a");
		aNode.id="myarkive_scrapbook_edit_loading";
		aNode.setAttribute("href", "#");
		aNode.appendChild(txt);
		
		$('li_MyARKive_edit').appendChild(aNode);*/
		
		debug('myARKiveAJAXListEditStarted finished');
}

function myARKiveAJAXListEditSuccess (request, json)
{
	debug('success');
	
	
}

function myARKiveAJAXListEditFailure (request, json)
{
	$('myarkive_scrapbooks_list_edit').style.display = 'none'; // should already be hidden but just in case
	$('myarkive_scrapbooks_list_edit').innerHTML = 'scrapbooks go here';
	$('myarkive_scrapbook_edit_button').innerHTML = "<a href=\"javascript:alert('Could not load list of scrapbooks. The MyARKive service may be undergoing essential maintainance. Please try again later.')\">ERROR - not saved</a>";
}

// not ideal - would be nice not to do this if it's failed but we have
// no access to finalised updated div contents in the onSuccess event 
// so have to do this here. must be a better way...
function myARKiveAJAXListEditComplete (request, json)
{
	debug('complete');
	debug($('myarkive_scrapbooks_list_edit').innerHTML);
	
	stringResult = new String($("myarkive_scrapbooks_list_edit").innerHTML);
	debug (stringResult);
	
	//$('li_MyARKive_edit').removeChild($('myarkive_scrapbook_edit_loading'));
	
	if (stringResult.search("Loading login page...") != -1)
	{
		debug("need to redirect to login page...");
		$('myarkive_scrapbooks_list_edit').style.display = 'none'; // should already be the case
		$('myarkive_scrapbook_edit_button').innerHTML = 'Loading login page...';
		window.location = '/myarkive/scrapbooks/list';
	} else if (stringResult.search("Sorry, MyARKive is unavailable") != -1)
	{
		debug("myARKive is offline...");
		$('myarkive_scrapbooks_list_edit').style.display = 'none'; // should already be the case
		$('myarkive_scrapbook_edit_button').innerHTML = 'Sorry, MyARKive is offline';
	}  else
	{
		debug("no need to redirect to login page...");
		$('myarkive_scrapbooks_list_edit').style.display = 'block';
		$('myarkive_scrapbook_edit_button').innerHTML = 'Go to scrapbook (cancel)';
		/*var txt = document.createTextNode("(cancel)");
		var aNode = document.createElement("a");
		aNode.id="myarkive_scrapbook_edit_cancel";
		aNode.setAttribute("href", "#");
		aNode.appendChild(txt);
		
		$('li_MyARKive_edit').appendChild(aNode);*/
	}
	
	Event.observe('myarkive_scrapbook_edit_button', 'click', myARKiveAJAXListEditCancel, false);
	//Event.observe('myarkive_scrapbook_edit_cancel', 'click', myARKiveAJAXListEditCancel, false);
	
}