// JScript source code
/************************************************************************************************************
Ajax dynamic articles
Copyright (C) 2006  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com


************************************************************************************************************/
var ajaxObjects = new Array();
var ajax = new sack();
var articleListObj;
var activeArticle = false;
var clickedArticle = false;

var menuObj	// Reference to menu content <div>

var contentObj		// Reference to content <div>
var sidecontent2 	// Reference to content <div>
var firstArticle = "";

function mouseoverArticle()	// Highlight article
{
	if(this==clickedArticle)return;
	if(activeArticle && activeArticle!=this){
		if(activeArticle==clickedArticle)
			activeArticle.className='articleClick';
		else
			activeArticle.className='';

	}
	this.className='articleMouseOver';
	activeArticle = this;	// Storing reference to this article
}

function showMenu(ajaxIndex)	// Displaying content in the content <div>
{
	if (ajaxObjects[ajaxIndex].responseStatus[0] == 500) {
		alert(ajaxObjects[ajaxIndex].response)
		return;
	}
	var lastlvl = -1;
	var response = ajaxObjects[ajaxIndex].responseXML;
	var x = response.getElementsByTagName("Item")
	var boxDiv = ""
  	for (i=0;i<x.length;i++)
  	{
		var lvl = XmlGet(x, i, "lvl");
		var desc = XmlGet(x, i, "desc");
		var parentId = XmlGet(x, i, "parentId");
		var modulo = XmlGet(x, i, "modulo");
		if (modulo=='null')
			var showSub = "onclick=\"listAjaxFile('" + desc + "','" + parentId + "');return false;\""
		else
			var showSub = "onclick=\"" + modulo + "_initAjax();return false;\""

		if (lvl==lastlvl) boxDiv += "</li>"
		if (lvl>lastlvl) boxDiv += "<ul>"
		if (lvl<lastlvl) boxDiv += "</ul></li>"

		lastlvl = lvl
		boxDiv += "<li id=top" + i + "><a href=\"#\" " + showSub + ">" + desc + "</a><span></span>";
   	}
	boxDiv += "</li>"
	boxDiv += "</ul>";
	menuObj.innerHTML = boxDiv;
	ajaxObjects[ajaxIndex] = false;
	showSideMenu();
}

function showSideMenu()	{
	WeekGames_initAjax()
}

function showListContent(ajaxIndex)	// Displaying content in the content <div>
{
	if (ajaxObjects[ajaxIndex].responseStatus[0] == 500) {
		alert(ajaxObjects[ajaxIndex].response)
		return;
	}
	var i;
	var response = ajaxObjects[ajaxIndex].responseXML;
	var articles = articleListObj.getElementsByTagName('LI');
	x = response.getElementsByTagName("Item")

	if (x.length > 0)initArticles()

	contentMode	= "Add"
 	for (i=0;i<x.length;i++)
 	{
		var testo = HTMLDecode(XmlGet(x, i, "intro"));
		var id = XmlGet(x, i, "id");
    articles[i].innerHTML = "<div style=\"width: 200px; float: left; height: 60px; cursor: hand\">" + testo + "</div>";
		articles[i].id = "article" + id;
    articles[i].style.display = "block";
    articles[i].style.visibility = 'visible';
    articles[i].style.background = "#fff";
		if (i <= 3)
			if (i == 0)
				getAjaxFile(id, "Add");
			else
				getAjaxFile(id, "Append");
 	}
	ajaxObjects[ajaxIndex] = false;
}

function showContent(ajaxIndex, contentMode)	// Displaying content in the content <div>
{
	if (ajaxObjects[ajaxIndex].responseStatus[0] == 500) {
		alert(ajaxObjects[ajaxIndex].response)
		return;
	}
	// contentObj.innerHTML = ajaxObjects[ajaxIndex].response;	// ajax.response is a variable that contains the content of the external file
	var i;
	var response = ajaxObjects[ajaxIndex].responseXML;
	x = response.getElementsByTagName("Item")
	var testo = "";
	if (contentMode	== "Append") testo += contentObj.innerHTML

	testo += "<div class='stacks_top'>";
	testo += "<div class='stacks_out' style='WIDTH: 100%'>"
	testo += "<div class='stacks_in' style='BORDER-RIGHT: #b7b7b7 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #b7b7b7 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; MARGIN: 2px; BORDER-LEFT: #b7b7b7 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: #b7b7b7 1px solid'>&nbsp;"
	testo += HTMLDecode(XmlGet(x, 0, "testo"));
	testo += "<BR>"
	testo += "</div>"
	testo += "</div>"
	testo += "</div>"

	contentObj.innerHTML = testo
	ajaxObjects[ajaxIndex] = false;
}

function showWaitMessage(pageDiv)
{
	if (pageDiv=="menu") menuObj.innerHTML = 'Finding article.....<br>Please wait';
	if (pageDiv=="content") contentObj.innerHTML = 'Finding article.....<br>Please wait';
}

function loadAjaxMenu()
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = "AJAXServer/pagine.aspx";
	ajaxObjects[ajaxIndex].onLoading = showWaitMessage("menu");	// Action when AJAX is loading the file
	ajaxObjects[ajaxIndex].setVar('menu',' ');
	ajaxObjects[ajaxIndex].onCompletion = function(){ showMenu(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}

function getAjaxFile(id, contentMode)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = "AJAXServer/pagine.aspx";
	ajaxObjects[ajaxIndex].onLoading = showWaitMessage("content");	// Action when AJAX is loading the file
	ajaxObjects[ajaxIndex].setVar('show',' ');
	ajaxObjects[ajaxIndex].setVar('PageId',id);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showContent(ajaxIndex, contentMode); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}

function listAjaxFile(categoria, sezione)
{
	if (typeof(categoria)=="undefined") categoria= " ";
	if (categoria=="null") categoria= " ";
	if (typeof(sezione)=="undefined") sezione= " ";
	if (sezione=="null") sezione= " ";
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = "AJAXServer/pagine.aspx";
	ajaxObjects[ajaxIndex].onLoading = showWaitMessage("list");	// Action when AJAX is loading the file
	ajaxObjects[ajaxIndex].setVar('list',categoria);
	ajaxObjects[ajaxIndex].setVar('sezione',sezione);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showListContent(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}

function selectArticle()	// User have clicked on an article
{
	getAjaxFile(this.id.substring(7),"Add");	// Calling the getAjasFile function. argument to the function is id of this <li> + '.html', example "article1.html"
	if(clickedArticle && clickedArticle!=this)clickedArticle.className='articleMouseOver';
	this.className='articleClick';
	clickedArticle = this;
}


function initAjaxDemo()
{
	initModules()

	menuObj = document.getElementById('toolbar');
	loadAjaxMenu();

	articleListObj = document.getElementById('articleList');

	initArticles()

	contentObj = document.getElementById('padding');
	sidecontent2 = document.getElementById('sidebarSubContainer2');
	listAjaxFile();
}
function initArticles() {
	var articles = articleListObj.getElementsByTagName('LI');
	if(articles.length == 0) {
    articleListObj.innerHTML = "<ul><li></li><li></li><li></li></ul><span><br><hr><br></span>"
	}

	for(var no=0;no<articles.length;no++){
		articles[no].onmouseover = mouseoverArticle;
		articles[no].onclick = selectArticle;
		articles[no].style.display = "none";
   		articles[no].style.visibility = 'hidden';
	}

}

function LoadScript(url)
{
   	var e = document.createElement("script");
   	e.src = url;
   	e.type="text/javascript";
   	document.getElementsByTagName("head")[0].appendChild(e);
}

function LoadStyle(url, modulo) {
	var cssNode = document.createElement('link');
	cssNode.type = 'text/css';
	cssNode.rel = 'stylesheet';
	cssNode.href = url
	cssNode.media = 'screen';
	cssNode.title = modulo;
	document.getElementsByTagName("head")[0].appendChild(cssNode);
}

// carcamento moduli
function initModules() {
 	LoadScript("Modules/Guestbook/Ajax/script.js");
 	LoadScript("Modules/WeekGames/Ajax/script.js");
 	LoadScript("Modules/Calendari/Ajax/script.js");

 	LoadStyle("Modules/Guestbook/Ajax/style.css","Guestbook");
 	LoadStyle("Modules/Calendari/Ajax/style.css","Calendari");
 	LoadStyle("Modules/WeekGames/Ajax/style.css","WeekGames");
}
