/* Global variable for the inscriptions */
var infos = {};
var lang = "en";
var selmenu = '';
var menutosel = '';

// Setting up the page - called the first time the page is loaded
function setup(language){
	// Set the language
	lang = language
	
	// Set how the throbber works
	/*
	var myGlobalHandlers = {
		onCreate: function(){
			Element.setStyle('throbber',{visibility:'visible'});
		},
	
		onComplete: function() {
			if(Ajax.activeRequestCount == 0){
				Element.setStyle('throbber',{visibility:'hidden'});
			}
		}
	};
	Ajax.Responders.register(myGlobalHandlers);
	*/
	
	var s = Element.getDimensions('menu');
	menuHeight = s.height;

	if (!window.location.hash)
	{
		window.location.hash = 'festival';
		menutosel = 'festival';
	}
	pollHash(); // handle the back button
	
	s = Element.getDimensions('frame');
	hMain = s.width - 230;
	Element.setStyle('main', {width: ''+hMain+'px'});
}

// Ajax functions
function call(task)
{
	menutosel = task;
	var extra = null;
	if (task == 'lang')
	{
		extra = {
			page: window.location.hash.substring(1)
		};
		lang = arguments[1];
		menutosel = selmenu;
		selmenu = '';
	}
	else if (task == 'reg1')
		extra = { reglang: arguments[1] };
	else if (task == 'seereg')
	{
		var search = window.location.hash.substring(1);
		if (search.indexOf('?') > 0)
		{
			search = search.substring(search.indexOf('?')+1);
			extra = {code: search};
		}
	}
	if (extra == null)
		doCall(task);
	else doCall(task, extra);
}
function doCall(task){
	if (task == 'seereg')
	{
		window.location.hash = task + '?' + arguments[1].code;
		expectedHash = task;
	}
	else if (task != 'lang'){
		window.location.hash = task;
		expectedHash = task;
	}
	var params = "task="+task+"&lang="+lang;
	if (arguments.length == 2){
		var hash = $H(arguments[1]);
		hash.keys().each(function(key, index){
			var vals = hash.values();
			params += '&'+key+'='+vals[index];
		});
	}
	var myAjax = new Ajax.Request( 
		'tango.php', 
		{ 
			method: 'post',
			parameters: params, 
			onComplete: updatePage
		});
	Element.setStyle('throbber',{visibility:'visible'});
}

function updatePage(originalRequest)
{
	var xml = originalRequest.responseXML;

	var locs = xml.getElementsByTagName('location');
	if (locs.length == 1)
	{
		window.location = locs.item(0).firstChild.nodeValue;	
		return;
	}
	
	var updates = $A(xml.getElementsByTagName('update'));
	updates.each(function(node){
		var section = node.getAttribute('target');
		if (section == 'inHeader' || section == 'menu')
		{
			var inner = '';
			for (i = 0; i < node.childNodes.length; i++)
				inner += node.childNodes[i].nodeValue;
			Element.update(section, inner);
		}
		else
		{
			var callback = function(){
				finishUpdate(section, node);
			};
			Effect.BlindUp(section, {duration: .5,
			                         afterFinish: callback});
        }
	});	
	

	var events = $A(xml.getElementsByTagName('event'));
	events.each(function(node){
		var eid = node.getAttribute('eid');
		var eac = node.getAttribute('action');
		var eifun = node.getAttribute('function');
		var fev = new Function(eifun);
		Event.observe(eid, eac, fev, false);
	});

	var scripts = $A(xml.getElementsByTagName('script'));
	scripts.each(function(node){
		eval(node.firstChild.nodeValue);
	});
}
function finishUpdate(section, node)
{
	var inner = '';
	for (i = 0; i < node.childNodes.length; i++)
		inner += node.childNodes[i].nodeValue;
	Element.update(section, inner);
	if (section == 'main') mmResize();
	Effect.BlindDown(section, {duration: .5});
	//alert('Section length: ' + inner.length);
	Element.setStyle('throbber',{visibility:'hidden'});
}

/* **************************************************** */
/* Test to have nice looking columns - doesn't work yet */
/* **************************************************** */
var menuHeight;
function mmResize()
{
	var menu = $('menu');
	var main = $('main');
	Element.setStyle('main', {height: 'auto'});
	s = Element.getDimensions('main');
	hMain = s.height;
	if (menuHeight < hMain)
		Element.setStyle('menu', {height: ''+hMain+'px'});
	else
	{
		Element.setStyle('main', {height: ''+menuHeight+'px'});
		Element.setStyle('menu', {height: ''+menuHeight+'px'});
	}
}

function menuSelection()
{
	if (menutosel != selmenu)
	{
		var menu = $('w'+selmenu);
		if (menu) $(menu).removeClassName('white');
		menu = $('w'+menutosel)
		if (menu)
		{
			$(menu).addClassName('white');
			selmenu = menutosel;
		}
	}
}

/* *********************************************** */
/* The section below takes care of the back button */
/* *********************************************** */
var expectedHash = '';
function handleHistory()
{
	var hash = '';
	if (window.location.hash)
	{
		hash = window.location.hash.substring(1);
		if (hash.indexOf('?') != -1)
			hash = hash.substring(0, hash.indexOf('?'));
	}
	if (hash.charAt(0) == '_')
		window.location.hash = expectedHash;
	else if (hash != expectedHash)
		call(hash);
	menuSelection();
	return true;
}
function pollHash() {
  handleHistory();
  window.setInterval("handleHistory()", 1000);
  return true;
}