var playing         = false;
var indicatorLeft   = 0;
var indicatorTimer
var xTracks         = {
                      54751 : 148,
                      54752 : 696,
                      54753 : 336,
                      54754 : 623,
                      54755 : 701
                    };

new Event.observe( window, 'load', function(){
      articleExpander();
      videoExpander();
      audioExpander();
      imageExpander();
      publicationExpander();
      homeAudio();
    });

// -----------------------------------------------------------------------------

function cfc( meth )
{
  return s_WEBROOT + "/shared/components/altena.cfc?method="+meth;
}

// -----------------------------------------------------------------------------

function articleClicked( element_id )
{
  var imageID = "image_" + element_id + "_1";
  var bodyID = "body_" + element_id;
  var introID = "intro_" + element_id;

  $$( "#media .image" ).each( function( image ){ 
    if( image.id != imageID )
      image.hide();
  });

  $$( "#content .body" ).each( function( body ){ 
    if( body.id != bodyID )
      body.hide();
  });

  $$( "#content .aa" ).each( function( aa ){
    aa.addClassName( 'closed' );
  });

  $$( "#content .audioplayer" ).each( function( aa ){
    aa.addClassName( 'closed' );
    aa.update();
    clearTimeout( indicatorTimer );
    Sound.enable();
    Sound.play( "", { replace:true } );
    Sound.disable();
  });

  if( $( imageID ) != undefined )
  {
    $( imageID ).toggle();
    $( imageID ).clonePosition( $( element_id ), { setLeft: false, setWidth: false, setHeight: false, offsetTop:4, offsetLeft: 122 } );
  }

  $( bodyID ).toggle();

  if( $( bodyID ).style.display != 'none' )
  {
    if( $$( "#"+introID+" .aa" )[0] != undefined )
      $$( "#"+introID+" .aa" )[0].removeClassName( 'closed' );

    if( b_audioPlayer )
    {
      $$( "#"+introID+" .audioplayer" )[0].removeClassName( 'closed' );

      Sound.enable();
      Sound.play( s_WEBROOT + "/shared/audio/track_"+element_id+".mp3", {replace:true} );

      indicatorLeft = 0;
      audioPlayerIndicator( element_id );
    }
  }
}

function articleExpander()
{
  if( b_articleExpander )
  {
    $$( "#content .body" ).each( function( body ){ body.hide(); });

    $$( "#content h2" ).each( function( h2 ){
      h2.addClassName( "clickable" );
      $( "intro_"+h2.id ).addClassName( "clickable" );

      new Event.observe( h2,                  "click", function( event ){ articleClicked( h2.id ); });
      new Event.observe( $( "intro_"+h2.id ), "click", function( event ){ articleClicked( h2.id ); });

      if( !b_audioPlayer )
      {
        var aa = new Element( 'div', { 'class': 'aa closed' } );
        $( "intro_"+h2.id ).insert( aa );
      }
    });
  }
}




// -----------------------------------------------------------------------------
function imageExpander()
{
  if( b_imageLinks )
  {
    $$( "#media .image" ).each( function( image ){
      image.absolutize(); 
      image.hide(); 
    });
  }

  if( $( "gallery" ) != undefined )
  {
    $$( "#gallery img" ).each( function( img ){
      new Event.observe( img, "mouseover", function( event ){ loadImageInMedia( img.id ) });
    });
  }
}

function loadImageInMedia( article_id )
{
  $$( '#media div' ).each( function( divs ){ divs.hide(); });
  $( 'large_' + article_id ).show();
}



// -----------------------------------------------------------------------------
function videoExpander()
{
  if( b_autoloadVideo )
  {
    $$( "#content h2" ).each(
      function( h2 )
      {
        loadVideoInMedia( h2.id ); 
      }
    );
  }
  else if( b_videoLinks )
  {
    $$( "#content .body" ).each( function( body ){ body.hide(); });

    $$( "#content h2" ).each( function( h2 ){
      h2.addClassName( "clickable" );
      $( "intro_"+h2.id ).addClassName( "clickable" );

      new Event.observe( h2,                  "click", function( event ){ loadVideoInMedia( h2.id ); });
      new Event.observe( $( "intro_"+h2.id ), "click", function( event ){ loadVideoInMedia( h2.id ); });

      var aa = new Element( 'div', { 'class': 'aa closed' } );
      $( "intro_"+h2.id ).insert( aa );
    });
  }
}

function loadVideoInMedia( article_id )
{
  if( !b_autoloadVideo )
    articleClicked( article_id )

  if( $( "body_" + article_id ).style.display == "none" )
  {
    $( "media" ).update();
    return;
  }

  FlashReplace.replace(
    "media", 
    s_WEBROOT + "/shared/images/ma-video.swf",
    "flash_video_obj",
    337,
    256,
    8,
    {
      flashvars : "videofile=" + s_WEBROOT + "/shared/video/vid-" + article_id + ".flv"
    }
  );

  $$( "#media>object" )[0].absolutize();
  $$( "#media>object" )[0].clonePosition( $( article_id ), { setLeft: false, setWidth: false, setHeight: false, offsetTop:4, offsetLeft: 122 } );
}




// -----------------------------------------------------------------------------
function publicationExpander()
{
  if( b_publications )
  {
    $$( "#publications .publication" ).each(
      function( pub )
      {
        var counter = 1;

        loadPageInPublication( pub.id, counter );

        new Event.observe( 
          $$("#"+pub.id+">.publeft")[0], 
          "click", 
          function( event )
          {
            counter--; 
            loadPageInPublication( pub.id, counter ); 
          }
        );

        new Event.observe( 
          $$("#"+pub.id+">.pubright")[0], 
          "click", 
          function( event )
          { 
            counter++; 
            loadPageInPublication( pub.id, counter ); 
          }
        );
      }
    );
  }
}

function loadPageInPublication( id, page )
{
  new Ajax.Updater(
        $$("#"+id+" .spread")[0],
        cfc( 'getPage' ),
        {
          parameters  : {
                          nAssetmetaID  : id,
                          nPageNum      : page
                        }
        }
      );
}



// -----------------------------------------------------------------------------
function audioExpander()
{
  if( b_audioPlayer )
  {
    $$( "#content h2" ).each( function( h2 ){
      var aa = new Element( 'div', { 'class': 'audioplayer closed' } );
      $( "intro_"+h2.id ).insert( aa );
    });
  }
}







// -----------------------------------------------------------------------------
function homeAudio()
{
  if( b_homeaudio )
  {
    Event.observe( $( "audio" ), "click", function( event ){ toggleHomeaudio( event ); });
  }
}

function toggleHomeaudio( event )
{
  event.stop();

  if( !playing )
  {
    Sound.enable();
    Sound.play( s_WEBROOT + "/shared/audio/tik.mp3", { replace : true } );

    $( "audio" ).src = s_WEBROOT + "/shared/images/homepageaudio.gif";
  }
  else
  {
    Sound.enable();
    Sound.play( "", { replace:true } );
    Sound.disable();

    $( "audio" ).src = s_WEBROOT + "/shared/images/homepageaudio_stop.gif";
  }

  playing = !playing;
}

function audioPlayerIndicator( article_id )
{
  trackWidth = 420;
  indicatorWidth = xTracks[article_id];
  updateLeft = trackWidth / indicatorWidth;
  indicatorLeft += updateLeft;

  $( "intro_" + article_id ).select( ".audioplayer" )[0].update();

  indicator = new Element( "div", { "class" : "indicator" } );
  indicator.style.marginLeft = Math.round( indicatorLeft ) + 10 + "px";

  $( "intro_" + article_id ).select( ".audioplayer" )[0].update( indicator );

  if( indicatorLeft >= trackWidth )
  {
    indicatorLeft = 0;
    Sound.enable();
    Sound.play( "", { replace:true } );
    Sound.disable();
  }

  indicatorTimer = setTimeout( "audioPlayerIndicator( " + article_id + " )", 1000 );
}

// -----------------------------------------------------------------------------
function filterContentByTag( nMenuID, nTagID )
{
  new Ajax.Updater(
        $("content").select("div")[0],
        cfc( 'filterContentByTag' ),
        {
          parameters  : {
                          nMenuID: nMenuID, 
                          nTagID: nTagID 
                        },
          evalScripts : true
        }
      );
}

