var geocoder = new google.maps.Geocoder();

function GoogleMap_getPosition(pos) {
	geocoder.geocode({
			latLng: pos
		}, function(responses) {
			if (responses && responses.length > 0) {
				document.getElementById('localisation_coordonnee').value=pos;
				document.getElementById('localisation_adresse').value=responses[0].formatted_address;
			} else {
				udocument.getElementById('localisation_adresse').value='';
			}
		}
	);
}

function GoogleMap_initialiser(zoom,mapType) {
	var latLng = new google.maps.LatLng(-25.363882,131.044922);
	var options = {
		zoom: zoom,
		center: latLng,
		mapTypeId: mapType //google.maps.MapTypeId.ROADMAP //HYBRID
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), options);
	marker = new google.maps.Marker({
		position: latLng,
		title: 'Frappe aérienne',
		map: map,
		draggable: false
	});
}
function GoogleMap_select()
{
	marker.setDraggable(true);
	
	google.maps.event.addListener(marker, 'dragend', function() {
		GoogleMap_getPosition(marker.getPosition());
	});
}

function GoogleMap_info(message)
{
	infowindow = new google.maps.InfoWindow({ content: message });
    google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map,marker);
    });
}

function GoogleMap_gps(p1,p2)
{
	latLng = new google.maps.LatLng(p1,p2);
	map.setCenter(latLng);
	marker.setPosition(latLng);
}

function GoogleMap_trouver(adresse)
{
	geocoder.geocode( { 'address': adresse}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			map.setCenter(results[0].geometry.location);
			marker.setPosition(results[0].geometry.location)
			if(marker.getDraggable)
				GoogleMap_getPosition(marker.getPosition())
		} else if(status==google.maps.GeocoderStatus.ZERO_RESULTS)
			alert("Google map n'a pas réussi à trouver l'adresse");
		else
			alert("Google map a émis une erreur : " + status);
	});
}


function Team_Membres(action)
{
	document.getElementById('Team_Membres_Form').action+=action
	document.getElementById('Team_Membres_Form').submit()
}


calendrier_events=new Array()
function Calendrier_addEvent(date_debut,date_fin,nom)
{
	sub=new Array(date_debut,date_fin,nom);
	calendrier_events.push(sub);	
}
function Calendrier_getEvent(date)
{
	text="";
	countA=calendrier_events.length;
	for (p = 0; p < countA; p++) // i doesn't work, problem with google map ?
	{
		if(date.getTime() >= calendrier_events[p][0].getTime() &&
		   date.getTime() <=  calendrier_events[p][1].getTime())
		{
			text+=calendrier_events[p][2]+" ";
		}
	}
	return text;
}

function Calendrier_initialiser(big)
{
	divObject=document.getElementById('calendrier');

	if(big==1)
	{
		divObject.className="calendrierBig";
		semNom=new Array('Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi','Dimanche');
	}
	else
		semNom=new Array('L','M','M','J','V','S','D');
	
	tableObject=document.createElement('table');
	divObject.appendChild(tableObject);
			
	//Flèches
	flecheGObject=document.createElement('a');
	flecheGObject.setAttribute('id','calendrier_fleche_g');
	flecheGObject.innerHTML='&#9664;';
	
	flecheDObject=document.createElement('a');
	flecheDObject.setAttribute('id','calendrier_fleche_d');
	flecheDObject.innerHTML='&#9654;';
	
	trObject=document.createElement('tr');
	trObject.className="calendrier_header"
	tableObject.appendChild(trObject);
	
	tdObject=document.createElement('td');
	tdObject.appendChild(flecheGObject);
	trObject.appendChild(tdObject)
	
	// Titre
	tdObject=document.createElement('td');
	tdObject.setAttribute('colspan',5);
	tdObject.setAttribute('id','calendrier_titre');
	tdObject.className='calendrier_titre';
	trObject.appendChild(tdObject)
	
	
	tdObject=document.createElement('td');
	tdObject.appendChild(flecheDObject);
	trObject.appendChild(tdObject)
	
	// Jours
	for (i = 0; i < 7; i++)
	{
		// TR
		trObject=document.createElement('tr');
		tableObject.appendChild(trObject);
		for (j = 1; j <= 7; j++)
		{
			tdObject=document.createElement('td');
			if(i==0)
			{
				tdObject.innerHTML=semNom[j-1];
				tdObject.className="calendrier_semaine"
			}
			else
			{
				tdObject.setAttribute('id','calendrier_td_'+((i-1)*7+j));
			}
			trObject.appendChild(tdObject)
		}			
	}
}

dateSelectStart=undefined
dateSelectEnd=undefined
function Calendrier_date(date)
{
	joursParMois=new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	nomMois=new Array('Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre');
	
	aujourdhui=new Date();
	aujourdhui=new Date(aujourdhui.getFullYear(),aujourdhui.getMonth(),aujourdhui.getDate())
	if(typeof(date)=="string")
	{
		split=date.split('/');
		cible=new Date(split[2],split[1]-1,split[0]);
	}
	else
		cible=date;

	anneeCible=cible.getFullYear()
	if ((anneeCible % 4 == 0 && anneeCible % 100 != 0) || anneeCible % 400 == 0)
		joursParMois[1] = 29;
	
	moisCible=cible.getMonth()
	
	premierJour=cible;
	premierJour.setDate(1);
	premierJourSemaine=premierJour.getDay()-1
	if(premierJourSemaine<0)
		premierJourSemaine=6
	moisPrecedant=moisCible-1
	if(moisPrecedant<0)
	{
		anneePrecedante=anneeCible-1
		moisPrecedant=11;
	}
	else
		anneePrecedante=anneeCible
		
	moisSuivant=moisCible+1
	if(moisSuivant>11)
	{
		anneeSuivante=anneeCible+1
		moisSuivant=0;
	}
	else
		anneeSuivante=anneeCible
		
	document.getElementById('calendrier_titre').innerHTML=nomMois[moisCible]+" "+anneeCible
	document.getElementById('calendrier_fleche_g').href="javascript:Calendrier_date('1/"+(moisPrecedant+1)+"/"+anneePrecedante+"')"
	document.getElementById('calendrier_fleche_d').href="javascript:Calendrier_date('1/"+(moisSuivant+1)+"/"+anneeSuivante+"')"
	
	if(document.getElementById('date_rdv') && document.getElementById('date_rdv').value!="")
	{
		split=document.getElementById('date_rdv').value.split('/');
		dateSelectStart=new Date(split[2],split[1]-1,split[0]);
	}
	//~ else
		//~ dateSelectStart=undefined
		
	if(document.getElementById('date_rdv_fin') && document.getElementById('date_rdv_fin').value!="")
	{
		split=document.getElementById('date_rdv_fin').value.split('/');
		dateSelectEnd=new Date(split[2],split[1]-1,split[0]);
	}
	//~ else
		//~ dateSelectEnd=undefined
	
	for (i = 1; i <= 6*7; i++)
	{
		
		if(i <= premierJourSemaine)
		{	// Mois précédant
			date=new Date(anneePrecedante,moisPrecedant,joursParMois[moisPrecedant]-premierJourSemaine+i);
			document.getElementById('calendrier_td_'+i).className="calendrier_moisdiff"
		}
		else if(i-premierJourSemaine <= joursParMois[moisCible])
		{	// Mois courant
			date=new Date(anneeCible,moisCible,i-premierJourSemaine);
			document.getElementById('calendrier_td_'+i).className=""
		}
		else
		{	// Mois suivant
			date=new Date(anneeSuivante, moisSuivant,i-premierJourSemaine-joursParMois[moisCible]);
			document.getElementById('calendrier_td_'+i).className="calendrier_moisdiff"
		}
		
		if(date < aujourdhui)
		{
			document.getElementById('calendrier_td_'+i).innerHTML=date.getDate()
			document.getElementById('calendrier_td_'+i).className="calendrier_passee"
		}
		else
		{
			
			aObject=document.createElement('a');
			aObject.innerHTML=date.getDate();
			aObject.href="javascript:Calendrier_a('"+date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear()+"')";
			document.getElementById('calendrier_td_'+i).innerHTML="";
			document.getElementById('calendrier_td_'+i).appendChild(aObject);
		}
		
		text=Calendrier_getEvent(date);
		if(text!="")
		{
			divObject=document.createElement('div');
			divObject.innerHTML=text;
			document.getElementById('calendrier_td_'+i).appendChild(divObject);
		}
		
		if(dateSelectStart!==undefined)
		{
			if(date.getTime() == dateSelectStart.getTime()) // date == dateSelectStart doesn't work !
				document.getElementById('calendrier_td_'+i).className="calendrier_selected"
			else if(dateSelectEnd!==undefined)
			{
				if(date > dateSelectStart && date <= dateSelectEnd)
					document.getElementById('calendrier_td_'+i).className="calendrier_selected"
			}
		}
	}
}

function Calendrier_a(date)
{
	if(document.getElementById('date_rdv'))
	{
		if(document.getElementById('date_rdv').value=="")
		{
			document.getElementById('date_rdv').value=date;
			document.getElementById('heure_rdv').focus()
			if(document.getElementById('date_rdv_fin').value=="" || document.getElementById('date_rdv_fin').value < date)
				document.getElementById('date_rdv_fin').value=date;
		}
		else
		{
			document.getElementById('date_rdv_fin').value=date
			document.getElementById('heure_rdv_fin').focus()
		}
		Calendrier_date(date)
	}
}

function Titre(nom)
{
	if(nom=="")
		nom="Airsoft Warm-UP <sup>Bêta</sup>"
	document.getElementById('Logo').innerHTML=nom
}


function addUtilisateurToList()
{
	pseudo=document.getElementById('utilisateur_pseudo').value;
	
}

function rechercher(nom)
{
	Ajax('Rechercher',"rechercher/"+nom,"GET","",document.getElementById('ReponseRechercher'))
}

function rechercheSelect(module,id,nom)
{
	labelObject=document.createElement('label');
	labelObject.style.display="block"
	inputObject=document.createElement('input');
	inputObject.type="checkbox"
	//~ inputObject.checked="checked"
	inputObject.setAttribute('checked','checked');
	inputObject.value=id
	
	add=""
	if(module==0) // Utilisateurs
		inputObject.setAttribute('name','utilisateurs[]');
	else if(module==1) // Teams membres
	{
		inputObject.setAttribute('name','teamMembres[]');
		add+=" <span class='small'>(Membres)</span>"
	}
	else if(module==2) // Teams membres + connus
	{
		inputObject.setAttribute('name','teamConnus[]');
		add=" <span class='small'>(Membres + Connus)</span>"
	}
	else if(module==3) // Emails
		inputObject.setAttribute('name','emails[]');
	
	labelObject.appendChild(inputObject)
	labelObject.innerHTML+=nom+add
	
	document.getElementById('liste_inviter').appendChild(labelObject);
}

function rechercherEmail(emails)
{
	array=emails.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
	if(array)
	{
		count=array.length
		for (i = 0; i < count; i++)
		{
			rechercheSelect(3,array[i],array[i])
		}
	}
}


