/**
 * JS Cron
 * Um cronômetro simples em JavaScript para seu navegador
 *
 * Por Alessandro Santos - http://alessandrosantos.com.br/
 *
 */

function Obj ( ID ) {
	return document.getElementById ( ID );
	};

function HMSParaSegundos ( HMS ) {
	var Pedaco = HMS.split ( ':' );
	var Horas = parseInt ( Pedaco[0] ) * 3600;
	var Minutos = parseInt ( Pedaco[1] ) * 60;
	var Segundos = parseInt ( Pedaco[2] );
	var Total = Horas + Minutos + Segundos;
	return Total;
};

function SegundosParaHMS ( Segundos ) {
	var H = parseInt ( Segundos / 3600 );
	var M = parseInt ( ( Segundos % 3600 ) / 60 );
	var S = parseInt ( Segundos - ( ( H * 3600 ) + ( M * 60 ) ) );
	if ( H < 10 ) { H = '0'+H; };
	if ( M < 10 ) { M = '0'+M; };
	if ( S < 10 ) { S = '0'+S; };
	return H+':'+M+':'+S;
};

var Cron = new Array();

var JSCron = function ( ) {
	var HTML = new Documento ( );
	this.CriaCronRegressivo = function ( ) {
		var CronID = Cron.length;
		var Nome = Obj ( 'InputCRNome' ).value;
		var Ciclico = Obj ( 'InputCRCiclico' ).checked;
		var Horas = Obj ( 'InputCRHoras' ).value;
		var Minutos = Obj ( 'InputCRMinutos' ).value;
		var Segundos = Obj ( 'InputCRSegundos' ).value;
		var Alerta = Obj ( 'InputCRAlerta' ).value;
		Cron.push ( new CronRegressivo ( CronID , Nome , Ciclico , Horas , Minutos , Segundos , Alerta ) );
		if ( Cron[CronID].Estado == true ) {
			HTML.CriaDIV ( CronID , 'R' , Cron[CronID].Ciclico , Cron[CronID].Alerta , Cron[CronID].Relogio );
			Obj ( 'InputCRNome' ).value = '';
			Obj ( 'InputCRCiclico' ).checked = true;
			Obj ( 'InputCRHoras' ).value = '00';
			Obj ( 'InputCRMinutos' ).value = '00';
			Obj ( 'InputCRSegundos' ).value = '00';
			Obj ( 'InputCRAlerta' ).value = '';
		} else {
			delete ( Cron[CronID] );
		};
	};
	this.CriaCronProgressivo = function ( ) {
		var CronID = Cron.length;
		var Nome = Obj ( 'InputCPNome' ).value;
		Cron.push ( new CronProgressivo ( CronID , Nome ) );
		if ( Cron[CronID].Estado == true ) {
			HTML.CriaDIV ( CronID , 'P' , Cron[CronID].Relogio );
			Obj ( 'InputCPNome' ).value = '';
		} else {
			delete ( Cron[CronID] );
		};
	};
	this.ApagaCron = function ( ID ) {
		HTML.InutilizaDIV ( ID );
		Cron[ID].Apaga();
		Cron[ID] = '';
	};
};

var CronRegressivo = function ( CronID , Nome , Ciclico , Horas , Minutos , Segundos , Alerta ) {
	this.CronID = CronID;
	this.Nome = Nome;
	this.Ciclico = Ciclico;
	this.Horas = parseInt ( Horas );
	this.Minutos = parseInt ( Minutos );
	this.Segundos = parseInt ( Segundos );
	this.Relogio = '';
	this.Tempo = 0;
	this.BackupTempo = 0;
	this.Alerta = Alerta;
	this.Estado = true;
	this.Roda = function ( ) {
		if ( this.Tempo > 0 ) {
			this.Tempo--;
			this.TVisual = window.setTimeout ( 'Obj(\'CronRegressivo_'+this.CronID+'\').innerHTML=SegundosParaHMS('+ ( this.Tempo - 1 ) +');' , 1000 );
			this.TCron = window.setTimeout ( 'Cron['+this.CronID+'].Roda();' , 1000 );
		} else if ( this.Tempo == 0 ) {
			alert ( this.Alerta );
			if ( this.Ciclico == true ) {
				this.Tempo = this.BackupTempo;
				this.Roda();
			};
		};
	};
	this.Reinicia = function ( ID ) {
		this.Tempo = this.BackupTempo;
		Obj('CronRegressivo_'+this.CronID).innerHTML = SegundosParaHMS(this.Tempo);
		window.clearTimeout ( this.TVisual );
		window.clearTimeout ( this.TCron );
		this.Roda();
		Obj ( 'BtnContinuar_'+ID ).style.display = 'none';
		Obj ( 'BtnPausar_'+ID ).style.display = '';
	};
	this.Pausa = function ( ID ) {
		window.clearTimeout ( this.TVisual );
		window.clearTimeout ( this.TCron );
		Obj ( 'BtnContinuar_'+ID ).style.display = '';
		Obj ( 'BtnPausar_'+ID ).style.display = 'none';
	};
	this.Continua = function ( ID ) {
		this.Roda();
		Obj ( 'CronRegressivo_'+this.CronID ).innerHTML = SegundosParaHMS ( this.Tempo );
		Obj ( 'BtnContinuar_'+ID ).style.display = 'none';
		Obj ( 'BtnPausar_'+ID ).style.display = '';
	};
	this.Apaga = function ( ) {
		window.clearTimeout ( this.TVisual );
		window.clearTimeout ( this.TCron );
		delete ( this.TVisual );
		delete ( this.TCron );
	};
	if ( isNaN ( this.Horas ) || isNaN ( this.Minutos ) || isNaN ( this.Segundos ) || ( this.Horas == 0 && this.Minutos == 0 && this.Segundos == 0 ) ) {
		this.Estado = false;
	} else {
		if ( Segundos >= 60 ) {
			this.Minutos += ( this.Segundos - ( this.Segundos % 60 ) ) / 60;
			this.Segundos = this.Segundos % 60;
		};
		if ( this.Minutos >= 60 ) {
			this.Horas += ( this.Minutos - ( this.Minutos % 60 ) ) / 60;
			this.Minutos = this.Minutos % 60;
		};
		this.Tempo = HMSParaSegundos ( this.Horas+':'+this.Minutos+':'+this.Segundos );
		this.BackupTempo = this.Tempo;
		this.Relogio = SegundosParaHMS ( this.Tempo );
		this.Roda();
	};
};

var CronProgressivo = function ( CronID , Nome ) {
	this.CronID = CronID;
	this.Nome = Nome;
	this.Relogio = '00:00:00';
	this.Tempo = 0;
	this.Estado = true;
	this.Roda = function ( ) {
		this.Tempo++;
		this.TVisual = window.setTimeout ( 'Obj(\'CronProgressivo_'+this.CronID+'\').innerHTML=SegundosParaHMS('+ ( this.Tempo ) +');' , 1000 );
		this.TCron = window.setTimeout ( 'Cron['+this.CronID+'].Roda();' , 1000 );
	};
	this.Reinicia = function ( ID ) {
		this.Tempo = 0;
		Obj('CronProgressivo_'+this.CronID).innerHTML = SegundosParaHMS(this.Tempo);
		window.clearTimeout ( this.TVisual );
		window.clearTimeout ( this.TCron );
		this.Roda();
		Obj ( 'BtnContinuar_'+ID ).style.display = 'none';
		Obj ( 'BtnPausar_'+ID ).style.display = '';
	};
	this.Pausa = function ( ID ) {
		window.clearTimeout ( this.TVisual );
		window.clearTimeout ( this.TCron );
		Obj ( 'BtnContinuar_'+ID ).style.display = '';
		Obj ( 'BtnPausar_'+ID ).style.display = 'none';
	};
	this.Continua = function ( ID ) {
		Obj ( 'CronProgressivo_'+this.CronID ).innerHTML = SegundosParaHMS ( this.Tempo );
		this.Roda();
		Obj ( 'BtnContinuar_'+ID ).style.display = 'none';
		Obj ( 'BtnPausar_'+ID ).style.display = '';
	};
	this.Apaga = function ( ) {
		window.clearTimeout ( this.TVisual );
		window.clearTimeout ( this.TCron );
		delete ( this.TVisual );
		delete ( this.TCron );
	};
	this.Roda();
};

var Documento = function ( ) {
	this.CriaDIV = function ( ID ) {
		var Saida = '';
		switch ( arguments[1] ) {
			case 'R' :
				Saida += '<div id="'+ID+'" class="DIVCron">';
				Saida += '<h4>Cron Regressivo #'+ID+': '+Cron[ID].Nome+'</h4>';
				Saida += '<p class="CronRegressivoRelogio" id="CronRegressivo_'+ID+'">'+arguments[4]+'</p>';
				Saida += '<p>';
				Saida += arguments[2] == true ? 'Cíclico.' : 'Não cíclico.';
				Saida += ' ';
				Saida += '<em>'+arguments[3]+'</em>';
				Saida += '</p>';
				Saida += '<p class="Opcoes">';
				Saida += '<button type="button" onclick="A.ApagaCron(\''+ID+'\');" id="BtnExcluir_'+ID+'">Excluir</button>'
				Saida += '<button type="button" onclick="Cron['+ID+'].Reinicia('+ID+');" id="BtnReiniciar_'+ID+'">Reiniciar</button>'
				Saida += '<button type="button" onclick="Cron['+ID+'].Pausa('+ID+');" id="BtnPausar_'+ID+'">Pausar</button>'
				Saida += '<button type="button" onclick="Cron['+ID+'].Continua('+ID+');" id="BtnContinuar_'+ID+'">Continuar</button>'
				Saida += '</p>';
				Saida += '</div>';
			break;
			case 'P' :
				Saida += '<div id="'+ID+'" class="DIVCron">';
				Saida += '<h4>Cron Progressivo #'+ID+': '+Cron[ID].Nome+'</h4>';
				Saida += '<p class="CronProgressivoRelogio" id="CronProgressivo_'+ID+'">'+arguments[2]+'</p>';
				Saida += '<p class="Opcoes">';
				Saida += '<button type="button" onclick="A.ApagaCron(\''+ID+'\');" id="BtnExcluir_'+ID+'">Excluir</button>'
				Saida += '<button type="button" onclick="Cron['+ID+'].Reinicia('+ID+');" id="BtnReiniciar_'+ID+'">Reiniciar</button>'
				Saida += '<button type="button" onclick="Cron['+ID+'].Pausa('+ID+');" id="BtnPausar_'+ID+'">Pausar</button>'
				Saida += '<button type="button" onclick="Cron['+ID+'].Continua('+ID+');" id="BtnContinuar_'+ID+'">Continuar</button>'
				Saida += '</p>';
				Saida += '</div>';
			break;
		};
		Obj ( 'Receptor' ).innerHTML += Saida;
		Obj ( 'BtnContinuar_'+ID ).style.display = 'none';
	};
	this.InutilizaDIV = function ( ID ) {
		if ( Obj ( ID ) ) {
			Obj ( ID ).innerHTML = '';
			Obj ( ID ).style.display = 'none';
		};
	};
};

var A = new JSCron ( );