/*
 * Menu Lines.
 */

var lineSteps = 3;
var lineMinSpeed = 750;
var lineMaxSpeed = 375;
var lineGlobalAnimating = false;

var lineTimeout = null;

function animateLines()
{
 lineGlobalAnimating = true;
 
 var animate = function( line )
 {
  $(this).data( "animating", true );
  
  var o = line.data( "originalHeight" );
  var m = line.data( "lastMinHeight" );
  var z = line.data( "lastMaxHeight" );
  
  var destination = null;
  var step = line.data( "steps" );
  
  // ( sin( x*2PI + 3PI/2 ) + 1 ) / 2
  var x = (step - 1) / (lineSteps - 1);
  var thisSpeed = ( ( Math.sin( x * 2 * Math.PI + 3 * Math.PI / 2 ) + 1 ) / 2 );
  thisSpeed = Math.round( lineMinSpeed - ( (lineMinSpeed - lineMaxSpeed) * thisSpeed ) );
  
  if( step == lineSteps )
   destination = o;
  else
  {
   if( line.height() < o || ( line.height() == o && Math.round( Math.random() ) == 1 ) )
   {
    o = o * ( ( lineSteps / Math.pow( 2, step ) ) / lineSteps );
    destination = Math.round( Math.random() * ( z - o ) + o );

    line.data( "lastMaxHeight", destination );
   }
   else
   {
    o = o + o * ( ( lineSteps / Math.pow( 2, step ) ) / lineSteps );
    destination = Math.round( Math.random() * ( o - m ) + m );

    line.data( "lastMinHeight", destination );
   }
  }
  
  line.data( "steps", ++step );
  
  line.animate( {
   "height": destination
  },
  {
   "duration": thisSpeed,
   "easing": "linear",
   "complete": function()
   {
    if( $(this).data( "steps" ) <= lineSteps )
     animate( $(this) );
    else
    {
     $(this).data( "animating", false );
     
     if( lineTimeout == null )
      lineTimeout = setTimeout( "checkLinesRestart()", 100 );
    }
   }
  } );
 };
 
 $.each( $("div#lines-container div"), function( i, line )
 {
  $(line).data( "steps", 1 );
  $(line).data( "lastMinHeight", 0 );
  $(line).data( "lastMaxHeight", $("div#lines-container").height() );
  $(line).delay( Math.round( Math.random() ) * 500 );
  animate( $(line) );
 } );
}

function stopAnimatingLines()
{
 lineGlobalAnimating = false;
 
 $("div#lines-container div").stop( true );
 
 $.each( $("div#lines-container div"), function( i, line )
 {
  $(line).animate( {
   "height": $(line).data( "originalHeight" )
  },
  {
   "duration": lineMaxSpeed,
   "easing": "swing"
  } );
 } );
}

function checkLinesRestart()
{
 var animating = false;
 
 $.each( $("div#lines-container div"), function( i, line )
 {
  if( $(line).data( "animating" ) == true )
  {
   animating = true;
   return false;
  }
 } );
 
 if( animating == true )
  lineTimeout = setTimeout( "checkLinesRestart()", 100 );
 else
 {
  lineTimeout = null;
  
  // no restart.
  lineGlobalAnimating = false;
  
  if( lineGlobalAnimating == true )
   animateLines();
 }
}



jQuery( function()
{
 /*
  * Menu lines.
  */
  
 $.each( $("div#lines-container div"), function( i, line )
 {
  $(line).data( "originalHeight", $(line).height() );
 } );
 
 /*$("ul#pages-menu").hover( function()
 {
  animateLines();
 },
 function()
 {
  stopAnimatingLines();
 } );*/
 
 $("ul#pages-menu li a, div#lines-container").hover( function()
 {
  if( !lineGlobalAnimating )
   animateLines();
 },
 function()
 {
  //stopAnimatingLines();
 } );

} );