var gyros = [];
var curGyro = 0;
var slideShow = null;

var tweets = [];
var curTweet = 0;
var tweeter = null;

function showAll() {
  $.each( gyros, function() {
    $(this).show();
  } );
}

function resetSlideShow() {
  clearInterval( slideShow );
  slideShow = setInterval( 'nextGyro()', 6000 );
}

function nextGyro() {  
  $( '#gyro-' + curGyro ).removeClass( 'active' );
  if( curGyro == gyros.length-1 ) {    
    curGyro = 0;
    $( gyros[curGyro] ).fadeIn( 'def', function() {
      showAll();
    } );
  } else {
    $( gyros[curGyro] ).fadeOut();
    curGyro++;
  }
  $( '#gyro-' + curGyro ).addClass( 'active' );
  resetSlideShow();
}

function prevGyro() {
  $( '#gyro-' + curGyro ).removeClass( 'active' );  
  if( curGyro == 0 ) {
    for( var i = 1; i <  gyros.length - 1; i++ ) {
      $( gyros[i] ).hide();
    }
    $( gyros[curGyro] ).fadeOut();    
    curGyro = gyros.length-1;
  } else {
    curGyro--;
    $( gyros[curGyro] ).fadeIn();
  }
  $( '#gyro-' + curGyro ).addClass( 'active' );
  resetSlideShow();
}

function setGyro( idx ) {
  if( curGyro == idx || idx < 0 || idx > gyros.length -1 ) {
    return;
  }
  $( '#gyro-' + curGyro ).removeClass( 'active' );
  if( idx > curGyro ) {
    for( var i = 1; i < idx - curGyro; i++ ) {
      $( gyros[idx-i] ).hide();
    }
    $( gyros[curGyro] ).fadeOut();
  } else {
    $( gyros[idx] ).fadeIn();
    for( var i = 1; i < curGyro - idx; i++ ) {
      $( gyros[idx+i] ).show();
    }
  }
  curGyro = idx;
  $( '#gyro-' + curGyro ).addClass( 'active' );
  resetSlideShow();
}

function nextTweet() {  
  if( curTweet < 4 ) {
     curTweet++;
  } else {
     curTweet = 0;
  }
  $( '#twitter-bar p' ).fadeOut( function() {
     $( '#twitter-bar p' ).html( '<a href="http://twitter.com/instaflex" target="_blank">' + tweets[curTweet] + '</a>' );
     $( '#twitter-bar p' ).fadeIn();
  } );
}

$(document).ready(function () {
  var i=1;
  $( '.gyro-image' ).each( function() {
    gyros.push( $(this) );
    $(this).css( 'z-index', 100-10*i );
    i++;
  } );
    
  
  resetSlideShow();
  
  $( '.gyro-selector' ).click( function() {
    var idx = $(this).attr( 'id' );
    idx = parseInt( idx.substr( idx.length-1, idx.length ) );
    setGyro( idx );
  } );
  
  $( '#gyro-prev' ).click( function() {
    prevGyro();
  } );
  
  $( '#gyro-next' ).click( function() {
    nextGyro();
  } );
  
  $( '#gyro-pause' ).toggle( 
    function() {
      clearInterval( slideShow );
      $( '#gyro-pause' ).removeClass( 'ui-icon-pause' ); 
      $( '#gyro-pause' ).addClass( 'ui-icon-play' );
    },
    function() {
      slideShow = setInterval( 'nextGyro()', 6000 );
      $( '#gyro-pause' ).removeClass( 'ui-icon-play' ); 
      $( '#gyro-pause' ).addClass( 'ui-icon-pause' );
    }
   );
   
   $.getJSON( 'http://twitter.com/statuses/user_timeline/instaflex.json?count=5&callback=?',
      function( json ) {
         for( datum in json ) {
            tweets.push( json[datum].text);
         }
         tweeter = setInterval( 'nextTweet()', 5000 );
      }, "jsonp"
   );
  
} );

