Event.observe
(
	window,
	'load',
	function()
	{
		
		/**	Main menu **/
		if (window.attachEvent) setNavHover();
		
		/**	Gallery of images **/
		
		if($("gallery_section") != null )
		{

			var galleryDir = "/portal/img/gallery/";
			var gallery = new Gallery();
					
			new Ajax.Request
			(
				"/portal/galleries/get_last_images",
				{
					onSuccess: function(transport, json)
					{
						
						if(json.length > 0)
						{					
							//Initialize the gallery using the data obtained by the ajax request
							for(var x=0; x<json.length; x++)
							{						
								var image = json[x].GalleryImage;
								gallery.addImage(new Thumbnail(image.title, galleryDir + image.thumbnail_file, galleryDir + image.image_file, null));						
							}
							
							//Display the first image
							gallery.displayImage(0);
							
							//Set previous and next links observers						
							Event.observe
							(
								"previous_image", 
								"click", 
								function(e)
								{
									gallery.previousImage();
									Event.stop(e);
								}
							);
					
							Event.observe
							(
								"next_image", 
								"click", 
								function(e)
								{
									gallery.nextImage();
									Event.stop(e);
								}
							);
							
							new PeriodicalExecuter
							(
								gallery.nextImage.bind(gallery),
								5
							);
							
						}
						else
						{
							$("gallery_section").down(".body").innerHTML = "La galería de imagenes esta vacía."
						}
											
					}
				}
			);					
		}	
	
		/**	Onomastics **/
		
		if($("onomastics_section") != null )
		{
			var studentsAgenda = new BirthdayAgenda("student_birthday");
			var teachersAgenda = new BirthdayAgenda("worker_birthday");
					
			new Ajax.Request
			(
				"/portal/onomastics/get_last_ones",
				{
					onSuccess: function(transport, json)
					{						
						var studentsBirthdays = json.Student;
						var teachersBirthdays = json.Teacher;
						
						if(studentsBirthdays.length > 0)
						{					
							//Initialize the birthday agenda using the data obtained by the ajax request
							for(var index=0; index<studentsBirthdays.length; index++)
							{
								var birthday = studentsBirthdays[index];														
								studentsAgenda.addBirthday(new Birthday(birthday.person, birthday.day, birthday.email));						
							}
							
							//Display the first image
							studentsAgenda.displayBirthday(0);
							
							new PeriodicalExecuter
							(
								studentsAgenda.nextBirthday.bind(studentsAgenda),
								5
							);
							
						}
						else
						{
							$("student_birthday").innerHTML = "Ningún alumno cumple años este mes."
						}

						if(teachersBirthdays.length > 0)
						{	
							//Initialize the birthday agenda using the data obtained by the ajax request
							for(var index=0; index<teachersBirthdays.length; index++)
							{
								var birthday = teachersBirthdays[index];														
								teachersAgenda.addBirthday(new Birthday(birthday.person, birthday.day, birthday.email));						
							}
							
							//Display the first image
							teachersAgenda.displayBirthday(0);						
							
							new PeriodicalExecuter
							(
								teachersAgenda.nextBirthday.bind(teachersAgenda),
								5
							);
							
						}
						else
						{
							$("worker_birthday").innerHTML = "Ningún maestro cumple años este mes."
						}

											
					}
				}
			);					
		}
		
		/**	Roll of Honour**/
		
		if($("honours_roll_section") != null )
		{

			var studentsDir = "/principal/fotos/";
			var honoursRoll = new HonoursRoll("honours_roll");
					
			new Ajax.Request
			(
				"/portal/honour_roll/get_best_ones",
				{
					onSuccess: function(transport, json)
					{
						
						if(json.length > 0)
						{					
							//Initialize the gallery using the data obtained by the ajax request

							for(var x=0; x<json.length; x++)
							{														
								honoursRoll.addPerson(new Student(json[x].name, json[x].identifier, studentsDir + json[x].identifier + ".jpg",  json[x].semester, json[x].group, json[x].average, json[x].career));			
							}
							
							//Display the first image
							honoursRoll.display(0);							
							
							new PeriodicalExecuter
							(
								honoursRoll.nextPerson.bind(honoursRoll),
								15
							);
							
						}
						else
						{
							$("honours_roll_section").down(".body").innerHTML = "El cuadro de honor está vacío."
						}
											
					}
				}
			);					
		}			
		
	}	
		
);

setNavHover = function() 
{
	var lis = document.getElementById("navigation_menu").getElementsByTagName("LI");
	for (var i=0; i<lis.length; i++) {
		lis[i].onmouseover=function() {this.className+=" iehover";}
		lis[i].onmouseout=function() {this.className=this.className.replace(new RegExp(" iehover\\b"), "")}
	}
}
