
RA.begin.push(function() {
    RA.textEvent = ( $.browser.mozilla ? 'text' : 'keyup' );
});

RA.ready.push(function() {

    /* Show the hidden element for a browser not supported script */
    $('.ajax').show();

    /* Swap the not found icon images */
    $('img[src*=apple]').error(function() {
        this.src = '/image/icon/iphone.png';
    }).each(function() {
        this.src = this.src;
    });

    $('.togglable .toggle').click(function() {
        $(this).closest('.toggled').find('.togglable').toggle();
    });

    var $search = $('#header form.search'),
        $query  = $search.find('input[type=text]'),
        search = $search.find('input[type=submit]').val();

    if ($query.val() == '') {
        $query.addClass('cCCC').val(search);
    }
    $query.one('focus', function() {
        if ($(this).hasClass('cCCC')) {
            $(this).removeClass('cCCC').val('');
        }
    });

    $('a[href^="http://"]').attr('target', '_blank');
    $('a[href^="http://click.linksynergy.com/"]').attr('target', 'itunes');
    $('input[type=text]').addClass('b1C');
    $('input[type=password]').addClass('b1C');

    /* Add a decoration to the showing page's link */
    $('#header ul a').each(function() {
        if ( $(this).attr('href') == location.pathname ) {
            $(this).css('text-decoration', 'underline');
        }
    });

});

RA.ready.push(function(dst) {

  // download button
  $(dst).find('button.download').click(function() {
    var uri = 'http://click.linksynergy.com/fs-bin/stat?id=T6VLfYuUqlQ&offerid=94348&type=3&subid=0&tmpid=2192&RD_PARM1=http%253A%252F%252Fitunes.apple.com%252FWebObjects%252FMZStore.woa%252Fwa%252FviewSoftware%253Fid%253D' + this.value + '%2526mt%253D8%2526uo%253D6%2526partnerId%253D30';
    location.href = uri;
  });

  // tweet button
  $(dst).find('button.tweet').click(function() {
    var uri = '/home/apps/edit?software_id=' + this.value;
    location.href = uri;
  });

  $('#login input[name="email"]').trigger('focus');

  $('#login input:radio').click(function() {
      var $form  = $(this).closest('form'),
          label = $(this).closest('label').attr('class');

      $form.attr('action', $(this).val());

      $form.find(':submit').each(function() {
          var $this = $(this);
          $this.hasClass(label) ? $this.show() : $this.hide();
      });
  });

  // tweet button
  $(dst).find('.software #tweetText').focus(function() {
    var softwareId = $('#tweet :input[name="software_id"]').val(),
        uri = '/home/apps/edit?software_id=' + softwareId;
    location.href = uri;
  });
  $(dst).find('.software #tweet').submit(function() {
    var softwareId = $('#tweet :input[name="software_id"]').val(),
        uri = '/home/apps/edit?software_id=' + softwareId;
    location.href = uri;
    return false;
  });

  $(dst).find('.timeline a.like').click(function() {
    var $user = $('#user'),
        userId = $user.find('.id').text(),
        nickname = $user.find('.nickname').text();
    var $like = $(this).closest('.tweet').next();

    $.ajax({
      type: 'POST',
      url: this.href,
      success: function(data) {
        if ( $(data).find('form[action*=login]').size() ) {
          location.href = '/user/login?required';
        }
        else {
          var likeId = data.match(/\d+/);
          if ( !likeId ) return;
          $like.show();
          var unlike = '<li><a class="nickname" href="/user/' + userId + '/">' + nickname + '</a><a class="unlike" href="/home/like/remove?like_id=' + likeId + '">&#xD7</a></li>';
          $like.find('ul').prepend(unlike);
          RA.run_ready($like);
        }
      },
    });

    return false;
  });

  $(dst).find('a.unlike').click(function() {
    var $li = $(this).parent();
    $.ajax({
      type: 'POST',
      url: this.href,
      success: function(data) {
        if ( $(data).find('form[action*=login]').size() ) {
          location.href = '/user/login?required';
        }
        else {
          var likeId = data.match(/\d+/);
          if ( !likeId ) return;
          if ( $li.parent().children().size() == 1 ) {
            $li.closest('.like').hide();
          }
          $li.remove();
        }
      }
    });
    return false;
  });

  $(dst).find('.appstore.ranking.rows').each(function() {
    RA.utils.autoPager('div.appstore.ranking.rows');
  });

  $(dst).find('.software.search.result').each(function() {
    RA.utils.autoPager('div.software.search.result');
  });

  RA.dispatch('^/software\?', function() {
    RA.utils.autoPager('div.software.root.rows');

    $('form select[name="order"]').change(function() {
      this.form.submit();
    });
  });


  RA.dispatch('^/software/tag', function() {
    RA.utils.autoPager('div.software.tag.rows');
  });

  RA.dispatch('.', function() {
    $(dst).find('.software.rating a').click(function() {
      var $this = $(this);
      $.ajax({
        type: 'POST',
        url: this.href,
        success: function(data) {
            var voteId = data.match(/\d+/);
            if ( !voteId ) return;
            $this.find('span').incr();
        },
      });
      return false;
    });
  });

  /* Google Analytics */
  try {
      var pageTracker = _gat._getTracker('UA-10341120-2');
      pageTracker._trackPageview();
  }
  catch(e) {
  }
});

