/* Home harmonica by D.J. Jansen */


// Initialiseren
function HomeHarmonica( ul ){
 
  this.Element = ul;

  this.Childs = new Array();

  var Items = $(ul).find("li.Li");

  this.Active = null;

  this.CurrentToOpen = null;
  this.ToOpen = new Array();
  this.ToOpenRunner = null;

  this.StartTime = null;
  this.AnimationOpenTime = 800;

  this.Teaser_Height = this.UL_Height = this.Element.offsetHeight;

  var self = this;

  this.accelOpenConst  = 0.0004;

  for( var i = 0; i < Items.length; i ++ ){
    var Item = Items[ i ];
    var Teaser = $(Item).find( ".Teaser" )[ 0 ];
    var Link = $(Item).find( "a.Item" )[ 0 ];

    this.Teaser_Height -= Link.offsetHeight;

    var Child = {
      Item: Item,
      Link: Link,
      Teaser: Teaser,
      Height: parseInt( $(Teaser).css( "height" ) )
    };

    Link.Index = this.Childs.length ;

    this.Childs[ Link.Index ] = Child;

    if ( $(Teaser).css( "height" ) != "0px" ){
      this.Active = Link.Index;
    } else {
       this.Childs[ Link.Index ].Height = 0;
    }

    Link.onclick = function(){ self.OpenLink( this.Index ); return false; }
  }

}

HomeHarmonica.prototype.OpenLink = function( Index ){

  if ( this.ToOpen.length == 0 && this.Active != Index || this.ToOpen.length > 0 && this.ToOpen[ this.ToOpen.length - 1 ] != Index ){
    this.ToOpen[ this.ToOpen.length ] = Index;
    if ( this.CurrentToOpen == null ){
      this.StartRunning();
    }
  }

}

HomeHarmonica.prototype.StartRunning = function(){
  this.CurrentToOpen = this.ToOpen[ 0 ];
  this.ToOpenRunner = clearInterval( this.ToOpenRunner );
  var self = this;
  this.StartTime = (new Date()).getTime();
  this.ToOpenRunner = setInterval( function(){ self.IntervalOpenLink(); }, 40 );

//  this.Childs[ this.CurrentToOpen ].Item.className += " Actief";

}

HomeHarmonica.prototype.IntervalOpenLink = function (){
  var ToOpen = this.Childs[ this.CurrentToOpen ];
  var ToClose = this.Childs[ this.Active ];

  if ( ToOpen.Height < this.Teaser_Height ){
    var Speed = 5;

    var elapsed = (new Date()).getTime() - this.StartTime;

    var d = Math.round(Math.pow( this.AnimationOpenTime - elapsed, 2) * this.accelOpenConst);

    d = Math.min( this.Teaser_Height, d );
    d = Math.max( 0, d );

    ToOpen.Height = this.Teaser_Height - d;
    ToClose.Height = d;

    ToOpen.Teaser.style.height = ToOpen.Height + 'px';
    ToClose.Teaser.style.height = ToClose.Height + 'px';

  }

  if ( ToOpen.Height >= this.Teaser_Height ){
    ToOpen.Height = this.Teaser_Height;
    ToClose.Height = 0;

    ToOpen.Teaser.style.height = this.Teaser_Height + 'px';
    ToClose.Teaser.style.height = '0px';

//    this.Childs[ this.Active ].Item.className = this.Childs[ this.Active ].Item.className.replace( ' Actief', '' );

    this.Active = this.CurrentToOpen;

    if ( this.ToOpen.length > 1 ){
      var newToOpen = new Array();
      for( var j = 1; j < this.ToOpen.length; j ++ ){
        newToOpen[ j - 1 ] = this.ToOpen[ j ];
      }
      this.ToOpen = newToOpen;
      this.StartRunning();
    } else {
      this.ToOpen = new Array();
      this.CurrentToOpen = null;
      this.ToOpenRunner = clearInterval( this.ToOpenRunner );
    }

  }

}

function InitHomeItem( Link ){
  var ul = Link.parentNode.parentNode;

  ul.HomeHarmonica = new HomeHarmonica( ul );

  ul.HomeHarmonica.OpenLink( Link.Index );

  return false;
}

function Submenu( Link, Sublist ){

  this.Link = Link;
  this.Sublist = Sublist;

  this.Height = $(this.SubList).find("li").length * 20;
  this.CurrentHeight = 0;

  var self = this;

  this.Actions = new Array();
  this.CurrentAction = null;

  this.Link.onclick = function(){
    self.OnClick();
    return false;
  }

  this.Childs = $(this.Sublist).find("a");

  this.MouseOn = false;
  this.MouseTimeout = null;

  this.Link.onmouseover = function(){
    self.MouseOn = true;
  }

  this.Link.onmouseout = function(){
    self.MouseOn = false;
    self.MouseTimer();
  }
  for( var i = 0; i < this.Childs.length; i ++ ) {
    var a = this.Childs[ i ];
    a.MouseOn = false;
    a.onmouseover = function(){
      this.MouseOn = true;
    }
    a.onmouseout = function(){
      this.MouseOn = false;
      self.MouseTimer();
    }
  }

}

Submenu.prototype.MouseTimer = function(){
  clearTimeout( this.MouseTimeout ); 
  var self = this;
  this.MouseTimeout = setTimeout( function(){self.CheckMouseState()}, 1500 );
}

Submenu.prototype.CheckMouseState = function(){
  var MouseOn = this.MouseOn;
  if ( ! MouseOn ){
    for( var i = 0; i < this.Childs.length; i ++ ) {
      var a = this.Childs[ i ];
      if ( a.MouseOn ){
        MouseOn = true;
      }
    }
  }
  if ( ! MouseOn ){
    this.Close();
  }
  clearTimeout( this.MouseTimeout );
}

Submenu.prototype.OnClick = function(){
  var LastAction = this.Actions.length > 0 ? this.Actions[ this.Actions.length - 1 ] : null;

  if ( LastAction == null && this.CurrentHeight == 0 || LastAction != null && LastAction.Heigth == 0 ){
    this.Open();
  } else {
    this.Close();
  }
  return false;
}

Submenu.prototype.Open = function(){
  this.Actions[ this.Actions.length ] = { Height: this.Height, Time: 750 };
  if ( this.CurrentAction == null ){
    this.CurrentAction = 0;
    this.MakeActions();
  }
}

Submenu.prototype.Close = function(){
  this.Actions[ this.Actions.length ] = { Height: 0, Time: 500 };
  if ( this.CurrentAction == null ){
    this.CurrentAction = 0;
    this.MakeActions();
  }
}

Submenu.prototype.MakeActions = function(){
  var Action = this.Actions[ this.CurrentAction ];

  if ( Action ){  
    var self = this;

    if ( Action.Height == this.Height ){
      this.Link.className = 'KopActiefLink';
    }

    $(this.Sublist).animate(
      {
        height: Action.Height + "px"
      },
      Action.Time,
      'swing',
      function(){ 
        self.CurrentHeight = Action.Height;
        if ( self.CurrentHeight == 0 ){
          self.Link.className = 'KopLink';
        }
        self.MakeActions();
      }
    );
    if ( this.CurrentAction < this.Actions.length - 1 ) {
      this.CurrentAction ++;
      return true;
    } 
  } 
  this.Actions = new Array();
  this.CurrentAction = null;
}

function InitSubmenu( Link ){
  Link.onclick = null;
  var id = Link.parentNode.id.substring( 4 );

  var SubList = document.getElementById( 'List' + id );

  if ( SubList ){ 
    Link.Submenu = new Submenu( Link, SubList );
    Link.Submenu.OnClick();
  }

  return false;
}

function SwapKleinImageToGrootImage( NewURL ){
  var AfGroot = document.getElementById( 'AfbeeldingGroot' );
  if ( AfGroot != null ){
    if ( AfGroot.src != NewURL ){
      AfGroot.src = NewURL;
    }
  }
  return false;
}


/**
 * ShowFlash displays a flash/movie with javascript
 */

function ShowFlashEN( url, width, height ){
  document.write( '<object height="260" width="297">' );
  document.write( '<param name="allowscriptaccess" value="always"></param>' );
  document.write( '<param name="allowFullScreen" value="true"></param>' );
  document.write( '<param name="wmode" value="transparent"></param>' );  
  document.write( '<param name="movie" value="http://www.youtube.com/v/' + url + '&amp;hl=nl&amp;fs=1&amp;rel=0&amp;fmt=18"></param>' );  
  document.write( '<embed height="260" width="297" src="http://www.youtube.com/v/' + url + '&amp;hl=nl&amp;fs=1&amp;rel=0&amp;fmt=18" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true"></embed>' );
  document.write( '</object>' );
}

function ShowFlashNL( url, width, height ){
  document.write( '<object height="260" width="297">' );
  document.write( '<param name="allowscriptaccess" value="always"></param>' );
  document.write( '<param name="allowFullScreen" value="true"></param>' );
  document.write( '<param name="wmode" value="transparent"></param>' );
  document.write( '<param name="movie" value="http://nl.youtube.com/v/' + url + '&amp;hl=nl&amp;fs=1&amp;rel=0&amp;fmt=18"></param>' );
  document.write( '<embed height="260" width="297" src="http://nl.youtube.com/v/' + url + '&amp;hl=nl&amp;fs=1&amp;rel=0&amp;fmt=18" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true"></embed>' );
  document.write( '</object>' );
}
