// lastmod 12 settembre 2011
// lastmod  7 luglio 2011 - adattamenti memento
// lastmod 11 novembre 2010  - adattamenti per il nuovo torinocultura
// lastmod 10 aprile 2008
// lastmod 31 marzo 2008
// lastmod 21 marzo 2008
// lastmod 15 marzo 2008
// lastmod 13 marzo 2008
// lastmod 10 marzo 2008
// lastmod  8 marzo 2008
// lastmod  6 marzo 2008
// lastmod  4 marzo 2008
// lastmod 29 febbraio 2008
// lastmod 28 febbraio 2008
// lastmod 16 febbraio 2008

// script di Sebastiano Midolo (sebastiano.midolo@comune.torino.it), per il calendario in home page


mesi = [null, 'gennaio','febbraio','marzo','aprile','maggio','giugno','luglio','agosto','settembre','ottobre','novembre','dicembre'];

////////////////////////////////////////////////////////////
// Tabella dei festivi
festivi = {};
festivi['gennaio'] = [1, 6];
festivi['febbraio'] = [];
festivi['marzo'] = [17];
festivi['aprile'] = [25];
festivi['maggio'] = [1];
festivi['giugno'] = [2, 24];
festivi['luglio'] = [];
festivi['agosto'] = [15];
festivi['settembre'] = [];
festivi['ottobre'] = [];
festivi['novembre'] = [1];
festivi['dicembre'] = [8, 25];

// Altezze calendario
altezze_accessibile = {4: '6.3em', 5: '8.0em', 6: '9.7em'};
altezze_normale = {4: '90px', 5: '100px', 6: '116px'};

////////////////////////////////////////////////////////////

function genera_calendario(cal,targetlink) {
    var ladata = new Date(),
	makelink=true,
	oggi=new Date().getDate(),
	spanclass = {0: 'festivo',1: 'lunedi'},
	tmpdata = new Date(),
	anchor, link, span, text;

    ladata.setDate(1);
    tmpdata.setDate(32);

    var anno = ladata.getFullYear(),
	mese = ladata.getMonth()+1,
	primoGiorno = ladata.getDay()-1,
        righe_calendario = 1,
	ultimoGiorno = 32 - tmpdata.getDate(); // grazie a http://snippets.dzone.com/posts/show/2099
	
    if(primoGiorno==-1) {
	primoGiorno=6;
	}
	  if(primoGiorno==0) {
        righe_calendario=0;
    }
    while(primoGiorno--) {
	span = document.createElement('span');
	span.setAttribute('class','vuoto');
	span.appendChild(document.createTextNode(''));
	cal.appendChild(span);
    }

    for (giorno = ladata.getDate(); giorno<=ultimoGiorno; giorno++) {
	span = document.createElement('span');
	ladata.setDate(giorno);
	text = document.createTextNode(giorno);
	makelink=true;
	if (ladata.getDay()==1) righe_calendario++;
	if (ladata.getDay()==0) makelink=false;

	for (var i=0; i<festivi[mesi[mese]].length; i++) {
	    if (festivi[mesi[mese]][i]==giorno) {
		makelink=false;
		break;
	    }
	}

	if (makelink==true) {
	    anchor = document.createElement('a');
	    // Fino al 14 novembre 2010
	    // link = targetlink + "&dataDa=" + giorno + "%2F" + mese + "%2F" + anno + "&dataAl=" + giorno + "%2F" + mese + "%2F" + anno;

	    // Dal 15 novembre 2010 : daDataPag=2010-11-01&aDataPag=2010-11-30
	    link = targetlink + "&daDataPag=" + anno + "-" + mese + "-" + giorno + "&aDataPag=" + anno + "-" + mese + "-" + giorno;
            // Dal 7 luglio 2011:
            link = targetlink + "&currDate=" + 2012 + "-" + mese + "-" + giorno;    
	    anchor.setAttribute('href',link);
	    // anchor.setAttribute('rel','esterno');
	    anchor.setAttribute('title',giorno + ' ' + mesi[mese]);
	    anchor.appendChild(text);
	    if (giorno==oggi) {
		// anchor.setAttribute('class','oggi');
		
	    }
	    span.appendChild(anchor);
	} else {
	    if (giorno==oggi) {
		span.setAttribute('class','oggi' + spanclass[ladata.getDay()]);
	    } else {
		// span.setAttribute('class',spanclass[ladata.getDay()]);

		$(span).setStyle({
		    color: '#c00',
		    borderColor: '#f00' 
		});
		// #calendario span.festivo {color:#c00; border-color:#f00;} 
	    }
	    span.appendChild(text);
	}

	cal.appendChild(span);
	if (giorno==ultimoGiorno) continue;
	span = document.createElement('span');
	//span.setAttribute('class','no');
	text = document.createTextNode(' | ');
	span.appendChild(text);
	cal.appendChild(span);
	// span.innerHTML='|';
	$(span).setStyle({
	    display: 'none'
	});
    }

    if(css=='accessibile') {
	var altezza = altezze_accessibile;
    } else {
	var altezza = altezze_normale;
    }
    $(cal).setStyle({
	height: altezza[righe_calendario]
    });
}

function x_intesta_calendario_home(destdiv,targetlink) {
    var ladata = new Date(),
	tmpdata = new Date(),
	anchor, link, span, strong, text;

    tmpdata.setDate(32);

    var anno = ladata.getFullYear(),
	mese = ladata.getMonth()+1,
	ultimoGiorno = 32 - tmpdata.getDate(); // grazie a http://snippets.dzone.com/posts/show/2099

    anchor = document.createElement('a');
    anchor.setAttribute('id','strumenti');
    // daDataPag=2010-11-15&aDataPag=2010-11-15
    link = targetlink + "&daDataPag=" + anno + "-" + mese + "-01&aDataPag=" + anno + "-" + mese + "-" + ultimoGiorno;
    link = targetlink; // link senza specificazione temporale
    anchor.setAttribute('href',link);
    // anchor.setAttribute('rel','esterno');

    anchor.setAttribute('title','Oggi in biblioteca');
    if (css=="null" || css=='normale') {
	anchor.setAttribute('style', 'no-repeat center top;');
    }
    span = document.createElement('span');
    strong = document.createElement('strong');
    text = document.createTextNode('Oggi in biblioteca');
    strong.appendChild(text);
    span.appendChild(strong);
    anchor.appendChild(span);
    destdiv.appendChild(anchor);
}


///////////////// public api

function intesta_calendario_home(targetlink) {
	// Fino al 14 novembre 2010
    // targetlink = "http://www.torinocultura.it/portal/page?_pageid=67,1667750&_dad=portal&_schema=PORTAL&tipoRicerca=generale&testoCosa=&testoLuogo=biblioteca%20AND%20musicale%20OR%20biblioteca%20AND%20civica%20OR%20punto%20AND%20prestito%20OR%20mausoleo&idPubblico=";

    // Dal 15 novembre 2010
    // targetlink = "http://www.torinocultura.it/servizionline/memento/include.php?context=ajax&action=cerca&findTag=BCT&urlDest=http://www.torinocultura.it/eventi/index.shtml";
    // daDataPag=2010-11-15&aDataPag=2010-11-15

    targetlink="http://www.comune.torino.it/cultura/biblioteche/agenda/servizionline/memento/include.php?refCanale=-1&currDate=today&urlDest=http://www.comune.torino.it/cultura/biblioteche/agenda/&context=memento&action=home"
    x_intesta_calendario_home(document.getElementById('mese'),targetlink);
}

function calendario_home() {
    // Fino al 14 novembre 2010
    // targetlink = "http://www.torinocultura.it/portal/page?_pageid=67,1667750&_dad=portal&_schema=PORTAL&tipoRicerca=generale&testoCosa=&testoLuogo=biblioteca%20AND%20musicale%20OR%20biblioteca%20AND%20civica%20OR%20punto%20AND%20prestito%20OR%20mausoleo&idPubblico=";
    // Dal 15 novembre 2010
    // targetlink = "http://www.torinocultura.it/servizionline/memento/include.php?context=ajax&action=cerca&findTag=BCT&urlDest=http://www.torinocultura.it/eventi/index.shtml";
    // Dal 7 luglio 2011
    targetlink = "http://www.comune.torino.it/cultura/biblioteche/agenda/servizionline/memento/include.php?refCanale=-1&urlDest=http://www.comune.torino.it/cultura/biblioteche/agenda/&context=memento&action=home";
    //   &currDate=2011-07-05";
    genera_calendario(document.getElementById('calendario'),targetlink);
}


