﻿/*
 * Créer le 27/01/2011 par Guillaume Roblès et Philippe Baranowicz
 */
var tmpImg ='';

function getBaseURL(){
        var url = location.host;
        return "http://"+url + "/";
}

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 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 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 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 close_form(){
	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 = $("#type_location");
		if(!test){
			var formLoca = $("#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{
			$("#type_location").val(location);
		}
	}
	$('#popup_location').remove();
	$('#fade').remove();
}

$(function(){
	$('#TRI').change(function(){
            var urlBase = getBaseURL();
            var selected = $(this).children('option:selected').val();
            var url = urlBase + 'immobilier/resultimmo/ordre/' + selected;
            $(location).attr('href',url);
    });
	$('input').focusin(function(){
            valeurDefault = $(this).val();
            $(this).val('');
        });
        $('input').focusout(function(){
            var valeurNew = $(this).val();
            if(valeurNew == '')
                $(this).val(valeurDefault);
        });
	//les tabs 
	$('.bottom_modules_contenu').hide();
	$('#coup_de_coeur').show();
	$('#ville').click(function(){
		$('#id_ville').val('');
	});
	$('.mini_sm').hover(function(){
        var lien = $(this).attr('rel');
		smart(lien)
	});
	
	$('#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_form();
		});
		$('#valider').click(function(){
			close_form();
		});
		$("#fade").show();
		$("#popup_location").show();
	});
});
 
/*
*selection
*/

function ajoutpanier(id,qte)
{

		var urlBase = getBaseURL();
        

        var uri = urlBase + "selection/panier?id=" + id + "&qte=" + qte;
        //var uri = 'http://localhost/clientsite/public/index/panier?id=' + id + '&qte=' + qte;
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
             xmlhttp=new XMLHttpRequest();
        }
        else{// code for IE6, IE5
             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.open("GET", uri, false);
        xmlhttp.send();
        if(xmlhttp.responseText != -1)
        {
            document.getElementById("nombre_selection").innerHTML = xmlhttp.responseText;
            //alert('Le bien a été ajouté à votre sélection.');
            desactiveritem(id);
        }
}

function supprimerpanier(id)
{
var urlBase = getBaseURL();
        
        var uri = urlBase + "selection/supprimerpanier?id=" + id;
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
             xmlhttp=new XMLHttpRequest();
        }
        else{// code for IE6, IE5
             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.open("GET", uri, false);
        xmlhttp.send();
        document.getElementById("nombre_selection").innerHTML = xmlhttp.responseText;
        
        var strBouton = "bloc_retirer_selection_" + id;
        var iOk = 0;
        if(document.getElementById(strBouton)==undefined)
        {
            strBouton = "bloc_detail_retirer_selection_" + id
            if(document.getElementById(strBouton))
                iOk = 2;
        }
        else
            iOk =1;
        if(iOk>0)
        {
            activeritem(id,iOk);
        }
}

function supprimerpanierselection(id)
{
        document.getElementById("bloc_liste_bien_detail_" + id).parentNode.removeChild(document.getElementById("bloc_liste_bien_detail_" + id))
        supprimerpanier(id);
}

function checkpanier()
{
var urlBase = getBaseURL();
        
    // Cette fonction va récupérer les id du paniers ...
    var uri = urlBase +"selection/getpanier";
    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
         xmlhttp=new XMLHttpRequest();
    }
    else{// code for IE6, IE5
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("POST", uri, false);
    xmlhttp.send();    
    var idList = eval("("+xmlhttp.responseText+")");
    for(i=0 ; i<idList.length;i++)
        desactiveritem(idList[i]);
    var elem = document.getElementById("nombre_selection");
    elem.innerHTML = idList.length;
}

function desactiveritem(id)
{
    var fiche = 1;
    var strBouton = "bloc_liste_selection_" + id;
    if(document.getElementById(strBouton)==undefined)
    {
        fiche = 2;
        strBouton = "bloc_detail_selection_" + id
    }    
    if(document.getElementById(strBouton))
    {
        var oElement = document.getElementById(strBouton);
        if(fiche==1)
        {
            oElement.id = "bloc_retirer_selection_"+id;
            oElement.className = "bloc_retirer_selection";
        }
        else
        {
            oElement.id = "bloc_detail_retirer_selection_"+id;
            oElement.className = "bloc_detail_retirer_selection";
        }
        
			oElement.getElementsByTagName("a")[0].href = "javascript:supprimerpanier("+id+")";
			oElement.getElementsByTagName("a")[0].innerHTML = deselection;
        
	}
}
function activeritem(id,fiche)
{
    var str = "";
    if(fiche==1)
        str = "bloc_retirer_selection_" + id;
    else
        str = "bloc_detail_retirer_selection_" + id;
    if(document.getElementById(str))
    {
        var oElement = document.getElementById(str); 
        str = "bloc_liste_selection";
        if(fiche == 2)
            str = "bloc_detail_selection"
        oElement.id = str+"_"+id;
        oElement.className = str;
		oElement.getElementsByTagName("a")[0].href = "javascript:ajoutpanier("+id+",0)";
		oElement.getElementsByTagName("a")[0].innerHTML = selection;
    }
}
$(document).ready(function () {
    checkpanier();
});
