// JavaScript Document for Inici Part

// Assigna les funcions als botons del calendari 
function setCalendarEvents(iMes, iAny) {

	oPrev = document.getElementById("prevm");
	oNext = document.getElementById("nextm");
	
	oPrev.onclick = function() {
		getMonth(iMes-1, iAny);
	};
	
	oNext.onclick = function() {
		getMonth(iMes+1, iAny);
	};

	setDayEvents();
}

// Assigna les funcions als dies del calendari
function setDayEvents() {
	oCalendarTable = document.getElementById("taula-calendari");
	aDays = oCalendarTable.getElementsByTagName("td");
	j = 0;
	aSelected = new Array();
	
	// Recorrem els dies del mes
	for(i=0; i<aDays.length; i++) {
		oCell = aDays[i];
		if (oCell.className == "calevent") {
			aSelected[j] = oCell;
			j++;
		}
	}
	
	// Marquem l'event pels seleccionats
	for(j=0; j<aSelected.length; j++) {
		oCell = aSelected[j];
		oCell.onclick = function() {
			getJornada(this.axis);
			return false;
		}
	}
	
}


// Assigna les funcions als botons de la graella 
function setGraellaEvents(iNum) {	
	aLinksGraella = document.getElementById("resultats-links").getElementsByTagName("a");
	
	if (iNum > 1 ) {
		try {
			oPrev = aLinksGraella[0];
			oPrev.onclick = function() {
				getJornada(iNum-1);
				return false;
			};
		
		} catch(exception) {}
		iAux = 1;
	} else {
		iAux = 0;
	}
	
	try {
		oNext = aLinksGraella[iAux];
		
		oNext.onclick = function() {
			getJornada(parseInt(iNum)+1);
			return false;
		};

	} catch(exception) {}
}


// Recupera per AJAX el mes indicat 
function getMonth(iMes, iAny) {
	
	if (iMes > 12) {
			iMes = 1;
			iAny++;
	} else if (iMes < 1) {
		iMes = 12;
		iAny--;
	}	
	
	sUrl = "/ajax/acalendari.php?mes=" + iMes + "&any=" + iAny;
	oCalendari = document.getElementById("taula-calendari");

	// Executem el canvi de mes al calendari 
	$("#calendari").load(sUrl, {}, function(responseText, textStatus, XMLHttpRequest){
		setCalendarEvents(iMes, iAny);
	});

}

function getJornada(iNum) {
	
	//sUrl = "ajax/agraella.php?lliga="+ iLliga + "&temp=" + iTemp + "&jorn=" + iNum;
	sUrl = "/ajax/agraella.php?jorn=" + iNum;
	
	// Executem el canvi de mes al calendari 
	$("#graella").load(sUrl, {}, function(responseText, textStatus, XMLHttpRequest){
		setGraellaEvents(iNum);
	});
	
}