/*
  DIV POPUP 1.0
*/

function divpopup()
{
  this.id = null;
  
  this.covername = 'divpopupcover';
  
  this.size = null;
  
  this.href = null;
  
  this.cache = null;
  
  this.interval = null;
  
  this.center = true;
  
  this.onclick = function()
  {
    this.action.open();
    return false;
  };
  
  this.setopacity = function(obj, value)
  {
    obj.style.opacity = value/10;
    obj.style.filter = 'alpha(opacity=' + value*10 + ')';
  };
  
  this.clientsize = function()
  {
    var xScroll, yScroll;
    var windowWidth, windowHeight;
    var pageWidth, pageHeight;
    
    if (window.innerHeight && window.scrollMaxY)
    {	
      xScroll = document.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight)
    { // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    }
    else
    { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }
    if (self.innerHeight)
    {	// all except Explorer
      windowWidth = self.innerWidth;
      windowHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body)
    { // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    }	
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight)
      pageHeight = windowHeight;
    else 
      pageHeight = yScroll;
    
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth)
      pageWidth = windowWidth;
    else
      pageWidth = xScroll;
      
    return {w: pageWidth, h: pageHeight};
  };
  
  this.topPosition = function()
  {
    var yScroll;
    
    if (self.pageYOffset)
      yScroll = self.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)	 // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
    else if (document.body) // all other Explorers
      yScroll = document.body.scrollTop;
    
    return yScroll;
  };
  
  this.resize = function()
  {
    var o = document.getElementById(this.covername);
    var l = document.getElementById('h'+this.covername);
    var pop = this.win('div'+this.id);
    pop.style.display = 'none';
    var s = this.clientsize();
    if (o)
    {
      o.style.width = s.w+'px';
      o.style.height = s.h+'px';
      //l.style.top = (this.center) ? Math.round(s.h/2)+'px' : l.offsetTop+'px';
      pop.style.left = Math.round((s.w-this.size.w)/2)+'px';
      pop.style.display = '';
    }
  };
  
  this.cover = function()
  {
    var s = this.clientsize();
    var o;
    this.center = (s.h > this.size.h);
    
    if (!document.getElementById(this.covername))
    {
      var cover = document.createElement('div');
      cover.id = this.covername;
      document.body.appendChild(cover);
      var line = document.createElement('div');
      var style = 'position:absolute;top:0;left:0;width:100%;height:1px;background:none;';
      line.id = 'h'+this.covername;
      if (document.all) line.style.setAttribute("cssText", style);
      else line.setAttribute("style", style);
      document.body.appendChild(line);
      this.setopacity(document.getElementById(this.covername), 9);
    }
    o = document.getElementById(this.covername);
    o.style.position = 'absolute';
    o.style.top = '0px';
    o.style.left = '0px';
    o.style.margin = '0px';
    o.style.padding = '0px';
    o.style.width = s.w+'px';
    o.style.height = s.h+'px';
    o.onclick = this.close;
  };
  
  this.win = function(id)
  {
    if (!document.getElementById(id))
    {
      var win = document.createElement('div');
      win.id = id;
      win.className = 'divpopupwin';
      document.getElementById('h'+this.covername).appendChild(win);
    }
    var o = document.getElementById(id);
    return o;
  };
  
  this.openeffect = function()
  {
    this.cache.w += Math.round((this.size.w-this.cache.w)/1.5);
    this.cache.h += Math.round((this.size.h-this.cache.h)/1.5);
    
    var pop = this.win('div'+this.id);
    var opacity = Math.round(this.cache.w * 10 / this.size.w);
    var o = document.getElementById(this.covername);
    var s = this.clientsize();
    this.setopacity(pop, opacity);
    if (this.size.w-this.cache.w < 2 || this.size.h-this.cache.h < 2)
    {
      clearInterval(this.interval);
      this.cache.w = this.size.w;
      this.cache.h = this.size.h;
      this.setopacity(pop, 10);
      pop.innerHTML = this.content(this.href);
      
      var a = document.createElement('a');
      a.href = '?close';
      a.onclick = this.close;
      a.innerHTML = 'Close window';
      a.className = 'divpopuplink';
      document.getElementById('menu'+this.id).appendChild(a);
    }
    pop.style.position = 'relative';
    pop.style.top = this.topPosition() + 40 + 'px';//(this.center) ? -Math.round(this.cache.h/2)+'px' : '40px';
    pop.style.left = Math.round((s.w-this.cache.w)/2)+'px';
    pop.style.width = this.cache.w+'px';
    pop.style.height = this.cache.h+'px';
    pop.onclick = function(){return false};
    if (o)
    {
      o.style.width = s.w+'px';
      o.style.height = s.h+'px';
    }
  };
  
  this.closeeffect = function()
  {
    if (!document.getElementById(this.covername))
    {
      clearInterval(this.interval);
      return false;
    }
    this.cache.w -= this.size.w - Math.round(this.cache.w/1.5);
    this.cache.h -= this.size.h - Math.round(this.cache.h/1.5);
    var pop = this.win('div'+this.id);
    var opacity = Math.round(this.cache.w * 10 / this.size.w);
    var s = this.clientsize();
    this.setopacity(pop, opacity);
    pop.style.position = 'relative';
    pop.style.top = this.topPosition() + 40 + 'px';//(this.center) ? -Math.round(this.cache.h/2)+'px' : '40px';
    pop.style.left = Math.round((s.w-this.cache.w)/2)+'px';
    pop.style.width = this.cache.w+'px';
    pop.style.height = this.cache.h+'px';
    if (this.cache.w < Math.round(this.size.w/4) || this.cache.h < (Math.round(this.size.h/4)+20))
    {
      clearInterval(this.interval);
      this.cache.w = 0;
      this.cache.h = 0;
      var o = document.getElementById(this.covername);
      o.parentNode.removeChild(o);
      var l = document.getElementById('h'+this.covername);
      l.parentNode.removeChild(l);
      if (document.all) document.body.onresize = null;
      else window.onresize = null;
    }
  };
  
  this.content = function(file)
  {
    var ext = file.substr(file.lastIndexOf('.')+1);
    var menu = '<div id="menu'+this.id+'" style="width:'+this.size.w+'px;height:20px;display:block;"></div>';
    if (ext == 'jpg' || ext == 'gif' || ext == 'png')
      return menu+'<img src="'+file+'" />';
    else
      return menu+"<p>Type de fichier incorrect.</p>";
  };
  
  this.open = function()
  {
    clearInterval(this.interval);
    this.cover();
    this.cache = {w: 0, h: 0};
    if (document.all) document.body.onresize = function(){self.resize()};
    else window.onresize = function(){self.resize()};
    this.interval = setInterval(function(){self.openeffect()}, 70);
  };
  
  this.close = function()
  {
    var o = document.getElementById('menu'+self.id);
    if (o)
    {
      clearInterval(self.interval);
      o.parentNode.removeChild(o);
      self.win('div'+self.id).innerHTML = '';
      self.interval = setInterval(function(){self.closeeffect()}, 70);
    }
    return false;
  };
  
  var self = this;
}

var atags = document.getElementsByTagName('a');
for (var i=0; i<atags.length; i++)
{
  if (atags[i].title)
  {
    var siz = atags[i].title.split('x');
    var uid = (atags[i].id) ? atags[i].id : 'popid_'+i;
    var w = parseFloat(siz[0])+2;
    var h = parseFloat(siz[1])+22;
    var pop = {w: w, h: h};
    atags[i].action = new divpopup();
    atags[i].action.id = uid;
    atags[i].action.size = pop;
    atags[i].action.href = atags[i].href;
    atags[i].onclick = atags[i].action.onclick;
  }
}
