// $Id: demo.js,v 1.2 2007-08-21 01:06:23 patrick Exp $

function demo_window_height()
{
 var a=0;
 if ('undefined' != typeof document.documentElement && document.documentElement.clientHeight > document.body.scrollHeight) 
 {
  a=parseInt(document.documentElement.clientHeight,10);
 }

 a=parseInt(document.body.scrollHeight,10);
 var b=parseInt(document.getElementById('body').scrollHeight,10)+25; // hack
 return (b > a)? b : a;
}

function demo_window_width()
{
 return parseInt(document.body.scrollWidth,10);
}

function demo_window_cover_full()
{
 if (document.getElementById('demo_layer_hide_full'))
 {
  document.body.removeChild(document.getElementById('demo_layer_hide_full'));
 }
 var div=document.createElement('div');
 div.id='demo_layer_hide_full';
 div.className='demo_layer_cover';
 div.style.height=demo_window_height()+'px';
 document.body.appendChild(div);

 window.onresize=function () { demo_window_cover_full(); return false; }
}

function demo_window_cover_with_hole(x,y,wx,wy)
{
 if (document.getElementById('demo_layer_hide_full'))
 {
  document.body.removeChild(document.getElementById('demo_layer_hide_full'));
 }
 var d=new Array(10); // Top row = 1 + 2 + 3, Middle = 4 + nothing + 6, Bottom Row = 7 + 8 + 9
 var doappend=0;
 if (! document.getElementById('demo_layer_hide_1'))
 {
  for(var i=1;i<=9;i++)
  {
   if (i==5) continue;
   d[i]=document.createElement('div');
   d[i].id='demo_layer_hide_'+i;
   d[i].className='demo_layer_cover';
  }
  doappend=1;
 } else
 {
  for(var i=1;i<=9;i++)
  {
   d[i]=document.getElementById('demo_layer_hide_'+i);
  }
 }

 var tx=demo_window_width();
 var ty=demo_window_height();

 d[1].style.height=d[2].style.height=d[3].style.height=y+'px';
 d[4].style.height=                  d[6].style.height=wy+'px';
 d[7].style.height=d[8].style.height=d[9].style.height=(ty-wy-y)+'px';

 d[1].style.width=d[4].style.width=d[7].style.width=x+'px';
 d[2].style.width=                 d[8].style.width=wx+'px';
 d[3].style.width=d[6].style.width=d[9].style.width=(tx-wx-x)+'px';
 
 d[2].style.left=d[8].style.left=x+'px';
 d[3].style.left=d[6].style.left=d[9].style.left=(x+wx)+'px';

 d[4].style.top=d[6].style.top=y+'px';
 d[7].style.top=d[8].style.top=d[9].style.top=(y+wy)+'px';

 var bs=5;
 d[4].style.width=(x-bs)+'px';
 d[6].style.width=(tx-wx-x-bs)+'px';
 d[2].style.height=(y-bs)+'px';
 d[8].style.height=(ty-wy-y-bs)+'px';
 d[2].style.borderBottom=d[8].style.borderTop=d[4].style.borderRight=d[6].style.borderLeft='solid '+bs+'px #f00';
 
 if (doappend)
 {
  for(var i=1;i<=9;i++)
  {
   if (! d[i]) continue;
   document.body.appendChild(d[i]);
  }
 }

 window.onresize=function () { demo_window_cover_with_hole(x,y,wx,wy); return false; }
// onscroll
}

function demo_window_cover_with_hole_around(id)
{
 var d=document.getElementById(id);
 if (! d) return;
 var p=object_position(d);
 var lx=d.offsetWidth;
 var ly=d.offsetHeight;
 demo_window_cover_with_hole(p[0]-10,p[1]-10,lx+20,ly+20);
 
 // Move body & demo_control up to selected widget
 var control=document.getElementById('demo_control');
 var h=control.offsetHeight; // hauteur de la boite
 var l=control.offsetWidth; // la largeur
 var newy=p[1]-h-30;
 
 if (newy > 0)
 {
  document.documentElement.scrollTop=newy;
  control.style.top=newy+'px';
 }
 if (parseInt(document.documentElement.clientWidth,10) < p[0]+lx)
 {
  document.documentElement.scrollLeft=p[0];
 }
 var cw=parseInt(document.documentElement.clientWidth,10);
 p=object_position(d);
 lx=d.offsetWidth;
 if (p[0]+lx+l+30 < cw)
 {
 // control.style.left=(p[0]+lx-30)+'px';
  //control.style.right=(cw-p[0]-lx-l)+'px';
 } else
 {
 // control.style.left=(cw-l-30)+'px';
 //  control.style.right='30px';
 }
 
 window.onresize=function () { demo_window_cover_with_hole_around(id); return false; }
}

function demo_mouse_move(ev)
{
 ev = ev || window.event;
 var d=document.getElementById('demo_control');
 if (d.demo_mouse_offset[0] == null && d.demo_mouse_offset[1] == null) return false;
 var mpos=mouse_coords(ev);
 d.style.top= (mpos[1]-d.demo_mouse_offset[1])+'px';
 d.style.left=(mpos[0]-d.demo_mouse_offset[0])+'px';
 return false;
}

function demo_setup_control(prefix)
{
 var d=document.getElementById('demo_control');

 var c=d.firstChild;
 var sp=0;
 while(c)
 {
  if (c.nodeType == Node.ELEMENT_NODE ) 
  {
   c.style.display='none';
   sp++;
   var a=c.getElementsByTagName('a');
   for(var i=0;i<a.length;i++)
   {
    if (a[i].className == 'demo_control_next_subpart')
    {
     a[i].onclick=myonclick(sp);
    }
   }
  }
  c=c.nextSibling;
 }

 d.onmouseup=function () { d.demo_mouse_offset=[null,null]; document.body.style.cursor='auto'; }
 d.onmouseup();
 document.onmousemove=demo_mouse_move;
 d.onmousedown=function (ev) { this.demo_mouse_offset=mouse_offset(this,ev); document.body.style.cursor='move'; return false; }


 function myonclick(sp) { return function () { demo_hide_control_subpart(sp); demo_show_control_subpart(sp+1); eval(prefix+(sp+1)+'()'); return false; }; }
}

function demo_show_control_subpart(num)
{
 document.getElementById('demo_control_subpart'+num).style.display='';
}

function demo_hide_control_subpart(num)
{
 document.getElementById('demo_control_subpart'+num).style.display='none';
}

function demo_form_inject_hidden(formid,name,value)
{
 var d=document.getElementById(formid);
 if (!d) return;
 var h=document.createElement('input');
 h.type='hidden';
 h.name=name;
 h.value=value;
 d.appendChild(h);
}

function demo_form_input_set(formid,name,value,lock)
{
 var d=document.getElementById(formid);
 if (!d) return;
 var i=d.elements[name];
 if (!i) return;
 i.value=value;
 if (lock) i.onfocus=function () { this.blur(); return false; }
}

