
$(document).ready(function(){
	
	$('#barralateral img').css({
		opacity: '.9'		
	});
		
	
	if($('#mapa').html()){
		sustituye_enlace_mapa();		
	}
	if(document.getElementById('canvas_mapa')) {
		api_mapas();
		crea_mapa();		
	}
		
	if ($.browser.msie && $.browser.version.substr(0,1) < 7 
		|| ! document.getElementById('galeria_fotografica'))			
		return;
	
	else {	
		
		var img_activa;
		var num_img;
		inicializa_elementos();	
		eventos_menu();	
		otros_eventos();
	}
	 
});



function inicializa_elementos() {
	
	img_activa = 1;

	$('.img_cursos li').css({
		display: 'none'
	});
	
	$('.img_cursos li:first-child').toggle();
	
	num_img = $('#galeria_fotografica ul li').length;
	
	$('#galeria_fotografica ul li').attr('title', 'Haga click para pasar a la siguiente foto').css('cursor', 'pointer');
	
	cambiar_a = 'fotos';
	if(ver == 'fotos')
		cambiar_a = 'prensa';
	
	var str_append = 
		"<div class='galeria_fotografica_menu'>" +
			"<div>"+	
				"Imagen <span class='foto_actual'>1</span>" +
					"  <span>de "+ num_img + "</span>" +	
			"</div>"+						
			"<div>"+
				"<span class='cambia_foto_left' title='Imagen anterior'>< anterior </span> / " +
					
				"<span class='cambia_foto_right' title='Imagen Siguiente'> siguiente ></span>" +
			"</div>"+
			"<div>" +				
				"<span class='menu_pulsable zoom_out' title='Alejar imágen'>- Alejar </span> / " +
				"<span class='menu_pulsable zoom_in' title='Acercar imágen'> Acercar + </span>" +
			"</div>";		
			
	if(hay_otro == 1)
		str_append+=
			"<div>"+
				"<span class='ver_lo_otro' title='Cambiar a "+ cambiar_a +"'>"+
					"<a href='/anteriores.php?fecha="+year+"&curso="+curso+"&ver="+cambiar_a+"'>Cambiar a "+ cambiar_a + "</a>"+
				"</span>"+
			"</div>";
	str_append+=
			"<div>" +
				"<span class='salir' title='Volver a la página anterior'><a href='javascript:history.go(-1)'>Volver atrás</a></span>"+
			"</div>"+
		"</div>";
	
	$('#galeria_fotografica ul').prepend(str_append);
	$('#galeria_fotografica').append(str_append);

}


function eventos_menu(){
	$('#galeria_fotografica li img').live('click', function(){cambia_img(+1);}).css('cursor', 'pointer');
	$('.cambia_foto_left').click(function(){cambia_img(-1)});
	$('.cambia_foto_right').click(function(){cambia_img(+1)});
	
	$('.zoom_in').click(function(){zoom(+1);});
	$('.zoom_out').click(function(){zoom(-1);});	
	
}

function zoom(sentido){	
	
	$('.img_cursos li:visible img').css({
		'width': $('.img_cursos li:visible img').width() + sentido * $('.img_cursos li:visible img').width() * 0.2 + 'px'
	});
}

function cambia_img(sentido){	
	var ie7 = false;
		if ($.browser.msie && $.browser.version.substr(0,1) < 8) 
			ie7 = true;
			
	if(ie7) 
		$('.img_cursos li:visible').hide();
	else
		$('.img_cursos li:visible').slideUp();
		
	img_activa += sentido;
	if(img_activa < 1) img_activa = num_img;
	else if(img_activa > num_img) img_activa = 1;	
			
	$('.img_cursos li:eq('+(img_activa - 1)+') img').css('width', '774px');		
	
	if(ie7)
		$('.img_cursos li:eq('+(img_activa - 1)+')').show();
	else
		$('.img_cursos li:eq('+(img_activa - 1)+')').slideDown();
	
	$('.foto_actual').html(img_activa);
}

function otros_eventos(){
	$(document).keyup(function(e){
		if (e.keyCode == 27) {
			javascript:history.back();
		}   // esc
	});	
}	

















function api_mapas(){	
	Mapa = function(){
		var directionsDisplay;
		var directionsService; 
		var rotonda; 
		var granda;

		var mapa_;
		var centro_;
		var divMapa_;		
		this.getMapa = function(){					
			return this.mapa_;
		}
		this.getCentro = function(){
			return this.centro_;
		}
		
		this.abreMapa = function(){				
			this.centro_ = new google.maps.LatLng(43.556573,-5.858374);			
			var that = this;			
			this.directionsDisplay = new google.maps.DirectionsRenderer();
			that.creaMapa();								
			
			this.directionsDisplay.setMap(this.mapa);
			this.calcRoute();
				
		}
		this.creaMapa = function(){			
			this.divMapa_=document.getElementById('canvas_mapa');
										
			var mapOptions = {
				zoom: 15,
				center: this.centro_,
				mapTypeId: google.maps.MapTypeId.HYBRID
			};
		
			this.mapa_ = new google.maps.Map(this.divMapa_, mapOptions);				
		}
		this.calcRoute = function() {
			
			var myCoordinates = [
				new google.maps.LatLng(43.549498,-5.869318),
				new google.maps.LatLng(43.549683,-5.869273),
				new google.maps.LatLng(43.549726,-5.869055),
				new google.maps.LatLng(43.549691,-5.868798),
				new google.maps.LatLng(43.549586,-5.868444),
				new google.maps.LatLng(43.549535,-5.868031),
				new google.maps.LatLng(43.549562,-5.867701),
				new google.maps.LatLng(43.549704,-5.867323),
				new google.maps.LatLng(43.549907,-5.867062),
				new google.maps.LatLng(43.550161,-5.866934),
				new google.maps.LatLng(43.550418,-5.866942),
				new google.maps.LatLng(43.550601,-5.866977),
				new google.maps.LatLng(43.550707,-5.866950),
				new google.maps.LatLng(43.550830,-5.867006),
				new google.maps.LatLng(43.550908,-5.867116),
				new google.maps.LatLng(43.550927,-5.867317),
				new google.maps.LatLng(43.550877,-5.867505),
				new google.maps.LatLng(43.550809,-5.867618),
				new google.maps.LatLng(43.550737,-5.867988),
				new google.maps.LatLng(43.550674,-5.868395),
				new google.maps.LatLng(43.550639,-5.868897),
				new google.maps.LatLng(43.550678,-5.869235),
				new google.maps.LatLng(43.550709,-5.869377),
				new google.maps.LatLng(43.550820,-5.869924),
				new google.maps.LatLng(43.550853,-5.870142),
				new google.maps.LatLng(43.551034,-5.870568),
				new google.maps.LatLng(43.551190,-5.870812),
				new google.maps.LatLng(43.551413,-5.870973),
				new google.maps.LatLng(43.551860,-5.871091),
				new google.maps.LatLng(43.552576,-5.871303),
				new google.maps.LatLng(43.553157,-5.871445),
				new google.maps.LatLng(43.553853,-5.871638),
				new google.maps.LatLng(43.554547,-5.871872),
				new google.maps.LatLng(43.554941,-5.872003),
				new google.maps.LatLng(43.555093,-5.872075),
				new google.maps.LatLng(43.555386,-5.871625),
				new google.maps.LatLng(43.555591,-5.871405),
				new google.maps.LatLng(43.555896,-5.871220),
				new google.maps.LatLng(43.556193,-5.871145),
				new google.maps.LatLng(43.556302,-5.871260),
				new google.maps.LatLng(43.556354,-5.871598),
				new google.maps.LatLng(43.556459,-5.871593),
				new google.maps.LatLng(43.556545,-5.871504),
				new google.maps.LatLng(43.556588,-5.871332),
				new google.maps.LatLng(43.556615,-5.870855),
				new google.maps.LatLng(43.556673,-5.870407),
				new google.maps.LatLng(43.556679,-5.869881),
				new google.maps.LatLng(43.556683,-5.869240),
				new google.maps.LatLng(43.556700,-5.868658),
				new google.maps.LatLng(43.556786,-5.867934),
				new google.maps.LatLng(43.556850,-5.867781),
				new google.maps.LatLng(43.556951,-5.867610),
				new google.maps.LatLng(43.556992,-5.867403),
				new google.maps.LatLng(43.557025,-5.866998),
				new google.maps.LatLng(43.557058,-5.866429),
				new google.maps.LatLng(43.557056,-5.865590),
				new google.maps.LatLng(43.557083,-5.865204),
				new google.maps.LatLng(43.557159,-5.864788),
				new google.maps.LatLng(43.557299,-5.864243),
				new google.maps.LatLng(43.557554,-5.863490),
				new google.maps.LatLng(43.557754,-5.863069),
				new google.maps.LatLng(43.558009,-5.862712),
				new google.maps.LatLng(43.558442,-5.862441),
				new google.maps.LatLng(43.558784,-5.862248),
				new google.maps.LatLng(43.559099,-5.862165),
				new google.maps.LatLng(43.559439,-5.862178),
				new google.maps.LatLng(43.559503,-5.862106),
				new google.maps.LatLng(43.559595,-5.861969),
				new google.maps.LatLng(43.559836,-5.861534),
				new google.maps.LatLng(43.560024,-5.861194),
				new google.maps.LatLng(43.560096,-5.861044),
				new google.maps.LatLng(43.560049,-5.860829),
				new google.maps.LatLng(43.559468,-5.860496),
				new google.maps.LatLng(43.558872,-5.860175),
				new google.maps.LatLng(43.558102,-5.859748),
				new google.maps.LatLng(43.557917,-5.859595),
				new google.maps.LatLng(43.557771,-5.859389),
				new google.maps.LatLng(43.557668,-5.859174),
				new google.maps.LatLng(43.557563,-5.858796),
				new google.maps.LatLng(43.557460,-5.858385),
				new google.maps.LatLng(43.557272,-5.857541),
				new google.maps.LatLng(43.557159,-5.857085),
				new google.maps.LatLng(43.556988,-5.856441),
				new google.maps.LatLng(43.556833,-5.855743),
				new google.maps.LatLng(43.556815,-5.855438),
				new google.maps.LatLng(43.556848,-5.855245),
				new google.maps.LatLng(43.557021,-5.854671),
				new google.maps.LatLng(43.557356,-5.853536),
				new google.maps.LatLng(43.557581,-5.852831),
				new google.maps.LatLng(43.557878,-5.851886),
				new google.maps.LatLng(43.557966,-5.851675),
				new google.maps.LatLng(43.558092,-5.851495),
				new google.maps.LatLng(43.558232,-5.851393),
				new google.maps.LatLng(43.558469,-5.851280),
				new google.maps.LatLng(43.558728,-5.851291),
				new google.maps.LatLng(43.559237,-5.851516),
				new google.maps.LatLng(43.559566,-5.851658),
				new google.maps.LatLng(43.559632,-5.851699)
			];
			
			var polyOptions = {
				path: myCoordinates,
				strokeColor: "#FF0000",
				strokeOpacity: 1,
				strokeWeight: 3
			}
			var it = new google.maps.Polyline(polyOptions);
			it.setMap(this.mapa_);
			
			this.anade_marca("<div><br />Centro comercial ParqueAstur</div>", new google.maps.LatLng(43.546379,-5.869671), 'ParqueAstur');
			this.anade_marca("<div><br />Tomar la salida 'Trasona 200m'</div>", new google.maps.LatLng(43.54954,-5.86933), 'Rotonda 1');
			this.anade_marca("<div><br />Tomar la salida 'Aviles 4km'</div>", new google.maps.LatLng(43.550797,-5.867595), 'Rotonda 2');
			this.anade_marca("<div><br />Tomar la salida a ArcelorMittal</div>", new google.maps.LatLng(43.551009,-5.870503), 'Entrada ArcerlorMittal');
			this.anade_marca("<div><br />Control de acceso de ArcelorMittal. <br />Pasar a muy reducida velocidad por el carril sin barreras</div>", new google.maps.LatLng(43.551499,-5.870975), 'Control de acceso');
			this.anade_marca("<div><br />Primer paso a nivel, cruzarlo con precaución</div>", new google.maps.LatLng(43.552625,-5.871299), 'Paso a nivel 1');
			this.anade_marca("<div><br />Segundo paso a nivel, cruzarlo con precaución</div>", new google.maps.LatLng(43.555799,-5.871278), 'Paso a nivel 2');
			this.anade_marca("<div><br />Cruce con ceda el paso. Girar a la izquierda</div>", new google.maps.LatLng(43.556242,-5.871195), 'Cruce 1');
			this.anade_marca("<div><br />Tomar la salida a la derecha inmediata al cruce anterior</div>", new google.maps.LatLng(43.55638,-5.871597), 'Salida 2');
			this.anade_marca("<div><br />Al llegar al pantano, tomar la salida a la derecha. <br />Seguir el camino hasta la residencia de La Granda</div>", new google.maps.LatLng(43.560102,-5.860946), 'Salida 3');
			this.anade_marca_fin("<div><br />Residencia de La Granda<br /><br />Coordenadas: 43º 33´33´´ N 5º 51´8´´ O</div>", new google.maps.LatLng(43.559651,-5.851732), 'Residencia de La Granda');
			
			
		}
		
		this.anade_marca = function(texto, lugar, titulo){
			
			var that = this;
			
			var infowindow = new google.maps.InfoWindow({
				content: texto
			});

			var marker = new google.maps.Marker({
				position: lugar,
				map: this.mapa_,
				title: titulo
			});

			google.maps.event.addListener(marker, 'click', function() {
			  infowindow.open(that.mapa_,marker);
			});
		}
		
		this.anade_marca_fin = function(texto, lugar, titulo){
			
			var that = this;
			
			var infowindow = new google.maps.InfoWindow({
				content: texto
			});

			var marker = new google.maps.Marker({
				position: lugar,
				map: this.mapa_,
				title: titulo
			});

			google.maps.event.addListener(marker, 'click', function() {
			  infowindow.open(that.mapa_,marker);
			});
			
			  infowindow.open(that.mapa_,marker);
			
		}
	}
}
function sustituye_enlace_mapa(){
	$('#mapa p a').attr('href', './mapa.php');
}
function crea_mapa(){		
	$(document).keyup(function(e) {
		if (e.keyCode == 27) {
			javascript:history.back();
		}   // esc
	});
	
	var mapa = new Mapa();
	mapa.abreMapa();
}



