/***[POPUP]***/
var popup={};
function popup_is_open() {
  return popup.win.style.display=='block';
}
function popup_open(header, content, footer) {
  var ie = (document.all) ? 1 : 0;
  var p = (ie) ? "filter" : "MozOpacity";

  /* n is the element node
     v is the opacity value, from 0 to 100. */
  function op(n,v){
    v = (ie) ? "alpha(opacity="+v+")" : v/100;
    n.style[p] = v;
  }

  popup.content=document.getElementById('popup_content');
  popup.footer=document.getElementById('popup_footer');
  popup.bg=document.getElementById('popup_bg');
  popup.div=document.getElementById('popup_div');
  popup.win=document.getElementById('popup');
  popup.header = document.getElementById('popup_header');

  function fade(opacity, max) {
    op(popup.bg, opacity);//popup.bg.style.opacity = opacity;
    if(opacity < max)
      setTimeout(function() { fade(opacity+5, max) }, 30);
  }
  fade(0, 60);

  if(popup.win.scrollIntoView)
    popup.win.scrollIntoView();
  else
    popup.win.style.top = window.scrollY + 'px';
  //  window.scroll(popup.win.offsetLeft, popup.win.offsetTop);

  popup.bg.style.display = popup.win.style.display='block';
  popup.win.onclick = function() { popup_close(true); };
  document.onkeypress = function(ev) { if(popup_is_open() && ev.keyCode == 27) popup_close(true); };
  popup.div.onclick = function(ev) { ev.stopPropagation(); };
    //FIXME: close bg on click
  popup.header.innerHTML = header;
  popup.content.innerHTML = content;
  popup.footer.innerHTML = footer;
}

function popup_set_content(content) {
  popup.content=document.getElementById('popup_content');
  popup.content.innerHTML = content;
}

function popup_open_iframe(title, url) {
  header='<a style="padding-right: 5px; padding-top: 5px; font-family: Arial; text-decoration: none; font-weight: 700;" href="javascript:popup_close()"><img src="/images/delete24.png"></a>';
  content='<iframe id="popup_iframe" style="width: 100%; height: 400px" src="'+url+'"></iframe>';
  popup_open(header, content, '');
  iframe = document.getElementById('popup_iframe');
  iframe.contentDocument.body.onquit = function() { alert('XX'); };
}

function popup_open_url(title, url, args) {
  function cbk(status, content) {
    if(status == 200)
      popup_set_content(content);
    else
      alert('popup_open_url.cbk, status: ' . status);
  }

  header = title + '<a style="float: right; padding: 5px; font-family: Arial; text-decoration: none; font-weight: 700;" href="javascript:popup_close()"><img src="/images/delete24.png"></a>';

  popup_open(header, '<h1>Please wait...</h1>', '');

  get(url, args, cbk);
}

function popup_href(href, args) {
  function cbk(status, body) {
      header='<a style="padding-right: 5px; padding-top: 5px; font-family: Arial; text-decoration: none; font-weight: 700;" href="javascript:popup_close()">[X]</a>';
      popup_open(header, body, '');
  }
  get(href, args, cbk);
}

function popup_close(confirm_before_closing) {
  if(!confirm_before_closing || confirm('Fermer le popup ?')) {
    popup.bg.style.display = popup.win.style.display = 'none';
    popup.header.innerHTML = '';
    popup.content.innerHTML = '';
    popup.footer.innerHTML = '';
  }
}

