var G_S = 0,
P_W = 0,
P_H = 0,
P_R = 30,
B_ROWS = 15,
B_COLS = 25,
B_W = 0,
B_H = 0,
B_G = 0,
B_GLAST = 0,
B_L = [],
B_C=["#505050","#505050","#505050","#505050","#505050","#505050","#8e8e8e","#8e8e8e","#8e8e8e","#8e8e8e","#8e8e8e","#8e8e8e","#d2d2d2","#d2d2d2","#d2d2d2","#d2d2d2","#d2d2d2","#d2d2d2"],
velRange = [ -12, -11, -10, -9, 9, 10, 11, 12 ],
game = function() {
    return {
        init: function() {
            $("#playground").playground({
                height: P_H,
                width: P_W,
                refreshRate: P_R
            });
            $("#playground").css("width", P_W);
            $("#playground").css("height", P_H);
            $("#playground").css("position", "relative");
            B_L = this.init_bricks();
            this.init_cwh_dust_your_shoulder_off();
            var f = this;
            G_S = 1;
            $("#playground").registerCallback(function() {
              if (G_S == 1) {
                $("#cwh_dust_your_shoulder_off").collision(".live_brick").each(function() {
                    B_G++;
                    $(this).css({ "background-color": B_C[B_G % B_C.length] }).removeClass("live_brick");
                    if (B_G >= B_L.length) {
                      setTimeout(function() {
                        B_G = 0;
                        $(".brick").css('background-color','transparent').addClass("live_brick");
                        $("#cwh_dust_your_shoulder_off").css("left", ((P_W / 2) - (cwh_dust_your_shoulder_off_width / 2))).css("top", ((P_H / 2) - (cwh_dust_your_shoulder_off_height / 2)))
  	                    $("#cwh_dust_your_shoulder_off").get(0).gameQuery.velX = velRange[Math.floor(Math.random() * velRange.length)]-7;
  	                    $("#cwh_dust_your_shoulder_off").get(0).gameQuery.velY = velRange[Math.floor(Math.random() * velRange.length)];
                      }, 3000);
                    }
                });
                f.render_cwh_dust_your_shoulder_off();
                return false;
              } else {
                return true;
              }
            },
           P_R)
         setInterval(function() {
          if (B_G == B_GLAST) {
  	   $("#cwh_dust_your_shoulder_off").get(0).gameQuery.velX = velRange[Math.floor(Math.random() * velRange.length)]-7;
  	   $("#cwh_dust_your_shoulder_off").get(0).gameQuery.velY = velRange[Math.floor(Math.random() * velRange.length)];
          } else {
           B_GLAST = B_G;
          }
         }, 10000);
        },
        init_bricks: function() {
          B_L=[];
          for (var a = new $.gameQuery.Animation({
              imageURL: "http://staticcdn.omgpop.com/error_code/blank.gif"
          }),
          B_L = [], b = Math.floor(P_W / B_W); b--;) {
              $.playground().addSprite("brick_" + b, {
                  posx: b * B_W,
                  posy: 0,
                  height: B_H,
                  width: B_W,
                  animation: a,
                  geometry: $.gameQuery.GEOMETRY_RECTANGLE
              });
              var d = $("#brick_" + b).addClass("brick").addClass("live_brick").css({
                  "background-color": "transparent"
              });
              B_L.push(d);
              for (var c = Math.floor(P_H / B_H); c--;) {
                  $.playground().addSprite("brick_" + b + "_" + c, {
                      posx: b * B_W,
                      posy: c * B_H,
                      height: B_H,
                      width: B_W,
                      animation: a,
                      geometry: $.gameQuery.GEOMETRY_RECTANGLE
                  });
                  d =
                  $("#brick_" + b + "_" + c).addClass("brick").addClass("live_brick").css({
                      "background-color": "transparent"
                  });
                  B_L.push(d)
              }
          }
          return B_L;
        },
        init_cwh_dust_your_shoulder_off: function() {
            $("#cwh_dust_your_shoulder_off").remove();
            $("#playground").addSprite("cwh_dust_your_shoulder_off", {
                animation: new $.gameQuery.Animation({
                    imageURL: "http://staticcdn.omgpop.com/error_code/blank.gif"
                }),
                width: cwh_dust_your_shoulder_off_width,
                height: cwh_dust_your_shoulder_off_height
            });
            $("#cwh_dust_your_shoulder_off").get(0).gameQuery.velX = velRange[Math.floor(Math.random() * velRange.length)]-7;
            $("#cwh_dust_your_shoulder_off").get(0).gameQuery.velY = velRange[Math.floor(Math.random() * velRange.length)];
            $("#cwh_dust_your_shoulder_off").css("left", ((P_W / 2) - (cwh_dust_your_shoulder_off_width / 2))).css("top", ((P_H / 2) - (cwh_dust_your_shoulder_off_height / 2)))
        },
        render_cwh_dust_your_shoulder_off: function() {
            var a = $("#cwh_dust_your_shoulder_off").position();
            a.left += $("#cwh_dust_your_shoulder_off").get(0).gameQuery.velX;
            a.top += $("#cwh_dust_your_shoulder_off").get(0).gameQuery.velY;
            if (a.top <= 0 || a.top + $("#cwh_dust_your_shoulder_off").height() >= P_H) $("#cwh_dust_your_shoulder_off").get(0).gameQuery.velY = -$("#cwh_dust_your_shoulder_off").get(0).gameQuery.velY;
            if (a.left <= 0) $("#cwh_dust_your_shoulder_off").get(0).gameQuery.velX = -$("#cwh_dust_your_shoulder_off").get(0).gameQuery.velX;
            if (a.left + $("#cwh_dust_your_shoulder_off").width() >= P_W) $("#cwh_dust_your_shoulder_off").get(0).gameQuery.velX = -$("#cwh_dust_your_shoulder_off").get(0).gameQuery.velX;
            $("#cwh_dust_your_shoulder_off").css("top", a.top);
            $("#cwh_dust_your_shoulder_off").css("left", a.left)
        },
        start: function() {
            $("#playground").startGame()
        }
    }
} ();
$(document).ready(function() {
    P_W = $('#container').width();
    P_H = $('#container').height();
    B_W = Math.floor(P_W / B_COLS);
    B_H = Math.floor(P_H / B_ROWS);
    game.init();
    game.start();
});

