//Event.observe(window, 'load', loadAccordions, false);
function loadAccordions() {
	var topAccordion = new accordion('rideau-bloc', {
		classNames : {
			toggle : 'rideau-titre',
			toggleActive : 'rideau-titre_actif',
			content : 'rideau-contenu'
		},
		defaultSize : {
			height : 471,
			width : 830
		},
		direction : 'horizontal'
		//onEvent: 'mouseover'
	});
	
	topAccordion.activate($$('#rideau-bloc .rideau-titre')[3]);
	
	var fabricAccordion = new accordion('RideauFabric', {
		classNames : {
			toggle : 'fabrication-titreRideau',
			toggleActive : 'fabrication-titreRideau-actif',
			content : 'fabrication-contenuRideau'
		}
	});
	

	


}

var galerie = Class.create();
galerie.prototype = {
	initialize: function(module_photo) {
        this.old_image = null;
		this.lis = $(module_photo).getElementsByTagName('li');
		for( i=0; i < this.lis.length; i++){
				this.lis[i].style.display = 'none';
		}
	},
	afficheimage: function(image) {
	      Effect.Appear(this.lis[image]);
		  if ( this.old_image!=null) {
			  if  (this.old_image!=this.lis[image]){
				  Effect.Fade( this.old_image);
			  }
		  }
		  this.old_image = this.lis[image]
    }
}
 function aboutToggle () {
	 Effect.toggle('aboutBox', 'blind',{ duration: .5 });
 }
/*
var rollHover = Class.create();
rollHover.prototype = {
	initialize: function() {
		this.Hovers = $$('a.mhover');
	},
		
	clear: function(){
			this.Hovers.each(function(anchor) {
				console.log('anchor', anchor);
				anchor.className = 'mhover';
			});
			 
			
	}
}


$$('#mhover').each(function(anchor) {
	Event.observe(anchor, 'click', function() {
		
		console.log('cool');
		
	}, false);
});	
	
	*/

		
 
// /////////////////////////////
//  Gestion de la MAP Google ///
////////////////////////////////

google.load("maps", "2");
var map;
var dealers = [];
var dealerMapInitialized = false;




//cm_baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";

/*
cm_baseIcon.iconSize = new GSize(20, 34);
cm_baseIcon.shadowSize = new GSize(37, 34);
cm_baseIcon.iconAnchor = new GPoint(9, 34);
cm_baseIcon.infoWindowAnchor = new GPoint(9, 2);
cm_baseIcon.infoShadowAnchor = new GPoint(18, 25);
*/		

/**
 *
 * @param string ssKey google spreadsheet key
 * @param string wsId worksheet
 */
function loadJsonDealers(ssKey, wsId) {
  // Retrieve the JSON feed.
  var script = document.createElement('script');
  script.setAttribute('src', 'http://spreadsheets.google.com/feeds/list'
                         + '/' + ssKey + '/' + wsId + '/public/values' +
                        '?alt=json-in-script&callback=prepareDealers');
  script.setAttribute('id', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
  
  
}


function prepareDealers(response) {
    var entries = response.feed.entry;
    //console.log('entries', entries);
      for (var i = 0; i < entries.length; i++) {
        var entry = entries[i];
        var dealer = {
            latitude: parseFloat(entry["gsx$latitude"].$t),
            longitude: parseFloat(entry["gsx$longitude"].$t),
            nom: entry["gsx$name"].$t,
            type: entry["gsx$type"].$t,
            adresse: entry["gsx$adresse"].$t,
            cp: entry["gsx$cp"].$t,
            localite: entry["gsx$localite"].$t,
            tel: entry["gsx$telephone"].$t,
            mail: entry["gsx$mail"].$t,
            fax: entry["gsx$fax"].$t,
           
            //categories
            bambou: entry["gsx$bambou"].$t,
            coco: entry["gsx$coco"].$t,
            corails: entry["gsx$corails"].$t,
            massages: entry["gsx$massages"].$t,
            verres: entry["gsx$verres"].$t,
            recharges: entry["gsx$recharges"].$t
        };
        dealers.push(dealer);       
      }
    //console.log('dealers', dealers);
	  map = new google.maps.Map2(document.getElementById("map"));
      //map.setCenter(new google.maps.LatLng(50.4741346 , 4.8983286), 5);

    var bounds = new GLatLngBounds();
    for (var i=0; i < dealers.length; i++) {
        var dealer = dealers[i];
        var point = new GLatLng(dealer.latitude, dealer.longitude);
        bounds.extend(point);
    }
    map.setCenter(bounds.getCenter());
    var zoom = map.getBoundsZoomLevel(bounds);
    //if (zoom < 3) zoom = 5;
    map.setZoom(zoom-1);
      map.setUIToDefault();
      map.setMapType(G_PHYSICAL_MAP);
	  

	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.

//showDealers();
}

function showDealers(category)
{
    map.clearOverlays();
    var bounds = new GLatLngBounds();
	var markerOpts = {}; 
	var bIcon = new GIcon();


    for (var i=0; i < dealers.length; i++) {
        var dealer = dealers[i];
        if (!category || dealer[category] != '') {
			
/*			
			bIcon.image = "http://www.bougiesbsab.be/images/icon-" + category + ".png";
			// console.log ('bIcon' , bIcon);
			markerOpts.icon = bIcon;
			markerOpts.title = dealer.name;
*/			
			dealer.category = category

			var point = new GLatLng(dealer.latitude, dealer.longitude);
            var marker = createMarker(point, dealer);
            // create the marker
            map.addOverlay(marker);
            bounds.extend(point);
			//console.log ('adresseIcon', adresseIcon);
			
        }
    }
   
    var zoom = map.getBoundsZoomLevel(bounds);
    //if (zoom < 3) zoom = 5;
    map.setZoom(zoom-1);
    map.setCenter(bounds.getCenter());
}

function createMarker(point, dealer) {
    var customIcon = new GIcon(G_DEFAULT_ICON);
    customIcon.image = "http://www.bougiesbsab.be/images/icon-" + dealer.category + ".gif"; //"http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png";
    
	//console.log (customIcon.image);
	
    // Set up our GMarkerOptions object
    var markerOptions = { icon:customIcon };

    var marker = new GMarker(point, markerOptions);
      GEvent.addListener(marker, "click", function() {
        var html = "<div style='font-size:12px'>";
            html += "<H2>" + dealer.nom +
            "</H2>" + dealer.adresse 
			+ "<br>" 
			+ dealer.cp + " " + dealer.localite
			+ "<br><a href='mailto:" + dealer.mail + "'>" + dealer.mail + "</a>" ;
			
			if (dealer.tel != '') {
				html += "<br> T&eacute;l : " + dealer.tel
			}
			if (dealer.fax != '') {
				html += "<br> fax : " + dealer.fax;
			}
			
            "</div>";
        map.openInfoWindowHtml(point, html);
      });
      return marker;
}



function initializeDealerMap() {
    if (!dealerMapInitialized) {
        loadJsonDealers('rEMn84zl4CL0MHRgRXbhTkg', 'od6');
		// key for localhost = ABQIAAAAaV7I0LTyi0Nf7OkXh6zh5RTb-vLQlFZmc2N8bgWI8YDPp5FEVBRL6dHJU_azJATdeD_Qf73dWmTY_w
		// key for www.bougiesbsab.be : ABQIAAAAaV7I0LTyi0Nf7OkXh6zh5RQ_xaiWdPUfJv6jUJkNUFyjPM72hxQmtQPh4jQF53oJMaXftnmVVzyCYA
		// key for bougiesbsab.be : ABQIAAAAaV7I0LTyi0Nf7OkXh6zh5RQTMUP936a_fDVhL5MPCtegl316wxSnK4IAzhx_EToVrGeY0ImmUehIew
        dealerMapIntialized = true;
		
		
    }
}

 //google.setOnLoadCallback(initializeDealerMap);
