google.load('search', '1', {"nocss" : true, "nooldnames" : true});

var imageSearch;

function searchComplete() {
  if (imageSearch.results && imageSearch.results.length > 0) {
    imageSearch.aBoard.images = [];

    var results = imageSearch.results;
    for (var i = 0; i < results.length; i++) {
      var result = results[i];
      imageSearch.aBoard.images.push(result.tbUrl);
    }

    nextImage();
  }
}

function nextImage() {
  if (imageSearch.aBoard.images && imageSearch.aBoard.images.length > 0) {
    targetSrc(imageSearch.aBoard.images.shift());
  } else {
    targetSrc("/static/images/spinner.gif");
    imageSearch.gotoPage(imageSearch.cursor.currentPageIndex + 1);
  }
}

function targetSrc(url) {
  if (url != "none") { url = 'url(' + url + ')'; }
  imageSearch.aBoard.style.backgroundImage = url;
}

function hammer(evt) {
  hideMessage();
  if (window.animating || $('smash').style.display == "block") { return; }
  window.animating = true;

  var target = $('target');
  target.setStyle({
    width: "147px",
    top: '492px',
    left: '47px'
  });

  setTimeout(
    function () {
      target.setStyle({
        width: "153px",
        top: '490px',
        left: '44px'
      });
    }, 
    100
  );

  new Effect.Move(
    'bell',
    {
      y: -274,
      duration: 0.4,
      transition: Effect.Transitions.sinoidal,
      afterFinish: function () {
        $('smash').style.display = "block";
        Effect.Shake('container', {duration: 0.2, distance: 5});
        new Effect.Move(
          'bell', 
          {duration: 0.4, y: 10, transition: Effect.Transitions.spring}
        );
        window.animating = false;
        setTimeout(reset, 750);
      }
    }
  );

  stopEvent(evt);
}

function reset(evt) {
  hideMessage();
  if (window.animating || $('smash').style.display == "none") { return; }
  window.animating = true;

  targetSrc('none');
  $('smash').hide();
  new Effect.Move(
    'bell',
    {
      y: 242,
      duration: 0.2,
      afterFinish: function () {
        new Effect.Move(
          'bell', 
          { 
            duration: 0.1,
            y: 22, 
            transition: Effect.Transitions.spring,
            afterFinish: function () { window.animating = false; nextImage(); }
          }
        );
      }
    }
  );

  stopEvent(evt);
}

function showMessage(evt) {
  $('message').show();
  stopEvent(evt);
}

function hideMessage() {
  $('message').hide();
}

function stopEvent(evt) {
  if (!evt) {
    return;
  }
  Event.stop(evt);
}

function setup() {
  if (typeof annoyance == 'undefined') { return; }
  imageSearch = new google.search.ImageSearch();
  imageSearch.setSearchCompleteCallback(this, searchComplete, null);
  imageSearch.execute(annoyance);

  imageSearch.aBoard = $('annoyingCntr');
  imageSearch.aBoard.onclick = showMessage;
  $('target').onclick = hammer;
  document.body.onclick = reset;
}

google.setOnLoadCallback(setup);


/* IE PNG SUPPORT */
if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
  var bIE = true;
  document.writeln('<style type="text/css">img { visibility:hidden; }</style>');
  window.attachEvent("onload", fnLoadPngs);
}

function fnLoadPngs() {
  var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
  var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

  for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
    if (itsAllGood && img.src.match(/\.png$/i) != null) {
      var src = img.src;
      var div = document.createElement("DIV");
      div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizing='scale')"
      div.style.width = img.width + "px";
      div.style.height = img.height + "px";
      div.id = img.id;
      div.className = img.className;
      img.replaceNode(div);
     }
     img.style.visibility = "visible";
   }
}

