$(document).ready(function() {
	
	// Views des visuels de la fiche
	// @todo Transformer en fonction jquery pour reutilisation
	$('.visuels').each(function(){
		var container = $(this);
		container.find(".minis a").each(function(i,val) {
			$(val).click(function(e){
				container.find('a.zoom').hide();
				container.find('a.zoom:eq('+i+')').show();
				var img = $(this).find('img');
				container.find(".legende").html(img.attr('alt'));
				$(this).blur();
				return false;
			});
		});
	});
	
	// Liste logements
	$('.logements').each(function(){
		var container = $(this);
		container.find("h3").click(function(e){
			if(container.hasClass('closed'))
				container.removeClass('closed').addClass('opened');
			else if(container.hasClass('opened'))
				container.removeClass('opened').addClass('closed');
			$(this).blur();
			return false;
		});
	});
	
	// Parser specifique au typologies de lot
    $.tablesorter.addParser({ 
        // set a unique id 
        id: 'typo', 
        is: function(s) {return false;}, 
        format: function(s) { 
            return s.toLowerCase().replace(/studio/,"1"); 
        }, 
        type: 'text' 
    });
    
    $.tablesorter.addParser({ 
        id: 'nombre', 
        is: function(s) { 
    		return s.match( new RegExp( /[m²€°]$/ ) );
    	}, 
        format: function(s) { 
    		return $.tablesorter.formatInt( parseInt( s.match( new RegExp(/([0-9 ]+)/) )));
        }, 
        type: 'numeric' 
    });
    
	// Tableaux triables
	$("table.sortable").bind("sortEnd",function(event){
		$(this).find("tbody tr").each(function(i,tr){
			if(i%2==0)
				$(tr).removeClass('second');
			else
				$(tr).addClass('second');
		});
	}).tablesorter({
        debug: false, // enable debug mode
        headers:{
			0:{sorter:'typo'},
			4:{sorter:false}
		}
	});
});

//Ajout a la selection (programme ou lot)
function addToSelection(obj, url, locale) {
	$.ajax({
		dataType: 'text',
		cache:false,
		url: url,
		data: {async:1},
		success: function(data) {
			// affichage textbox
			var tbUrl = '#TB_inline?inlineId=addSelection&amp;locale='+locale;
			if (data != "") {
				$("#addSelectConfirm").html(data);
			}
			tbx_show($(obj).attr('title'), tbUrl);
			$("#maSelection").show();
			return false;
		}
	});
}
