/*********************************
 *	coded by xlsa.
 *	xlsa.coder@gmail.com
 *	coded in Asturies
 *********************************/

 //I think this is used only by the main.htm
//depends on prototype.js (prototype.lite.js is enough)

//do this so that IE explorer doesn't break with the firebug debugging.
var console = (!console)? {log: function(){}} : console;

/* menuAnimator "class" */
menuAnimatorText = function(sIt, eIt)
{
	this.startIt = sIt;
	this.endIt = eIt;
	this.timer = null;
	this.iterationsDone = null;
	this.itersToDo = 3;
};

menuAnimatorText.prototype.start = function()
{
	this.iterationsDone = 0;
	this.timer = window.setInterval(this.animCode.bind(this),400);
}

menuAnimatorText.prototype.animCode = function()
{
	if (this.iterationsDone < this.itersToDo)
	{
		//console.log(this.iterationsDone);
		if (this.startIt) this.startIt.innerHTML = this.startIt.innerHTML.replace("&gt;","");
		this.endIt.innerHTML = ">" + this.endIt.innerHTML;
		this.iterationsDone++;
	}
	else
		window.clearInterval(this.timer);
}
/* end menuAnimator "class"*/

/*
this is a nice case for using a closure instead of a "normal" object.
More than data with methods, we've got a method with some data (currentlyDecorated) associated
so I think this is more appropiate than creating a menuDecorator object with a decorate method.
*/
var menuDecorator = function(currentlyDecorated)
{
	//"this" will be the item on which we've done click
	return function()
	{
		/*
		if (currentlyDecorated) currentlyDecorated.innerHTML = currentlyDecorated.innerHTML.replace("&gt;&gt;&gt;","");
		this.innerHTML = ">>>" + this.innerHTML;
		*/
		window.animator = new menuAnimatorText(currentlyDecorated,this);
		window.animator.start();
		currentlyDecorated = this;
		
	}	
}(null);

/*
localQueryString class, it's a singleton (sort of...)
the idea is that clientside javascript can receive data from the previous screen, by means of queryStrings like this
myPage.html#name1=val1&name2=val2
*/
var localQueryString = function()
{
	this.qItems = {};
};
localQueryString.myReference = null;

localQueryString.prototype.parseQueryString = function()
{
	try
	{
		var query = window.location.href.split("#")[1];
		var queries = query.split("&");
		for (i=0; i<queries.length;i++)
		{
			var queryItem = queries[i].split("=");
			this.qItems[queryItem[0]] = queryItem[1];
		}
	}
	catch(ex){}
}
//this is the only part accessible by the user
localQueryString.getQueryString = function()
{
	if (!localQueryString.myReference)
	{
		localQueryString.myReference = new localQueryString();
		localQueryString.myReference.parseQueryString();
	}
	return localQueryString.myReference;	
}
/* end of localQueryString class --------------------------------*/

function getInitialScreen()
{
	return locQString.qItems["initialScreen"] ? locQString.qItems["initialScreen"] : "presentacion";
}

function init()
{
	var items = $("menuLeft").getElementsByTagName("a")
	console.log("items.length = " + items.length);
	for (i=0; i<items.length; i++)
	{
		items[i].onclick = menuDecorator;
	}
	
	menuDecorator.apply($(initialScreen + "Id"));
	//$("contentsIFrame").location.href = initialScreen + ".htm";
	$("contentsIFrame").src = initialScreen + ".htm";
}


//main

var locQString = localQueryString.getQueryString();
//I prefer having the initialScreen as something programmable, instead of something into the <html>
var initialScreen = getInitialScreen();
window.onload = init;
