var SITE_ROOT = '';
var SOUSMENU_CAT = '';
var SOUSMENU_ITEM = '';
var CURRENT_PAGE = '';

var CURRENT_PROD_IMG_NB = 0;

var MAPLAT = "45.7850292";
var MAPLNG = "2.6140964";
var MAPZOOM = "5";
var ENABLE_SEARCH = false;

var map;
var geoXml;

var geocoder;

function findCP(){
	var cp = $('gmap-cp').value;
	if(parseInt(cp) == 75000 || parseInt(cp) == 13000 || parseInt(cp) == 69000){
		cp = cp.substring(0, 4) + '1';
	}
	if(cp.length != 5) alert('Code postal invalide');
	else if(parseInt(cp)>95999) alert('France Métropolitaine uniquement');
	else showAddress(cp + ', france');
}

function showAddress(address) {
	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert("Code postal : " + address + " introuvable");
			} else {
				findCloserPoint(point);
				
				//var marker = new GMarker(point);
				//map.addOverlay(marker);
			}
		}
	);
	
}

function findCloserPoint(point) {
	var closer_dist = 9999999;
	var closer_nb = 9999999;
	
	for(var i=0,j=gmap_data.length;i<j;i++){
		var temp_pt = new GLatLng(gmap_data[i][6], gmap_data[i][5]);
		var temp_dist = point.distanceFrom(temp_pt);
		if(temp_dist < closer_dist){
			closer_dist = temp_dist;
			closer_nb = i;
		}
	}
	
	var N = Math.max(point.lng(), gmap_data[closer_nb][5]);
	var S = Math.min(point.lng(), gmap_data[closer_nb][5]);
	var E = Math.max(point.lat(), gmap_data[closer_nb][6]);
	var W = Math.min(point.lat(), gmap_data[closer_nb][6]);
	
	var NS = (N - S) / 7;
	var EW = (E - W) / 7;
	
	N += NS;
	S -= NS;
	E += EW;
	W -= EW;
	
	
	var new_zoom = map.getBoundsZoomLevel(new GLatLngBounds( new GLatLng(W, S), new GLatLng(E, N) ) );
	
	map.setCenter(new GLatLng( ((E-W)/2+W), ((N-S)/2+S)), new_zoom);
	
	var ids = [closer_nb];
	
	var map_bounds = map.getBounds();
	
	for(var i=0,j=gmap_data.length;i<j;i++){
		if(i == closer_nb) continue;
		var temp_pt = new GLatLng(gmap_data[i][6], gmap_data[i][5]);
		if(map_bounds.containsLatLng(temp_pt) && ids.length < 3){
			ids.push(i);
		}
	}
	
	var p = $('distrib-resultats-content');
	p.empty();
	for(var i=0,j=ids.length;i<j;i++){
		var div = document.createElement('div');
		var h = document.createElement('h6');
		var p1 = document.createElement('p');
		var p2 = document.createElement('p');
		var p3 = document.createElement('p');
		h.appendChild(document.createTextNode(gmap_data[ids[i]][0]));
		p1.appendChild(document.createTextNode(gmap_data[ids[i]][1]));
		p2.appendChild(document.createTextNode(gmap_data[ids[i]][3]+' '+gmap_data[ids[i]][2]));
		p3.appendChild(document.createTextNode('Tél: '+gmap_data[ids[i]][4]));
		div.appendChild(h);
		div.appendChild(p1);
		div.appendChild(p2);
		div.appendChild(p3);
		p.appendChild(div);
	}
	
}


function displayProdImg(nb){
	//alert(nb);
	
	var prodimgs = $('produit-image-small');
		
	if($defined(prodimgs)){
		var DIVs =  $$(prodimgs.getChildren('div'));
		
		
		var d = DIVs[nb].getFirst('div');
		var url = d.style.backgroundImage;
		url = url.substring(4,url.length-11);
		//alert(url);
		
		$('produit-image-big-content').style.backgroundImage = 'url(' + url + '.jpg)';
		
	}
}

function showBigProdImg(){
	var url = $('produit-image-big-content').style.backgroundImage;
	url = url.substring(4,url.length-5);
	Slimbox.open(url + '-big.jpg');
}

var enInit = new Class({
	initialize: function(){
		window.addEvent('domready',this.domReady.bind(this));	
	},
	
	
	
	domReady:function(){
	
	
		var menu = $('blocmenu');
		
		$$(menu.getChildren('li')).each(function(li,i){
			li.addEvent('mouseenter', function(){
				li.parentNode.style.backgroundPosition = '0 '+((i+1)*-71)+'px';
			});
			li.addEvent('mouseleave', function(){
				li.parentNode.style.backgroundPosition = '0 0';
			});
		});
		
		var sousmenu = $('sous-menu');
		
		if($defined(sousmenu)){
			sousmenu = sousmenu.getFirst('ul');
			var LIs =  $$(sousmenu.getChildren('li'));
			
			LIs.each(function(li,i){
				var ul = li.getFirst('ul');
				var a = li.getFirst('a');
				
				if(li.title == SOUSMENU_CAT){
					a.style.backgroundImage = 'url('+SITE_ROOT+'/img/produits-menu-liens-bg-hove.png)';
					a.style.backgroundPosition = '0 0';
					a.style.color = '#fff';
				}
				
				if($defined(ul)){
					/*
					a.addEvent('click', function(){
						var tul = li.getFirst('ul');
						tul.style.display = (tul.style.display == 'none') ? 'block' : 'none';
						return false;
					});
					*/
					if(li.title != SOUSMENU_CAT) ul.style.display = 'none';
					else {
						var SLIs = $$(ul.getChildren('li'));
						SLIs.each(function(sli,j){
							if(sli.title == SOUSMENU_ITEM)
								sli.getFirst('a').style.textDecoration = 'underline';
						});
					}
				}
				
			});
			
		}
		
		var prodimgs = $('produit-image-small');
		
		if($defined(prodimgs)){
			var DIVs =  $$(prodimgs.getChildren('div'));
			
			DIVs.each(function(div,i){
				var d = div.getFirst('div');
				d.addEvent('click', function(){
					displayProdImg(i);
				});
			});
			displayProdImg(0);
		}
		
		if( $defined( $('distrib-gmap') ) ){
			if( GBrowserIsCompatible() ) 
			{
				gmap_data = gmap_data.split(";");
				for(var i=0,j=gmap_data.length;i<j;i++){
					gmap_data[i] = gmap_data[i].split("|");
				}
				
				
				map = new GMap2(document.getElementById('distrib-gmap'));
				map.addControl(new GSmallMapControl());
				map.addControl(new GMapTypeControl());
				geoXml = new GGeoXml('http://www.naturna.fr/gmap-ok.kml');
				map.addOverlay(geoXml);
				MAPZOOM = parseInt(MAPZOOM);
				map.setCenter(new GLatLng(MAPLAT, MAPLNG), MAPZOOM);
				
				geocoder = new GClientGeocoder();
				
				if(ENABLE_SEARCH) {
					findCP();
				}
			}
		}
	}
});


new enInit();

