//*********************************************************
// js/mpa.js
// 
// Librairies de fonctions javascript spécifiques au projet
// --------------------------------------------------------
// Projet   : MaPetiteAgence.com
// Date     : Mai 2007
// Auteur   : Eric Miquelard, contact@em-xnet.com
//*********************************************************

// Index du sous-menu affiché
var idxSousMenuAffiche = 0;

function mpa_clicRechTerrain(e) {
	//~ alert($(e).is(':checked'));
	var $this = $(e);
	if ($this.is(':checked')) {
		$this.parents('td:first').find('input[type=checkbox]').not($this).attr('checked', '');
		$this.parents('form:first').find('input[value=vente]').attr('checked', 'checked');
	}
}

function afficherPhotoSecondaire(elt) {
	var s = elt.src;
	var imgPrinc = document.getElementById("imgPrinc");
	imgPrinc.src = s.replace("_vignette_", "");
	imgPrinc.title = elt.title;
}

function calculerEconomie(elt) {
    var v = fmtMontantEnInt(elt.value);
    if (v != "") {
        v = parseInt(v);
        elt.value = fmtIntEnMontant(v, false);
        var fa = parseInt(v * 0.06)
        var eco = fa - parseInt(document.getElementById("coutEvalImmoMpa").innerHTML);
        if (v != "" && v > 0 && eco > 0) {
            document.getElementById("fraisAgence").innerHTML = fmtIntEnMontant(fa, true);
            document.getElementById("calculEconomie").innerHTML = fmtIntEnMontant(eco, true);
            if (timeoutCalculEconomie == null) {
    	        timeoutCalculEconomie = setTimeout('enregistrerCalculEconomie(0)', 2000);
            }
        } else {
            document.getElementById("fraisAgence").innerHTML = "&nbsp;";
            document.getElementById("calculEconomie").innerHTML = "";
        }
    } else {
        elt.value = "";
    } 
}

var timeoutCalculEconomie = null;
function enregistrerCalculEconomie(montant) {
	var elt = document.forms["formEconomie"].elements["prix"];
    var v = fmtMontantEnInt(elt.value);
    if (v != "") {
        v = parseInt(v);
        if (v == montant) {
        	// La saisie du montant n'a pas changé depuis 5 secondes
        	// -> on enregistre
        	calculEconomieEnregHttpReq(v);
        	timeoutCalculEconomie = null;
        } else {
        	// On attend de nouveau 5 secondes
        	montantCalculEconomie = v;
            timeoutCalculEconomie = setTimeout('enregistrerCalculEconomie(' + v + ')', 2000);
        }
    }
}

function calculEconomieEnregHttpReq(montant) {
    var tag = Math.round((Math.random()*99999)+1);
    var url = cheminRacine + "lib/calculEconomieEnregistrer.php?montant=" + montant + "&tag=" + tag;
    oHttpReq.open("GET", encodeURI(url), true);
    oHttpReq.onreadystatechange = calculEconomieEnregRetour;
    oHttpReq.send(null);
    return true;
}

function calculEconomieEnregRetour() {
    if (oHttpReq.readyState == 4) {
        if (oHttpReq.status == 200) {
            //alert(oHttpReq.responseText);
            // rien à faire
        } else {
            // alert("XMLHttpRequest a retourné une erreur ! (status = " + oHttpReq.status + ")");
        }
    }
    return true;
}

function calendrierChangerEtat(itemId, eltId, ts) {
    var tag = Math.round((Math.random()*99999)+1);
    var url = cheminRacine + "lib/calendrierChangerEtat.php?itemId=" + itemId + "&eltId=" + eltId + "&ts=" + ts + "&tag=" + tag;
    oHttpReq.open("GET", encodeURI(url), true);
    oHttpReq.onreadystatechange = calendrierChangerEtatRetour;
    oHttpReq.send(null);
    return true;
}

function calendrierChangerEtatRetour() {
    if (oHttpReq.readyState == 4) {
        if (oHttpReq.status == 200) {
            //alert(oHttpReq.responseText);
            // attendu : "eltId:classe"
            var arrResponse = oHttpReq.responseText.split(":");
            var elt = document.getElementById(arrResponse[0]);
            elt.className = arrResponse[1];
        } else {
            // alert("XMLHttpRequest a retourné une erreur ! (status = " + oHttpReq.status + ")");
        }
    }
    return true;
}

function calendrierInitSaisieDate(elt) {
    if (elt.value == "jj/mm/aaaa") {
        elt.value = "";
    }
}

function calendrierPeriode(itemId, etat) {
    var msg = "";
    var ok = true;
    var elt, dtdeb, dtfin;
    
    elt = document.forms["meForm"].elements["dtdeb"];
    bao_dateEnFr(elt);
    if (bao_estDateValide(elt.value)) {
        dtdeb = elt.value.replace("/", "").replace("/", "");
    } else {
        ok = false;
        msg += "La date de début de période n'est pas valide\n";
    }
    
    elt = document.forms["meForm"].elements["dtfin"];
    bao_dateEnFr(elt);
    if (bao_estDateValide(elt.value)) {
        dtfin = elt.value.replace("/", "").replace("/", "");
    } else {
        ok = false;
        msg += "La date de fin de période n'est pas valide\n";
    }
    
    if (ok) {
        var tag = Math.round((Math.random()*99999)+1);
        var url = cheminRacine + "lib/calendrierPeriode.php?itemId=" + itemId + "&dtdeb=" + dtdeb + "&dtfin=" + dtfin + "&etat=" + etat + "&tag=" + tag;
        oHttpReq.open("GET", encodeURI(url), true);
        oHttpReq.onreadystatechange = calendrierPeriodeRetour;
        oHttpReq.send(null);
    } else {
        alert(msg);
    }
    
    return ok;
}

function calendrierPeriodeRetour() {
    var arrItem, elt;
    if (oHttpReq.readyState == 4) {
        if (oHttpReq.status == 200) {
            //alert(oHttpReq.responseText);
            // attendu : "eltId:classe|eltId2:classe2|..."
            var arrResponse = oHttpReq.responseText.split("|");
            for (var i=0; i<arrResponse.length; i++) {
                arrItem = arrResponse[i].split(":");
                elt = document.getElementById(arrItem[0]);
                elt.className = arrItem[1];
            }
        } else {
            // alert("XMLHttpRequest a retourné une erreur ! (status = " + oHttpReq.status + ")");
        }
    }
    return true;
}

function calendrierVerifSaisieDate(elt) {
    var ok = true;
    bao_dateEnFr(elt);
    if (!bao_estDateValide(elt.value)) {
        ok= false;
        alert("La date n'est pas valide (format : JJ/MM/AAAA)");
        focusTimer(elt);
    }
    return ok;
}

function calendrierMouseover(elt) {
    elt.className += " rollover";
}

function calendrierMouseout(elt) {
    elt.className = elt.className.replace(" rollover", "");
}

function diapos_afficherDiaporama(table, itemId) {
    var html = '<div id="diaposTitre">';
    // Bouton pour fermer la fenêtre
    html += '<img id="diaposFermer" src="' + cheminRacine + 'img/btn-fermer.jpg" title="fermer le diaporama" onclick="diapos_fermer();" />';
    // Boutons de navigation
    if (diaposImgSrc.length > 1) {
        html += '<a id="diaposNavPrec" href="#" onclick="diapos_changerPhoto(-1);return false;" title="photo précédente">photo précédente</a>';
        html += '<a id="diaposNavSuiv" href="#" onclick="diapos_changerPhoto(1);return false;" title="photo suivante">photo suivante</a>';
    } else {
        html += '&nbsp;'
    }
    html += '</div>';
    html += '<div><img id="diapos_img" src="" width="550" height="408" title="Utilisez les flèches en haut pour naviguer" alt="Photo non disponible" /></div>';
    html += "</a>";
    //alert(diaposImgSrc[diaposIdx]);
    var eltZoom = document.getElementById("diapos");
    eltZoom.style.height = (eltZoom.offsetHeight + 16) + "px";
    eltZoom.innerHTML = html;
    MM_swapImage("diapos_img",'',diaposImgSrc[diaposIdx],1);
    //eltZoom.style.borderColor = "#bababa";
    eltZoom.style.backgroundColor = "#303030";
}

function diapos_changerPhoto(offset) {
    diaposIdx = diaposIdx + offset;
    if (diaposIdx < 0) { diaposIdx = diaposImgSrc.length - 1; }
    if (diaposIdx > diaposImgSrc.length -1) { diaposIdx = 0; }
    //alert(diaposIdx + ":" + diaposImgSrc[diaposIdx]);
    MM_swapImage("diapos_img",'',diaposImgSrc[diaposIdx],1);
} 
function diapos_fermer() {
    var eltZoom = document.getElementById("diapos");
    eltZoom.innerHTML = "";
    eltZoom.style.borderColor = "#303030";
    var x = parseInt(eltZoom.style.left);
    var y = parseInt(eltZoom.style.top);
    var w = eltZoom.offsetWidth;
    var h = eltZoom.offsetHeight;
    var cibleX = offsetDOM(zoomEltDepart, 'offsetLeft') - 2;
    var cibleY = offsetDOM(zoomEltDepart, 'offsetTop') - 2;
    var cibleW = zoomEltDepart.offsetWidth;
    var cibleH = zoomEltDepart.offsetHeight;
    eltZoom.style.backgroundColor = "transparent";
    zoom_lancer(eltZoom.id, x, y, w, h, -130, -45, cibleW, cibleH, 8);
}

function diapos_ouvrir(elt, table, itemId, imgFic) {
    var eltZoom = document.getElementById("diapos");
    if (zooming) {
        clearTimeout(zooming);
        eltZoom.style.display = "none";
    }
    var x = offsetDOM(elt, 'offsetLeft') - 2;
    var y = offsetDOM(elt, 'offsetTop') - 2;
    eltZoom.style.left = x + "px";
    eltZoom.style.top = y + "px";
    var w = elt.offsetWidth;
    var h = elt.offsetHeight;
    eltZoom.style.width = w + "px";
    eltZoom.style.height = h + "px";
    eltZoom.style.borderColor = "#303030";
    eltZoom.style.display = "block";
    zoomEltDepart = elt;
    zoomTable = table;
    zoomItemId = itemId;
    // Déterminer la photo actuellement affichée, et son index
    var imgFics = imgFic.split(":");
    diaposIdx = 0;
    diaposImgSrc = new Array();
    var nomImgEncours = elt.src.substring(elt.src.lastIndexOf("/") + 1, elt.src.length);
    for (var i = 0; i < imgFics.length; i++) {
        diaposImgSrc[i] = cheminRacineVo + imgFics[i];
        if (diaposImgSrc[i].indexOf(nomImgEncours) > -1) {
            diaposIdx = i;
        }
    }
    //zoom_lancer(eltZoom.id, x, y, w, h, 130, 45, 550 + 21*550/408, 408 + 21, 4);
    zoom_lancer(eltZoom.id, x, y, w, h, 130, 45, 550, 408, 4);
}

function fmtMontant(elt, avecEuro) {
    var v = fmtMontantEnInt(elt.value);
    if (v != "") {
        v = fmtIntEnMontant(v, avecEuro);
    }
    elt.value = v;
}

function fmtIntEnMontant(entier, avecEuro) {
    if (entier > 999) {
        sMontant = parseInt(entier / 1000) + " " + fmtPadL(entier % 1000, 3, "0");
    } else {
        sMontant = entier;
    }
    if (avecEuro && sMontant != "") {
        sMontant += " &euro;"
    }
    return sMontant;
}

function fmtMontantEnInt(sMontant) {
    var c;
    var sEntier = "";
    for (var i=0; i< sMontant.length; i++) {
        c = sMontant.charAt(i);
        if (c.match(/^[0-9]$/)) {
            sEntier += c;
        }
    }
    return sEntier;
}

function fmtPadL(s, lg, carac) {
    var sPadded = "" + s;
    while (sPadded.length < lg) {
        sPadded = carac.concat(sPadded);
    }
    return sPadded;
}

function gmaps_calculerCoord() {
    var f = document.forms["meForm"];
    var adresse = f.elements["adresse"].value;
    var cp = f.elements["code_postal"].value;
    var ville = f.elements["ville"].value;
    if (adresse != "" && cp != "" && ville != "") {
        var txt = adresse + "," + cp + " " + ville;
        var geocoder = new GClientGeocoder();
        geocoder.getLatLng(
            txt,
            function (point) {
                if (point) {
                    // Renseigner les champs cachés du formulaire
                    f.elements["lat"].value = point.lat();
                    f.elements["lng"].value = point.lng();
                    //alert("lat = " + point.lat() + ", lng = " + point.lng());
                } else {
                    //alert("position non trouvée : " + txt);
                    f.elements["lat"].value = "";
                    f.elements["lng"].value = "";
                }
            }
        );
    }
}

function suggestion(e) {
	if (e.value.length > 2) {
		var popup = document.getElementById("popupSuggestion");
		var x = bao_offsetDOM(e, 'offsetLeft');
		var y = bao_offsetDOM(e, 'offsetTop');
		y = y + 17;
		popup.style.left = x + "px";
		popup.style.top = y + "px";
		var f = e.parentNode;
		while(f.tagName != "FORM" && f.tagName != "BODY") {
			f = f.parentNode;
		}
	    var tag = Math.round((Math.random()*99999)+1);
	    var url = cheminRacine + "lib/suggestion.php?v=" + e.value + "&tag=" + tag + "&form=" + f.name + "&element=" + e.name;
	    oHttpReq.open("GET", encodeURI(url), true);
	    oHttpReq.onreadystatechange = suggestionRetour;
	    oHttpReq.send(null);
	}
}

function suggestionFermer() {
    var popup = document.getElementById("popupSuggestion");
    popup.style.visibility = "hidden";
    popup.innerHTML = "";
}

function suggestionRetour() {
    if (oHttpReq.readyState == 4) {
        if (oHttpReq.status == 200) {
        	var r = oHttpReq.responseText;
            //alert(r);
            var popup = document.getElementById("popupSuggestion");
            popup.innerHTML = r;
            popup.style.visibility = "visible";
			bulle_cacher();
        } else {
            // alert("XMLHttpRequest a retourné une erreur ! (status = " + oHttpReq.status + ")");
        }
    }
    return true;
}

function suggestionSelectionner(f, e, v) {
	document.forms[f].elements[e].value = v.replace("&#146;","'");
	suggestionFermer();
}

function zoom_fermer() {
    var eltZoom = document.getElementById("zoom");
    eltZoom.innerHTML = "";
    eltZoom.style.borderColor = "#303030";
    eltZoom.style.backgroundColor = "transparent";
    var x = parseInt(eltZoom.style.left);
    var y = parseInt(eltZoom.style.top);
    var w = eltZoom.offsetWidth;
    var h = eltZoom.offsetHeight;
    var cibleX = offsetDOM(zoomEltDepart, 'offsetLeft') - 2;
    var cibleY = offsetDOM(zoomEltDepart, 'offsetTop') - 2;
    var cibleW = zoomEltDepart.offsetWidth;
    var cibleH = zoomEltDepart.offsetHeight;
    var offsetY = ( zoomEltDepart.id.indexOf("actu_") == 0 ? 50 : 0 );
    zoom_lancer(eltZoom.id, x, y, w, h, 0, offsetY, cibleW, cibleH, 8);
}

function zoom_ouvrir(elt, table, itemId, imgFic, pageListe, itemTitre) {
    var eltZoom = document.getElementById("zoom");
    if (zooming) {
        clearTimeout(zooming);
        eltZoom.style.display = "none";
    }
    
    var x = offsetDOM(elt, 'offsetLeft') - 2;
    var y = offsetDOM(elt, 'offsetTop') - 2;
    eltZoom.style.left = x + "px";
    eltZoom.style.top = y + "px";
    var w = elt.offsetWidth;
    var h = elt.offsetHeight;
    eltZoom.style.width = w + "px";
    eltZoom.style.height = h + "px";
    eltZoom.style.borderColor = "#303030";
    eltZoom.style.backgroundColor = "transparent";
    eltZoom.style.display = "block";
    zoomEltDepart = elt;
    zoomTable = table;
    zoomItemId = itemId;
    zoomImgFic = imgFic;
    zoomPageListe = pageListe;
    zoomItemTitre = itemTitre;
    zoom_lancer(eltZoom.id, x, y, w, h, 0, 0, 280, 208, 4);
}

function zoom_ouvrirActu(elt, table, itemId, imgFic, pageListe, itemTitre) {
    var eltZoom = document.getElementById("zoom");
    var ie = document.all;
    if (zooming) {
        clearTimeout(zooming);
        eltZoom.style.display = "none";
    }
    
    var x = offsetDOM(elt, 'offsetLeft') - 2;
    if (ie) { x -= 50; }
    var y = offsetDOM(elt, 'offsetTop') - 2;
    eltZoom.style.left = x + "px";
    eltZoom.style.top = y + "px";
    var w = elt.offsetWidth;
    var h = elt.offsetHeight;
    eltZoom.style.width = w + "px";
    eltZoom.style.height = h + "px";
    eltZoom.style.borderColor = "#303030";
    eltZoom.style.backgroundColor = "transparent";
    eltZoom.style.display = "block";
    zoomEltDepart = elt;
    zoomTable = table;
    zoomItemId = itemId;
    zoomImgFic = imgFic;
    zoomPageListe = pageListe;
    zoomItemTitre = itemTitre;
    zoom_lancer(eltZoom.id, x, y, w, h, 0, -50, 280, 208, 4);
}

function zoom_afficherPhoto(table, itemId, imgFic, pageListe, itemTitre) {
    var imgSrc = cheminRacineVo + "documents/" + table + "/" + itemId + "/" + imgFic;
    if (pageListe == "direct") {
        //var html = "<a href=\"" + cheminRacine + "fiche-bien.php?t=" + table + "&id=" + itemId + "&direct=1\">";
    	//var html = "<a href=\"" + cheminRacine + "annonce/" + itemTitre + "/t," + table + "/id," + itemId + "/direct,1\">";
        var html = "<a href=\"" + cheminRacine + itemTitre + ",direct,1.htm\">";
    } else {
        //var html = "<a href=\"" + cheminRacine + "fiche-bien.php?t=" + table + "&id=" + itemId + "&p=" + pageListe + "\">";
    	//var html = "<a href=\"" + cheminRacine + "annonce/" + itemTitre + "/t," + table + "/id," + itemId + "/p," + pageListe + "\">";
        var html = "<a href=\"" + cheminRacine + itemTitre + ",p," + pageListe + ".htm\">";
    }
    html += '<img id="zoom_img" src="" onmouseout="' + "zoom_fermer();" + '" width="280" height="208" title="Cliquez pour voir la fiche détaillée" alt="Zoom non disponible" />';
    html += "</a>";
    var eltZoom = document.getElementById("zoom");
    eltZoom.innerHTML = html;
    MM_swapImage("zoom_img",'',imgSrc,1);
    // eltZoom.style.borderColor = "#ededed";
    eltZoom.style.backgroundColor = "#303030";
}

function zoom_lancer(eltZoomId, x, y, w, h, offsetX, offsetY, cibleW, cibleH, pas) {
    if (cibleW > w) {
        var deltaX = (2 * offsetX * pas) / (cibleW - w);
        var deltaY = (2 * offsetY * pas * cibleH / cibleW) / (cibleH - h);
        zoom_zoomer(eltZoomId, x, y, w, h, deltaX, deltaY, cibleW, cibleH, pas);
    } else if (w > cibleW) {
        var deltaX = (2 * offsetX * pas) / (w - cibleW);
        var deltaY = (2 * offsetY * pas * cibleH / cibleW) / (h - cibleH);
        zoom_dezoomer(eltZoomId, x, y, w, h, deltaX, deltaY, cibleW, cibleH, pas);
    }
}

function zoom_dezoomer(eltZoomId, x, y, w, h, deltaX, deltaY, cibleW, cibleH, pasX) {
    var eltZoom = document.getElementById(eltZoomId);
    var pasY = pasX * cibleH / cibleW;
    if (w > cibleW && h > cibleH) {
        x = x + pasX + deltaX;
        y = y + pasY + deltaY;
        w = w - 2 * pasX;
        h = h - 2 * pasY;
        eltZoom.style.left = parseInt(x) + "px";
        eltZoom.style.top = parseInt(y) + "px";
        eltZoom.style.width = parseInt(w) + "px";
        eltZoom.style.height = parseInt(h) + "px";
        var relance = "zoom_dezoomer('" + eltZoomId + "'," + x + "," +  y + "," +  w + "," +  h + "," +  deltaX + "," +  deltaY + "," +  cibleW + "," +  cibleH + "," +  pasX + ")";
        zooming = setTimeout(relance, 5);
    } else {
        eltZoom.style.display = "none";
    }
}

function zoom_zoomer(eltZoomId, x, y, w, h, deltaX, deltaY, cibleW, cibleH, pasX) {
    var eltZoom = document.getElementById(eltZoomId);
    var pasY = pasX * cibleH / cibleW;
    if (w < cibleW && h < cibleH) {
        x = x - pasX + deltaX;
        y = y - pasY + deltaY;
        w = w + 2 * pasX;
        h = h + 2 * pasY;
        eltZoom.style.left = parseInt(x) + "px";
        eltZoom.style.top = parseInt(y) + "px";
        eltZoom.style.width = parseInt(w) + "px";
        eltZoom.style.height = parseInt(h) + "px";
        var relance = "zoom_zoomer('" + eltZoomId + "'," + x + "," +  y + "," +  w + "," +  h + "," +  deltaX + "," +  deltaY + "," +  cibleW + "," +  cibleH + "," +  pasX + ")";
        zooming = setTimeout(relance, 5);
    } else {
        if (eltZoom.id == "zoom") {
            zoom_afficherPhoto(zoomTable, zoomItemId, zoomImgFic, zoomPageListe, zoomItemTitre);
        } else {
            diapos_afficherDiaporama(zoomTable, zoomItemId);
        }
    }
}

var zoomEltDepart;
var zoomTable;
var zoomItemId;
var zoomImgFic;
var zoomPageListe;
var zoomItemTitre;
var zooming = false;
var diaposImgSrc = new Array();     // noms des fichiers images du diaporama
var diaposIdx = 0                   // index dans diaposImgSrc de la photo affichée
var toAa = null;
var aaTempo = 1000 * 12;   // emiq 17/06/2010 : tps doublé (12 sec ald 6)

function aaChange() {
	var $actus = $('.aa');
	var n = $actus.length;
	if (n == 1) {
		$actus.eq(0).show();
	} else if (n > 1) {
		var $actuOn = $actus.filter(':visible');
		var $actuNext;
		if ($actuOn.length) {
			$actuNext = $actuOn.next('.aa');
			if (!$actuNext.length) {
				$actuNext = $actus.eq(0);
			}
			$actuOn.hide();
		} else {
			var i = Math.floor(Math.random() * n);
			$actuNext = $actus.eq(i);
		}
		$actuNext.fadeIn("fast");
		toAa = setTimeout(aaChange, aaTempo);
	}
}

function aaPause() {
	clearTimeout(toAa);
}

function mpa_simuCreditMontantPret() {
	var $f = $('form[name=meForm]');
	var $r = $f.find('input[name=s1resultat]').val('?');
	var m = parseInt($f.find('input[name=s1mensualite]').val());
	var d = parseInt($f.find('input[name=s1duree]').val()) * 12;
	var i = parseFloat($f.find('input[name=s1taux]').val().replace(',', '.')) / 1200;
	if (m > 0 && d > 0 && i > 0) {
		var r = Math.round(m * (1 - Math.pow(1 + i, -d)) / i);
		$r.val(r);
	}
	return false;
}

function mpa_simuCreditMensualite() {
	var $f = $('form[name=meForm]');
	var $r = $f.find('input[name=s2resultat]').val('?');
	var p = parseInt($f.find('input[name=s2pret]').val());
	var d = parseInt($f.find('input[name=s2duree]').val()) * 12;
	var i = parseFloat($f.find('input[name=s2taux]').val().replace(',', '.')) / 1200;
	if (p > 0 && d > 0 && i > 0) {
		var m = Math.round(p * i / (1 - Math.pow(1 + i, -d)));
		$r.val(m);
	}
	return false;
}

function mpa_simuCreditDuree() {
	var $f = $('form[name=meForm]');
	var $r = $f.find('input[name=s3resultat]').val('?');
	var $r2 = $f.find('input[name=s3resultat2]').val('?');
	var p = parseInt($f.find('input[name=s3pret]').val());
	var m = parseInt($f.find('input[name=s3mensualite]').val());
	var i = parseFloat($f.find('input[name=s3taux]').val().replace(',', '.')) / 1200;
	if (p > 0 && m > 0 && i > 0) {
		var d = -1 * Math.log(1 - (p * i / m)) / Math.log(1 + i);
		$r.val(parseInt(d));
		var a = d/12;
		$r2.val(a.bao_fmtMontant());
	}
	return false;
}

var g_toMenu = null;
$(document).ready(function() {
	//~ alert('ok ' + $('.aa').length);
	if ($('.aa').length > 1) {
		$('.aas').hover(aaPause, function() { setTimeout(aaChange, aaTempo); });
	}
	aaChange();
	
	// menu
	$('#menu li ul').each(function(i, e) {
		$(e).parents('li:first')
			.bind('mouseenter', function() {
				g_toMenu = setTimeout(function() {$(e).slideDown('fast');}, 100);
				$(this).find('span:first').addClass('hover');
				//$('#msgDefilant').hide();
			})
			.bind('mouseleave', function() {
				clearTimeout(g_toMenu);
				$(this).find('ul').slideUp('fast');
				$(this).find('span:first').removeClass('hover');
				//$('#msgDefilant').show();
			});
	});
	$('#menu > li').bind('mouseover', function() {
		$(this).siblings('.on').removeClass('on').addClass('onOff');
	})
	.bind('mouseout', function() {
		$(this).siblings('.onOff').removeClass('onOff').addClass('on');
	});
	
	$('h1').each(function(i, e) {
		if ($(e).attr('className') == '' && !$(e).parents(':first').hasClass('h1mpa2')) {
			$(e).wrap('<div class="h1mpa2"></div>');
		}
	});
});
