
$.extend(Site,{
	Map: {
		maps: {},
		images: null,
		countryFocus: -1,
		initMap: function(idMap,addresses,countries,images,lang){	
			
			Site.Map.images = images;
			Site.Map.maps[idMap] = new Map();
			Site.Map.maps[idMap].init(idMap,lang);
			
			//Views
			var viewSite = Site.Map.maps[idMap].addView('site',[5,19]);
			viewSite.addMarkersAddresses(addresses,{nameIcon:'mmgreen20',zoomClick:14});
			
			var viewCountries = Site.Map.maps[idMap].addView('country',[3,4]);
			viewCountries.addMarkersAddresses(countries,{nameIcon:'classic'});
			
			var viewRegion = Site.Map.maps[idMap].addView('region',[1,2]);
			viewRegion.addMarkersAddresses([{
											 'id':1,
											 'title':{'fr':'Europe','en':'Europe','de':'Europa','cn':'欧洲'},
											 'address':{'latitude':45.829854,'longitude':8.984852}
										 },
										 {
											 'id':2,
											 'title':{'fr':'Asie','en':'Asia','de':'Asien','cn':'亚洲'},
											 'address':{'latitude':20.962853,'longitude':99.687977}
										 }],{nameIcon:'classic2',zoomClick:4});
			Site.Map.maps[idMap].displayView();	
		},
		
		displayZone: function(idMap,nameView,idMarker){
			if(!nameView || !idMarker){
				Site.Map.maps[idMap].reload();
			}else{
				Site.Map.countryFocus = idMarker;
				Site.Map.maps[idMap].displayZone(nameView,idMarker);
			}
		},
		
		initCountry: function(idCountry){
			if(idCountry){ $("#map_select_country").val(idCountry); }
			else{ $("#map_select_country option:first").attr('selected','selected'); }
		},
		initRegion: function(idRegion){
			if(idRegion){ $("#map_select_region").val(71); }
			else{ $("#map_select_region option:first").attr('selected','selected'); }
		}
	}
});

//Class MAP
function Map(){
	
	this.id;
	this.lang;
	this.gmap;
	this.listView = [];
	this.defaultOptions = {
		zoom: 2,
		center: new google.maps.LatLng(40,60),
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	
	
	// constructeur
	this.init = function(idMap,lang){
		if(!idMap || !lang){ return ; }
		
		this.id = idMap;
		this.lang = lang;
		this.gmap = new google.maps.Map(document.getElementById(this.id),this.defaultOptions );
		
		// events
		google.maps.event.addListener(this.gmap, 'click', function() {});
		google.maps.event.addListener(this.gmap, 'zoom_changed', function() {
			Site.Map.maps[idMap].displayView(); //toujours appelée
		});
	};
	
	this.reload = function(){
		this.gmap.setOptions(this.defaultOptions);
	};
	
	this.addView = function(nameView,visibleFromTo){
		var view = new View();
		view.init(nameView,visibleFromTo,this.lang);
		this.listView[nameView] = view;
		return view;
	};
	
	this.displayView = function(){
		var zoom = this.gmap.getZoom();
		var visibleFromTo;
		
		for (view in this.listView){
			visibleFromTo = this.listView[view].visibleFromTo;
			var displayThisView = (zoom>=visibleFromTo[0]&&zoom<=visibleFromTo[1]);
			this.listView[view].setCountryFocus(Site.Map.countryFocus);
			this.listView[view].display(displayThisView,this.gmap); 
		}
	};
	
	this.displayZone = function(nameView,idMarker){
		if(!nameView || !idMarker) this.init;
		var view = this.listView[nameView];
		var marker = view.getMarker(nameView+"_"+idMarker);
		if(!marker){
			return false;
		}
		this.gmap.setOptions({
			'zoom':marker.zoomClick,
			'center':marker.gmarker.position
		});
		if(nameView == "site"){
			var infoWindow = InfoWindow.getInstance();
			infoWindow.init(marker.contentInfoWindow,this.lang);
			infoWindow.ginfoWindow.open(this.gmap,marker.gmarker);
		}
	}
}

//Class View
function View(){
	
	this.name;
	this.lang;
	this.countryFocus;
	this.listMarkers = [];
	this.visibleFromTo = [];
	
	// constructeur
	this.init = function(nameView, visibleFromTo, lang){
		this.name = nameView;
		this.visibleFromTo = visibleFromTo;
		this.lang = lang;
	};	
	
	this.addMarkersAddresses = function(listAddresses,paramsMarkers){
		if(!listAddresses || !paramsMarkers){ return ; }
		var marker = null;
		var lang = this.lang;
		var listMarkers = this.listMarkers;
		var typeMarkers = this.name; 
		$.each(
			listAddresses,
			function(){
				marker = new Marker();
				marker.init(typeMarkers,this,lang,paramsMarkers);
				listMarkers.push(marker);
			}
		);
		this.listMarkers = listMarkers;
	};
	
	this.setCountryFocus = function(countryFocus){
		this.countryFocus = countryFocus;
	};
	
	this.display = function(displayThisView,gmap){
		var listMarkers = this.listMarkers;
		var marker = null;
		var countryFocus = this.countryFocus;
		var viewName= this.name
		
		if(displayThisView == true && viewName=="site" ){
			//Site.Map.initCountry(countryFocus);
			Site.Map.initRegion();
		}
		if(displayThisView == true && viewName=="country" ){
			Site.Map.initCountry();
		}
		if(displayThisView == true && viewName=="region") {
			Site.Map.initCountry();
		}
		
		$.each(
			listMarkers,
			function(){	
				if(displayThisView == true){
					if(typeof this.gmarker != 'undefined'){
						if(viewName == "site"){
							this.setFocus(this.idCountry == countryFocus);	
						}
						this.gmarker.setMap(gmap);
					}	
				}else{
					InfoWindow.getInstance();
					if(typeof this.gmarker != 'undefined'){
						this.gmarker.setMap(null);
					}	
					this.actualyVisible = false;
				}
			}	
		);
	};
	
	this.getMarker = function(idMarker){
		var listMarkers = this.listMarkers;
		var marker = null;
		$.each(
			listMarkers,
			function(){
				if(this.id==idMarker){
					marker = this;
				}
			}
		);	
		return marker;
	};
}

//Class Marker
function Marker(){

	this.id;
	this.gmarker;	
	this.type;
	this.iconMarker;
	this.contentInfoWindow;
	this.zoomClick;
	this.latLng;
	
	// contruct
	this.init = function(type, addressOrCountry, lang, paramsMarkers){
		if(!addressOrCountry || !lang || !paramsMarkers){ return; }
		
		this.type = type;
		if(type == 'region'){
			// Region
			this.id = type+'_'+addressOrCountry.id;
			this.zoomClick = paramsMarkers.zoomClick;
			var iconMarker = new IconMarker();
			iconMarker.init(paramsMarkers.nameIcon);
			this.iconMarker = iconMarker;
			this.latLng = new google.maps.LatLng(addressOrCountry.address.latitude,addressOrCountry.address.longitude);
			this.gmarker = new google.maps.Marker({
				position:		this.latLng,
				title: 			addressOrCountry.title[lang],
				visible: 		true,
				icon: 			iconMarker.icon,
				shadow: 		iconMarker.shadow
			});

		}else if (type == 'country'){
			// Country
			this.id = type+'_'+addressOrCountry.id;
			this.idCountry = addressOrCountry.id;
			this.zoomClick = this.getZoomClick(addressOrCountry.bounds_distance);
			var iconMarker = new IconMarker();
			iconMarker.init(paramsMarkers.nameIcon);
			this.iconMarker = iconMarker;
			this.latLng = new google.maps.LatLng(addressOrCountry.latitude,addressOrCountry.longitude);
			
			this.gmarker = new google.maps.Marker({
				position:		this.latLng,
				title: 			addressOrCountry.name, 
				visible: 		true,
				icon: 			iconMarker.icon,
				shadow: 		iconMarker.shadow
			});
			
		}else if(type == 'site'){
			// Site
			this.id = type+'_'+addressOrCountry.id;
			this.idCountry = addressOrCountry.address.idCountry;
			this.zoomClick = paramsMarkers.zoomClick;
			var iconMarker = new IconMarker();
			iconMarker.init(paramsMarkers.nameIcon);
			this.iconMarker = iconMarker;
			this.latLng = new google.maps.LatLng(addressOrCountry.address.latitude,addressOrCountry.address.longitude);
			this.gmarker = new google.maps.Marker({
				position:		this.latLng,
				title: 			addressOrCountry.title[lang],
				visible: 		true,
				icon: 			iconMarker.icon,
				shadow: 		iconMarker.shadow
			});

			
			//create content InfoWindow
			var content = '<div class="infowindow" style="">';
			
			var styleP = 'style="margin: 0 0 10px 0 ; padding: 0; font-size: 12px; line-height: 13px;"';
			var styleIMG = 'style="float: right; display: block; margin : 0 4px 8px 8px; height: 60px; border: 1px solid #444;"' ;
			
			if(addressOrCountry.thumbpath && addressOrCountry.thumbpath!="#"){
				content += '<a href="javascript:slideshow(\''+addressOrCountry.images+'\')"><img src="'+addressOrCountry.thumbpath+'" '+styleIMG+' /></a>';
			}	
			content+= '<p '+styleP+' ><strong style="font-size: 13px;">'+addressOrCountry.title[lang]+'</strong></p>';
			if(addressOrCountry.description[lang]){
				content+= '<p '+styleP+' >'+addressOrCountry.description[lang]+'</p>';
			}
			
			// address
			content+= '<p '+styleP+' ><em>'; 
				content+=addressOrCountry.address.street+'<br>';
				content+=addressOrCountry.address.zipcode+' '+addressOrCountry.address.city+'<br>';
				content+=addressOrCountry.address.country;
			content+= '</em></p>';	
		
			//person contact 1
			if(addressOrCountry.namecontact || addressOrCountry.email){
				content+= '<p '+styleP+' >';
				if(addressOrCountry.namecontact) { content+= addressOrCountry.namecontact; }
				if(addressOrCountry.email) { content+= "<br>"+'Email : <a href="mailto:'+addressOrCountry.email+'">'+addressOrCountry.email+'</a>'; }
				if(addressOrCountry.phone){	content+="<br>"+'T.: '+addressOrCountry.phone; }
				if(addressOrCountry.gsm){ content+="<br>"+'M.: '+addressOrCountry.gsm; }
				if(addressOrCountry.fax){ content+="<br>"+'F.: '+addressOrCountry.fax; }		
				content+='</p>';
			}
			
			//person contact 2
			if(addressOrCountry.namecontact2 || addressOrCountry.email2 || addressOrCountry.phone2 || addressOrCountry.fax2){
				content+= '<p '+styleP+' >';
				if(addressOrCountry.namecontact2) { content+= addressOrCountry.namecontact2; }
				if(addressOrCountry.email2) { content+= "<br>"+'Email : <a href="mailto:'+addressOrCountry.email2+'">'+addressOrCountry.email2+'</a>'; }
				if(addressOrCountry.phone2) { content+= "<br>"+'T.: '+addressOrCountry.phone2; }
				if(addressOrCountry.gsm2){ content+="<br>"+'M.: '+addressOrCountry.gsm2; }
				if(addressOrCountry.fax2) { content+= "<br>"+'F.: '+addressOrCountry.fax2; }
				content+='</p>';
			}
			
			content+='</div>';
		
			this.contentInfoWindow = content;
		};	
		
		this.setFocus = function(focus){
			if(focus){
				var nameIcon = "mmgreen20";
			}else{
				var nameIcon = "mmgray20";
			}
			var iconMarker = new IconMarker();
			iconMarker.init(nameIcon);
			this.iconMarker = iconMarker;
			this.gmarker.setOptions({
				'icon':		iconMarker.icon,
				'shadow':	iconMarker.shadow
			});
		};
	
		// event
		if(typeof this.gmarker != 'undefined'){
			var zoomClick = this.zoomClick;
			var type = this.type;
			var contentInfoWindow = this.contentInfoWindow;
			this.gmarker.idCountry = this.idCountry;
			google.maps.event.addListener(this.gmarker, 'click', function() {
				if( type == "country" ) {
					Site.Map.countryFocus = this.idCountry;
				}
				this.map.setOptions({
					'zoom':zoomClick,
					'center':this.position
				});
				
				if(type == "site" && contentInfoWindow){
					var infoWindow = InfoWindow.getInstance();
					infoWindow.init(contentInfoWindow,lang);
					infoWindow.ginfoWindow.open(this.map,this);
				}
			});
		}
	}	
	
	this.getZoomClick = function(boundsDistance){
		var distance = Math.round(boundsDistance);
		var coef = 1;
		if(distance<(100*coef)){ return 10; }
		if(distance<=(200*coef)){ return 9; }
		if(distance<=(300*coef)){ return 8; }
		if(distance<=(600*coef)){ return 7; }
		if(distance<=(1000*coef)){ return 6; }else{ return 5; }
	}
	
}

//Class InfoWindow
function InfoWindow(){
	
	this.ginfoWindow;
	
	// construct
	this.init = function(content,lang){
		this.ginfoWindow = new google.maps.InfoWindow({ 
		});
		this.ginfoWindow.setOptions({maxWidth: 110});
		this.ginfoWindow.setContent(content);
	};
	this.hide = function(){
		this.ginfoWindow.close();
	};
	if ( InfoWindow.caller != InfoWindow.getInstance ) {  
		throw new Error("This object cannot be instanciated");  
	};
}
InfoWindow.getInstance = function() {  
	if (this.instance == null) {  
		this.instance = new InfoWindow();  
	}  
	if(this.instance.ginfoWindow){
		this.instance.ginfoWindow.close();
	}
	return this.instance;  
};

//Class IconMarker
function IconMarker(){
	
	this.icon;
	this.shadow;
	 
	this.init = function(nameIcon){
		var listIcon = {
				'mmblue20' : {
					'icon':'http://labs.google.com/ridefinder/images/mm_20_blue.png', 
					'iconSizeX'	:12,
					'iconSizeY'	:20,
					'shadow':'http://labs.google.com/ridefinder/images/mm_20_shadow.png',
					'shadowSizeX' :22,
					'shadowSizeY' :20
				},
				'mmgreen20' : {
					'icon':'http://labs.google.com/ridefinder/images/mm_20_green.png', 
					'iconSizeX'	:12,
					'iconSizeY'	:20,
					'shadow':'http://labs.google.com/ridefinder/images/mm_20_shadow.png',
					'shadowSizeX' :22,
					'shadowSizeY' :20
				},
				'mmgray20' : {
					'icon':'http://labs.google.com/ridefinder/images/mm_20_gray.png', 
					'iconSizeX'	:12,
					'iconSizeY'	:20,
					'shadow':'http://labs.google.com/ridefinder/images/mm_20_shadow.png',
					'shadowSizeX' :22,
					'shadowSizeY' :20
				},
				'classic' : {
					'icon':'http://www.google.com/mapfiles/marker.png', 
					'iconSizeX'	:20,
					'iconSizeY'	:34,
					'shadow':'http://www.google.com/mapfiles/shadow50.png',
					'shadowSizeX' :37,
					'shadowSizeY' :34
				},
				'classic2' : {
					'icon':'http://www.google.com/mapfiles/dd-end.png', 
					'iconSizeX'	:20,
					'iconSizeY'	:34,
					'shadow':'http://www.google.com/mapfiles/shadow50.png',
					'shadowSizeX' :37,
					'shadowSizeY' :34
				}
				
		}
		this.icon = new google.maps.MarkerImage(
			listIcon[nameIcon].icon,
			new google.maps.Size(listIcon[nameIcon].iconSizeX, listIcon[nameIcon].iconSizeY),
			new google.maps.Point(0,0),
			new google.maps.Point(0, listIcon[nameIcon].iconSizeY));
		this.shadow = new google.maps.MarkerImage(
			listIcon[nameIcon].shadow,
			new google.maps.Size(listIcon[nameIcon].shadowSizeX, listIcon[nameIcon].shadowSizeY),
			new google.maps.Point(0,0),
			new google.maps.Point(0, listIcon[nameIcon].shadowSizeY));
	}
}



function slideshow(listIdImage){
	var arrayId = listIdImage.split(',');
	$('#mapslideshow').html('');
	$.each(
		arrayId,
		function(key,id){
			$('#mapslideshow').append('<a href="'+Site.Map.images[id]+'" class="test"><img src="'+Site.Map.images[id]+'" class="test"'+'/></a>');
		}
	);
	$('#mapslideshow a.test').lightBox();
	$('#mapslideshow a.test:first').trigger('click');
}

