var valeurDefault = '';
var tmpImg ='';

function showDivn(button, id){
            $('.bottom_modules_contenu').hide();
            $('.actived').removeClass();
            var elem = document.getElementById(id);
			if(elem == null){
				var url = getBaseURL() + "immobilier/tabsn"
				$.get(url, { tab: id},
				   function(data){
				     $(button).parent().next().after(data);
				});
			}
            $(button).attr('class', 'actived');
            $(elem).show();
        }

function showDiv(button, id){
            $('.bottom_modules_contenu').hide();
            $('.actived').removeClass();
            var elem = document.getElementById(id);
            $(button).attr('class', 'actived');
            $(elem).show();
        }
function switchImg(obj){
       var src = $('.bloc_detail_photo_gm').children('img').attr('src');
       var oldSrc = src.split("/");
       oldSrc.pop();
       var prefix = oldSrc.join('/');
       var miniSrc = obj.src;
       var tab2Src = miniSrc.split("/");
       var newImg = tab2Src.pop();

       var newSrc = prefix +"/"+newImg
       $('.bloc_detail_photo_gm').children('img').attr('src', newSrc);
   }
function switchImgMulti(obj,module){
       var parent = obj.parentNode;
       var src = $('.bloc_' + module + '_detail_photo_gm').children('img').attr('src');
       var oldSrc = src.split("/");
       oldSrc.pop();
       var prefix = oldSrc.join('/');
       var miniSrc = obj.src;
       var tab2Src = miniSrc.split("/");
       var newImg = tab2Src.pop();
       var newSrc = prefix +"/"+newImg
       $('.bloc_' + module + '_detail_photo_gm').children('img').attr('src', newSrc);
       getMediaInfo('#' + parent.id);
   }

function getMediaInfo(id)
{
    var divMedia = $(id).find('div');
    $('.titre_photo').html(divMedia.find('span').html() + '&nbsp;');
    $('.description_photo').html(divMedia.find('div').html() + '&nbsp;');
}


   
function smart(lien){
	tmpImg =  $('.bloc_detail_photo_gm').children('img');
	var w = $('.bloc_detail_photo_gm').width();
	var h = $('.bloc_detail_photo_gm').height();
	var iFrame ='<iframe height="'+h+'" width="'+w+'" name="smartFrame" id="smartFrame" src="' + lien + '"></iframe>';
	$('.bloc_detail_photo_gm').html(iFrame);
}
   
   
function getBaseURL(){
        var url = location.host;
        return "http://"+url + "/";
        //var url = location.href;

//        var pathname = location.pathname;
//        var index1 = url.indexOf(pathname);
//        var index2 = url.indexOf("/", index1 + 1);
//        var baseLocalUrl = url.substr(0, index1);
//        //return baseLocalUrl + "/public/";
//        return baseLocalUrl + "/";
    }

    function mandat(){
        var urlBase = getBaseURL();
        var mandat = $('#no_mandat').val();
        var url = urlBase + 'immobilier/detail/reference/' + mandat;
        $(location).attr('href',url);
    }
    function alertmailimmo(){
        var urlBase = getBaseURL();
        var email = $('#mail_alerte').val();
        var url = urlBase + 'immobilier/alertemailimmo/mailalerte/' + email;
        $(location).attr('href',url);
    }

$(function(){

        $('#TRI').change(function(){
            var urlBase = getBaseURL();
			//ajouter par philippe le 09.05
			
			var cible = location.pathname;
			var tester = cible.indexOf('immobilier')
			if(tester != -1)
				cible = 'resultats_immobilier_gard.html';
			else 
				cible = 'references.html';
			//fin ajout
			
            var selected = $(this).children('option:selected').val();
            //modification 09.05
			//var url = urlBase + 'immobilier/resultimmo/ordre/' + selected;
			var url = urlBase + cible +'/ordre/' + selected; 
            $(location).attr('href',url);
        });
        //section block
        $('.bottom_modules_contenu').hide();
        $('#coup_de_coeur').show();
		//$('#coupcoeur').show();
        //$('#bt__coup_de_coeur').attr('class', 'actived');
        $('input:text').focusin(function(){
            valeurDefault = $(this).val();
            $(this).val('');
        });
        $('input:text').focusout(function(){
            var valeurNew = $(this).val();
            if(valeurNew == '')
                $(this).val(valeurDefault);
        });
		/*$('input:textarea').focusin(function(){
            valeurDefault = $(this).val();
            $(this).val('');
        });
        $('input:textarea').focusout(function(){
            var valeurNew = $(this).val();
            if(valeurNew == '')
                $(this).val(valeurDefault);
        });*/
        $('#ville').click(function(){
            $('#id_ville').val('');
        });
	$('.mini_sm').hover(function(){
        var lien = $(this).attr('rel');
		smart(lien)
	});
	function close_modal(){
			var elements = document.getElementsByName("location");
			for( i = 0; i <  elements.length; i++ ){
				if( elements[i].checked == true )
					var location = elements[i].value;
			}
			
			if(!isNaN(location)){
				var test = document.getElementById("type_location");
				if(!test){
					var formLoca = document.getElementById("form_recherches_immo");
					var inputLoca = document.createElement("input");
					inputLoca.type = "hidden";
					inputLoca.value = location
					inputLoca.name = "type_location";
					inputLoca.id = "type_location";
					formLoca.appendChild(inputLoca);
				}
				else{
					document.getElementById("type_location").value = location;
				}
			}
			
			document.body.removeChild(document.getElementById('popup_location'));
			document.body.removeChild(document.getElementById('fade'));
		}
	$('#type_transaction-2').click(function(){
		var form = '<div id="popup_location" class="popup_location">';
		form += '<form name="location_form">';
		form += location1 + ' :<br /><input type="radio" name="location" value="1122" />&nbsp;&nbsp;'+location2+'<br /><input type="radio" name="location" value="1121" />&nbsp;&nbsp;'+location3+'<br /><input type="radio" name="location" value="0" checked/>&nbsp;&nbsp;'+location4+'<hr />';
		form +='<input type="button" name="cancel"  id="cancel" value="'+buttonValider+'" />';
		form +='<input type="button" name="valider" id="valider" value="'+buttonAnuler+'" />';
		
		form +='</form></div><div id="fade"></div>';
		$('#container').before(form);
		$('#cancel').click(function(){
			close_modal();
		});
		$('#valider').click(function(){
			close_modal();
		});
		document.getElementById("fade").style.display = "inline";
		document.getElementById("popup_location").style.display = "inline";
	});
});
 

