var Main = {
	
	start: function(){
		
		/*
		 * MENÚ SUPERIOR
		 */
		$('ul#menu_sup li').mouseover(function(){
			$('ul#menu_sup li').not($(this)).stop().fadeTo(300, 0.1);
		});
		$('ul#menu_sup li').mouseout(function(){
			$('ul#menu_sup li').stop().fadeTo(300, 1);
		});
		
		/*
		 * LLAMAMOS AL SCROLL DE NOTICIAS
		 */
		if($('#cuerpo').hasClass('portada')){
			Main.scroll_noticias_portada();
		}
		if ($('#principal').hasClass('contacto')) {
			Main.mapa();
		}
		
	},
	
	scroll_noticias_portada: function(){
		var indice = $('#ultimas_noticias .nodo').length;
		var array_resultados = new Array();
		var tope = 0;
		if(indice > 1){
			$.getJSON('scroll_noticias.php', function(resultados){
				tope = resultados.length;
				if(tope > 2){
					indice = 0;
					var cambia_noticia = function(){
						$('#ultimas_noticias .nodo').fadeOut('slow', function(){
							$('#ultimas_noticias .nodo:eq(1), #ultimas_noticias .nodo:eq(2)').remove();
							if (indice >= tope - 2) {
								indice = 0;
							}
							$('<div></div>').hide().fadeIn('slow').addClass('nodo').append($('<h3></h3>').append($('<a>' + resultados[indice].titulo + '</a>').attr({
								title: resultados[indice].titulo,
								href: 'detalle_noticia/' + resultados[indice].id
							}))).append($('<p></p>').addClass('ultimo').html(resultados[indice].texto)).appendTo('#ultimas_noticias').hide().fadeIn('slow');
							indice++;
							if(indice < tope){
								$('<div></div>').addClass('nodo').append($('<h3></h3>').append($('<a>' + resultados[indice].titulo + '</a>').attr({
									title: resultados[indice].titulo,
									href: 'detalle_noticia/' + resultados[indice].url
								}))).append($('<p></p>').addClass('ultimo').html(resultados[indice].texto)).appendTo('#ultimas_noticias').hide().fadeIn('slow');
								indice++;							
							}
						})
					}
					var intervalo = setInterval(cambia_noticia, 9000);
				}
			});
		}
		
	},
	
	mapa: function(){
		var map = new GMap2(document.getElementById("mapa"));
		var coordenadas = new GLatLng(37.97945393307615, -1.1220216751098633);
		var marcador = new GMarker(coordenadas);
		map.setCenter(coordenadas, 15);
		map.addOverlay(marcador);
		map.addControl(new GSmallMapControl());
	}
	
}

$(document).ready(function(){
	Main.start();
});

