/**
 * Lista de indisponibilidade do servico.
 */


function printStops(startIndex, endIndex) {
  if (startIndex == endIndex) {
		 step = 1;
	} else {
	  step = (endIndex - startIndex) / Math.abs(endIndex - startIndex);
	}
	count = Math.abs(endIndex - startIndex) + 1;

	i = startIndex;
	do {
  		duracao = parseDate(indisp[i][FIM]).getTime() - parseDate(indisp[i][INICIO]).getTime();
  		document.write(indisp[i][INICIO] + 'h a ' + indisp[i][FIM] + 'h (' + formatDuracao(duracao) + ')');
		if (indisp[i][DESCRICAO] != null) {
			document.write(' - ' + indisp[i][DESCRICAO] + '<br>');
		} else {
			document.write('<br>');
		}
		i += step;
		count--;
	} while (count > 0);
}

function formatDuracao(duracao) {
	 dia = 1000 * 60 * 60 * 24;
	 horas = 1000 * 60 * 60;
	 minutos = 1000 * 60;
	 
   out = '';
	 out = out + Math.floor(duracao / dia) + 'd ';
	 duracao = duracao % dia;
	 out = out + Math.floor(duracao / horas) + 'h ';
	 duracao = duracao % horas;
	 out = out + Math.floor(duracao / minutos) + 'm ';
	 
	 return out;
}

function findNextStops() {
	hoje = new Date();
  i = indisp.length - 1;
  
  while (i >= 0) {
  		dia = parseDate(indisp[i][INICIO]);
  		if (dia.getTime() <= hoje.getTime()) {
				 i++;
  			 break;
  		}
  		i--;
  }
  if (i < 0) i = 0;

	return i;
}

function parseDate(stringDate) {
				 var data = new Date();

				 data.setDate(stringDate.substr(0,2));
				 data.setMonth((stringDate.substr(3,2) - 1));
				 data.setFullYear(stringDate.substr(6,4));
				 data.setHours(stringDate.substr(11,2));
				 data.setMinutes(stringDate.substr(14,2));
				 data.setSeconds(0);
				 data.setMilliseconds(0);
				 
				 return data;
}

function printNextStops() {
  nextStops = findNextStops();

  if (nextStops < indisp.length) {
		 printStops(nextStops, indisp.length - 1);
	} else {
		document.write("Nenhuma parada programada<br>");
	}
}

function isStopped() {
  hoje = new Date();
	
	for (i = indisp.length - 1; i >= 0; i--) {
			if (parseDate(indisp[i][FIM]).getTime() < hoje) {
				 break;
			}
			if ((parseDate(indisp[i][INICIO]).getTime() < hoje) && (parseDate(indisp[i][FIM]).getTime() > hoje)) {
				 return true;
			}
	}
	
	return false;
}

function getCurrentStop() {
  hoje = new Date();
	
	for (i = indisp.length - 1; i >= 0; i--) {
			if (parseDate(indisp[i][FIM]).getTime() < hoje) {
				 break;
			}
			if ((parseDate(indisp[i][INICIO]).getTime() < hoje) && (parseDate(indisp[i][FIM]).getTime() > hoje)) {
				 return i;
			}
	}
	
	return -1;
}

function printCurrentStop() {
				 stop = getCurrentStop();
				 
				 if (stop > 0) {
				 		printStops(stop, stop);
				 }
}

function printPreviousStops() {
  printStops(findNextStops() - 1, 0);
}