var unidadlo_calendar = Class.create();
unidadlo_calendar.prototype	= 
{
	initialize:function()
	{
		try{
			this.instancia		= '';
			this.dia			= 0;
			this.mes			= 0;
			this.ano			= 0;
			this.prefijo		= '';
			this.o_JSON			= '';
		}catch(ex)
		{
			alert("error en respuesta de datos." + ex);
			this.pedirdatos();
		}
	}
	,
	json:function(json)
	{
		try{
			if(json!="" && json !=undefined)
			{
				try{
					this.o_JSON 		= eval( '(' + json + ')' );
					if(typeof(this.o_JSON.year)=="object" || typeof(this.o_JSON.year)=="boolean")
					{
						if(this.o_JSON.dia !=undefined)
						{
							this.dia			= this.o_JSON.dia;
						}
						else
						{
							this.dia			= 0;
						}
						if(this.o_JSON.mes !=undefined)
						{
							this.mes			= this.o_JSON.mes;
						}
						else
						{
							this.mes			= 0;
						}
						if(this.o_JSON.ano !=undefined)
						{
							this.ano			= this.o_JSON.ano;
						}
						else
						{
							this.ano			= 0;
						}
						this.iniciar();
					}
					else
					{
						this.pedirdatos();
					}
				}catch(ex)
				{
					alert("error en respuesta de datos." + ex);
					this.pedirdatos();
				}
			}
			else
			{
				this.pedirdatos();
			}
		}catch(ex)
		{
			alert("json::" + ex);
			this.pedirdatos();
		}
	}
	,
	iniciar_instancia:function(instancia)
	{
		try{
			this.instancia		= instancia;
			this.pedirdatos();
		}catch(ex)
		{
			alert("iniciar_instancia::" + ex);
			this.pedirdatos();
		}
	}
	,
	textomes:function(mesentrada)
	{
		try{
			if(mesentrada==1)
			{
				return "January";
			}
			else if(mesentrada==2)
			{
				return "February";
			}
			else if(mesentrada==3)
			{
				return "March";
			}
			else if(mesentrada==4)
			{
				return "April";
			}
			else if(mesentrada==5)
			{
				return "May";
			}
			else if(mesentrada==6)
			{
				return "June";
			}
			else if(mesentrada==7)
			{
				return "July";
			}
			else if(mesentrada==8)
			{
				return "August";
			}
			else if(mesentrada==9)
			{
				return "September";
			}
			else if(mesentrada==10)
			{
				return "October";
			}
			else if(mesentrada==11)
			{
				return "November";
			}
			else if(mesentrada==12)
			{
				return "December";
			}
		}catch(ex)
		{
			alert("iniciar::" + ex);
			this.pedirdatos();
		}
	}
	,
	iniciar:function()
	{
		try{
			$(this.prefijo + '_calendar_titulo').innerHTML	= this.textomes(this.o_JSON.mes) + " " + this.o_JSON.ano;
			for(var i=1; i<=41; i++)
			{
				$(this.o_JSON.prefijo + i).innerHTML		= "";
			}
		    for(var week in this.o_JSON.year.month.week)
		    {
		    	if((week >= 0)||(week < 0))
		    	{
				    for(var dia in this.o_JSON.year.month.week[week])
				    {
				    	if(((dia >= 0)||(dia < 0)) && this.o_JSON.year.month.week[week][dia]!=null 
						    	&& this.o_JSON.year.month.week[week][dia].dia!="null")
				    	{
				    		s_IDTEMPORAL						= this.prefijo + parseInt(dia);
				    		if(this.o_JSON.year.month.week[week][dia].classs!='')
				    		{
				    			Element.removeClassName($(s_IDTEMPORAL),'calendar_dia_evento');
				 				this.otorgar_stylo( s_IDTEMPORAL	, this.o_JSON.year.month.week[week][dia].classs);
				    		}

				    		if((typeof($(s_IDTEMPORAL))=="object" || typeof($(s_IDTEMPORAL))=="boolean") && $(s_IDTEMPORAL) != null)
				    		{
				    			var diatemporal						= this.o_JSON.year.month.week[week][dia].dia;
				    			$(s_IDTEMPORAL).innerHTML			= diatemporal;
				    			eval('funcionCLICK	=  function(){' + this.instancia + '.day_click("' + s_IDTEMPORAL + '","' + $(s_IDTEMPORAL).innerHTML + '");}');
								$(s_IDTEMPORAL).observe('click',eval(funcionCLICK));
				    		}
				    	}
				    }
		    	}
		    }
		}catch(ex)
		{
			alert("iniciar::" + ex);
			this.pedirdatos();
		}
	}
	,
	pedirdatos:function()
	{
		try{
			var post			= 'prefijo=' + this.prefijo + '&instancia=' + this.instancia + '&ajax=1&mes=' + this.mes + '&dia=' + this.dia + '&ano=' + this.ano;
			var opt = {
				method: 'post',
				postBody:post,
				requestHeaders: {Accept: 'application/json'},
			    onSuccess: function(t)
			    {
					json = eval('(' + t.responseText + ')');
					eval(json.instancia + ".json('" + t.responseText + "');");	
			    }
			}
			Respuesta	= new Ajax.Request("/json_calendar", opt);
		}catch(ex)
		{
			alert("pedirdatos::" + ex);
			this.pedirdatos();
		}
	}
	,
 	day_click:function(objetoID,dia_entrada)
 	{
 		try{
 			if(dia_entrada == '' || dia_entrada == null )
 			{
 				 dia_entrada	= $(objetoID).innerHTML;
 			}
 			
	   		if($(objetoID).hasClassName('calendar_dia_evento'))
	   		{
	   			window.location = "/calendar?day=" + this.ano + '-' + this.mes + '-' + dia_entrada;
	   		}
			return false;
		}catch(ex)
		{
			alert("day_click::" + ex);
		}
 	}
	,
	siguiente:function()
	{
		try{
			if(this.mes<12)
			{
				this.mes	= parseInt(parseInt(this.mes) + 1);
			}
			else
			{
				this.mes		= 1;
				this.ano++;
			}
			this.pedirdatos();
		}catch(ex)
		{
			alert("siguiente::" + ex);
			this.pedirdatos();
		}
	}
	,
	atras:function()
	{
		try{
			if(this.mes>1)
			{
				this.mes	= parseInt(parseInt(this.mes) - 1);
			}
			else
			{
				this.mes		= 12;
				this.ano--;
			}
			this.pedirdatos();
		}catch(ex)
		{
			alert("atras::" + ex);
			this.pedirdatos();
		}
	}
 	,
 	otorgar_stylo:function(id,style)
 	{
	 	try{
	 		if($(id))
	 	 	{
	 	 		Element.addClassName($(id), style);
	 	 	}
	 	 	else
	 	 	{
				return false;		
	 	 	}
		}catch(ex)
		{
			alert("otorgar_stylo::" + ex);
			this.pedirdatos();
		}
 	}
};