function initialize() {
	// initialize the DhtmlHistory
	// framework
	dhtmlHistory.initialize();

	// if this is the first time the page
	// has loaded, fetch the list of
	// topics remotely
	if (dhtmlHistory.isFirstLoad())
	{
		topics = loadTopics();
		historyStorage.put("topics", topics);
	}
	else
	{
		// else, simply extract it from our
		// history storage
		topics = historyStorage.get("topics");
	}

	// display our topics list
	// displayTopicsList();

	// initialize our initial state from
	// the browser location after the hash
	var currentTopic = dhtmlHistory.getCurrentLocation();

	displayTopic(currentTopic);

	// catch when a user clicks on a new
	// topic
	var menu = document.getElementById("menuItems");

	xAddEventListener(menu, "click", handleTopicChange, false);

	var submenu = document.getElementById("submenu");

	xAddEventListener(submenu, "click", handleTopicChange, false);

	// set ourselves up to listen to
	// history events
	dhtmlHistory.addListener(handleHistoryEvent);
}

function handleHistoryEvent(newLocation, historyData)
{
	var topicID = newLocation;

	// display this topic
	displayTopic(topicID);
}

function handleTopicChange(e)
{
	var evt = new xEvent(e);
	var target = evt.target;
	var topicID = target.getAttribute("topicID");

	// display this topic
	var content = displayTopic(topicID);

	// add this to our history
	dhtmlHistory.add(topicID, content);

	// cancel the default behavior of hyperlinks
	return evt.cancel();
}

function displayTopic(topicID)
{
	var topic;

	document.getElementById("menu").style.borderRightColor = "#FFFFFF"; // Ocultar borde menu

	// if no topic passed in then get the
	// default topic
	if (topicID == null || topicID == "")
	{
		for (var i = 0; i < topics.length; i++)
		{
			if (topics[i].isDefault)
			{
				topic = topics[i];
				break;
			}
		}
	}
	else
	{
		// fetch the topic with this ID
		for (var i = 0; i < topics.length; i++)
		{
			if (topics[i].id == topicID)
			{
				topic = topics[i];
				break;
			}
		}
	}

	// see if we have cached the contents
	// of this topic in our history storage
	var content;

	if (historyStorage.hasKey(topic.id))
	{
		content = historyStorage.get(topic.id);
	}
	else
	{
		// get the filename to load
		var url = topic.src;

		// load this file synchronously
		var request = new XMLHttpRequest();

		request.open("GET", url, false);
		request.send(null);
		content = request.responseText;

		// persist this value into our
		// history storage
		historyStorage.put(topic.id, content);
	}

	// Actualizar logo
	changeLogo(topic.site);

	// now place this content and title
	// into the HTML
	var topicTitle = document.getElementById("topicTitle");

	topicTitle.innerHTML = topic.title;
	var topicContent = document.getElementById("content");
	topicContent.innerHTML = content;

	if ((topic.site != 0) && (topic.submenu == 1))
	{
		document.getElementById("pic").innerHTML = "<img src='pic/" + topic.id + ".jpg' alt='" + topic.title + "'  width='378' height='137' />";
	}
	else
	{
		fo.write("pic");
	}

	// Refrescar contenido de menu y submenu
	displayMenus(topic.id, topic.site, topic.menu);

	// Mostrar borde menu
	document.getElementById("menu").style.borderRightColor = "#999999";

	// Mostrar titulo pagina
	if (topic.title != "freeco?")
	{
		document.title = "freeco :: " + topic.title;
	}
	else
	{
		document.title = "freeco";
	}

	return content;
}

function loadTopics()
{
	// load our remote .xml document
	// synchronously into an XML DOM object

	var topicsXMLURL = "xml/sitemap" + idioma + ".xml";

	// parse out our topics from the XML,
	// building up a JavaScript array that
	// mirrors these values
	var topics = new Array();

	$.ajax({
		type: "GET",
		url: topicsXMLURL,
		dataType: "xml",
		async: false,
		success: function(xml) {
			$(xml).find('topic').each(function(){
				var currentTopic = new Object();

				currentTopic.id = $(this).attr("id");
				currentTopic.site = $(this).attr("site");
				currentTopic.menu = $(this).attr("menu");
				currentTopic.submenu = $(this).attr("submenu");
				currentTopic.title = $(this).attr("title");
				currentTopic.src = $(this).attr("src");
				currentTopic.isDefault = $(this).attr("default");

				if (currentTopic.isDefault == null || currentTopic.isDefault == undefined)
					currentTopic.isDefault = false;

				// add a toString() method for
				// debugging
				currentTopic.toString = function()
				{
					return "[id="+this.id
						+ ", site="+this.site
						+ ", menu="+this.menu
						+ ", submenu="+this.submenu
						+ ", title="+this.title
						+ ", src="+this.src
						+ ", isDefault="
						+ this.isDefault
						+ "]";
				};

				topics.push(currentTopic);
			});
		}
	});

	return topics;
}

function displayMenus(whatID,whatSite,whatMenu)
{
	var submenu = document.getElementById("submenu");

	// reset del menu i submenu
	submenu.innerHTML = "";
	document.getElementById("menuItems").innerHTML = "";
	document.getElementById("nav2").style.display = "none";

	var topicList = document.createElement("ul");

	var menu = document.getElementById("menuItems");

	var menuList = document.createElement("ul");

	for (var i = 0; i < topics.length; i++)
	{
		if (topics[i].site == whatSite)
		{
			// use each topic to update
			// our user interface
			if (topics[i].submenu == 0)
			{
				var newTopic = document.createElement("a");
				var listItem = document.createElement("li");

				listItem.appendChild(newTopic);

				newTopic.href = topics[i].src;
				newTopic.title = topics[i].title;

				// note: avoid using the id attribute of
				// hyperlinks if they will clash with a
				// location you store into history; if these
				// values are the same you can run into
				// some strange bugs in Internet Explorer;
				// to avoid this, we use setAttribute with
				// a custom attribute named "topicID"
				newTopic.setAttribute("topicID", topics[i].id);

				if ((topics[i].id == whatID) || ((topics[i].menu == whatMenu) && (topics[i].menu!="")))
					newTopic.setAttribute("id","current");

				newTopic.innerHTML = topics[i].title;

				menuList.appendChild(listItem);
			}

			if ((topics[i].submenu == 1) && (topics[i].menu == whatMenu))
			{
				document.getElementById("nav2").style.display = "inline";

				var newTopic2 = document.createElement("a");
				var listItem2 = document.createElement("li");

				listItem2.appendChild(newTopic2);

				newTopic2.href = topics[i].src;
				newTopic2.title = topics[i].title;

				// note: avoid using the id attribute of
				// hyperlinks if they will clash with a
				// location you store into history; if these
				// values are the same you can run into
				// some strange bugs in Internet Explorer;
				// to avoid this, we use setAttribute with
				// a custom attribute named "topicID"
				newTopic2.setAttribute("topicID", topics[i].id);

				if (topics[i].id == whatID)
					newTopic2.setAttribute("id","current");

				newTopic2.innerHTML = topics[i].title;

				topicList.appendChild(listItem2);
			}
		}
	}

	menu.appendChild(menuList);
	submenu.appendChild(topicList);
}

// Para acceder a Flash desde Javascript
var movieName = "logos";

function thisMovie(movieName)
{
	// IE and Netscape refer to the movie object differently.
	// This function returns the appropriate syntax depending on the browser.
	if (navigator.appName.indexOf("Microsoft") !=-1)
	{
		return window[movieName];
	}
	else
	{
		return document[movieName];
	}
}

// Checks if movie is completely loaded.
// Returns true if yes, false if no.
function movieIsLoaded (theMovie)
{
	// First make sure the movie's defined.
	if (typeof(theMovie) != "undefined")
	{
		// If it is, check how much of it is loaded.
		return theMovie.PercentLoaded() == 100;
	}
	else
	{
		// If the movie isn't defined, it's not loaded.
		return false;
	}
}

logoShown = null;

function changeLogo(id)
{
	if (logoShown!=id)
	{
		variableFlash = "whatsite=site" + id;

		fo2.addParam("FlashVars", variableFlash);
		fo2.write("logo");

		logoShown = id;
	}

	return true;
}

// an array of our topics
var topics = new Array();

$(document).ready(function(){
	initialize();
});
