﻿
$(document).ready(function(){
	handlePieces();
	handleBasicBehaviours();
	finishDesign();
	emailBuilder();
});


/* This function finishes design */
function finishDesign(){
	$('#mainCtnrBlk')
		.prepend('<div id="leftShade" class="shade" />')
		.append('<div id="rightShade" class="shade" />');
};


function handlePieces(){
	$('#habitation').change(function(){
		var habitation=$(this).val();
		$('#pieces').children().remove();
		if(habitation==0){
		$('#pieces').append('<option value="0">Choisissez le type d\'habitation</option>');
		}
		else{
			var complement='';
			if(habitation==1){
				for(var i=1;i<=10;i++){
					complement+='<option value="'+i+'">'+i+'</option>';
				}
			}
			if(habitation==2){
				for(var i=1;i<=18;i++){
					complement+='<option value="'+i+'">'+(i+2)+'</option>';
				}
			}
			$('#pieces').append(complement);
		}
	});
	if($('#habitation').val()!=0){
		var habitation=$('#habitation').val();
		$('#pieces').children().remove();
		var complement='';
		if(habitation==1){
			for(var i=1;i<=10;i++){
				var selectedStatus=nbPieces==i?' selected="selected"':null;
				complement+='<option value="'+i+'"'+selectedStatus+'>'+i+'</option>';
			}
		}
		if(habitation==2){
			for(var i=1;i<=18;i++){
				var selectedStatus=nbPieces==i?' selected="selected"':null;
				complement+='<option value="'+i+'"'+selectedStatus+'>'+(i+2)+'</option>';
			}
		}
		$('#pieces').append(complement);
	}
};

/********************************
 *	Generic functions	*
 ********************************/



/****************** Gestion des URL **********************************/

/**
 * Récupère des valeurs d'une URL
 *
 * @param	{Array}		aNames		Liste des paramètres à récupérer
 * @param	{String}	sURL		URL à traiter
 *
 * @return	{Object}				Liste des valeurs pour les paramètre trouvés
 *
 * Récupère les valeurs d'une liste de paramètres de l'URL fournie (ou de celle courante)
 */
function getUrlParamsValues(aNames, sURL) {
	sURL = (sURL || window.location.toString());
	var reg = new RegExp('(\\?|&amp;|&)('+aNames.join('|')+')(=([^&#]*))?(?=(&amp;|&|#|$))', 'g');
	var match, results = {};
	while(match = reg.exec(sURL)) {
		results[match[2]] = match[4] || '';
	}
	return results;
};



/**
 * Récupère une valeur d'une URL
 *
 * @param	{String}	sName		le paramètre à récupérer
 * @param	{String}	URL			URL à traiter
 *
 * @return	{string}				La valeur du paramètre recherché
 * 
 * Récupère la valeur d'un paramètre de l'URL fournie (ou de celle courante)
 */
function getUrlParamValue(sName, sURL) {
	return getUrlParamsValues([sName], sURL)[sName];
};

/* Basic functions and behaviors... */
function handleBasicBehaviours(){
// popUp and target="_blank" simulator : rel="TARGETxWxH"
	$("a.popUp:not([rel!=''])").addClass('externalLnk').each(function(){
		var titleLnk=$(this).attr('title');
		$(this).attr('title',titleLnk+' (s\'ouvre dans une nouvelle fenêtre)');
	});
	$("a.popUp").click(function(){
		var lien=$(this).attr("href");
		var params=$(this).attr("rel").split('x');
		var target=params[0]!=''?params[0]:'';
		var sizing=typeof(params[1])!='undefined'&&typeof(params[2])!='undefined'?'width='+params[1]+',height='+params[2]:'';
		window.open(lien,target,sizing);
		return false;
	});
// closeBtn
	$('a.closeLnk').click(function(){
		window.close();
		return false;
	});
// PrintLnk
	$('.printLnk').click(function(){
		window.print();
		return false;
	});
// Confirm alerts
	$('a.confirm').click(function(){
		var msg=$(this).attr('rel')!=''?$(this).attr('rel'):'Etes-vous sûr de vouloir effectuer cette action ?';
		if(confirm(msg)){
			location.href=$(this).attr('href');
		}
		return false;
	});
};


// destroy blocks
function destroyBlk(selector){
	$(selector).remove();
};


// eMail recompozer
function emailBuilder(){
	var pattern=/^http:\/\/www\.([^\/]+)\/([^\.]+)(\.(html|htm|php|asp))?$/;
	var humanDetected=false;
	var human=function(){
		humanDetected=true;
		$('a.courriel').each(function(){
			if($(this).html().match(pattern)){
				$(this).html($(this).html().replace(pattern,'$2@$1'));
			}
			else{
				if($('span.accessibility',this).html()!=null){
					if($('span.accessibility',this).html().match(pattern)){
						$('span.accessibility',this).html($('span.accessibility',this).html().replace(pattern,'$2@$1'));
					}
				}
			}
		});
	};
	if(!humanDetected){
		$(document).mousemove(human).keypress(human);
	}
	$('a.courriel').click(function(){
		if(!humanDetected){return false;}
		if(this.href.match(pattern)){
			this.href=this.href.replace(pattern,'mailto:$2@$1');
		}
	});
	$('a.courrielSubject').click(function(){
		if(!humanDetected){return false;}
		if(this.href.match(pattern)){
			var subject=$(this).attr('rel');
			this.href=this.href.replace(pattern,'mailto:$2@$1'+'?Subject='+subject);
		}
	});
	return false;
};
