﻿var posX;
var posY;
var cId = 0;
var fichePosX = '10';
var fichePosY = '10';
var ficheEnq = null;
var winZindex = 100;

Event.observe(window, 'load', initLib);

function initLib() {
//  ficheEnq = new Element('div', { 'class': 'popup', href: '/foo.html', id: 'fiche_enq' }).update("Next page");
  Event.observe(document, "mousemove", showPosition, false);
  ///*
  $$('div.popup').each(
    function(elt) {
      elt.style.display = 'none';
      new Draggable(elt);
    }
  );
  //*/
  //$$('div.popup').invoke('hide').invoke('draggable');
}

function tec() {
  $('wip').style.display = 'block';
}


function showPosition(e) {
  posX = Event.pointerX(e);
  posY = Event.pointerY(e);
//  alert(posX+' - '+posY);
}


function p_update_liste(elt, start, asc, param, order_field) {
  url = param + '&start=' + start + '&asc=' + asc + '&order_field=' + order_field;
  eval(elt + '_start' + ' = ' + start);
  eval(elt + '_param' + ' = \'' + param + '\'');
  new Ajax.Updater(elt, url, {method: 'get'});
  switch(elt) {
    case 'liste_distr' : startDistr = start; break;
    case 'liste_attr'  : startAttr  = start; break;
    case 'resultats'   : startEnq   = start; break;
  }
}

function wip() {
  wipElt = '<img id="wip_id" src="style/wip.png" alt="Work in progress" ' +
            '    style="width:450px; height:450px; display:none; border:none; position:absolute; top:30%; left:30%;" ' +
//            '    onclick="Effect.Fade(\'wip_id\', {duration:0.3, onComplete:function(){$(\'wip_id\').remove();}});">';
            '    onclick="this.remove();">';
  $$('body').each(function(elt) {elt.insert(wipElt)});
  py = (posY > 500)?$posY - 500:posY - 200;
  $('wip_id').style.top = py;
  $('wip_id').style.zIndex = '99999';
  $('wip_id').style.display = 'block';
}

function tec() {
  wip();
}

function wait() {
  $('wait').style.display = 'block';
}

function attn(titre, texte, fonction, params) {
  warnElt = '<div id="warning">\n' +
            '  <h1>' + titre + '</h1>\n' +
            '  <p>' + texte + '</p>\n' +
            '  <p>\n' + 
            '    <a onclick="$(\'warning\').remove();">Annuler</a>\n' +
            '    <a onclick="$(\'warning\').remove();' +  fonction + '('+ params +');">OK</a>\n' +
            '  </p>\n' +
            '</div>\n';
  $$('body').each(function(elt) {elt.insert(warnElt)});
  $('warning').style.top = (posY - 100) + 'px';
  $('warning').style.left = (posX - 380) + 'px';
/*
$('warning').innerHTML = '<h1>' + titre + '</h1>\n<p>' + texte + '</p>\n<p>\n' +
                           '<a onclick="$(\'warning\').style.display=\'none\';">Annuler</a>\n' +
                           '<a onclick="$(\'warning\').style.display=\'none\';' + 
                           fonction + '('+ params +');">OK</a>\n</p>\n';
*/
  $('warning').grow({transition: Effect.Transitions.spring});
}

/*
function popupCreate(titre, id, w, h, t, l) {
  if ($(id) == null) {
    pid = id;
//  id = 'popup_' + id;
    frameWin = '<div class="win" id="' + id + '">' +
               '  <div class="wtl">&nbsp;</div>' +
               '  <div class="wt" id="wt_' + pid + '">' +
               '    <span id="titre_' + id + '" class="titre">' + titre + '</span>' +
               '    <span class="controles">' +
               '      <img src="../../res_jm/style/mini_close.png"' + 
               '           title="Cliquer pour fermer"  ' +
               '           alt="Cliquer pour fermer" ' +
               '           onclick="ancestors(this)[2].remove()" />' +
               '    </span>' +
               '  </div>' +
               '  <div class="wtr">&nbsp;</div><div style="clear:left"></div>' +
               '  <div class="wl" id="wl_' + pid + '"></div>' +
               '  <div class="wr" id="wr_' + pid + '">&nbsp;</div><div style="clear:left"></div>' +
               '  <div class="wbl">&nbsp;</div>' +
               '  <div class="wb" id="wb_' + pid + '">&nbsp;</div>' +
               '  <div class="wbr" id="wbr_' + pid + '">&nbsp;</div>' +
               '</div>';
//alert(frameWin);
    $$('body').each(function(elt) {elt.insert(frameWin)});
    new Draggable(pid);
    $(id).style.width = w + 'px';
    $(id).style.height = h + 'px';
    $(id).style.top = t + 'px';
    $(id).style.left = l + 'px';
    $(('wt_' + pid)).style.width = w - 48 + 'px';
    $(('wl_' + pid)).style.width = w - 32 + 'px';
    $(('wb_' + pid)).style.width = w - 48 + 'px';
    $(('wl_' + pid)).style.height = h - 48 + 'px';
    $(('wr_' + pid)).style.height = h - 48 + 'px';
    winZindex++;
    $(id).style.zIndex = winZindex + 1;
    Event.observe(
      $(id), 
      'click',  
      function(event) { 
        winZindex++; 
        $(id).style.zIndex = winZindex; 
//      alert(winZindex); 
      }
    );
/*
    Event.observe(
      $(('wbr_' + id)), 
      'mouseDown',  
      function(event) { 
        w = $(id).getWidth();
        h = $(id).getHeight();
        dx = posX - w;
        dy = posY - h;
        $(id).style.width = w + 'px';
        $(id).style.height = h + 'px';
        
        $(id).style.zIndex = winZindex; 
//      alert(winZindex); 
      }
    );
* /  
  }
} 
*/

function popupCreate(titre, id, w, h, t, l) {
  if ($(id) == null) {
    pid = id;
//  id = 'popup_' + id;
    frameWin = '<div class="win" id="' + id + '">' +
               '  <div class="wtl">&nbsp;</div>' +
               '  <div class="wt" id="wt_' + pid + '">' +
               '    <span id="titre_' + id + '" class="titre">' + titre + '</span>' +
               '    <span class="controles">' +
               '      <img src="style/mini_close.png"' + 
               '           id="close_' + pid + '"  ' +
               '           title="Cliquer pour fermer"  ' +
               '           alt="Cliquer pour fermer" ' +
               '           onclick="ancestors(this)[2].remove()" />' +
               '    </span>' +
               '  </div>' +
               '  <div class="wtr">&nbsp;</div><div style="clear:left"></div>' +
               '  <div class="wr" id="wr_' + pid + '">' + 
               '    <div class="wl" id="wl_' + pid + '"></div>' +
               '  </div>' +
               '  <div style="clear:left"></div>' +
               '  <div class="wbl">&nbsp;</div>' +
               '  <div class="wb" id="wb_' + pid + '">&nbsp;</div>' +
               '  <div class="wbr" id="wbr_' + pid + '">&nbsp;</div>' +
               '</div>';
//alert(frameWin);
    $$('body').each(function(elt) {elt.insert(frameWin)});
    new Draggable(pid);
    $(id).style.width = w + 'px';
//    $(id).style.height = h + 'px';
    $(id).style.top = t + 'px';
    $(id).style.left = l + 'px';
    $(('wt_' + pid)).style.width = w - 48 + 'px';
    $(('wl_' + pid)).style.width = w - 36 + 'px';
    $(('wr_' + pid)).style.width = w + 'px';
    $(('wb_' + pid)).style.width = w - 48 + 'px';
//    $(('wl_' + pid)).style.height = h - 48 + 'px';
//    $(('wr_' + pid)).style.height = h - 48 + 'px';
    winZindex++;
    $(id).style.zIndex = winZindex + 1;
    Event.observe(
      $(id), 
      'click',  
      function(event) { 
        winZindex++; 
        if ($(id)) $(id).style.zIndex = winZindex; 
//      alert(winZindex); 
      }
    );
  }
} 


function miniCal(calId, champ, dt) {
  if ($(calId) != null) $(calId).remove();
  calWin = '<div class=" cal" id="'+ calId + '">' +
           '</div>';

  $$('body').each(function(elt) {elt.insert(calWin)});
  new Draggable(calId);
  url = '../res_jm/miniCalEmb.php' +
        '?cal_id=' + calId + 
        '&champ=' + champ + 
        '&ag_date=' + dt;
  $(calId).style.top = (posY - 15) + 'px';
  $(calId).style.left = (posX - 150) + 'px';

  $(calId).style.zIndex = 9999;
  new Ajax.Updater(
    calId, 
    url, {
      method:'get',
      onComplete: function() {
        $(calId).show();
      }
    }
  );
}

function miniCalSetDate(calId, champ, dt) {
  url = '../res_jm/miniCalEmb.php' +
        '?cal_id=' + calId + 
        '&champ=' + champ + 
        '&ag_date=' + dt;
  new Ajax.Updater(calId, url, {method:'get'});
}

function miniCalRetour(calId, champ, dt) {
  fld = champ;
  $(fld).value = dt;
  miniCalClose(calId);
}

function miniCalClose(calId) {
  $(calId).remove();
}
