/*



    === Multi-level Accordians === April 2009
    
    Author: michael@0mg.ch
    
    Thanks to G.S.Navin Raj Kumar (http://dezinerfolio.com)
    for the Simple Accordians version which has been modified
    to obtain this multi-level one.
    
    HOW TO USE IT:
    """"""""""""
    add the "Accordian" function to the "onload" body parameter
    <body onLoad="new Accordian('masterdiv','headerstyle',1);">
    where..
    ..'masterdiv' is the name of the top div that contains the "accordians",
    ..'headerstyle' is the class that is used for 'open' headers,
    ..boolean value 1 to enable more than one open content,
    ................0 to enable just one open content.

    "Accordians" are composed of two divs:
    DIVNAME-header
    DIVNAME-content
    where "DIVNAMEs" have to be different for each "accordians", but must be
    similar in the header and corresponding content div name.
    
    <div id="masterdiv">
    
      <div id="first-header">
        Header Title Here
      </div>
      <div id="first-content">
        content here..
      </div>
      
      <div id="second-header">
        Header Title Here
      </div>
      <div id="second-content">
        Content here..
        with sublevels..
        
        <div id="third-header">
          Header Title Here
        </div>
        <div id="third-content">
          Content here..
        </div>
        
      </div>
      
    </div>
    
    etc..

   ===========================================================================================
    === Images Handling ===
    
    Shows and hides bigger image when mouse cursor is over the thumbnail.
    
    -------------------------------
    <img src="images/musiclive.jpg"
                   onmouseover="javascript:visibleimage(this.id,event);"
                   onmouseout="javascript:invisibleimage(this.id);"
                   id="img_musiclive" />
                   <div id="img_musiclive_big" class="invisible"><img src="images/musiclivebig.jpg" /></div>
    -------------------------------
                   
    OnMouseOver: let the big image appear at mouse cursor position.
    OnMouseOut:  hide the big image.
    
    Div's id have to be the same as Image's id with "_big" at the end.
    In the exemple above:
      image's id : img_musiclive
      div's id   : img_musiclive_big
      
    CSS must contain those styles:
    -----------------------------
    .visible{
      display:block;
      position:absolute;
      top: 0px;
      left: 0px;
      border:1px solid #000000;
    }
    .invisible{
      display:none;
    }
    -----------------------------
*/

//------------------------------------------------accordian vars
var TIME_TIC = 10;      // can be modified to change animation speed
var ANIM_STEPS = 7;     // can be modified too..
var MULTI_EXPAND = 0;
var CONTAINER_DIV = "";

//------------------------------------------------images handling vars
var tempX = 0;
var tempY = 0;
var classname_viz='visible';
var classname_hid='invisible';



//==================================================ACCORDIAN
function $(d) {return document.getElementById(d); }
function dsp(d,v)
{
	if(v==undefined) return d.style.display;
	else d.style.display=v;
}
function get_depth(d)
{
  p=0;
  tt=d;
  while (tt.id!=CONTAINER_DIV)
  {
    while (tt.id=="") tt=tt.parentNode;
    p++;
    tt=tt.parentNode;
  }
  return p;
}

function cc(n,v){
	s=n.className;
	if (v=='ex') n.className=s+" "+n.tc;
  else if (s.indexOf(' ')>0) n.className=s.substr(0,s.indexOf(' '));
}

function divinit(d)
{
  d=$(d);
	viz = d.style.visibility;
	d.style.visibility = 'hidden';
	dsp(d,'block');
  d.hh=parseInt(d.offsetHeight);
  d.h=0;
  d.nh=0;
  d.style.visibility = viz;
	d.style.height="0px";
	d.style.overflow='hidden';
	dsp(d,'none');
}

function cl(d)
{
  d.nh=0;
  if (MULTI_EXPAND==1) d.hh=parseInt(d.offsetHeight);
  fix_parents(d,d.h*(-1));
  refresh('bottom-up',d.c,d.dp,d.md);
}

function ex(d)
{
  var oh=0;
  if (MULTI_EXPAND==0)
  {
    for(ii=0;ii<d.c.length;ii++)
    {
      if (($(d.c[ii]).parentNode == d.parentNode)&&($(d.c[ii]).h>0)) oh=$(d.c[ii]).h;
      $(d.c[ii]).nh=0;
    }
  }
  d.nh=d.hh;
  fix_parents(d,d.nh-oh);
  refresh('top-down',d.c,d.dp,d.md);
}

function fix_parents(d,m)
{
    tt=d.parentNode;
    while (tt.id=="") tt=tt.parentNode;
    while(tt.id!=CONTAINER_DIV)
    {
      if (tt.id.substr(tt.id.indexOf('-')+1,tt.id.length)=='content')
      {
        tt.nh=tt.h+m;
        if (tt.nh<0) tt.nh=0;
      }
      tt=tt.parentNode;
      while(tt.id=="") tt=tt.parentNode;
    }
}

function resize(d)
{
  if ((d.nh-d.h)!=0)
  {
    if (TIME_TIC==0)
    {
      d.style.height=d.nh+"px";
      d.h = d.nh;
      if (d.nh==0) dsp(d,'none');
    }
    else
    {
        dh=Math.round((d.nh-d.h)/ANIM_STEPS);
        clearInterval(d.t);
        d.t = setInterval('ct("'+d.id+'",'+d.nh+','+dh+')',TIME_TIC);
    }
  }
}

function ct(d,t,dh)
{
  d=$(d);
  d.h=d.h+dh;
  if ((dh>0)&&(d.h>t)) d.h=t;
  if ((dh<0)&&(d.h<t)) d.h=t;
  d.style.height=d.h+"px";
  if (d.h==0) dsp(d,'none');
  if (d.h==t)
  {
    clearInterval(d.t);
    fix_headers(d.c);
  }
}

function refresh(m,c,dp,md)
{
  if (m=='top-down')
  {
    for(rd=0;rd<=md;rd++)
    {
      for(ri=0;ri<c.length;ri++)
      {
        if (dp[ri]==rd)
        {
          if ((dsp($(c[ri]))!='block')&&($(c[ri]).nh>0)) dsp($(c[ri]),'block');
          if ((($(c[ri]).nh==0)&&($(c[ri]).h>0))&&(MULTI_EXPAND==1)) $(c[ri]).hh=parseInt($(c[ri]).offsetHeight);
          resize($(c[ri]));
        }
      }
    }
  }
  if (m=='bottom-up')
  {
    for(rd=md;rd>=0;rd--)
    {
      for(ri=0;ri<c.length;ri++)
      {
        if (dp[ri]==rd)
        {
          if ((dsp($(c[ri]))!='block')&&($(c[ri]).nh>0)) dsp($(c[ri]),'block');
          resize($(c[ri]));
        }
      }
    }
  }
  fix_headers(c);
}

function fix_headers(c)
{
  for(k=0;k<c.length;k++)
  {
    if (dsp($(c[k]))=='block') cc($(c[k].substr(0,c[k].indexOf('-'))+'-header'),'ex');
    else cc($(c[k].substr(0,c[k].indexOf('-'))+'-header'),'cl');
  }
}

function Accordian(d,tc,m)
{
  var err="";
  CONTAINER_DIV=d;
  MULTI_EXPAND=m;
  
  if (d=="") err="You must specify the master container div.";
  if ((m<0)||(m>1)) err="Mode can either be 0 or 1, nothing else..";
  
  l=$(d).getElementsByTagName('div');
  c=[];
  dp=[];
  maxdepth=0;
  
  $(d).style.display='block';
  for(i=0;i<l.length;i++)
  {
      if (l[i].id.substr(l[i].id.indexOf('-')+1,l[i].id.length)=='content')
      {
        c.push(l[i].id);
        tmpd=get_depth(l[i]);
        if (tmpd>maxdepth) maxdepth=tmpd;
        dp.push(tmpd);
      }
      if (l[i].id.substr(l[i].id.indexOf('-')+1,l[i].id.length)=='header') l[i].tc=tc;
  }
  if (c.length==0) err="There is no accordian item..\nTry to add content and header divs.";
  
  if (err!="") alert(err);
  else
  {
    for(i=0;i<c.length;i++)
    {
      $(c[i]).c=c;
      $(c[i]).dp=dp;
      $(c[i]).md=maxdepth;
    }
    for(i=maxdepth;i>=0;i--)
    {
      for(j=0;j<c.length;j++)
      {
        if (dp[j]==i) divinit(c[j]);
      }
    }

    for(i=0;i<l.length;i++)
    {
      if (l[i].id.substr(l[i].id.indexOf('-')+1,l[i].id.length)=='header')
      {
        l[i].onclick = function()
        {
          n=this.id.substr(0,this.id.indexOf('-'));
          if (dsp($(n+'-content'))=='block') cl($(n+'-content'));
          else ex($(n+'-content'));
        }
      }
    }
    for(i=0;i<l.length;i++)
    {
      if (l[i].id.substr(l[i].id.indexOf('-')+1,l[i].id.length)=='header')
      {
        if(l[i].className.match(/selected+/)!=undefined)
        {
          n=l[i].id.substr(0,l[i].id.indexOf('-'));
          ex($(n+'-content'));
        }
      }
    }
  }
}

// =========================================== IMAGES HANDLING
	function visibleimage(id,e){
		var ImgId = id + '_big';
		getMouseXY(e);
		document.getElementById(ImgId).className = classname_viz;
		document.getElementById(ImgId).style.top=(tempY+10)+"px";
		document.getElementById(ImgId).style.left=(tempX+10)+"px";
	}


	function invisibleimage(id){
		var ImgId = id + '_big';
		document.getElementById(ImgId).className = classname_hid;
	}

	function getMouseXY(e) {
    if (e.pageX)
    {
      tempX = e.pageX;
      tempY = e.pageY;
    }
    else if (e.clientX)
    {
      tempX = e.clientX + (document.documentElement.scrollLeft ?
                              document.documentElement.scrollLeft :
                              document.body.scrollLeft);
      tempY = e.clientY + (document.documentElement.scrollTop ?
                              document.documentElement.scrollTop :
                              document.body.scrollTop);
    }
    else return false;
  }



// =====================================================FORM SUBMIT
function send_form()
{
  var res = document.getElementById('form_result');
  res.innerHTML = "Email system not yet implemented..";
}

