function IncludeJavaScript(jsFile)
{
    document.write('<script type="text/javascript" src="'+ jsFile + '"></script>');
}

//IncludeJavaScript('theme_front/theme_front_1/javascript/labeled_marker.js');
IncludeJavaScript('theme_front/theme_front_1/javascript/ReloadStateControl.js');
IncludeJavaScript('theme_front/theme_front_1/javascript/SelectControl.js');
IncludeJavaScript('theme_front/theme_front_1/javascript/strokeEffect.js');
IncludeJavaScript('theme_front/theme_front_1/javascript/thickbox.js');

function createMarker(point, html, options, link, infowidth, zoom, coords, level, map) {
    
  var marker = new LabeledMarker(point, options);
  var infooptions = {maxWidth: infowidth}

    if(zoom && coords != null){
        GEvent.addListener(marker, "click", function() {
            map.setCenter(coords, level);
        });     
    }

  if(html.length) {
	  GEvent.addListener(marker, "click", function() {
	    marker.openInfoWindowHtml(html, infooptions);
	  });
  } else if(link.length) { 
	  GEvent.addListener(marker, "click", function() {
	    window.location = link;
	  });
  }
  markersArray.push(marker);
  return marker;
}


function myInfoWindowHtml(map, num)
{
  map.setCenter(aLocations[num][3]);
  aLocations[num][0].openInfoWindowHtml(aLocations[num][2]);
}

var aLocations = new Array;

var iconsmall = new GIcon();
iconsmall.image = "/theme_front/theme_front_1/image/contenu/icon-gmap-zoomed.png";
//iconsmall.shadow = "theme_front/theme_front_1/image/contenu/mm_20_shadow.png";
iconsmall.iconSize = new GSize(20, 30);
//iconsmall.shadowSize = new GSize(20, 18);
iconsmall.iconAnchor = new GPoint(10, 30);
iconsmall.infoWindowAnchor = new GPoint(3, 3);

var iconbig = new GIcon();
iconbig.image = "http://www.google.com/mapfiles/marker.png";
iconbig.shadow = "http://www.google.com/mapfiles/shadow50.png";
iconbig.iconSize = new GSize(20, 34);
iconbig.shadowSize = new GSize(37, 34);
iconbig.iconAnchor = new GPoint(6, 34);
iconbig.infoWindowAnchor = new GPoint(5, 1);




var iconcity = new GIcon();
iconcity.image = "theme_front/theme_front_1/image/contenu/hotel-map.png";
iconcity.iconSize = new GSize(40, 38);
iconcity.iconAnchor = new GPoint(20, 38);
iconcity.infoWindowAnchor = new GPoint(10, 10);


//modified by MGF 26/06/2009

var iconairport = new GIcon();
iconairport.image = "theme_front/theme_front_1/image/contenu/icon-fly-map.png";
//iconairport.shadow = "theme_front/theme_front_1/image/contenu/icon-fly-map-shadow.png";
iconairport.iconSize = new GSize(42,34);
//iconairport.shadowSize = new GSize(51, 26);
iconairport.iconAnchor = new GPoint(30, 20);
iconairport.infoWindowAnchor = new GPoint(30, 20);

//================== end=============================


function buildMap(map, geo_lat, geo_long, zoom, desc, ptype, hotels, cities, airports, format, overview , zoom_out_map , zoom_out_ter,mark, controls, limit, translations){

    if(controls == null){
       controls = new Object();
    }

    map.addControl(new GSmallZoomControl()) ;
    map.addControl(new GMapTypeControl(true)) ;
    map.addControl(new GScaleControl()) ;
    map.removeMapType(G_HYBRID_MAP)
    map.addMapType(G_PHYSICAL_MAP);
    
    /*if(mark != true){
    
    	map.removeMapType(G_NORMAL_MAP);
    	map.removeMapType(G_SATELLITE_MAP);
    	         
    }     */                   

    if(overview) {
        map.addControl(new GOverviewMapControl()) ;
    }   
    
    var latlng = new GLatLng(geo_lat, geo_long) ;
    map.setCenter(latlng, zoom + zoom_out_map);
    map.setMapType(format);       
    
    // ROP 30/9/10 Save Coordination to return when do the search
    //map.savePosition();
    map.clearOverlays();

                         
    if( mark == true){
		    GEvent.addListener(map, "maptypechanged", function() {
			  
		    	if( map.getCurrentMapType() == G_NORMAL_MAP ){
		    		
		    		map.setCenter(latlng, zoom + zoom_out_map);
		    		
		    	}else
		    		if( map.getCurrentMapType() == G_PHYSICAL_MAP ){
		    			
		    			map.setCenter(latlng, zoom + zoom_out_ter);
		    			
		    		}else{
		    			
		    			map.setCenter(latlng, zoom);
		    			
		    		} 
			                       
			})
	
		  point = new GLatLng(geo_lat, geo_long);
	      InfoHTML = desc;
	      marker = createMarker(point, InfoHTML, iconbig, 200);
	      map.addOverlay(marker);
    } else {
		 
            GEvent.addListener(map, "maptypechanged", function() {
              
                if( map.getCurrentMapType() == G_NORMAL_MAP ){
                    
                    map.setCenter(latlng, zoom + zoom_out_map);
                    
                }else
                    if( map.getCurrentMapType() == G_PHYSICAL_MAP ){
                        
                        map.setCenter(latlng, zoom + zoom_out_ter);
                        
                    }else{
                        
                        map.setCenter(latlng, zoom);                       
                    }                                
            })	 
	 }
    
     for(i=0;i<airports.length;i++) {

          var point = null;
          var InfoHTML = null;
          var marker = null;
          if ( airports[i][0] != '' && airports[i][1] != '' ){
	          point = new GLatLng(airports[i][0], airports[i][1]);
	          InfoHTML = '<b>' + airports[i][2] + '</b>';
            var option = {
                icon: iconairport, 
                title: airports[i][2]
                }
	          marker = createMarker(point, InfoHTML, option, "", 300);
	          map.addOverlay(marker);
	          aLocations[i] = new Array(marker, "xx", InfoHTML, point);
          }
        }
     

   if(ptype=="city" || ptype=="airport")
    {
      point = new GLatLng(geo_lat, geo_long);
      InfoHTML = desc;
      marker = createMarker(point, InfoHTML, iconbig, 200);
      map.addOverlay(marker);
    }

    validCities = 0;
    for(i=0;i<cities.length;i++) {
        if(parseInt(cities[i][0]) != 0 && parseInt(cities[i][1]) != 0){
            validCities++;  
        }
    }
    
    if(hotels.length < limit || validCities == 0){
        hotelPoints = generateHotels();
        if(cities.length == 1){
            wasCommuneCentered = centerMapInCommune(cities[0][8]); 
            if(!wasCommuneCentered) fromCurrentPoint = false;
            else fromCurrentPoint = true;
            showAllMarkers(hotelPoints, fromCurrentPoint);      
        }
    } else{
        if(cities.length > 1){
            generateCities();
        }       
    }
    
    for(i=0;i<airports.length;i++) {

      var point = null;
      var InfoHTML = null;
      var marker = null;
      point = new GLatLng(airports[i][0], airports[i][1]);
      InfoHTML = '<h2>' + airports[i][2] + '<h2>';
        var option = {
            icon: iconairport, 
            title: airports[i][2]
            }
      marker = createMarker(point, InfoHTML, option, "", 300);
      map.addOverlay(marker);
      aLocations[i] = new Array(marker, "xx", InfoHTML, point);
    }

    
    if(controls.viewCommunes == true && cities.length > 1){
        //Setting the reloadStateButton
        var reloadState = new ReloadStateControl();
        reloadState.setTitle(translations['voircommunes']);
        map.savePosition();
        map.addControl(reloadState);
        reloadState.hide()
        GEvent.addListener(reloadState, 'reloadedState', function(){
            map.clearOverlays();
            //generateHotels();
            generateCities(); 
            if(comboCommunes != null){
                comboCommunes.toDefaultOption();
                //map.removeControl(reloadState);
                reloadState.hide();
            }    
        }); 
    }
 
    //Select for the hebergement types
    if(controls.hebergementTypeSelect == true){
        var hebergementsSelect = new Array(controls.hebergementTypeSelectData.length);
        for(i= 0; i < controls.hebergementTypeSelectData.length; i++){
            hebergementOption = new Object();
            hebergementOption.text = controls.hebergementTypeSelectData[i].type_nom + controls.hebergementTypeSelectData[i].standing; 
            hebergementOption.value = i;
            if(controls.hebergementTypeSelectDefaultSelected == i){
                hebergementOption.selected = true;
            }
            hebergementsSelect[i] = hebergementOption;
        }
        var comboHebergements = new SelectControl();
        comboHebergements.setOptions(hebergementsSelect);
        comboHebergements.setCaption('Hebergement type');
        if(controls.communeSelect == true && cities.length > 1) x_offset = 265;
        else x_offset = 150;
        map.addControl(comboHebergements, new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(x_offset, 7)));
        comboHebergements.setWidth('130px');
        GEvent.addListener(comboHebergements, 'valueChanged', function(value){
            window.location.href = controls.hebergementTypeSelectData[value].lien.replace('&amp;', '&');
        });
    }
    
    if(controls.communeSelect == true && cities.length > 1){
        var citiesSelect = new Array(cities.length);
        var cityOption = new Object();
        
        for(i=0;i<cities.length;i++) {
            if(cities[i][9] > 0){
                cityOption = new Object();
                cityOption.text = cities[i][2]+ ' (' + cities[i][9] + ')';
                cityOption.value = cities[i][8];
                citiesSelect[i] = cityOption;
            }
        }
        //Setting the communes select
        var comboCommunes = new SelectControl();
        comboCommunes.setOptions(citiesSelect);
        comboCommunes.setCaption('Commune');   
        map.addControl(comboCommunes, new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(150, 7)));
        GEvent.addListener(comboCommunes, 'valueChanged', function(value){
            map.clearOverlays(marker);
            wasCommuneCentered = centerMapInCommune(value);
            hotelPoints = generateHotels(value);
            if(!wasCommuneCentered) fromCurrentPoint = false;
            else fromCurrentPoint = true;
            showAllMarkers(hotelPoints, fromCurrentPoint);
            if(reloadState){
                reloadState.show();
            }     
        });
    }
    
    // search count of hotels
    function counthotelsbyCity(commune_id){
        // variables necesary
        var hotelSums = new Array();        
        var typeSums = new Array();         
        var a = 0;
        // build condictions values
        for(var i = 0; i < hotels.length; i++) {
            if(commune_id == null || commune_id == hotels[i][4]){
                if(hotels[i][0] != 0 && hotels[i][1] != 0) {
                    var type = hotels[i][6];
                    
                    if(type == 1) { // hotels
                        var standing = hotels[i][5];
                        
                        if(hotelSums[standing] == null) hotelSums[standing] = 0;
                        hotelSums[standing]++;
                        
                    } else { //
                        if(typeSums[type] == null) typeSums[type] = 0;
                        typeSums[type]++;
                        
                    }
                    a++;    
                }
            }
        }      
                   
        // ROP 23/11/10 codiction for not show   PD begin
         var html = null;
        if(a > 0){
             html = '<span class="orange_fonce" style="clear: both !important; display: block;"><strong>'+a+' ';
                // ROP 14/9/10
                if (a > 1){
                    html += translations['hebergements'];
                } else {                    
                    html += translations['hebergement'];
                }
                
                html +='</strong></span>';
                html += '<ul class="listePuces">';
            
            for(var j in hotelSums) {
                var st = j.length/2;
                if (hotelSums[j]==1){
                    html += '<li><span><a id="type_'+commune_id+'_1_'+st+'" >' + hotelSums[j] + ' '+translations['type1s']+ ' '+j+'</a></span></li>';    
                } else {                    
                    html += '<li><span><a id="type_'+commune_id+'_1_'+st+'" >' + hotelSums[j] + ' '+translations['type1p']+ '  '+j+'</a></span></li>';    
                }
            }
            
            for(var k in typeSums) { 
                if (typeSums[k]==1){
                    html += '<li><span><a id="type_'+commune_id+'_'+k+'">' + typeSums[k] + ' '+translations['types'+k] + '</a></span></li>';
                } else {
                    html += '<li><span><a id="type_'+commune_id+'_'+k+'">' + typeSums[k] + ' '+translations['typep'+k] + '</a></span></li>';
                }
            }
              html += "</ul>";
              
        // ROP 23/11/10 PD else...
        } else {      
            html = 'X';   
        }// ... PD End   
        
        return html;    
    }
           
    //Generate the cities
    function generateCities(){
        for(var i=0;i<cities.length;i++) {
            if(cities[i][9] > 0){             
                //alert(counthotelsbyCity(cities[i][8]));
                generateCity(cities[i][8], cities[i][0], cities[i][1], cities[i][7], cities[i][2], cities[i][4], cities[i][5],cities[i][6], cities[i][3] ,map, hotels);  
            }
        }
        processTextStroke('markerLabel');   
    }
    
    
    //Function that generates a city
    function generateCity(commune_id, latitude, longitude, zoom_level, commune_nom, sel, aiLabel_x, aiLabel_y, link){
       // Send parameters to display 
        var point = null;
        var InfoHTML = null; 
        var marker = null;
        var option = null;
        var label_x = 6;
        var label_y = -9;
        var TotalHotelsCity = counthotelsbyCity(commune_id);
        
       // alert(counthotels);                
        point = new GLatLng(latitude, longitude);
        if(aiLabel_x) label_x = aiLabel_x;                                                                       
        if(aiLabel_y) label_y = aiLabel_y;
        //  alert(latitude);             
                                      
        // ROP 23/11/10 condiction for not display if value is 0,  PX1 Begin
        if (TotalHotelsCity != 'X'){
                    // ---
                    InfoHTML = '<div id="google_bubble"><h2>'+translations['commune']+' '+ commune_nom +'</h2>'+TotalHotelsCity+'</div> <br/> <div class="voirDetails" > <a id="viewcity'+commune_id+'" > <span> '+translations['Bdetails']+ ' </span> </a></div>';    
                         
                    if(sel) {
                      //option = {icon: iconcity, title: commune_nom, labelText: commune_nom, labelClass: "markerLabelSel", labelOffset: new GSize(label_x, label_y)}
                      option = {icon: iconcity, title: commune_nom, zIndexProcess:zIndexCity}
                    } else {
                      //option = {icon: iconcity, title: commune_nom, labelText: commune_nom, labelClass: "markerLabel", labelOffset: new GSize(label_x, label_y)}
                      option = {icon: iconcity, title: commune_nom, zIndexProcess:zIndexCity}
                    }
                                  
                     // Buble Simple..
                     marker = createMarker(point, InfoHTML, option, link, 300);
                     //marker = createMarker(point, "", option, link, 300, true, point, zoom_level, map, hotels);   
                                               
                // Display Information of Hotels                                                    
                 GEvent.addListener(marker, "click", function(){    
                    marker.openInfoWindowHtml(InfoHTML); 
                 });
         
                     
                    // Adding zoom link
                    GEvent.addListener(marker, "infowindowopen", function(){
                         // Display buble...
                         $("#viewcity" + commune_id).click( function() {
                            map.clearOverlays(marker);
                            wasCommuneCentered = centerMapInCommune(commune_id);
                            hotelPoints = generateHotels(commune_id, null, null);  
                            
                            if(!wasCommuneCentered) fromCurrentPoint = false;
                            else fromCurrentPoint = true;
                            showAllMarkers(hotelPoints, fromCurrentPoint);
                            
                            if(reloadState){
                                reloadState.show();
                            }
                            if(comboCommunes != null){
                                comboCommunes.valueSelected(commune_id);
                            }        
                         });
                         
                        // HOTELS for display on map
                        for (var st = 2; st < 6; st++){      
                            $('#type_'+commune_id+'_1_'+st).click( function() {
                                
                                type = 1;
                                standing = this.id.split('_')[3]; //.substr(-1);    
                                
                                map.clearOverlays(marker);
                                wasCommuneCentered = centerMapInCommune(commune_id);
                                hotelPoints = generateHotels(commune_id, type, standing);  

                                if(!wasCommuneCentered) fromCurrentPoint = false;
                                else fromCurrentPoint = true;
                                showAllMarkers(hotelPoints, fromCurrentPoint);

                                if(reloadState){
                                    reloadState.show();
                                }
                                if(comboCommunes != null){
                                    comboCommunes.valueSelected(commune_id);
                                }        
                            });
                        }  // ...

                        // OTHER TYPES for display
                        for (var t = 2; t < 7; t++){      
                            $('#type_'+commune_id+'_'+t).click( function() {
                                
                                type = this.id.split('_')[2];    
                                map.clearOverlays(marker);
                                wasCommuneCentered = centerMapInCommune(commune_id);
                                hotelPoints = generateHotels(commune_id, type, null);  

                                if(!wasCommuneCentered) fromCurrentPoint = false;
                                else fromCurrentPoint = true;
                                showAllMarkers(hotelPoints, fromCurrentPoint);

                                if(reloadState){
                                    reloadState.show();
                                }
                                if(comboCommunes != null){
                                    comboCommunes.valueSelected(commune_id);
                                }        
                            });
                        }  // ...
                    });   
                                            
                    //-------------------------------------------------------
                    map.addOverlay(marker); 
                    aLocations[i] = new Array(marker, "xx", InfoHTML, point); 
                   
        }// ...   PX1 End   
        
    }  
     
        
    function zIndexCity(){
        return 1;    
    }
    
    function zIndexCommune(){
        return 0;    
    }    
    
    function getCoordsFromCommuneID(commune_id){
        for(i=0; i< cities.length; i++){
            if(cities[i][8] == commune_id){
                return new GLatLng(cities[i][0], cities[i][1]);    
            }
        }    
    }
    
    function showAllMarkers(points, fromCurrentPoint){
        var bounds = new google.maps.LatLngBounds ();
        
        //Expand from the current point??
        if(fromCurrentPoint){
            bounds = map.getBounds();
        }
        
        //  Go through each...
        for (var i = 0; i < points.length; i++) {
          //  And increase the bounds to take this point
          bounds.extend (points[i]);
        } 
        map.setCenter (bounds.getCenter( ), map.getBoundsZoomLevel( bounds ) );
    }
    
    function centerMapInCommune(commune_id){
        for(i=0; i< cities.length; i++){
            if(cities[i][8] == commune_id){
                if(cities[i][0] != 0 && cities[i][1] != 0){
                    map.setCenter(new GLatLng(cities[i][0], cities[i][1]), cities[i][7]);    
                    return true;
                } else {
                    return false;
                }    
            }
        } 
    }
    
    function showViewCommunes(){

    }
    
    function hideViewCommunes(){
        
    }    
                                            
      function generateHotels(commune_id, type, standing){
          
        returnPoints = [];
        var a = 0; 
        // Value's condiction important.               
        var aces = 0;        
        
        for(var i=0; i < hotels.length;i++) {
            
            if(commune_id == null || commune_id == hotels[i][4]){
                var point = null;
                var InfoHTML = null;
                var marker = null;
                
                //alert("A"+hotels[i][6]);
                //alert("B"+type);
                //alert("A"+stan);
                //alert("B"+standing);
                 var stan = hotels[i][5].length/2;  
                // conditions ..
                 if(hotels[i][0] != geo_lat && hotels[i][1] != geo_long && hotels[i][0] != 0 && hotels[i][1] != 0 && (type == null) && (standing == null)) { 
                        aces = 1;
                } else if(hotels[i][0] != geo_lat && hotels[i][1] != geo_long && hotels[i][0] != 0 && hotels[i][1] != 0 && (hotels[i][6] == (""+type) && (stan == standing))){
                        /// && (hotels[i][5] == standing)
                        aces = 1;    
                } else if(hotels[i][0] != geo_lat && hotels[i][1] != geo_long && hotels[i][0] != 0 && hotels[i][1] != 0 && (hotels[i][6] == (""+type) && (standing == null  || stan == 0))){
                        aces = 1;                                            
                } else if(hotels[i][0] == geo_lat && hotels[i][1] == geo_long && hotels[i][0] != 0 && hotels[i][1] != 0 ){
                        aces = 1;                                            
                }//..
                                                        
                // conditions values importants
                if(aces == 1) {                     
                    point = new GLatLng(hotels[i][0], hotels[i][1]);
                    returnPoints[a++] = point;
                    InfoHTML = hotels[i][3];

                    var option = {icon: iconsmall, title: hotels[i][2],zIndexProcess:zIndexCity}
                    marker = createMarker(point, InfoHTML, option, "", 400);                                                      
                    map.addOverlay(marker);
                    aLocations[i] = new Array(marker, "xx", InfoHTML, point);
                    // resert value again to repect
                    aces = 0;  
                }
            }
        }
        return returnPoints;    
    }    
}   
