var timer1, timer2, timer3;
var bgmenu = new Array("#381B14", "#583229", "#70514A", "#977870", "transparent", "#000000");
var nu_bg = 0;
var taille_min = 0.8;
//if (navigator.userAgent.match(/Safari/) && navigator.userAgent.match(/Windows/) == 0) { taille_min = 0.9; }



/*********************************/
// Accesskey
var inp_focus = 0;
window.document.onkeyup = acces;
function acces(ev) {
    // empecher ACCESSKEY si texte selectionne 
    if (document.getSelection) var sel = document.getSelection();
    else if (document.selection) var sel = document.selection.createRange().text;
    if (sel != "") return;
    
    
    // empecher ACCESSKEY si champ selectionne 
    var element;
    if (ev.target) element=ev.target;
    else if (ev.srcElement) element=ev.srcElement;
    if (element.nodeType==3) element=element.parentNode;

    if (element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;
    
    var touche = document.all?ev.keyCode:ev.which;
    //alert(touche);
    if (touche == 90) zoom();
    if (touche == 97 || touche == 49) window.location.href="/";
    if (touche == 76) window.location.href="/art-lieu.html";
    if (touche == 65) window.location.href="/art-exposition.html";
    if (touche == 68) window.location.href="/art-dossier.html";
    if (touche == 67) window.location.href="/art-article.html";
    if (touche == 105 || touche == 57) window.location.href="/art-contact.html";
    if (touche == 99 || touche == 51) window.location.href="/art-plan.html";
    if (touche == 96 || touche == 48 || touche == 191) HELP_volante();
    if (touche == 100 || touche == 52) document.getElementById("q").focus();
    if (touche == 101 || touche == 53) window.location.href="/trouvemoi.php";
    if (touche == 98 || touche == 50) window.scrollTo(0, jsmove("col_gauche", "top"));
    if (touche == 102 || touche == 54) window.scrollTo(0, 0);
    
    
}


/*********************************/
// HELP volante
function HELP_volante() {
    clearTimeout(timer_vol);
    var elm = document.getElementById('volante');
    
    if (elm.style.display != 'block') {
    
        elm.style.display = 'block';
        elm.style.width = 'auto';
        elm.innerHTML = '<h2>Touches d\'accès</h2><b>0</b> / <b>?</b> Aide<br /> <b>1</b> Page d\'accueil<br /> <b>2</b> Aller au contenu<br /> <b>3</b> Plan du site<br /> <b>4</b> Recherche<br /> <b>5</b> Recherche avanc&eacute;e<br /> <b>6</b> Aller au menu<br /> <b>9</b> Page contact et informations<br /> <b>L</b> Répertoire des lieux d\'art<br /> <b>A</b> Agenda des expositions et événements d\'art<br /> <b>D</b> Dossiers de l\'art<br /> <b>C</b> Communiqués<br /><b>&gt;</b> Page suivante<br /> <b>&lt;</b> Page précédente <br /> <b>Z</b> Zoom sur le texte';
        var dimp = getPageSize();
        elm.style.left = (dimp[2] / 2 - jsmove("volante", "w") / 2) + "px";
        elm.style.top = ( dimp[3] / 2 - jsmove("volante", "h") / 2 + dimp[5]) + "px";
    
    } else {
        init_ferme_volante();
    }
    
}



function PlayList(Url) {
    var hideoutPop = window.open("/play.swf?file="+Url+"&playlist=bottom&playlistsize=180&autostart=true&logo=/img/player.gif&repeat=list&displayclick=next", 'player', 'menubar=no, status=no, scrollbars=no, resizable=no ,width=460,height=410');
    hideoutPop.focus();
}


/*********************************/
// MENU principal HiLi
function hl(nu) {
    document.getElementById('menu' + nu).className = 'menuAct';
    for (i=0; i<4; i++) setTimeout("change_bg("+nu+", "+i+")", (10*i));
}
function bl(nu) {
    document.getElementById('menu' + nu).className = 'menu';
    for (i=7; i>-1; i--) setTimeout("change_bg("+nu+", "+i+")", 10*(i-1));
}

function change_bg(nu, bg) {
    if( !bg ) bg = 4;
    document.getElementById('menu' + nu).style.background = bgmenu[bg];
}

function hl2(nu) {
    document.getElementById('menu' + nu).className = 'menuAct';
    change_bg(nu, 5);
    //setTimeout("change_bg("+nu+", 5)", 151);
}
function bl2(nu) {
    document.getElementById('menu' + nu).className = 'menu';
    change_bg(nu, 4);
    //setTimeout("change_bg("+nu+", 4)", 150);
}



/*********************************/
// Sous menu
function smenu(menu) {
    clearTimeout(timer1);
    var aff = "";
    //if (menu == "agenda") aff = 'Aujourd\'hui &nbsp; | &nbsp; Cette semaine &nbsp; | &nbsp; Ce mois';
    if (menu == "accueil") aff = '<a href="art-presentation.html">Présentation</a><br /><a href="art-artist.html">Liste d\'artistes</a><br /><a href="art-plan.html">Plan du site</a><br /><a href="art-contact.html">Contact</a><br /><a href="art-partenaire.html">Partenaires</a>';
    if (aff != "") {
		var elm = document.getElementById("smenu");
        elm.style.background = "#000";
        elm.style.height = "auto";
        elm.style.borderBottom = "1px solid #000";
        elm.style.padding = "7px 7px 17px";
        elm.style.lineHeight = "16px";
        elm.style.fontSize = "14px";
        elm.innerHTML = aff;
    } else {
        ferme_menu();
    }
}
function ferme_menu() {
	var elm = document.getElementById("smenu");
    elm.style.background = "transparent";
    elm.style.height = "0px";
    elm.style.borderBottom = "none";
    elm.style.padding = "0";
    elm.style.lineHeight = "0";
    elm.style.fontSize = "0";
    elm.innerHTML = "";
}
function menu_cancel() {
    clearTimeout(timer1);
}


/*********************************/
// Open / Close
function open_close(elm) {
    var obj = document.getElementById(elm);
    if (obj.style.display == "none" || obj.style.display == "") {
        obj.style.display = "block";
        obj.style.opacity = "0.1";
        open_close_fade(elm);
    } else {
        obj.style.display = "none";
    }
}
function open_close_fade(elm) {
    var obj = document.getElementById(elm);
    var opak = parseFloat(obj.style.opacity);
    if (opak < 1) {
        opak = opak + 0.1;
        obj.style.opacity = opak;
        window.scrollBy(0,opak * 50);
        setTimeout("open_close_fade('"+elm+"')", 70);
    }
}


/*********************************/
// COOKIES
function ecrire_cookie(nom, valeur, expires) {
    document.cookie=nom+"="+escape(valeur)+
    ((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
    ';path=/';
}

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

function lire_cookie(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 arguments_cookie(j);
        i=document.cookie.indexOf(" ",i)+1;
        if (i==0) break;
    }
    return null; 
}

var taille_zoom = lire_cookie("taille");
if (taille_zoom == null) taille_zoom = taille_min;


function zoom() {
    taille_zoom = parseFloat(taille_zoom) + 0.2;
    if (taille_zoom > taille_min+0.4) taille_zoom = taille_min;
    document.body.style.fontSize = taille_zoom + "em";
    ecrire_cookie("taille", taille_zoom);
}

function boot() {
    if ( !navigator.userAgent.match(/iPhone/) ) document.body.style.fontSize = taille_zoom + "em";
}

function parse() {
    if (document.getElementById('col_gauche')) {
        var page = document.getElementById('col_gauche').innerHTML;
        //page = page.replace(/([a-zA-Z\.0-9\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z]+)/g, "<a href=\"mailto:$1\">$1</a>");
        page = page.replace(/%p%/, ".");
        page = page.replace(/(%m%)(.*)(%fm%)/, "<a href=\"mailto:$2\">$2</a>");
        document.getElementById('col_gauche').innerHTML = page;
    }
}

// PROXIMITE

function distance() {
    var dist = document.getElementById('distance').value;
    ecrire_cookie("distance", dist);
    document.location.href="art-proximite.html";
}



/* ######################################### */
// Vignette volante
var timer_vol, even;

function volante(e, id) {
    even = e;
    check(id);
    clearTimeout(timer_vol);
    var elm2 = document.getElementById('volante2');
    var elm = document.getElementById('volante');
    
    elm.style.display = 'block';
    elm.style.width = 'auto';
    elm2.style.display = 'block';
    elm2.style.left = (jsmove(e, "x") + 10) + "px";
    elm2.style.top = (jsmove(e, "y") - 20 ) + "px";
    
    document.getElementById('volante').style.left = (jsmove(e, "x")+20) + "px";
    document.getElementById('volante2').style.left = (jsmove(e, "x")+10) + "px";
    
    if (e.clientY < 150 && e.clientY > 0) {
        document.getElementById('volante').style.top = (jsmove(e, "y") + 25 ) + "px";
        document.getElementById('volante2').style.top = (jsmove(e, "y") + 20 ) + "px";
    } else {
        document.getElementById('volante').style.top = (jsmove(e, "y") - 60 ) + "px";
        document.getElementById('volante2').style.top = (jsmove(e, "y") - 20 ) + "px";
    }
}



function init_ferme_volante() {
    timer_vol = setTimeout("ferme_volante()", 100);
}
function ferme_volante() {
    var elm = document.getElementById('volante');
    elm.style.display = 'none';
    elm.innerHTML = '';
    var elm2 = document.getElementById('volante2');
    elm2.style.display = 'none';
    window.document.onmousemove = '';
}

window.document.onmousedown = ferme_volante;


// DOSSIERS
var haut_max, timer_glisse, action_glisse, haut_fenetre = "";

function deplie(blid) {
    var elm = document.getElementById(blid);
    if (parseInt(elm.style.height) == 0 || !elm.style.height) {
        elm.style.height = "auto";
        elm.style.lineHeight = "120%";
        init_glisse(blid, 'ouvre');
    } else if (action_glisse == "") {
        init_glisse(blid, "ferme");
    }
}

function init_glisse(blid, sens) {
    var elm = document.getElementById(blid);
    
    haut_max = jsmove(blid, "h");
    
    if (sens == "ouvre") elm.style.height = "0px";
    if (document.body) {
        haut_fenetre = (document.body.clientHeight);
    } else {
        haut_fenetre = (window.innerHeight);
    }
    clearTimeout(timer_glisse);
    timer_glisse = setTimeout("glisse('" + blid + "', '" + sens + "')", 10);
}

function glisse(blid, sens) {
    action_glisse = 1;
    var elm = document.getElementById(blid);
    var haut = parseInt(elm.style.height);
    if (sens == "ouvre") { 
        haut = haut + 50;
        if (haut > haut_max) haut = haut_max;
    } else {
        haut = haut - 70;
        if (haut < 0) haut = 0;
    }
    elm.style.height = haut + "px";
    
    if (sens == "ouvre") {
        if (haut < haut_max) {
            timer_glisse = setTimeout("glisse('" + blid + "', '" + sens + "')", 10);
        } else {
            action_glisse = "";
        }
    } else {
        if (haut > 0) {
            timer_glisse = setTimeout("glisse('" + blid + "', '" + sens + "')", 10);
        } else {
            action_glisse = "";
            elm.style.lineHeight = "0";
        }
    }
}


// Augmente le contrast
function fort_contrast() {
    var elm = document.getElementById("fond_page");
    if (elm.className == "") {
        elm.className = "contrast";
    } else {
        elm.className = "";
    }
}
