window.addEvent( 'domready', function() {
    
    if ($$(".type_dechets").length>0){
		lightbox($$(".dechets"));
    }

    checkFieldUnique();
    initExternalLinks();
  
    if ($('commune_acceptee')) {
	    commune_display();
    }
 
    if( $('SendMail') )	{
		$('SendMail').addEvent( 'click', function() { return initSendMail(); } );
 	}

} );

function commune_display() {

	if ($('deplier')) {
		$('deplier').destroy();
	}
	var all_li = $$('.li');
	if (all_li.length > 0) {
		all_li.each(function(item,i) {
				item.removeClass('li_display');
				item.addClass('li_display_none');
		});
		var lien = new Element('a', {'href':'#', 'text':'Liste Complète', 'onClick':'commune_display_none()', 'id':'replier'});
		lien.inject($('commune_acceptee'), 'after');
	}
}
function commune_display_none() {
	if ($('replier')) {
		$('replier').destroy();
	}
	var all_li = $$('.li');
	all_li.each(function(item,i) {
			item.removeClass('li_display_none');
			item.addClass('li_display');
	});
	var lien = new Element('a', {'href':'#', 'text':'Replier', 'onClick':'commune_display()', 'id':'deplier'});
	lien.inject($('commune_acceptee'), 'after');

}

function initExternalLinks()
{
    $$( 'a[rel=external]' ).each( function( item ) {
        item.target = '_blank';
    } );
}

/* ouverture d'1 lightbox pour les critères de recherche */
function lightbox(el) {
	
    $$(".type_dechets").addClass("lightbox");
    $$(".type_dechets").addClass("closed");
    
    var paragraphe = new Element('p', {'class':'super_link'});
    var lien = new Element('a', {'href':'#', 'text':'Sélectionnez des types de déchets'});
    $$(".type_dechets").grab(paragraphe, "before");
    paragraphe.grab(lien);
    
    var paragraphe_cancel = new Element('p', {'class':'super_link super_link_left'});
    var lien_cancel = new Element('a', {'href':'#', 'text':'Annuler'});
    $$(".type_dechets .overflow").grab(paragraphe_cancel, "bottom");
    paragraphe_cancel.grab(lien_cancel);
    
    var paragraphe_close = new Element('p', {'class':'super_link'});
    var lien_close = new Element('a', {'href':'#', 'text':'Validez la sélection'});
    $$(".type_dechets .overflow").grab(paragraphe_close, "bottom");
    paragraphe_close.grab(lien_close);
    
    lien.addEvent('click', function(){
        $$(".type_dechets").removeClass("closed");
        $$("body").set('styles',{'height':'100%', 'overflow':'hidden'});
	})
    
    lien_cancel.addEvent('click', function(){
        $$(".type_dechets").addClass("closed");
        $$("body").set('styles',{'height':'auto', 'overflow':'auto'});
	tout_decocher();
	})
    lien_close.addEvent('click', function(){
        $$(".type_dechets").addClass("closed");
        $$("body").set('styles',{'height':'auto', 'overflow':'auto'});
        valider_choix_dechet();
	})
}

// Afficher les dechets selectionne dans la pop up , sur le front
function valider_choix_dechet()
{  
	var all_checkbox = $$('.checkbox');
	var tmp = '';
	all_checkbox.each(function(item,i) {
		if (item.checked == true) {
			  tmp +=" "+item.getParent().getText()+", ";
		}
	});

	if (tmp != '' ) {
		tmp = tmp.substring(0,tmp.length-2);
		$('selection_dechet').set('text',tmp);
	} else {
		$('selection_dechet').set('text', 'Aucun type de déchets séléctionnés');
	}

}

// Decoche toutes les cases
function tout_decocher() {
	var all_checkbox = $$('.checkbox');
	var tmp = '';
	all_checkbox.each(function(item,i) {
		item.checked = false;
	});
	$('selection_dechet').set('text', 'Aucun types de déchets séléctionnés');
		
}

// Permet d avoir un champ rempli pour soumettre la recherche
// la combinaison des champs ville, libre et departement est impossible 
// 1 champ seulement
function checkFieldUnique() {
	
	// Mise a jour du champ pour la selection des critere dechet
	if($('selection_dechet')) {
		if ($('selection_dechet').getText() == "") {
			$('selection_dechet').set('text', 'Aucun type de déchets séléctionnés');
		}
	}
	var tab = $$('.departement');
	tab.each(function(item,i) {
		item.addEvent('click', function(){
			if (item.checked == true) {
				$('SearchText').set('value', '');
				$('ville').set('value', '');
				$('tous').checked = false;
			}
		});

	});
	
	if($('tous')) {
	$('tous').addEvent('focus', function(){
		resetListDepartement();
	});
	}
	
	if($('ville')) {
	$('ville').addEvent('focus', function(){
			resetListDepartement();
			$('tous').checked = true;
			$('SearchText').set('value', '');
	});
	}
	
	if($('SearchText')) {
	$('SearchText').addEvent('focus', function(){
			resetListDepartement();
			$('tous').checked = true;
			$('ville').set('value', '');
	});
	}
}

function resetListDepartement(){
	var tab = $$('.departement');
	tab.each(function(item,i) {
			item.checked = false;
	});
}

// Autocompletion pour les communes
document.addEvent('domready', function() {
		
if ($('ville')) {
	var tokens = result;
	
	// Our instance for the element with id "ville"
	new Autocompleter.Local('ville', tokens, {
		'minLength': 1, // We need at least 1 character
		'selectMode': 'type-ahead', // Instant completion
		'multiple': false // Tag support, by default comma separated
	});	
}
});


// Permet de centrer la carte au clique sur le bon site
function centreSiteSurCarte(lat, lgt, zoom) {
	
	map.setCenter(new GLatLng(lat, lgt), 11);
}



function redirectChange(url) {
	var SearchText = $('SearchText').value;
	if(SearchText != '') {url += '/(SearchText)/'+SearchText;}
	document.location.href = url
}


function initSendMail(){
	var comment = $('Comment').value;
	var nom = $('VotreNom').value;
	var prenom = $('VotrePrenom').value;
	var mail = $('VotreEmail').value;
	var bool = new Boolean(true);
	var resultat;

	if(comment == "")  {
		afficheMessageErreurBackground('Comment','#eccdcd');
		bool = false;
	} else {
		afficheMessageErreurBackground('Comment','#ffffff');
	}
	if(nom == "")  {
		afficheMessageErreurBackground('VotreNom','#eccdcd');
		bool = false;
	}else {
		afficheMessageErreurBackground('VotreNom','#ffffff');
	}
	if(prenom == "")  {
		afficheMessageErreurBackground('VotrePrenom','#eccdcd');
		bool = false;
	}else {
		afficheMessageErreurBackground('VotrePrenom','#ffffff');
	}
	if(mail == "")  {
		afficheMessageErreurBackground('VotreEmail','#eccdcd');
		bool = false;
	
	}else {
		var c = new RegExp(getEmailRegexp());
		var tab = c.exec(mail);
		if(tab == null)  {        
			afficheMessageErreurBackground('VotreEmail','#eccdcd');
			bool = false;
		} else {	
			afficheMessageErreurBackground('VotreEmail','#ffffff');
		}
	}
	
	return bool;
	//return false;
}

function afficheMessageErreur(message,divid) {
		//$(divid).set('html',message);
		$(divid).setStyles({
			color: '#c62d2d'
		});
}

function afficheMessageErreurBackground(divid,couleur) {
		$(divid).setStyles({
				background:couleur
			});
}

function getEmailRegexp() {
    return "^[a-z0-9A-Z]+([_|\.|-]{1}[a-zA-Z0-9]+)*@[a-z0-9]+([_|\.|-]*[a-z0-9A-Z]+)*[\.]{1}[a-zA-Z]{2,6}$";
}



