/**
 * Snepo tags JS
 *
 * Here we provide a jQuery tiles shuffler
 */
$(function() {
  // Vars
  var tiles = {};
  
  // Get original positions and sizes
  $( 'div.snepo_project_tile' ).each(function() {
    // Set zIndex
    var zIndex = $( this ).css( 'zIndex' );
    $( this ).data( 'zIndex', zIndex );
       
    // Create group if necessary
    var key = $( this ).outerWidth() + '_' + $( this ).outerHeight();
    if (key in tiles) {
      //
    } else {
      tiles[key] = [];
    }
    
    // Add to group
    tiles[key].push( $( this ) );
  });
  
   // Only groups with > 1 tile
   var deletes = [];
   for( var i in tiles ) {
     if( tiles[i].length == 1 ) {
       deletes.push( i );
     }
   }
   
   for( var i in deletes ) {
     delete tiles[deletes[i]];
   }
   
   // Into array
   var tiles_array = [];
   for( var i in tiles ) {
     tiles_array.push( tiles[i] );
   }
   
   // Add to container
   $( 'div#snepo_tiles' ).data( 'tiles', tiles_array );
   
   // Init
   $( 'div#snepo_tiles' ).data( 'moving', 0 );
  
  // Catch "t" for shuffle
  $( window ).keydown(function( event ) {
    if ( event.keyCode == 84 && $( 'div#snepo_tiles' ).data( 'moving' ) == 0)
    {
      // Blocking
      $( 'div#snepo_tiles' ).data( 'moving', 2 );
      $.snepoTileShuffle();
    }
  });
  
  // Catch "b" for box-shadow
  $( window ).keydown(function( event ) {
    if ( event.keyCode == 66 && $( 'div#snepo_tiles' ).data( 'moving' ) == 0)
    {
      // Blocking
      $.snepoTileShadow();
    }
  });
  
});

// Plugin to allow for control of project block
(function ( $ ) {
  
  $.snepoTileShuffle = function( ) {
    // Get tiles
    var tiles = $( 'div#snepo_tiles' ).data( 'tiles' );
    
    // Check there are some
    if (tiles.length == 0)
    {
      $( 'div#snepo_tiles' ).data( 'moving', 0 );
      return true;
    }
    
    // Get random element
    var tile_set = tiles[ Math.round( Math.random() * (tiles.length - 1)) ];
    
    // Get 1st random index
    var index1 = Math.round( Math.random() * (tile_set.length - 1) );
    
    // Get second random index
    var index2;
    while( true ) {
      index2 = Math.round( Math.random() * (tile_set.length - 1) );
      if( index2 != index1 ) {
        break;
      }
    }
    
    // Set up zIndex
    tile_set[index1].css( 'zIndex', 200 );
    tile_set[index2].css( 'zIndex', 200 );
    
    // Animate
    tile_set[index1].animate({
      left: tile_set[index2].position().left + 'px',
      top:  tile_set[index2].position().top + 'px'
    }, 1000, function() {
      var zIndex = $( this ).data( 'zIndex' );
      $( this ).css( 'zIndex', zIndex );
      var moving = $( 'div#snepo_tiles' ).data( 'moving' );
      moving--;
      $( 'div#snepo_tiles' ).data( 'moving', moving );
    });
    tile_set[index2].animate({
      left: tile_set[index1].position().left + 'px',
      top:  tile_set[index1].position().top + 'px'
    }, 1000  , function() {
        var zIndex = $( this ).data( 'zIndex' );
        $( this ).css( 'zIndex', zIndex );
        var moving = $( 'div#snepo_tiles' ).data( 'moving' );
        moving--;
        $( 'div#snepo_tiles' ).data( 'moving', moving );
      });
  };
  
  $.snepoTileShadow = function() {
    // Get tiles and toggle shadow
    var tiles = $( 'div.snepo_project_tile' ).toggleClass( 'snepo_shadow' );
  }
  
})( jQuery );
