
var agendaCal = null;

function mesIsSpecialAgenda(year, month) {
	var strAnoMes = year + "" + month;
    var m = SPECIAL_DAYS_A[strAnoMes];
    if(!m) return false;
    if(m.size() > 0){
    	return true;
    }
    
    return false;
};

function dateIsSpecialAgenda(year, month, day) {
	var strAnoMes = year + "" + month;
    var m = SPECIAL_DAYS_A[strAnoMes];
    if (!m) return false;
    for (var i in m) if (m[i] == day) return true;
    return false;
};

function dateIsSpecialStyle(year, month, day) {
	var strAnoMes = year + "" + month;
    var m = SPECIAL_DAYS_A[strAnoMes];
    var t = TIPO_EVENTO_A[strAnoMes];
    if (!m) return false;
    for (var i in m){ 
    	if (m[i] == day) 
    		return t[i];
    }
    
    return "";
};

  function dateChangedAgenda(agenda) {
	  if (agenda.dateClicked) {
	      // OK, a date was clicked, redirect to /yyyy/mm/dd/index.php
	      var year = agenda.date.getFullYear();
	      var month = agenda.date.getMonth();     // integer, 0..11
	      var day = agenda.date.getDate();      // integer, 1..31
	      var m2 = month + 1;
	      // redirect...
	      
	    var strano = year +''+month;
	    var m = SPECIAL_DAYS_A[strano];
	    var cont = NUM_CONTEUDOS_A[strano];
	    if (!m) return false;
	    for (var i in m) {
	        if (m[i] == day){ 
	        	//SetCookie( 'selopt', 'texto23472' ); 
	    		//SetCookie( 'lastopend', 'opt23471' );
	    		return window.location = "displayconteudo.do2?numero=" + cont[i] + "&date=" + year + "-" + m2 + "-" + day;
	        }
	    }
	       
	    }
	  return false; 
  };

  function ourDateStatusFuncAgenda(date, y, m, d) {

    if (dateIsSpecialAgenda(y, m, d)){
    	var strTipo = dateIsSpecialStyle(y, m, d);
    	
    	if(strTipo == "Seminários")
    		return "seminarios";
    	else if(strTipo == "Feiras")
    		return "feiras";
    	else if(strTipo == "Workshops")
    		return "workshops";
    	else if(strTipo == "organizacional")
    		return "corganizacional";
    	else if(strTipo == "defesa")
    		return "cdefesa";
    	else if(strTipo == "sistemas")
    		return "csistemas";
    	else if(strTipo == "negócio")
    		return "cnegocio";
    	else if(strTipo == "território")
    		return "cterritorio";
    	else if(strTipo == "central")
    		return "ccentral";
    	else if(strTipo == "municipal")
    		return "cmunicipal";
    	else if(strTipo == "administração")
    		return "cadministracao";
    	else if(strTipo == "projectos")
    		return "cprojectos";
    	else if(strTipo == "turismo")
    		return "cturismo";
    	else 
    		return "outros";
    }else
      return false; // other dates are enabled
      // return true if you want to disable other dates
  };

// code to change the active stylesheet
function setActiveStyleSheet(link, title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (oldLink) oldLink.style.fontWeight = 'normal';
  oldLink = link;
  link.style.fontWeight = 'bold';
  return false;
}

// This function gets called when the end-user clicks on some date.
function selectedAgenda(agendaCal, date) {
  agendaCal.sel.value = date; // just update the date in the input field.
  if (agendaCal.dateClicked && (agendaCal.sel.id == "sel1" || agendaCal.sel.id == "sel3"))
    // if we add this call we close the agenda on single-click.
    // just to exemplify both cases, we are using this only for the 1st
    // and the 3rd field, while 2nd and 4th will still require double-click.
    agendaCal.callCloseHandler();
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the agenda without
// destroying it.
function closeHandler(agendaCal) {
  agendaCal.hide();                        // hide thagendaar
//  cal.destroy();
  _dynarch_popupAgenda = null;
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showAgenda(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupAgenda != null) {
    // we already have some calendar created
    _dynarch_popagendaar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var agendaCal = new Agenda(1, null, selectedAgenda, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      agendaCal.showsTime = true;
      agendaCal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      agendaCal.showsOtherMonths = true;
    }
    _dynarch_popupAgenda = agendaCal;                  // remember it in the global var
    agendaCal.setRange(2000, 2070);        // min/max year allowed.
    agendaCal.create();
  }
  _dynarch_popupAgenda.setDateFormat(format);    // set the specified date format
  _dynarch_popupAgenda.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupAgenda.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupAgenda.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

// If this handler returns true then the "date" given as
// parameter will be disabled.  In this example we enable
// only days within a range of 10 days from the current
// date.
// You can use the functions date.getFullYear() -- returns the year
// as 4 digit number, date.getMonth() -- returns the month as 0..11,
// and date.getDate() -- returns the date of the month as 1..31, to
// make heavy calculations here.  However, beware that this function
// should be very fast, as it is called for each day in a month when
// the calendar is (re)constructed.
function isDisabled(date) {
  var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}

function flatSelected(agendaCal, date) {
  var el = document.getElementById("previewAgenda");
  el.innerHTML = date;
}

function showFlatAgenda() {
  var parent = document.getElementById("previewAgenda");

  // construct a calendar giving only the "selected" handler.
  agendaCal = new Agenda(0, null, dateChangedAgenda /*, flatSelected*/);
	
  // hide week numbers
  agendaCal.weekNumbers = false;

  // We want some dates to be disabled; see function isDisabled above
  //cal.setDisabledHandler(isDisabled);
  agendaCal.setDateFormat("%A, %B %e");

  agendaCal.setDateStatusHandler(ourDateStatusFuncAgenda);
  // this call must be the last as it might use data initialized above; if
  // we specify a parent, as opposite to the "showAgenda" function above,
  // then we create a flat calendar -- not popup.  Hidden, though, but...
  agendaCal.create(parent);
  
  // ... we can show it here.
  agendaCal.show();
  
  showHiddeLegenda(agendaCal);
}

function proxMes(){
	var el = document.getElementById("previewAgenda");
	Agenda.setNextMonth(el, agendaCal);
	showHiddeLegenda(agendaCal);
}

function prevMes(){
	var el = document.getElementById("previewAgenda");
	Agenda.setPrevMonth(el, agendaCal);
	showHiddeLegenda(agendaCal);
}

function showHiddeLegenda(agenda){
	
	var year = agenda.date.getFullYear();
    var month = agenda.date.getMonth();     // integer, 0..11
    
    var agd = $('agendaLegenda'); 
    
    if(mesIsSpecialAgenda(year, month)){
    	this.showElem(agd);
    }else{
    	$('agendaLegendaSeminarios').style.visibility = "hidden";
        $('agendaLegendaFeiras').style.visibility = "hidden";
        $('agendaLegendaWorkshops').style.visibility = "hidden";
        $('agendaLegendaOutros').style.visibility = "hidden";
        
        $('agendaLegendaSDefesa').style.visibility = "hidden";
        $('agendaLegendaMAMunicipal').style.visibility = "hidden";
        $('agendaLegendaMACentral').style.visibility = "hidden";
        $('agendaLegendaGITerritorio').style.visibility = "hidden";
        $('agendaLegendaSNegocio').style.visibility = "hidden";
        $('agendaLegendaCDISistemas').style.visibility = "hidden";
        $('agendaLegendaGAdministracao').style.visibility = "hidden";
        $('agendaLegendaGAdministracao').style.visibility = "hidden";
        $('agendaLegendaMOrganizacional').style.visibility = "hidden";
        $('agendaLegendaHTurismo').style.visibility = "hidden";
        $('agendaLegendaGProjectos').style.visibility = "hidden";
        
        
    	return;
    }
    
    var sem = $('agendaLegendaSeminarios');
    var feir = $('agendaLegendaFeiras');
    var wor = $('agendaLegendaWorkshops');
    var outr = $('agendaLegendaOutros');
    
    var sdef = $('agendaLegendaSDefesa');
    var mamun = $('agendaLegendaMAMunicipal');
    var macen = $('agendaLegendaMACentral');
    var giter = $('agendaLegendaGITerritorio');
    var sneg = $('agendaLegendaSNegocio');
    var cdisis = $('agendaLegendaCDISistemas');
    var gadmin = $('agendaLegendaGAdministracao');
    var gproj = $('agendaLegendaGProjectos');
    var morg = $('agendaLegendaMOrganizacional');
    var htur = $('agendaLegendaHTurismo');
    
	
	if(mesIsSpecialLegenda(year, month, "território"))
		this.showElem(giter);
    else
    	this.hiddeElem(giter);
	
	if(mesIsSpecialLegenda(year, month, "central"))
		this.showElem(macen);
    else
    	this.hiddeElem(macen);
	
	if(mesIsSpecialLegenda(year, month, "municipal"))
		this.showElem(mamun);
    else
    	this.hiddeElem(mamun);
	
	if(mesIsSpecialLegenda(year, month, "administração"))
		this.showElem(gadmin);
    else
    	this.hiddeElem(gadmin);
	
	if(mesIsSpecialLegenda(year, month, "projectos"))
		this.showElem(gproj);
    else
    	this.hiddeElem(gproj);
	
	if(mesIsSpecialLegenda(year, month, "organizacional"))
    	this.showElem(morg);
    else
    	this.hiddeElem(morg);

	if(mesIsSpecialLegenda(year, month, "defesa"))
		this.showElem(sdef);
    else
    	this.hiddeElem(sdef);
    
	if(mesIsSpecialLegenda(year, month, "sistemas"))
		this.showElem(cdisis);
    else
    	this.hiddeElem(cdisis);
	
	if(mesIsSpecialLegenda(year, month, "negócio"))
		this.showElem(sneg);
    else
    	this.hiddeElem(sneg);
	
	if(mesIsSpecialLegenda(year, month, "turismo"))
		this.showElem(htur);
    else
    	this.hiddeElem(htur);
	
    if(mesIsSpecialLegenda(year, month, "Seminários"))
    	this.showElem(sem);
    else
    	this.hiddeElem(sem);
    
    if(mesIsSpecialLegenda(year, month, "Feiras"))
    	this.showElem(feir);
    else
    	this.hiddeElem(feir);
    
    if(mesIsSpecialLegenda(year, month, "Workshops"))
    	this.showElem(wor);
    else
    	this.hiddeElem(wor);
    
    if(mesIsSpecialLegenda(year, month, "Agenda"))
    	this.showElem(outr);
    else
    	this.hiddeElem(outr);
    
    if(mesIsSpecialLegenda(year, month, ""))
	   this.showElem(outr);
    else
	   this.hiddeElem(outr);
    
}

function mesIsSpecialLegenda(year, month, tipo) {
	var strAnoMes = year + "" + month;
    var t = TIPO_EVENTO_A[strAnoMes];
    if (!t) return false;
    for (var i in t){ 
    	if (t[i] == tipo) 
    		return true;
    }
    
    return false;
};

function hiddeElem(el){
	el.style.visibility = '';
    el.style.display = 'none';
}

function showElem(el){
	el.style.visibility = 'visible';
    el.style.display = 'inline';
}
