/* Bomb */
Bomb.prototype = {
  
  buttons: '[class*="bomb_button"]',
  solution: ['red','blank','green'],
  clock: '.bomb_clock',
  minutesDiv: '#minutes',
  secondsDiv: '#seconds',
  explosionDiv: '#explosion',
  explosionMessage: '#explosion_message',
  diffusedDiv: '.bomb_diffused',
  secondsLeft: 0,
  seconds: 30,
  diffused: false,
  resetButton: '.reset_bomb',
  aftermathMessages: [
    "I am glad you are here with me.<br />Here at the end of all things",
    "End? No, the journey doesn't end here. Death is just another path... One that we all must take. The grey rain-curtain of this world rolls back, and all turns to silver glass... And then you see it. White shores... and beyond, a far green country under a swift sunrise.",
    "Your hands are cold. Life is leaving you.  This was your choice... there is no ship now that can bear you hence.",
    "Here at last, on the shores of the sea... comes the end of our journey. I will not say do not weep, for not all tears are an evil. "
  ],
  
  init: function() {    
    var _this=this;
    this.secondsLeft = this.seconds;
    // shuffle the solution
    this.solution = this.shuffleArray( this.solution );    
    // start the clock
    this.countDown();    
    // set listeners
    $(this.buttons).live('click', function(e){
      e.preventDefault();
      _this.buttonClick($(this));      
    });
    $(this.resetButton).live('click', function(e){
      e.preventDefault();
      _this.reset();
    });
  } ,
  
  countDown: function() {
    if ( this.diffused ) {
      return;
    } else if ( this.secondsLeft < 0 ) {
      this.explode();
      return;
    }    
    var _this=this, minutesLeft = Math.floor( this.secondsLeft/60 ),
        secondsLeft = this.secondsLeft - minutesLeft*60;        
    if ( minutesLeft < 10 ) {
      minutesLeft = '0' + minutesLeft;
    }
    if ( secondsLeft < 10 ) {
      secondsLeft = '0' + secondsLeft;
    }
    $(this.minutesDiv).text(minutesLeft);
    $(this.secondsDiv).text(secondsLeft);
    if ( this.secondsLeft < 10 && this.secondsLeft%2!=0 ) {
      $(this.clock).addClass('alert');
    } else if ( this.secondsLeft < 10 ) {
      $(this.clock).removeClass('alert');
    }
    this.secondsLeft--;
    setTimeout( function(){_this.countDown();}, 1000 );
  },
  
  buttonClick: function( $button ) {    
    this.switchColors( $button );
    this.checkForSuccess();    
  } ,
  
  switchColors: function( $button ) {    
    if ( $button.hasClass('bomb_button_green') ) {
      $button.removeClass('bomb_button_green');
      $button.addClass('bomb_button_red');
      $button.data('value','red');      
    } else if ( $button.hasClass('bomb_button_red') ) {
      $button.removeClass('bomb_button_red');
      $button.data('value','blank');      
    } else {
      $button.addClass('bomb_button_green');
      $button.data('value','green');
    }    
  } ,
  
  checkForSuccess: function() {    
    if ( this.diffused ) return;    
    var success = true, solution = this.solution;    
    $(this.buttons).each(function(i){      
      if ( $(this).data('value') != solution[i] )
        success = false;      
    });    
    if ( success ) this.doSuccess();    
  } ,
  
  doSuccess: function() {  
      
    this.diffused = true;
    $(this.diffusedDiv).fadeIn();
    
  } ,
  
  explode: function() {
    var _this = this;
    
    $(this.explosionMessage).html(this.aftermathMessages[Math.floor(Math.random()*this.aftermathMessages.length)]);
    $(this.explosionDiv).css('display','block');
    $(this.explosionDiv).animate({
      'opacity':1
    },3000,function(){
      $(_this.explosionDiv).animate({
        'backgroundColor':'#000000'
      },5000);
    });
    
  },
  
  shuffleArray: function( arr ){
    for( var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
    return arr;
  },
  
  reset: function() {    
    this.secondsLeft = this.seconds;
    this.diffused = false;
    $(this.explosionDiv).css({
      'display':'none',
      'backgroundColor':'#ffffff',
      'opacity':0
    });
    this.countDown();    
  }
  
}

function Bomb(){}
var _bomb = new Bomb();

$(document).ready(function(){
  
  _bomb.init();

});
