var feriados_fixos = new Array (8);
feriados_fixos[0] = "01/01";
feriados_fixos[1] = "21/04";
feriados_fixos[2] = "01/05";
feriados_fixos[3] = "07/09";
feriados_fixos[4] = "12/10";
feriados_fixos[5] = "02/11";
feriados_fixos[6] = "15/11";
feriados_fixos[7] = "25/12";


//     * 01/01 - Confraternização Universal
//     * 04/04 - Tiradentes
//     * 01/05 - Dia do Trabalho
//     * 07/09 - Independência do Brasil
//     * 12/10 - Nossa Senhora Aparecida
//     * 02/11 - Finados
//     * 15/11 - Proclamação da República
//     * 25/12 - Natal

//epb date selector control.
function epb_dateselector(idWebSite, str_name, str_id, object_name, eventos)
{  
   this.selected_date = new Date();
   this.active_day_class = 'epb_active_day_class';
   this.day_class = 'epb_day_class';
   this.day_class_feriado = 'feriado';
   this.dayrow_class = 'epb_dayrow';
   this.table_class = 'epb_cal_table';
   this.table_border = '0';
   this.cal_heading_class = 'epb_cal_head';
   this.cellpadding = 0;
   this.cellspacing = 0;
   
   this.next_month_class = 'epb_next_month_class';
   this.last_month_class = 'epb_last_month_class';
   
   this.next_month_text = '&gt;&gt;';
   this.last_month_text = '&lt;&lt;';
   
   this.q_month_class = 'epb_q_month_class';
   this.q_year_class = 'epb_q_year_class';
   this.str_name = str_name;
   this.str_id = str_id;
   this.object_name = object_name;
   this.day_mouse_over_class = 'epb_mouse_over';
   this.table_container_class = '';
   this.min_year = this.selected_date.getFullYear() - 1;
   this.max_year = this.selected_date.getFullYear() + 1;
   this.overall_class = 'caixa_agenda';
   this.view_year = this.selected_date.getFullYear();
   this.view_month =this.selected_date.getMonth();
   
   //textual versions of things are retrieved here.
   this.__get_month_name = function __get_month_name(monthnum)
   {
	   var monthnames = new Array('Janeiro', 'Fevereiro', 'Mar&#231;o', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro');
	   
	   return monthnames[monthnum % 12];
	   
   }
   
   this.__get_day_shortname = function __get_day_shortname(daynum)
   {
	  var daynames = new Array('D', 'S', 'T', 'Q', 'Q', 'S', 'S');	  
	  return daynames[daynum % 7];
   }
   
   this.set_active_date = function saproto(date, month, year)
   {
	   var tempnewactive, tempoldactive;
	   
	/*   if(this.selected_date.getMonth() == month && this.selected_date.getFullYear() == year)
	   {
		   tempoldactive = document.getElementById(this.str_id + '__date'+this.selected_date.getDate());
		   tempoldactive.className = this.day_class;		   
		   
		   tempnewactive = document.getElementById(this.str_id + '__date'+date);
		   tempnewactive.className = this.active_day_class;
		   tempoldactive.onMouseOver = new Function('this.className=\''+this.day_mouse_over_class+'\';');
		   tempoldactive.onMouseOut = new Function('this.className=\''+this.day_class+'\';');
		   //onmouseover="this.className=\''+this.day_mouse_over_class+'\'" onmouseout="this.className=\''+this.day_class+'\'"
		   //tempnewactive
		   
		   this.selected_date.setMonth(month);
		   this.selected_date.setFullYear(year);	
		   this.selected_date.setDate(date);	   
	   }
	   else
	   {*/
		   this.selected_date.setMonth(month);
		   this.selected_date.setFullYear(year);
		   this.selected_date.setDate(date);
		   tempnewactive = document.getElementById(this.str_id + '__tablecontainer');
		   this.view_year = this.selected_date.getFullYear();
           	   this.view_month = this.selected_date.getMonth();
		   tempnewactive.innerHTML = this.__build_month(this.view_month, this.view_year);
	   //}
       //Here we set the value of our hidden element so as to be able to pass this on to
       //the next page in a form submission.
       document.getElementById(this.str_id+'__value').value = this.formatted_date();
   }
   
   //Respond to changes in the quick select menus or clicks on the next and last month
   //links.
   this.quick_select_respond = function quick_select_respond(command, number)
   {
	   var tableitem;
	   var newmonth, newyear;
	   if(command == 'change_month')
	   {
		   this.view_month = number;
		   tableitem = document.getElementById(this.str_id + '__tablecontainer');
		   tableitem.innerHTML = this.__build_month(this.view_month, this.view_year);
	   }
	   if(command == 'change_year')
	   {
		   this.view_year = number;
		   tableitem = document.getElementById(this.str_id + '__tablecontainer');
		   tableitem.innerHTML = this.__build_month(this.view_month, this.view_year);
		   
	   }
	   if(command == 'next_month')
	   {
		   newmonth = this.view_month + 1;
		   if(newmonth == 12)
		   {
			   newmonth = 0;
			   this.view_year = this.view_year + 1;
			   
		   }
		   this.view_month = newmonth;
		   tableitem = document.getElementById(this.str_id + '__tablecontainer');
		   tableitem.innerHTML = this.__build_month(this.view_month, this.view_year);		   
	   }
	   if(command == 'last_month')
	   {
		   newmonth = this.view_month - 1;
		   if(newmonth == -1)
		   {
			   newmonth = 11;
			   this.view_year = this.view_year - 1;
		   }
		   this.view_month = newmonth;
		   tableitem = document.getElementById(this.str_id + '__tablecontainer');
		   tableitem.innerHTML = this.__build_month(this.view_month, this.view_year);	
	   }
   }
   
   this.bind_to_existing_by_id = function replace_existing_item(id)
   {
	   var to_replace = document.getElementById(id);
	   var newthingy;
	   if(to_replace != null)
	   {		
		   if(document.createElement)
		   {   
			   newthingy = document.createElement('div');
			   newthingy.innerHTML = this.generate_control();
			   to_replace.parentNode.replaceChild(newthingy, to_replace);
			   newthingy.id = this.str_id;
           }
	   }
   }
   
   //Returns HTML/JS for this control.
   this.generate_control = function gencntl(date, month, fullyear)
   {
	   var i, isselected;
	   var output;
	   this.selected_date.setDate(date != undefined ? date: this.selected_date.getDate());
	   this.selected_date.setMonth(month != undefined ? month: this.selected_date.getMonth());
	   this.selected_date.setFullYear(fullyear != undefined ? fullyear: this.selected_date.getFullYear());
	   
	   output = '<div id="'+this.str_id+'_overallcontainer" class="'+this.overall_class+'">';
	   
output += '<div id="'+this.str_id+'__tablecontainer" class="'+this.table_container_class+'">'
	   +this.__build_month(this.selected_date.getMonth(), this.selected_date.getFullYear()) + '</div>';	   
	   //Add hidden value hold here.
	   output += '<input id="'+this.str_id+'__value" type="hidden" value="'+this.formatted_date()+'" name="'+this.str_name+'" />';
	   
	   output += '<div id="NavegacaoAgenda">';
	   //Month quick selection.
	   output += '<select class="'+this.q_month_class+'" onchange="'+this.object_name+'.quick_select_respond(\'change_month\', this.value);"  id="'+this.str_id+'__monthpick">';	   
// 	   output += '<option value="'+this.selected_date.getMonth()+'">'+
// 	   this.__get_month_name(this.selected_date.getMonth())+'</option>';
	   for(i = 0; i < 12; i++)
	   {  
	      output += '<option value="'+i+'"';
	      
	      if (this.selected_date.getMonth() == i) {
	      	output += ' selected ';
	      }
	      
	      output += '>'+this.__get_month_name(i)+'</option>';
	   }
	   output += '</select>';	 
	   
	   //Year quick selection.
	   output += '<select class="'+this.q_year_class+'"  onchange="'+this.object_name+'.quick_select_respond(\'change_year\', this.value);"  id="'+this.str_id+'__yearpick">';	   
// 	   output += '<option value="'+this.selected_date.getFullYear()+'">'+	   
// 	   this.selected_date.getFullYear()+'</option>';
	   for(i = this.min_year; i <= this.max_year; i++)
	   {  
	      output += '<option value="'+i+'"';
	      
	      if (this.selected_date.getFullYear() == i) {
	      	output += ' selected ';
	      }
	      
	      output += '>'+i+'</option>';
	   }
	   output += '</select>';
	   output += '</div>';
	   output += '<div id="LegendaAgenda">';
	   output += '<div id="DescricaoLegenda">Legenda:</div>';
	   output += '<div onmouseover="window.document.getElementById(\'DescricaoLegenda\').innerHTML=\'Hoje\'" onmouseout="window.document.getElementById(\'DescricaoLegenda\').innerHTML=\'Legenda:\'" id="DiaAtual" class="legenda">H</div>';
	   output += '<div id="Feriado" onmouseover="window.document.getElementById(\'DescricaoLegenda\').innerHTML=\'Feriado\'" onmouseout="window.document.getElementById(\'DescricaoLegenda\').innerHTML=\'Legenda:\'"class="legenda">F</div>';
	   output += '<div id="Eventos" onmouseover="window.document.getElementById(\'DescricaoLegenda\').innerHTML=\'Eventos\'" onmouseout="window.document.getElementById(\'DescricaoLegenda\').innerHTML=\'Legenda:\'"class="legenda">E</div>';
	   output += '</div>';
// 	   output += '<a class="'+this.next_month_class+'" id="'+this.str_id+'__nextmonth" href="javascript: '+this.object_name+'.quick_select_respond(\'last_month\')">'+this.last_month_text+'</a>';
// 	   output += '<a class="'+this.last_month_class+'"  id="'+this.str_id+'__lastmonth" href="javascript: '+this.object_name+'.quick_select_respond(\'next_month\')">'+this.next_month_text+'</a>';
	   
	   output += '</div>';
	   return output;
   }
   
   this.formatted_date = function formatted_date()
   {
	   var out = this.selected_date.getFullYear() + '-';
	   
	   out += (this.selected_date.getMonth()+1)>9 ? (this.selected_date.getMonth()+1) : ('0'+(this.selected_date.getMonth()+1));
	   out += '-';
	   out += this.selected_date.getDate() > 9 ? this.selected_date.getDate() : '0' + this.selected_date.getDate();
	   return out;
   }
   
   // Constructs the HTML-visible part of the control.
   // month is the integer value for this month.
   // year is the integer value for this 
   this.__build_month = function __build_month(month, year)
   {
	   var daythingy = new Date();
	   var i;
	   daythingy.setMonth(month);
	   daythingy.setFullYear(year);
	   daythingy.setDate(1);
	   var tablehtml = '<table  id="'+this.str_id+'__table"  border="'+this.table_border+'" cellpadding="'+this.cellpadding+'" cellspacing="'+this.cellspacing+'" class="'+this.table_class+'">';
// 	   <tr ><td colspan="7" class="'+this.cal_heading_class+'">' + this.__get_month_name(month) + '&nbsp;&nbsp;' + year + '</td></tr>
	   tablehtml += '<tr class="'+this.dayrow_class+'">';
	   for(i = 0 ; i < 7; i++)
	   {
		   tablehtml += '<td>'+ this.__get_day_shortname(i) + '</td>';		            
	   }
	   tablehtml += '</tr><tr>';
	   
	   //Now that the heading and such was created, now let us build the dates.
	   //If one of the dates matches this.selected_date, then off we go!
	   
	   
	   //First we need to make the empty spaces. 
	   for(i = 0; i < daythingy.getDay(); i++)
	   {
		   tablehtml += '<td>&nbsp;</td> '; 		  
	   }
	   
	   var cx = daythingy.getDay();  //cx hold the current position for building the table.
	   var rowopen = true;
	   var continue_along = true;
	   while(continue_along)
	   {
		   if(rowopen == false)
		   {tablehtml += '<tr>'; rowopen = true;}
		   
		   var hoje = 0;
		   if(daythingy.getDate() == this.selected_date.getDate() &&
		   daythingy.getMonth() == this.selected_date.getMonth() &&
		   daythingy.getFullYear() == this.selected_date.getFullYear())
		   {
		   	   hoje =1;
// 			   //This is the active date.
// 			   tablehtml += '<td id="'+this.str_id+'__date'+daythingy.getDate()+'" class="'+this.active_day_class+'">'+daythingy.getDate()+'</td>';
		   }
		   
		/*   
		   
		   else*/
// 		   {
		   	var verifica_dia = 0;
			//Verificando se eh um feriado
			if (feriados_fixos != null) {
				for (var j=0; j < feriados_fixos.length; j++) {
					var data  = feriados_fixos[j].split("/");
					var dia = parseInt(data[0],10);
					var mes = parseInt(data[1],10) - 1;
					
					if (daythingy.getDate() == dia &&
					daythingy.getMonth() == mes) {
						tablehtml += '<td id="'+this.str_id+'__date'+daythingy.getDate()+'"';
						if (hoje == 1) {
							tablehtml += 'class="'+this.day_class_feriado+' '+this.active_day_class+'"';
						} else {
							tablehtml += 'class="'+this.day_class_feriado+'"';
						}
						
						tablehtml += '><a href="javascript:;">'+daythingy.getDate()+'</a></td>';
						verifica_dia = 1;
					}
				}
			}
			
			//Verificando se eh um feriados
			if (eventos != null && verifica_dia == 0) {
				for (var x=0; x < eventos.length; x++) {
					var data  = eventos[x][0].split("/");
					var dia = parseInt(data[0],10);
					var mes = parseInt(data[1],10) - 1;
					var ano = parseInt(data[2],10);
					
					if (daythingy.getDate() == dia &&
					daythingy.getMonth() == mes && daythingy.getFullYear() == ano) {
						tablehtml += '<td id="'+this.str_id+'__date'+daythingy.getDate()+'"';
						 
						if (hoje == 1) {
							tablehtml += 'class="eventos '+this.active_day_class+'"';
						} else {
							tablehtml += 'class="eventos"';
						}
						
						 tablehtml += '><a href="http://'+location.hostname+'/oktiva.net/'+idWebSite+'/nota/'+eventos[x][1]+'">'+daythingy.getDate()+'</a></td>';
						verifica_dia = 1;
					}
				}
			}
			
			if (verifica_dia == 0) {
				tablehtml += '<td id="'+this.str_id+'__date'+daythingy.getDate()+'"';
				
				if (hoje == 1) {
					tablehtml += 'class="'+this.active_day_class+'"';
				} else {
					tablehtml += 'class="'+this.day_class+'"';
				}

				tablehtml += '><a href="javascript:;">'+daythingy.getDate()+'</a></td>';
			}
			
// 		   }  
		   
		   cx++;
		   
		   if((cx % 7) == 0)
		   {
			  tablehtml += '</tr>';
			  rowopen = false;
		   }
		   daythingy.setDate(daythingy.getDate()+1);
		   if(daythingy.getDate() == 1)
		   {
			   //We've gotten to next month, let's stop.
			   continue_along = false;
		   }
	   }
	   
	   //now if we didn't get to the end of the calendar row, let's fill it in with empty spaces.
	   if((cx % 7) != 0)
	   {
		   while((cx % 7) != 0)
		   {
			  tablehtml += '<td>&nbsp</td>'; 
			  cx++;
		   }
		   tablehtml += '</tr>';
       }
	   
	   tablehtml += '</table>';
	   
	   return tablehtml;
   }
}