// JavaScript Document
var url="http://www.panoramio.com/map/get_panoramas.php?order=popularity&set=public&from=0&to=20&minx=-180&miny=-90&maxx=180&maxy=90&size=medium";
		var map_layer_pictures=[];
var panoramioPhotos;
var tabMarkerPhoto;
function getPanoramioPictures(callBackFunction,picCount)
{
var bl=map.getBounds().getSouthWest();var tr=map.getBounds().getNorthEast();map_minx=bl.lat();map_miny=bl.lng();map_maxx=tr.lat();map_maxy=tr.lng();
var feedUrl='http://www.panoramio.com/map/get_panoramas.php?order=popularity&set=public&from=0&to='+picCount+'&minx='+map_miny+'&miny='+map_minx+'&maxx='+map_maxy+'&maxy='+map_maxx+'&callback='+callBackFunction+'&size=small';
var headID=document.getElementsByTagName("head")[0];
var newScript=document.createElement('script');
newScript.type='text/javascript';
newScript.src=feedUrl;
headID.appendChild(newScript);
return true;
}

function showPanoramioPicturesOnMap(panoramio)
{
panoramioPhotos=panoramio.photos;
addPanoramioPicturesOnMap();;
addPanoramioPicturesOnSite();
setTimeout('pageLoad()',1000);
}
function showPanoramioPicturesOnMap2(panoramio)
{
panoramioPhotos=panoramio.photos;
addPanoramioPicturesOnMap();;
addPanoramioPicturesOnSite();
setTimeout('pageLoad2()',1000);
}

function togglePanoramioPictures(object)
{
	if( document.getElementById(object).lang =="1")
	{
		var loading=document.createElement("div");
		loading.id="loadingPanoramio";
		loading.className="loading_photo";
		//loading.innerHTML="loading";
		var img=document.createElement("img");
		img.src="/themes/tooristic/images/ajax-loader.gif";
		var map1 = document.getElementById('globalMap');
		loading.appendChild(img);
		map1.appendChild(loading);
		//loadContent("/themes/tooristic/js/yahoo-dom-event.js","1");
		loadContent("/themes/tooristic/js/utilities.js","3");
		loadContent("/themes/tooristic/js/container_core-min.js","4");
		loadContent("/themes/tooristic/js/carousel.js","2");
		document.getElementById(object).lang="0";
		retunPan=getPanoramioPictures('showPanoramioPicturesOnMap','20');
		
	}
	else
	{
		document.getElementById(object).lang="1";
	}
}

function loadContent(file,id){
	
  var scriptTag = document.getElementById('loadScript');
  var header = document.getElementsByTagName('head').item(0);
  if(scriptTag) header.removeChild(scriptTag);
  script2 = document.createElement('script');
  script2.src = file;
script2.type = 'text/javascript';
script2.id = id; 
header.appendChild(script2);

}

function togglePanoramioPictures2(object)
{
	if( document.getElementById(object).lang =="1")
	{
			
				var loading=document.createElement("div");
		loading.id="loadingPanoramio";
		loading.className="loading_photo_fiche";
		var img=document.createElement("img");
		img.src="/themes/tooristic/images/ajax-loader.gif";
		var map1 = document.getElementById('globalMap');
		loading.appendChild(img);
		map1.appendChild(loading);
		
		// on integre les JS nécessaire pour panoramio et carousel
		//loadContent("/themes/tooristic/js/yahoo-dom-event.js","1");
		loadContent("/themes/tooristic/js/utilities.js","3");
		loadContent("/themes/tooristic/js/container_core-min.js","4");
		loadContent("/themes/tooristic/js/carousel.js","2");
		document.getElementById(object).lang="0";
		getPanoramioPictures('showPanoramioPicturesOnMap2','20');
	}
	else
	{
		document.getElementById(object).lang="1";
	}
}

function addPanoramioPicturesOnMap()
{
	tabMarkerPhoto = Array();
	for(var i=0;i<panoramioPhotos.length;i++)
	{
		var icon = new GIcon();
        /*icon.image = "/images/icon_photo.png"; */
		icon.image = panoramioPhotos[i].photo_file_url.replace("small","mini_square");
		icon.iconSize = new GSize(25, 19);
		icon.iconAnchor = new GPoint(13, 16);  
		icon.shadow = "/images/icon_search_o.png";
		icon.shadowSize = new GSize(26, 24); 
		icon.infoWindowAnchor = new GPoint(13, 2); 
		var marker=new GMarker(new GLatLng(panoramioPhotos[i].latitude,panoramioPhotos[i].longitude),icon);
		tabMarkerPhoto[i]=marker;
		marker.photo=panoramioPhotos[i];
		map.addOverlay(marker);
		map_layer_pictures.push(marker);
	}
GEvent.addListener(map,"click",
function(overlay,point)
{
if(!overlay||!overlay.photo)
return;
var p=overlay.photo;
if(p.photo_title.length>33)
{
	p.photo_title=p.photo_title.substring(0,33)+"&#8230;";
}
stringAffich="<div id='infowin'>"+"<a href='"+p.photo_file_url.replace("small","medium")+"' onclick='showLightbox(this); return false;' rel=\"lightbox\" >"+"<img width='"+p.width+"' height='"+p.height+"' src='"+p.photo_file_url+"'/><\/a>"+"<div style='overflow: hidden; width: 240px;'>"+"<p><a  class='photo_title' href='"+p.photo_url+"' target='_blank' ><strong>"+p.photo_title+"<\/strong><\/a></p>"+"<p style='font-size: 80%;'>Posté par <a target='_blank' href='http://www.panoramio.com/user/"+p.owner_id+"'>"+
p.owner_name+"<\/a> sur <a target='_blank' href='http://www.panoramio.com'>Panoramio<\/a></p><\/div>"+"<\/div>"
overlay.openInfoWindowHtml(stringAffich);
});
}

function show_hide(objet, idObjet)
{
if(objet.lang=="1")
{
	objet.className=objet.className+"_hover";
	objet.lang="0";
	new Effect.SlideDown(document.getElementById(idObjet));

}else{
	var stringTemp = new String(objet.className);
	objet.className=stringTemp.substring(0,stringTemp.length-6);
	objet.lang="1";
	new Effect.SlideUp(document.getElementById(idObjet));

}
}
function show_hide2(objet, idObjet)
{
if(objet.lang=="1")
{
	objet.className=objet.className+"_hover";
	objet.lang="0";
	document.getElementById(idObjet).style.display="";
	
}else{
	var stringTemp = new String(objet.className);
	objet.className=stringTemp.substring(0,stringTemp.length-6);
	objet.lang="1";
	document.getElementById(idObjet).style.display="none";
}
}


var panoramioPhotos2=Array();
function addPanoramioPicturesOnSite()
{
	
	for(var i=0;i<panoramioPhotos.length;i++)
	{
		
		panoramioPhotos2[i]=panoramioPhotos[i].photo_file_url.replace("small","thumbnail");
		var divImage=document.createElement("DIV");
		divImage.className="panoramio_div";
		divImage.onmouseover=function(){this.className='panoramio_div_hover';};
		divImage.onmouseout=function(){this.className='panoramio_div';};
		var image=document.createElement("IMG");
		image.src=panoramioPhotos2[i];
		image.i=i;
		image.onclick= function(){myclick(this.i);};
		var listeImage=document.getElementById('listeImage2');
		divImage.appendChild(image);
		listeImage.appendChild(divImage);
		
	}
	//panoramioPhotos=Array();
	//panoramioPhotos2=Array();
}

var lastRan = -1;

/**
 * Since carousel.addItem uses an HTML string to create the interface
 * for each carousel item, this method formats the HTML for an LI.
 **/

var fmtItem = function(imgUrl, url, title) {

  	var innerHTML = 
  		'<img onclick="myclick('+url+')" src="' + 
  		imgUrl +
		'" width="' +
		75 +
		'" height="' +
		75+
		'"/>';
  
	return innerHTML;
	
};

/**
 * Custom inital load handler. Called when the carousel loads the initial
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadInitHandler
 **/
var loadInitialItems = function(type, args) {
	var start = args[0];
	var last = args[1]; 
	load(this, start, last);	
};

/**
 * Custom load next handler. Called when the carousel loads the next
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadNextHandler
 **/
var loadNextItems = function(type, args) {	

	var start = args[0];
	var last = args[1]; 
	var alreadyCached = args[2];
	
	if(!alreadyCached) {
		load(this, start, last);
	}
};

/**
 * Custom load previous handler. Called when the carousel loads the previous
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadPrevHandler
 **/
var loadPrevItems = function(type, args) {
	var start = args[0];
	var last = args[1]; 
	var alreadyCached = args[2];
	
	if(!alreadyCached) {
		load(this, start, last);
	}
};

var loadNextItems = function(type, args) {
	var start = args[0];
	var last = args[1]; 
	var alreadyCached = args[2];
	
	if(!alreadyCached) {
		load(this, start, last);
	}
};


var load = function(carousel, start, last) {
	for(var i=start;i<=last;i++) {
		carousel.addItem(i, fmtItem(panoramioPhotos2[i], i, "Number " + i));
/*
		// Example of an alternate way to add an item (passing an element instead of html string)
		var p = document.createElement("P");
		var t = document.createTextNode("Item"+i);
		p.appendChild(t);
		carousel.addItem(i, p );
*/
	}
};


/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the previous button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: prevButtonStateHandler
 **/
var handlePrevButtonState = function(type, args) {

	var enabling = args[0];
	var leftImage = args[1];
	if(enabling) {
		/*leftImage.src = "images/left.gif";	*/
	} else {
		/*leftImage.src = "images/left.gif";*/
	}
};
/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the next button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: nextButtonStateHandler
 **/
var handleNextButtonState = function(type, args) {
    var enabling = args[0];
    var rightImage = args[1];
    if(enabling) {
		/*leftImage.src = "images/left.gif";	*/
	} else {
		/*leftImage.src = "images/left.gif";*/
	}
};
/**
 * You must create the carousel after the page is loaded since it is
 * dependent on an HTML element (in this case 'dhtml-carousel'.) See the
 * HTML code below.
 **/

var carousel; // for ease of debugging; globals generally not a good idea
var carousel2; // for ease of debugging; globals generally not a good idea
var pageLoad = function() 
{
	carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
		{
			numVisible:        4,
			animationSpeed:    0.25,
			scrollInc:         3,
			navMargin:         40,
			prevElement:       "prev-arrow",
			nextElement:       "next-arrow",
			loadInitHandler:   loadInitialItems,
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems,
			prevButtonStateHandler:   handlePrevButtonState,
			nextButtonStateHandler:   handleNextButtonState
		}
	);
	carousel2 = new YAHOO.extension.Carousel("dhtml-carousel2", 
		{
			numVisible:        4,
			animationSpeed:    0.25,
			scrollInc:         3,
			navMargin:         40,
			prevElement:       "prev-arrow2",
			nextElement:       "next-arrow2",
			loadInitHandler:   loadInitialItems,
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems,
			prevButtonStateHandler:   handlePrevButtonState,
			nextButtonStateHandler:   handleNextButtonState
		}
	);
	var loading = document.getElementById('loadingPanoramio');
	var map1 = document.getElementById('globalMap');
	map1.removeChild(loading)
	
};

var pageLoad2 = function() 
{
	carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
		{
			numVisible:        6,
			animationSpeed:    0.25,
			scrollInc:         3,
			navMargin:         40,
			prevElement:       "prev-arrow",
			nextElement:       "next-arrow",
			orientation:"vertical",
			loadInitHandler:   loadInitialItems,
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems,
			prevButtonStateHandler:   handlePrevButtonState,
			nextButtonStateHandler:   handleNextButtonState
		}
	);
	var loading = document.getElementById('loadingPanoramio');
	var map1 = document.getElementById('globalMap');
	map1.removeChild(loading);
	
};

var pageLoad3 = function() 
{
	carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
		{
			numVisible:        1,
			animationSpeed:    0.25,
			scrollInc:         1,
			navMargin:         40,
			prevElement:       "prev-arrow",
			nextElement:       "next-arrow",
			loadInitHandler:   loadInitialItems,
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems,
			prevButtonStateHandler:   handlePrevButtonState,
            nextButtonStateHandler:   handleNextButtonState
		}
	);	
		carousel = new YAHOO.extension.Carousel("dhtml-carousel2", 
		{
			numVisible:        3,
			animationSpeed:    0.25,
			scrollInc:         1,
			navMargin:         40,
			prevElement:       "prev-arrow2",
			nextElement:       "next-arrow2",
			loadInitHandler:   loadInitialItems,
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems,
			prevButtonStateHandler:   handlePrevButtonState,
            nextButtonStateHandler:   handleNextButtonState
		}
	);	
};

/*YAHOO.util.Event.addListener(window, 'load', pageLoad);*/
function selectionneCategorie(objet)
{
	if(objet.lang=="1")
	{
		switch(objet.id)
		{
			case "sorties" : 			
			objet.src="/themes/tooristic/images/quoi_search_o_03.jpg";
			break;
			case "sites_touristiques" : 			
			objet.src="/themes/tooristic/images/quoi_search_o_04.jpg";
			break;
			case "hebergements" : 			
			objet.src="/themes/tooristic/images/quoi_search_o_05.jpg";
			break;
			case "villages" : 			
			objet.src="/themes/tooristic/images/quoi_search_o_06.jpg";
			break;
			case "carnets" : 			
			objet.src="/themes/tooristic/images/quoi_search_o_07.jpg";
			break;
		}
		document.getElementById(objet.id+"_check").checked=true;
		objet.lang="0";
	}else
	{
				switch(objet.id)
		{
			case "sorties" : 			
			objet.src="/themes/tooristic/images/quoi_search_03.jpg";
			break;
			case "sites_touristiques" : 			
			objet.src="/themes/tooristic/images/quoi_search_04.jpg";
			break;
			case "hebergements" : 			
			objet.src="/themes/tooristic/images/quoi_search_05.jpg";
			break;
			case "villages" : 			
			objet.src="/themes/tooristic/images/quoi_search_06.jpg";
			break;
			case "carnets" : 			
			objet.src="/themes/tooristic/images/quoi_search_07.jpg";
			break;
		}
		document.getElementById(objet.id+"_check").checked=false;
		objet.lang="1";
		
	}
}


function click_sidebar(idx) {
  if(isArray(marker_html[idx])) 
  { 
  	markers[idx].openInfoWindowTabsHtml(marker_html[idx]); 
  }
  else { 
  	markers[idx].openInfoWindowHtml(marker_html[idx]); 
  }
}
  // This function picks up the click and opens the corresponding info window
  function myclick(i) {
	GEvent.trigger(map_layer_pictures[i], "click");
  }

var bounds= new GLatLngBounds();
function fit(){
map.panTo(bounds.getCenter()); 
map.setZoom(map.getBoundsZoomLevel(bounds));
}
  function drawAcircle(taille,lon,lat)
  {
	var point1 = new GLatLng(lat,lon);
 	map.setCenter(point1);
	drawCircle(map.getCenter(),taille, 40);
	fit();
  }
  
    function drawAcircle2(taille,lon,lat)
  {
	var point1 = new GLatLng(lat,lon);
 	map.setCenter(point1);
	drawCircle(map.getCenter(),taille, 40);
	//fit();
  }
  
  function over_sidebar(idx) {
	 markers[idx].setImage(markers[idx].getIcon().image.replace(".png","")+"Over.png")
  }
  
    function out_sidebar(idx) {
 	markers[idx].setImage(markers[idx].getIcon().image.replace("Over",""))
  }
  
  function drawCircle(center, radius, nodes, liColor, liWidth, liOpa, fillColor, fillOpa)
{
// Esa 2006
	//calculating km/degree
	var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
	var lngConv = center.distanceFrom(new GLatLng(center.lat(), parseFloat(center.lng())+0.1))/100;
	//Loop 
	var points = [];
	var step = parseInt(360/nodes)||10;
	for(var i=0; i<=360; i+=step)
	{
	var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), parseFloat(center.lng()) + 
	(radius/lngConv * Math.sin(i * Math.PI/180)));
	points.push(pint);
	bounds.extend(pint); //this is for fit function
	}
	fillColor = fillColor||liColor||"#0055ff";
	liWidth = liWidth||2;
	var poly = new GPolygon(points,liColor,liWidth,liOpa,fillColor,fillOpa);
	map.addOverlay(poly);
}

function centerMap(lng, lat,zoom){
	alert("longitude "+lng);
	alert("latitude "+lat);
 var point1 = new GLatLng(lat,lng);
 map.setCenter(point1, zoom);
}


  function searchAddress()
  {
	   geocoder = new GClientGeocoder();
       if (geocoder) 
	   {
        geocoder.getLatLng(document.searchform.input_search.value,
          function(point) 
		  {
            if (!point) 
			{
			 if(document.searchform.input_search.value=="")
			 {
				 alert('Aucune adresse saisie');
			 }
			 else
			 {
              alert(document.searchform.input_search.value + " non trouvée");
			 }
			  document.getElementById("searchlat").value=""
			  document.getElementById("searchlng").value=""
			  return false
            } else 
			{
			  document.getElementById("searchlat").value = point.lat().toFixed(5);
		      document.getElementById("searchlng").value = point.lng().toFixed(5);
			  document.searchform.submit();
			  
            }
          });
      }
    }
	
	
	function selectSousCateg(id,depart)
	{
		if(depart!="checkbox")
		{
			if(document.getElementById(id).checked==true)
			{
				document.getElementById(id).checked=false;
			}
			else
			{
				document.getElementById(id).checked=true;
			}
		}

				// on recherche dans le DOM la div correspondante
				divSousCateg=document.getElementById('souscateg-'+id);
				var checkbox = divSousCateg.getElementsByTagName('input');
				for (var i = 0; i < checkbox.length; i++) 
				{ 
						if(document.getElementById(id).checked==true)
							{
							checkbox[i].checked=true;
							}else{
							checkbox[i].checked=false;
							}
				}
	}
	
		function selectSousCateg2(id,depart)
	{
				if(depart!="checkbox")
				{
					if(document.getElementById(id).checked==true)
					{
						document.getElementById(id).checked=false;
						document.getElementById('span-'+id).innerHTML="";
					}
					else
					{
						document.getElementById(id).checked=true;
					}
				}

				// on recherche dans le DOM la div correspondante
				divSousCateg=document.getElementById('souscateg-'+id);
				var checkbox = divSousCateg.getElementsByTagName('input');
				for (var i = 0; i < checkbox.length; i++) 
				{ 
						if(document.getElementById(id).checked==true)
							{
							checkbox[i].checked=true;
							}else{
							checkbox[i].checked=false;
							document.getElementById('span-'+checkbox[i].id).innerHTML="";
							}
				}
	}
	function selectSousCateg3(id)
	{
					if(document.getElementById(id).checked==true)
					{
						document.getElementById(id).checked=false;
						document.getElementById('span-'+id).innerHTML="";
					}
	}
	function afficheFiltre(id)
	{
		divSousCateg=document.getElementById('souscateg-'+id);
		if(divSousCateg.style.display=="")
		{
			divSousCateg.style.display="none";
		}else{
			divSousCateg.style.display="";
		}
	}
	
    var gdir;
    var geocoder = null;
    var addressMarker;

    function initializeDirection() {
      if (GBrowserIsCompatible()) {      
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);

        //setDirections(depart, latArrivee,lonArrivee, "fr");
      }
    }
    
    function setDirections(fromAddress,latArrivee,lonArrivee, locale) {
		if(document.getElementById("pub"))
		{
			document.getElementById("pub").style.display="none";
		}
		if(document.getElementById("listeSites"))
		{
			document.getElementById("listeSites").style.display="none";
		}
		
		document.getElementById("directions").style.display="";
		document.getElementById('errorDirection').innerHTML="";
	 gdir.loadFromWaypoints([fromAddress, new GLatLng(latArrivee,lonArrivee)], { "locale": locale ,preserveViewport : true });
    }

    function handleErrors(){
	   divDirection=document.getElementById('errorDirection');
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	   divDirection.innerHTML="L'adresse saisie n'est pas reconnue par tOOristic, Veuillez essayer une autre adresse, ou saisir à nouveau celle-ci";
	   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(){ 
	}

function gestResult(lequel,id)
{
	switch(lequel)
	{
		case 'tag':
				if(document.getElementById('photo-'+id))document.getElementById('photo-'+id).style.display="none";
				if(document.getElementById('desc-'+id))document.getElementById('desc-'+id).style.display="none";
		break;
		case 'photo':
				if(document.getElementById('tag-'+id))document.getElementById('tag-'+id).style.display="none";
				if(document.getElementById('desc-'+id))document.getElementById('desc-'+id).style.display="none";
		break;
		case 'desc':
				if(document.getElementById('photo-'+id))document.getElementById('photo-'+id).style.display="none";
				if(document.getElementById('tag-'+id))document.getElementById('tag-'+id).style.display="none";
		break;
	}
	document.getElementById(lequel+'-'+id).style.display="";
}

