$(document).ready(function(){

    //- Variables --------------------------------------------------------------
    var $base_url = 'http://intra.sa_hlm/';
    
    //- Initialisation ---------------------------------------------------------
    
    //Slide en accueil
	if($('.SlideTab').size() > 1){
		$('.SlideTab').tabSwitch('create', {type: 'slide', height: 80, width: 620, speed: 5000 });
		$('.SlideTab').tabSwitch('startAuto',{interval: 5000});
	}
	
	//Slide des exclusivités
	if($('.slideExclu').size() > 1){
		$('.slideExclu').tabSwitch('create', {type: 'scroll', height: 130, width: 200, speed: 5000 });
		$('.slideExclu').tabSwitch('startAuto',{interval: 5000});
	}
	
	//Lien pour nouvelle page
	$("a[rel^='_blank']").click(function(){
		window.open(this.href, '_blank');
		return false;
    });

	if( parseInt(jQuery.browser.version) != 6 ){

		//Select custom
		$('select.select').sSelect({
			ddMaxHeight: '205px'
		});
	}
	
	//Efface le label dans la textbox quand focus
	if( $('input.label-inside').length ){
		var value = $('input.label-inside').val();
		
		$('input.label-inside').focus(function(){
			if( value == $('input.label-inside').val() ){
				$('input.label-inside').val(null);
			}
		});
		
		$('input.label-inside').blur(function(){
			if( $('input.label-inside').val() == '' ){
				$('input.label-inside').val(value);
			}
		});
	}
	
	//Effet pour le sous-menu
	$('#menu').droppy();
	
	//Footer toujours en bas
	if( $('#wrapper').height()+30 < $(window).height() ){
		$('#wrap-footer').css('position', 'absolute').css('bottom', '0px').css('width', '100%');
	}
	
	//Set la hauteur du bloc gauche (fond rouge) photos (en absolute)
	if( $('.bloc-droite-bottom') ){
		var heightbloc = $('.bloc-droite-bottom').height();
		$('#scroller').height(heightbloc+40);
	}
	
	//Effet sur les textbox
	$('input:text').click(function(){
		$(this).addClass('click').stop().fadeTo(100, 0.2).fadeTo(500, 1);
	});
	$('input:text').blur(function(){
		$(this).removeClass('click');
	});
	
	//Effet sur le rollover des boutons
	$('input.submit-recherche, a.bt-extensible').hover(function(){
		$(this).css('opacity', '0.8');
	}, function(){
		$(this).css('opacity', '1');
	});
	
	//Formulaire dynamique de recherche
	if( $('#form-recherche').length ){
		
		$('input:text').keyup(function(){
			parametre = getParametre('form-recherche');
			rechercheAjax(parametre);
		});
		
		$('select#type_id').change(function(){
			parametre = getParametre('form-recherche');
			rechercheAjax(parametre);
		});
		
		$('input:checkbox').click(function(){
			parametre = getParametre('form-recherche');
			rechercheAjax(parametre);
		});
	}
	
	//Carte bretagne poue les dossiers
	$('.lien-map').click(function(){
		
		if( $('#texte-'+$(this).attr('id')).css('display') == 'none' ){
			$('.texte-map').hide();
			$('#texte-'+$(this).attr('id')).show();
		}
		else{
			$('#texte-'+$(this).attr('id')).hide();
		}
	});
	$('.close_texte').click(function(){
		$('.close_texte').parent().hide();
	});
});

function rechercheAjax(parametre){
	
	$.ajax({
		url: "/annonces/rechercheAjax/",
		global: false,
		type: 'POST',
		data: parametre,
		success: function(data){
			$('#nb_annonce').html("Annonce(s) trouvée(s) : "+data);
			//alert(data);
		}
	});
}

function getParametre(idFormulaire){
	
	var parametre = '';
	var oColChamp = $('#'+idFormulaire).find('input');
	
	//Input
	jQuery.each(oColChamp, function(key, champ){
		
		if( ($(champ).attr('type')!='button') && ($(champ).attr('type')!='submit') && $(champ).val() != '' ){
			
			if(parametre){ parametre += '&'; }
			
			if( $(champ).attr('type') == 'checkbox' ){
				parametre += $(champ).attr('name')+'='+encodeURIComponent($(champ).attr('checked'));
			}else{
				parametre += $(champ).attr('name')+'='+encodeURIComponent($(champ).attr('value'));
			}
		}
	});
	
	//Select
	var oColChamp = $('#'+idFormulaire).find('select');

	jQuery.each(oColChamp, function(key, champ){

		if(parametre){ parametre += '&'; }
		parametre += $(champ).attr('name')+'='+encodeURIComponent($(champ).attr('value'));		
	});
	
	return parametre;
}
