var idPromo;
var urlBase;
var action;
var menuHref;
var oGal;
var marker = new Array;

/*
function navigFic() {
	sendAjax('NavigFic', this.id.substr(-1));
}

function setNavigFic() {
	window.location.reload();
}
*/
function setGetModuleRub() {
	var val = this.req.responseText;
	$('contModRub' + ajaxArgs[3]).innerHTML = val;
}

function setGetModuleMarche() {
	var val = this.req.responseText;
	$('contModMarche' + ajaxArgs[2]).innerHTML = val;
}

function initGal() {
	sendAjax('InitGal', idFiche);
}

function setInitGal() {
	var val = this.req.responseText;	
	var url;
	var x;
	var y;
	var type;
	
	if(val.substr(0,2) != 'ko') {
		oGal = val.evalJSON();
		$('galFiche').style.width = oGal.ref.wTotal + 'px';
		if(oGal.data[0].dataImgVign.id_img != null) {
			for(i = 0; i < oGal.data.length ; i++) {
				if(oGal.data[i].ref.id_img_g) {
					url = oGal.data[i].dataImgG.url_img;
					x = oGal.data[i].dataImgG.img_x;
					y = oGal.data[i].dataImgG.img_y;
					oGal.data[i].ref.type = 'img';
				}
				else {
					if(oGal.data[i].ref.id_video) {
						url = oGal.data[i].dataVideo.code_video;
						oGal.data[i].ref.type = 'video';
					}
					else {
						oGal.data[i].ref.type = 'flash';
					}
				}

				gal.setImg(
					oGal.data[i].ref.type,
					oGal.data[i].dataImgVign.url_img,
					url,
					oGal.data[i].dataImgVign.titre,
					oGal.data[i].dataImgVign.wAbs,
					x,
					y
				);
			}
			/*
			$('defilD').onmouseover = stopGal;
			$('defilD').onclick = manScroll;
	
			$('defilD').onmouseout = go;
			*/
		}
	}
}

function ajSendContactF(action, typeF) {
	var libInscr;
	var cp;
	var idFicheC;
	if(typeF == 1) {
		libInscr = 'inscr';
		cp = '';
		idFicheC = '';
	}	
	else {
		if(typeF == 2) {
			libInscr = 'FM';
			cp = $F('contCpFM');
			idFicheC = '';			
		}	
		else {
			libInscr = 'luc';
			cp = $F('contCpluc');
			idFicheC = idFiche;
		}
	}
		
	var tChp = new Array($F('contNom' + libInscr),$F('contPre' + libInscr),$F('contEma' + libInscr),
				$F('contTel' + libInscr),$F('contAdr' + libInscr), cp,
				$F('contVil' + libInscr),$F('contMes' + libInscr));
	var arg1;
	var arg11;
	if(action == 'refresh') {
		arg1 = 'refresh';
	}
	else {
		//arg1 = $F('captchaResult' + libInscr);
		arg1 = 'off';
	}
	if(typeF == 1) {
		arg11 = 'inscr';
	}
	else {
		if(typeF == 2) {
			arg11 = 'sendFM';
		}
	}
	sendAjax('SendMailF', arg1, idFicheC, tChp[0], tChp[1], tChp[2], tChp[3], tChp[4], tChp[5], tChp[6],tChp[7], arg11);
}

function setInscrNews() {
	var val = this.req.responseText;	
	var statut = val.substr(0, 2);
	var val = val.substr(2);
	alert(val);
}
function ajSendContactFinscr() {
	ajSendContactF(null, 1);
}

function ajSendContactFFM() {
	ajSendContactF(null, 2);
}

function ajSendContactNews() {
	var tChp = new Array($F('contNomnews'),$F('contPrenews'),$F('contEmanews'));
	sendAjax('InscrNews', tChp[0], tChp[1], tChp[2]);
}

function ajSendContactFM() {
	var tChp = new Array($F('contNom'),$F('contPre'),$F('contEma'),$F('contTel'),$F('contAdr'),$F('contCp'),$F('contVil'),$F('contMes'));
	var arg1;
	if(action == 'refresh') {
		arg1 = 'refresh';
	}
	else {
		//arg1 = $F('captchaResult');
		arg1 = 'off';
	}
	
	sendAjax('SendMailF', arg1, idFiche, tChp[0], tChp[1], tChp[2], tChp[3], tChp[4], tChp[5], tChp[6],tChp[7], 'sendFM');
}

function ajSendContactFriend(action) {
	var tChp = new Array($F('contNomYour'),$F('contEmailSend'),$F('contEmaYour'),$F('contMesSend'));
	var arg1;
	if(action == 'refresh') {
		arg1 = 'refresh';
	}
	else {
		//arg1 = $F('captchaResult2');
		arg1 = 'off';
	}
	
	sendAjax('SendMailF', arg1, idFiche, tChp[0], tChp[1], tChp[2], 'sendFriend', tChp[3]);
}

function setSendMailF() {
	var val = this.req.responseText;	
	var statut = val.substr(0, 2);
	if(statut == 'ko' | statut == 'ok' | statut == 'ob') {
		var val = val.substr(2);
	}
	var url;
	
	switch (statut) {
	case 're':
		if(isTagClass1 == '1' | isPageRub == 1) {
			url = oDesUrl + '/' + libUrlFiche + '/contact-' + lg + '.htm';
		}
		else {
			url = oDesUrl + '/contact-' + lg + '.htm';
		}
		window.location.href = url;
		break;
	case 'rF':
		window.location.reload(true);
		break;
	case 'ko':
		alert(val);
		//window.location.href = oDesUrl + '/' + 'contact-fr.htm';
		break;
	case 'ok':
		alert(val);
		if($('contMes')) {
			$('contMes').value = '';
		}
		closeSend();
		//window.location.href = oDesUrl;
		break;
	default:
		alert(val);
		break;
	}
}

function sendUrlAjax(n) {
	var nMenu;
	var id;
	// Evenement onclick
	if (typeof n == 'object') {
		var href = this.href;
		this.href = "javascript:void(0);";
		tCh = this.id.split('_');
		//id promo
		id = tCh[1];
		nMenu = 4;
		action = 'navigPromo';
	}
	// Click sur un menu
	else {
		if (n == 5 & mapReg != 1) {
			isLoadMap = true;
		}
		mapReg = null;
		nMenu = n;
		action = nMenu;
		var href = menuHref;
	}

	var baseHref = BASEHREF + WEBROOT;
	var url = href.substr(baseHref.length);

	sendAjax('UrlAj', url, nMenu, idFiche, id, idRub, isCartRub);
}

function setUrlAj() {
	var val = this.req.responseText;
	switch (action) {
	case 'navigPromo':
		$('contNavPromoPromo').innerHTML = val;
		initSendAjax('navigPromo');
		break;
	case '5':
		if(val == '') {
			
		}
		else {
			var FMZoom;
			if(idRubMod == 13) {
				FMZoom = 8;
			}
			else {
				FMZoom = 10;
			}
			
			var oMap = val.evalJSON();
			var oMapR = oMap["data"]["ref"];
			var oMapD = oMap["data"]["data"];
			// alert(oMapD[1].ref["id_fiche"] + '----' + oMapR['txGps'] + "," +
			// oMapR['tyGps'] + ' ----total' +
			// oMapR['total'] + "------" +
			// oMapR['xGps'] + "," + oMapR['yGps']);
	
			var myLatlng = new google.maps.LatLng(oMap['ref']['xGps'],
					oMap['ref']['yGps']);
			var myOptions = {
				zoom : FMZoom,
				center : myLatlng,
				mapTypeId : google.maps.MapTypeId.ROADMAP
			};
			var map = new google.maps.Map(document.getElementById("contOnglMap"),
					myOptions);
	
			setMap(map, oMapD, oMapR);
		}

	//	initialize();
		break;

	default:
		break;
	}
}

function setMap(map, data, ref) {
	// Add markers to the map
	var shape = {
		coord : [ 1, 1, 1, 25, 18, 20, 18, 1 ],
		type : 'poly'
	};
	var contentString;
	var ficheD;
	var ficheR;
	var myLatLng;
	var infowindow;
	for ( var i = 0; i < data.length; i++) {
		ficheD = data[i];
		ficheR = ref[i].ref;
		myLatLng = new google.maps.LatLng(ficheD[1], ficheD[2]);

		contentString = 
			'<div id="gCont' + ficheR['id_fiche'] + '" class="gInfo">' + 
			'<b>' + ficheR['titre'] + '</b>' + 
			'<br />';
		if(ficheR['imgColG'] != undefined) {
			contentString  += '<img src="' + ficheR['imgColG']['url_img'] + '"' + 
			' title="' + ficheR['imgColG']['titre'] + '"' +
			//' width="' + ficheR['imgColG']['img_x'] + 'px"' +
			//' height="' + ficheR['imgColG']['img_y'] + 'px"' +
			' height="85px"' +
			'alt="' + ficheR['imgColG']['titre'] + '" />' +
			'<br/>';
		}
		contentString  += 
			ficheR['adress_postal'] +
			'<br/>' +
			ficheR['cp'] + ' ' + ficheR['ville'] +
			'<br/>' +
			'Tel. : ' + ficheR['tel1'] +
			'<br/>' +
			'<a href="' + ficheR['url'] + '">FranceMarket</a>';
	
		marker[i] = new google.maps.Marker({
			position : myLatLng,
			map : map,
			icon : new google.maps.MarkerImage(ficheR['imgMap']['url_img'],
			// This marker is 20 pixels wide by 32 pixels tall.
			new google.maps.Size(25, 21),
			// The origin for this image is 0,0.
			new google.maps.Point(0, 0),
			// The anchor for this image is the base of the flagpole at 0,32.
			new google.maps.Point(0, 21)),
			shape : shape,
			title : ficheD[0],
			zIndex : ficheD[3],
			infowindow : new google.maps.InfoWindow({
			    content: contentString
			})
		});
		google.maps.event.addListener(marker[i], 'click', function() {
			this.infowindow.open(map,this);
		});
	}
}

var map;
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var myOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}

 map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

 // Add 5 markers to the map at random locations
 var southWest = new google.maps.LatLng(-31.203405,125.244141);
 var northEast = new google.maps.LatLng(-25.363882,131.044922);
 var bounds = new google.maps.LatLngBounds(southWest,northEast);
 map.fitBounds(bounds);
 var lngSpan = northEast.lng() - southWest.lng();
 var latSpan = northEast.lat() - southWest.lat();
 for (var i = 0; i < 5; i++) {
 var location = new google.maps.LatLng(southWest.lat() + latSpan * Math.random(),
 southWest.lng() + lngSpan * Math.random());
 var marker = new google.maps.Marker({
 position: location,
 map: map
 });
 var j = i + 1;
 marker.setTitle(j.toString());
 attachSecretMessage(marker, i);
 }
}

// The five markers show a secret message when clicked
// but that message is not within the marker's instance data

function attachSecretMessage(marker, number) {
 var message = ["This","is","the","secret","message"];
 var infowindow = new google.maps.InfoWindow(
 { content: message[number]
 });
 google.maps.event.addListener(marker, 'click', function() {
 infowindow.open(map,marker);
 });
} 
