$(document).ready(function() {
    lstCloseCat = LireCookie("AccClsCat").split(",");

    
/*
    // SplashScreen 1 fois page d'accueil
    if(LireCookie("intersticiel_vue") != "pubNewPac"){
	Shadowbox.open({
	    content:    '<div style="background-color:#FFFFFF;" id="welcome-msg"><img class="center" onclick="Shadowbox.close();" src="/ban.php?img=NouveauLesPAC_intersticiel.jpg&infst=CA_2067;"></div>',
	    player:     "html",
	    title:      "LesPAC.com s'est refait une beauté!",
	    height:     502,
	    width:      587
	});

	var theDate = new Date();
	var oneYearLater = new Date( theDate.getTime() + 31536000000 );
	var expiryDate = oneYearLater.toGMTString();

	document.cookie = "intersticiel_vue=pubNewPac;expires="+expiryDate;
    }

    setTimeout("Shadowbox.close();",5000);
*/
    // Colonne #1
    $("#cat-vehicules").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });
    $("#cat-ameublement").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });
    $("#cat-electronique").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });
    $("#cat-informatique").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });
    $("#cat-rencontres").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });
    $("#cat-communautaire").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });

    // Colonne #2
    $("#cat-immobilier").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });
    $("#cat-vacances-voyages").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });
    $("#cat-famille").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });
    $("#cat-sports-loisirs").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });

    // Colonne #3
    $("#cat-emploi").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });
    $("#cat-outils-materiaux").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });
    $("#cat-animaux").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });
    $("#cat-service").click( function() {
	toggleMenu($(this).attr("id"),"slow");
    });
    
    for ( var cat in lstCloseCat ){
	toggleMenu(lstCloseCat[cat]);
    }
    
    

    

    $("#ville_rech_text").result(findValueCallback).next().click(function() {
	$(this).prev().search();
    });

    $("#ville_rech_text").autocomplete('/search_ville.ajax.php', {
	width: 200,
	minChars: 3,
	max:50,
	matchContains:true
    });
/* Fin AutoComplete */
    
});

function getCookieVal(offset)
{
    var endstr=document.cookie.indexOf (";", offset);
    if (endstr==-1) endstr=document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function LireCookie(nom)
{
    var arg=nom+"=";
    var alen=arg.length;
    var clen=document.cookie.length;
    var i=0;
    while (i<clen)
    {
	var j=i+alen;
	if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
	i=document.cookie.indexOf(" ",i)+1;
	if (i==0) break;

    }
    return "";
}

/* AutoComplete */
function findValueCallback(event, data, formatted) {
    // date expiration 1 an
    var theDate = new Date();
    var oneYearLater = new Date( theDate.getTime() + 31536000000 );
    var expiryDate = oneYearLater.toGMTString();

    $("#ville_rech_text").val(data[0]);
    document.cookie = "ville_rech_text=" + data[0]+";expires="+expiryDate;

    $tmpData = data[1];
    $tmpData = $tmpData.split("_");
    $tmpReg = $tmpData[1];
    $tmpVil = $tmpData[0];

    $("#ville_rech_id").val($tmpVil);
    document.cookie = "ville_rech_id=" + $tmpVil+";expires="+expiryDate;
    $("#num_reg_ori").val($tmpReg);
    document.cookie = "num_reg_ori=" + $tmpReg+";expires="+expiryDate;
}

function toggleMenu(catName, speed){
    // date expiration 1 an
    var theDate = new Date();
    var oneYearLater = new Date( theDate.getTime() + 31536000000 );
    var expiryDate = oneYearLater.toGMTString();
    
    $("div:#"+catName+"-top-box").toggleClass("close");
    if($("div:#"+catName+"-top-box").hasClass("close")){
	$("#"+catName+"-bottom-box").hide(speed);
	if(jQuery.inArray(catName,lstCloseCat) < 0){
	    lstCloseCat.push(catName);
	    jQuery.unique(lstCloseCat);
	}
    }else{
	$("#"+catName+"-bottom-box").show(speed);
	index = jQuery.inArray(catName,lstCloseCat);
	if(index >= 0){
	    lstCloseCat.splice(index,1);
	}
    }
    
    document.cookie="AccClsCat="+lstCloseCat.join()+";expires="+expiryDate;
}

//-------------------------------------------------------------------
// FONCTION RECHERCHER
//-------------------------------------------------------------------

var etatRecherche = 0;

function rechercher(groupe,categorie,nbann,categorietitre,vR01) {
    var doc=document.recherche;

    // on verifie le groupe
    if (groupe == '') {
	doc.gr.value = doc.lstgr[doc.lstgr.selectedIndex].value;
    } else {
	doc.gr.value=groupe;
    }

    // on vérifie la catégorie
    if (categorie == '') {
	doc.cp.value = 0;
    } else {
	doc.cp.value=categorie;
    }

    // on vérifie le nb d'annonce
    if (nbann >= 0) {
	//doc.total.value=nbann;
	doc.total_categories.value=nbann;
    } else {
	//doc.total.value=0;
	doc.total_categories.value=0;
    }

    if(groupe==14) {
	//doc.parindex.value='1';
	doc.regionmembre.value = 'O';
    }

    if (etatRecherche == 0) {
	doc.mots.value = '';
    }

    if(categorie==27) {
	doc.R19.value=vR01;
    } else {
	doc.R01.value=vR01;
    }

    doc.cpt.value=categorietitre;
    doc.action='/recherche.php';

    doc.submit();
}

function changeEtat(objField) {
    changeColor(objField);
    if (etatRecherche == 0) {
	objField.value = "";
	etatRecherche = 1;
    }
}

function changeColor(objField) {
    objField.style.color='#000';
}

function chkCat() {
    if (document.lieurecherche.cp.value==""){
	alert("Vous devez sélectionner une catégorie.");
	return false;
    }
    return true;
}