function loadModule(module){
	try{
		
		$(document.getElementById("contenidor")).load("pages/"+module+".html","",function(){
				contenidor.start();
			$.getScript("pages/"+module+".js", function() {
				contenidor.start();
			});
		});
	}catch(e){
		document.getElementById("contenidor").innerHTML="ERROR!";
	}
}

function readLocation(){
	
	var address="";
	if (document.url) address=document.URL;
	else address=document.location.href;

	var module="";
	for (var i=0;i<address.length;i++){
		if (module!="") module+=address[i];
		else if (address[i]=='#')module+=address[++i];
	}
	if (module) loadModule(module);
	else loadModule("index");
}

//crea la data adequada
function refreshClock(){
	var currentTime=new Date();

	var day=new Array('dilluns','dimarts','dimecres','dijous','divendres','dissabte','diuemnge');
	var month=new Array('gener','febrer','març',
			'abril','maig','juny',
			'juliol','agost','septembre',
			'octubre','novembre','desembre');
	
	
	var year=currentTime.getYear();
	if (year<1000)year+=1900;
	document.getElementById("clock").innerHTML=day[currentTime.getDay()]+", "+currentTime.getDate()+" de "+month[currentTime.getMonth()]+" de "+year;
}

function clickaMenu(){
	var dir="";
	dir=this.accTitle.getElementsByTagName("a")[0].href;
	var moduleName="";
		for (var i=0;i<dir.length;i++){
			if (moduleName!="")moduleName+=dir.charAt(i);
			else if (dir.charAt(i)=='#')moduleName+=dir.charAt(++i);
		}
	loadModule(moduleName);
}

//inicialització de la pàgina
var contenidor;
function init(){
	refreshClock();
	readLocation();


//	createScrollBar(document.getElementById("scroll"));
	var menu=document.getElementById("menu");
//	AccordeonStart(menu);
	var acc=new Accordeon(menu);
	acc.start(clickaMenu);
	contenidor= new ScrollBar(document.getElementById("contenidor"),"contentScroll","scrollBar");
	contenidor.start();
}

function processMenuLink(linkText){
	var realText="";
	if (linkText.charAt(0)=='#'){
		for (var i=1;i<linkText.length;i++)realText+=linkText.charAt(i);
		loadModule(realText);
	}
//	else document.location.href=link;
}

function frameCreate(full,params){
	var image=document.createElement("img");
	image.src=params.url;
	return image;
}
function addRightImage(elem,imageUrl){
	new FullImage(elem,imageUrl,frameCreate,{url:imageUrl});
}

function addWork(workTable,imgUrl,title,body,pdfUrl,client){
	{
	if (!workTable.childNodes.length){
		workTable.appendChild(document.createElement("TBODY"));
	}
	if (workTable.childNodes[0].tagName == "TBODY") workTable=workTable.childNodes[0];

		var i=0;

		var tr=document.createElement("tr");
		var tds=new Array();
		for (;i<3;i++)tds[i]=document.createElement("td");

		var image=document.createElement("img");
		image.src=imgUrl;

		var div=document.createElement("div");
		var h1=document.createElement("h1");
		h1.appendChild(document.createTextNode(title));
		div.appendChild(h1);
		div.appendChild(document.createTextNode(body));

		var link=document.createElement("a");
		link.href=pdfUrl;
		link.innerHTML="&nbsp;";
	
		tds[0].className="worlTableImageColumn";
		tds[0].appendChild(image);
		tds[1].className="worlTableDescColumn";
		tds[1].appendChild(div);
		tds[2].className="worlTableClientColumn";
		if (!client)tds[2].className="worlTablePdfColumn";
		tds[2].appendChild(link);

		i=0;

		if (imgUrl==""){
			i=1;
			tds[1].setAttribute("colspan","2");
		}

		for (;i<2;i++)tr.appendChild(tds[i]);
		if (pdfUrl)tr.appendChild(tds[i]);
		else tds[i-1].setAttribute("colspan","3");
		workTable.appendChild(tr);
	}

}


