Array.prototype.shuffle = function() {
  var tmp, rand;
  for(var i =0; i < this.length; i++){
    rand = Math.floor(Math.random() * this.length);
    tmp = this[i]; 
    this[i] = this[rand]; 
    this[rand] = tmp;
  }
}

var game = {
  wating: false,

  createBoard: function(target, x, y) {
    // create card deck
    var cards = new Array;
    for (var i = 1; i < x * y / 2 + 1; i++) {
      cards.push({'no':i,'sub':'a'});
      cards.push({'no':i,'sub':'b'});
    }
    cards.shuffle();

    // create html
    for (var i = 0; i < y; i++) {
      target.append($('<div>').addClass('clear'));
      for (var j = 0; j < x; j++) {
        var card = $('<div>');
        card.addClass('card');
        card.data('card', cards[i * x + j].no);
        card.data('sub', cards[i * x + j].sub);
        target.append(card);
      }
    }
  },

  toggleCard: function() {
    // check if game is waiting for hiding cards
    if (game.waiting) {
      return;
    }
    // check if card is already opened
    if ($(this).hasClass('on')) {
      return;
    }

    // show clicked card
    $(this).addClass('on');
    $(this).addClass('image' + $(this).data('card') + $(this).data('sub'));

    // check if two cards are shown
    if (2 == $('.on').size()) {
      // check if a pair was found
      if ($('.on').first().data('card') == $('.on').last().data('card')) {
        // modify found cards, to take them out of the game
        $('.on').each(function(idx, obj) {
          $(obj).unbind('click');
          $(obj).addClass('removed');
          $(obj).removeClass('on');
          $(obj).removeClass('card');
        });

        // count move
        game.incrementMoves();

        // check if game is finished
        if (0 == $('.card').size()) {
          alert('Congratulation! All Cards cleared.');
          game.incrementWons();
          $('#new').show();
        }
      }
      else {
        // when no pair was found, prepare to hide opened cards. do so with delay.
        game.waiting = true;
        setTimeout(game.hideCards, 1000);
      }
    }
  },

  hideCards: function() {
    $('.on').each(function(idx, obj) {
      $(obj).removeClass('on');
      $(obj).removeClass('image' + $(obj).data('card') + $(obj).data('sub'));
    });

    game.incrementMoves();
    game.waiting = false;
  },

  incrementMoves: function(val) {
    var moves_tracker = $('#moves');
    var num_of_moves = moves_tracker.data('moves') || 0;
    if (typeof(val) != 'undefined') {
      num_of_moves = --val;
    }
    moves_tracker.data('moves', ++num_of_moves).text(num_of_moves);
  },

  incrementWons: function() {
    var won_tracker = $('#finished');
    var num_won = won_tracker.data('won') || 0;
    won_tracker.data('won', ++num_won).text(num_won);
  },

  init: function() {
    $('#board').html('');
    game.createBoard($('#board'), 6, 4);
    game.incrementMoves(0);

    $('.card').bind('click', game.toggleCard);

    $('#new').hide();
  }
}

var loadCss = function(filename) {
  if(document.createStyleSheet) {
    document.createStyleSheet(filename);
  }
  else {
    var styles = "@import url('" + filename + "');";
    var newSS = document.createElement('link');
    newSS.rel = 'stylesheet';
    newSS.href = 'data:text/css,' + escape(styles);
    document.getElementsByTagName("head")[0].appendChild(newSS);
  }
}

$(document).ready(function(){
  loadCss('http://www.scholler-prepress.com/wp-content/themes/scholler/pairs.css');

  game.init();
});

