document.write("<script src=\"../js/ionline/translate.js\"></script>");

var DATEFORMAT = {
	els: null,
	now:0,

	setEls:function(objs){
		DATEFORMAT.els = objs;
	},

	getEls:function(){
		DATEFORMAT.els = $$('.dateFormat');
	},

	render:function(){

		new Request({
						url:'../_post/ionline/getCurrentDate.php',
						onSuccess:function(r){
										if(r!=''){
											DATEFORMAT.now = r.toInt();
											DATEFORMAT.calculate();
										}
									}
						}).get(); //Realizo el Request

	},

	calculate:function(){
		if(!DATEFORMAT.els)	return true;
		DATEFORMAT.els.each(function(obj,index){
			date = obj.getProperty('fechaalta');
			obj.innerHTML += ' '+DATEFORMAT.getFecha(date, DATEFORMAT.now, 3)+' ago';
		});
	},

	getFecha:function(fechaAlta, ahora, idCMSIdioma){
		var bool 		= true;
		retorno			= 0;
		diff = parseInt(ahora) - parseInt(fechaAlta);
		//Segundos
		diff=(diff/60)
		if((diff<1) && bool){
			bool    = false;
			if(diff<1) {
				retorno = ' 1 '+arrTranslate[idCMSIdioma]['minuto'];
			} else {
				retorno = (diff*60)+ ' '+arrTranslate[idCMSIdioma]['segundos'];
			}
		}

		//Minutos
		diff=(diff/60)
		if((diff<1) && bool){
			bool    = false;
			retorno = (diff*60);
		  retorno = Math.round(retorno);
		  if(retorno == 1)
			  retorno = retorno + ' '+arrTranslate[idCMSIdioma]['minuto'];
		  else
			  retorno = retorno + ' '+arrTranslate[idCMSIdioma]['minutos'];
		}

		//Horas
		diff=(diff/24)
		if((diff<1) && bool){
			bool    = false;
			retorno = (diff*24);
		  retorno = Math.round(retorno);
		  if(retorno == 1)
		    retorno = retorno + ' '+arrTranslate[idCMSIdioma]['hora'];
		  else
		    retorno = retorno + ' '+arrTranslate[idCMSIdioma]['horas'];
		}
		//Dias
		diffDias=(diff/30)
		if((diffDias<=1) && bool){
			retorno = (diffDias*30);
		  round = Math.round(retorno);
		  dias = round;
		  //Si es mas de 7 dias, utilizo semanas
		  if(round<7){
			  bool    = false;
			  if(retorno == 1)
				  retorno = retorno + ' '+arrTranslate[idCMSIdioma]['dia'];
			  else
				  retorno = round + ' '+arrTranslate[idCMSIdioma]['dias'];
		  }
		}else{
			dias = diff;
		}

		//Semanas
		semanas=(dias/7);
		if((semanas>=1) && (semanas<=4)&& bool){
		  bool    = false;
		  if((semanas>=1) && (semanas<2)){
			  fracPart = (semanas%1);
			  fracInt  = Math.floor(semanas);

			  if(fracPart == 0)
				  retorno = fracInt +' '+arrTranslate[idCMSIdioma]['semana'];
			  else
				  retorno = arrTranslate[idCMSIdioma]['mas de']+' '+ fracInt +' '+arrTranslate[idCMSIdioma]['semana'];
			}else{
				  if(semanas <4){
						fracPart = (semanas%1);
					  fracInt  = Math.floor(semanas);
					  if(fracPart == 0)
						  retorno = fracInt +' '+arrTranslate[idCMSIdioma]['semanas'];
					  else
						  retorno = arrTranslate[idCMSIdioma]['mas de']+' '+fracInt +' '+arrTranslate[idCMSIdioma]['semanas'];
				  }
			}
		}
		if(semanas>=4){
			mes=(semanas/4);
			mesPart = (mes%1);
		  mesInt  = Math.floor(mes);
			if((mes>=1) && (mes<2)){
				if(mesPart == 0)
					retorno = mesInt + ' '+arrTranslate[idCMSIdioma]['mes'];
				else
				    retorno = arrTranslate[idCMSIdioma]['mas de']+' '+mesInt+' '+arrTranslate[idCMSIdioma]['mes'];
		  }else{
			  if(mesPart == 0)
			      retorno = mesInt + ' '+arrTranslate[idCMSIdioma]['meses'];
			  else
			      retorno = arrTranslate[idCMSIdioma]['mas de']+' '+mesInt + ' '+arrTranslate[idCMSIdioma]['meses'];
			}
		}

		return retorno;
		//alert(retorno);
		//target.value=retorno;
		//document.write(retorno);

	},getActualTime:function(){

		new Request({
						url:'../_post/ionline/getCurrentDate.php',
						onSuccess:function(r){
										if(r!=''){
											return r.toInt();
										}
									}
						}).get(); //Realizo el Request

	},

	setTimeOnObject:function(idObjeto, fechaAlta, idIdioma){
		if($(idObjeto)){
			//alert('idObjeto: '+idObjeto +' actualTime: '+ actualTime + ' fechaAlta: ' +fechaAlta);
			$(idObjeto).innerHTML = DATEFORMAT.getFecha(fechaAlta,actualTime, idIdioma);
		}
	},
	
	setTimeOnClass:function(className, fechaAlta, idIdioma){
		
		objs = $$("."+className);
		
		if(objs){
			
			objs.each(function(obj){
				obj.innerHTML = DATEFORMAT.getFecha(fechaAlta,actualTime, idIdioma);
			});

	
		}
	},

	setTimeOnObject2:function(fechaAlta, idIdioma){
		if (typeof(document.readyState) == "undefined" || document.readyState == "interactive" || is_safari) {
			document.write(DATEFORMAT.getFecha(fechaAlta,actualTime, idIdioma));
		}
	}
}