/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/
var map;
var gdir;
var map_canvas;

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // descomentar las siguientes 3 lineas para hacerlo random
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

		
function handleErrors()
{
 if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
   alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
 else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
   alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
 else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
   alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
 else if (gdir.getStatus().code == G_GEO_BAD_KEY)
   alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
 else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
   alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
 else alert("An unknown error occurred.");
 
}

function onGDirectionsLoad()
{ 
  // Use this function to access information about the latest load()
  // results.

  // e.g.
  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	// and yada yada yada...
}

function setDirections(fromAddress, locale)
{		    	
	document.getElementById("directions").innerHTML = "";
  gdir.load("from: " + fromAddress + " to: " + "41.13842,1.38240",
            { "locale": 'es' });

  document.getElementById("tbl_direcciones").className = "visible"; 
	document.getElementById("map").style.display = "none";
}
		
function loadMap()
{
	if (GBrowserIsCompatible()) {		
		map = new GMap2(document.getElementById("map"));
		map_canvas = new GMap2(document.getElementById("map_canvas"),
  { size: new GSize(325,400) } );

		
		gdir = new GDirections(map_canvas, document.getElementById("directions"));    
	  GEvent.addListener(gdir, "load", onGDirectionsLoad);
	  GEvent.addListener(gdir, "error", handleErrors);		
	  
		GEvent.addListener(map, "load", function() {
			$("#map").append("<span class='top'></span>");
			$("#map").append("<span class='left'></span>");
			$("#map").append("<span class='right'></span>");
			$("#map").append("<span class='bottom'></span>");
		});
		
		GEvent.addListener(map, "infowindowopen", function() {
		  var infoWindow = $(this.getInfoWindow().getContentContainers());
		  $("a", infoWindow).attr("target", "_self");
		});
	  	  		
		map.addMapType(G_PHYSICAL_MAP)
		map.setCenter(new GLatLng(41.13842, 1.38240), 10, G_PHYSICAL_MAP);

		map.addControl(new GScaleControl());
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());

		map.enableContinuousZoom();
		map.enableScrollWheelZoom();	
		
		map.addOverlay(new GGeoXml("http://www.campingsonline.com/santaeulalia2/_include/kml/6c5db8d4ea76fad2cb3ad17de0447995_es.kml?dump=" + new Date().getTime()));
	}	
}

$(document).ready(function(){
    setInterval( "slideSwitch()", 5000 );
		$("#slider").easySlider({
			auto: false,
			continuous: true 
		});  
		
		$("#diapos .foto").easySlider({
			auto: true,
			controlsShow: false,
			continuous: true 
			
		});	  
		
	$("a[action=contact]").fancybox({
		'height'					: 470,
		'width'					: 550,
    'autoScale'     	: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'type'				: 'iframe'
	});	
	
	$("a[action=video]").fancybox({
		'height'					: 400,
		'width'					: 650,
    'autoScale'     	: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'type'				: 'iframe'
	});		
	
	$(".foto > a").fancybox();
	$("#instal_lista > li > a").fancybox();
	$("a.ampliar").fancybox();
	
	$("form[name='form_contacto']").FormValidator({
		send_url: '_include/asp/ajax/send_contact.asp',
		alert_img: '_include/i/fleche_iz.png',
		abs_offset: -18
	});	
	if ($("#map").length > 0)
	{
		loadMap();
		
		$("#btn_Route").click(function(){
			setDirections($("#txt_Route").val(),"es");
			return false;
		});
	}
	
});
