
alfa=9;		// hora inicial dos horarios
omega=19;	// hora final dos horarios

//----------------------------------------------------------------------|
// Horario: gestao dos blocos, agrupados por dias e sua visualizacao	|
//									|

function Horario() {
  this.dia = new Array();
  for(var i=2; i<=6; i++)
    this.dia[i] = new Dia();
  this.mostra=mostra;
  this.marca= new Array();
  this.marcas=0;
  this.entra=entra;
}

function Dia() { 
  this.bloco = new Array(); 
  this.blocos = 0; // numero de blocos no array (!= bloco.length)
  this.tira = tira;
  this.poem = poem;
  this.sobre=false;
}

function Bloco(turma,sala,linha,cols) {
  this.turma=turma;
  this.sala=sala;
  this.linha=linha;	// linha inicial
  this.cols=cols;	// altura do bloco
  this.livre=cols;	// numero de colunas efectivamente livres
  this.cor='ligthBlue';	// cor em que apresentado o bloco
}

function entra(feira,turma,sala,hora,dura) {
  with(this) 
    dia[feira].poem(turma,sala,(hora-alfa)*2,dura*2);
}

// coloca um novo bloco mantendo ordenacao
function poem(turma,sala,linha,cols) {
  with(this) {
    for(var i=0; i<blocos; i++)
      if(bloco[i].linha>linha || 
	 (bloco[i].linha == linha && cols<bloco[i].cols))
	break;
    for(var j=blocos; j>i; j--)
      bloco[j]=bloco[j-1];
    bloco[i]=new Bloco(turma,sala,linha,cols);
    blocos++;    
  }
  sobrepoem(this);
  marca(this.bloco[i]);    
}
  
function marca(obj) {
  if(obj.cor == "lightBlue") {
    // obj.cor = "slateBlue";
    horario.marca[horario.marcas++] = obj;
  }
}

function desmarca() {
  for(var i=0; i<horario.marcas; i++) 
    if(horario.marca[i].cor == "slateBlue")
      horario.marca[i].cor = 'lightBlue';
  marcas=0;
}

// retira bloco mantendo ordenacao
function tira(turma,sala,linha,cols) {
  with(this) {
    for(var i=0; i<blocos; i++)
      if(bloco[i].turma==turma &&
	 bloco[i].sala==sala   &&
	 bloco[i].linha==linha &&
	 bloco[i].cols==cols)
	break;
    blocos--;
    for(var j=i; j<blocos; j++)
      bloco[j]=bloco[j+1];
    bloco[blocos] = null;
  }
  sobrepoem(this);
}

//verificar se há subreposições
function sobrepoem(obj) {
  with(obj) {
    obj.sobre=false;
    if(blocos>0)
      obj.bloco[0].cor='lightBlue';
    for(i=0; i<blocos; i++) {
      with(bloco[i]) {
	if(i+1 == blocos)
	  prox=99;
	else 
	  prox=bloco[i+1].linha;

	if(linha+cols > prox) {
	  obj.sobre=true;
	  obj.bloco[i+1].cor='red';
	  livre=prox-linha;
	} else {
	  if(prox<99)
	    obj.bloco[i+1].cor='lightBlue';
	  livre=cols;	
	}
      }
    }
  }  
}

// escrever o horario numa nova janela
function mostra() {
  var topo = new Array(7);
  var base = new Array(7);
  var label, sobre_turma, sobre_sala;

  document.writeln("<body bgcolor='lightGray'>");
  document.writeln("<table border=1>");
  document.write("<tr><th>Hora</th>");
  for(d=2;d<=6;d++) {
    document.write("<th width=200 >"+d+"</th>");    
    topo[d]=0;
    base[d]=0;
  }
  document.writeln("<th>Hora</th></tr>");  
  

  for(var i=0;i<(omega-alfa)*2;i++) {
    h=alfa+(i-(i%2))/2;
    m=(i%2)*3;
    document.write("<tr><th>"+h+":"+m+"0</th>");
    sobre_turma = sobre_sala = "";
    for(d=2;d<=6;d++) {

      // passar à frente os blocos sem espaço livre (totalmente sobrepostos)
      with(this.dia[d])
	while(topo[d]<blocos && 
	      bloco[topo[d]].linha == i && 
	      bloco[topo[d]].livre == 0) {
	  sobre_turma += (" & "+ bloco[topo[d]].turma);
	  sobre_sala  += (" & "+ bloco[topo[d]].sala);
	  with(bloco[topo[d]])
	  topo[d]++;
	}
      
      if(this.dia[d].blocos<=topo[d]) { 
	// sem entradas neste dia, se existe espaco livre ocupar ate ao fim
	if(i==base[d]) {
	  linha=(omega-alfa)*2;
	  document.write("<td bgcolor='white' ");
	  document.write("rowspan="+(linha-base[d])+">");
	  document.writeln("&nbsp</td>"); 
	  base[d]=linha;
	}
	continue;    
      }       
      with(this.dia[d].bloco[topo[d]]) {
	if(linha==i) {
	  //formata as etiquetas consoante o espaço livre
	  if(livre == 0) 	  //sem switch por causa do JS 1.0
	    alert("erro: não há espaço para este bloco");
	  else if(livre==1) { 
	    label =h+":"+m+"0 <b>"+turma+sobre_turma+"</b> "; 
	    label+=" "+sala+sobre_sala; 
	  } else if(livre==2) { 
	    var liga=(sobre_sala=="" ? "Email: ": "")
	    label =h+":"+m+"0 &nbsp;&nbsp;"+liga+sala+sobre_sala; 
	    label+="<br><center><b>"+turma+sobre_turma+"</b></center>"; 
	  } else {
	    label =h+":"+m+"0<br>";
	    label+="<center><b>"+turma+sobre_turma+"</b></center><br>";
	    label+="<center>"+""+sala+sobre_sala+" "+"</center>";	    
	  }
	  document.write("<td valign='top' bgcolor='"+cor+"' ");
	  document.writeln("rowspan="+livre+">");
	  document.writeln(""+label+"</td>");

	  sobre_turma = sobre_sala = "";      
	  base[d] += livre;
	  topo[d]++;	    
	  
	} else if(i==base[d]) {
	  document.write("<td bgcolor='white' ");
	  document.write("rowspan="+(linha-base[d])+">");
	  document.writeln("&nbsp</td>"); 
	  base[d]=linha;
	}
      }
    }
    document.write("<th>"+h+":"+m+"0</th></tr>");
  }
  document.writeln("</table>");

  /*
  document.writeln("<table>");
  var prev="";
  for(var c=0;c < dados.length; c++)
    with(dados[c])
    {
      if(sigla==prev)
	continue;
      prev=sigla;
      document.writeln("<tr><td>"+sigla+"</td><td>"+nome+"</td></tr>");
    }
  document.writeln("</table>");
  */
}


//  Horario								|
//									|
//----------------------------------------------------------------------|


