var mapahome;
var markers;
$(document).ready(function() {

	// slideshow grande
	
    $('.slideshow').cycle({
		fx: 'fade',
		speed:  2500,
		timeout: 19000, //Inicialmente estaba a 6000.
		pager:  '#snav'
	});

	cycleState = "playing";
	$('#pauseslide').click(function(event) {
		if (cycleState == "playing") {
			$('.slideshow').cycle('pause');	
			cycleState = 'paused';
			$(this).attr('class','play');
		} else {
			$('.slideshow').cycle('resume');	
			cycleState = 'playing';
			$(this).attr('class','pausa');
		}
			
		
		event.preventDefault();
	});

	// slideshow sidebar
	
	$('#minigaleria').cycle({
		fx: 'scrollHorz',
		speed:  1000,
		timeout: 0,
		prev: '#mgprev',
		next: '#mgnext'
	});
	
	// text / image slideshows
	$('.textslideshow').each(function(e) {
//		$(this).find('.seccion').not('div:first').css('display','none');
		var sections = $(this).find('.seccion');
		var total = sections.length;
		var showing = parseInt(1) + Math.round(Math.random() * (total - 1));	//Calculo aleatoriamente el elemento que se va a mostrar primero
		$(this).find('.seccion').css('display','none');							//Oculto todos los elementos del slideshows
		$(this).find('.seccion').eq(showing-1).css('display','block');			//Muestro el elemento que se ha generado aleatoriamente
		
		$(this).find('.ncurrent').text(showing);
		$(this).find('.ntotal').text(total);
		
		$(this).attr('title', showing + "/" + total);
	});
	
	$('.textslideshow div.nav ul li a').click(function(e) {
		e.preventDefault();
		var textslideshow = $(this).closest('.textslideshow');
		$(textslideshow).find('.seccion').css('display','none');
		var data = $(textslideshow).attr('title');
		var showing = Number(data.split('/')[0]);
		var total = Number(data.split('/')[1]);
		
		if ($(this).hasClass('next'))
			var toShow = (showing == total) ? 1 : showing + 1;
		else
			var toShow = (showing == 1) ? total : showing - 1;
			
		$(textslideshow).find('.seccion').eq(toShow-1).fadeIn("slow");
		
		$(textslideshow).attr('title', toShow + "/" + total);
		$(textslideshow).find('.ncurrent').text(toShow);
		$(textslideshow).find('.ntotal').text(total);

	});
	// crear 
	var mapa = document.getElementById("mapahome");
	if ((mapa == null) || (mapa == 'undefined')) 
		return false;
		 
	mapahome = new google.maps.Map2( document.getElementById("mapahome"));
	mapahome.setCenter(new GLatLng( tenerifeLat, tenerifeLng), 9);
	mapahome.addControl(new GLargeMapControl());

	var i = 0;
	markers = [];
	for( el in mapdata) {
		var establecimiento = mapdata[el];
		var point = new GLatLng(establecimiento.latitud, establecimiento.longitud);
		var message = setDataTag(establecimiento);
		var icono = makeIcon(establecimiento.icono);
		markers[i] = addTag(mapahome, point, message, {icon: icono, title: establecimiento.nombre	} ) ;
		mapahome.addOverlay(markers[i]);
		i++;
	}; 
 	$('.sfotos ol li a').click(function(e) {
		e.preventDefault();
		var ref = $(this).attr('rel');
		markers[ref].openInfoWindowHtml(markers[ref].message); 	
	}); /*  */

});

