(function($){
  var filterSelect = false;

  // OnLoad:
  $(window).bind('load', function() {
    var selector = $('#productselector');

    // Tabs
    selector.find('ul.sections li').each(function(i) {
      var id = 'tabs_';
      if      (i==0) id += 'devices';
      else if (i==1) id += 'support';
      else if (i==2) id += 'accessories';

      var link = $(this).find('a');
      link.unbind('mousedown')
      link.mousedown(function() { trackLink(id); });
    });

    // View tabs
    var viewLinks = selector.find('ul.views a');
    viewLinks.unbind('mousedown');
    viewLinks.mousedown(function() {
      var id = 'view_' + $(this).text().toLowerCase().replace(/[^\w\d]+/g, '_');
      trackLink(id);
    });

    // Products
    var productLinks = selector.find('div.products a');
    productLinks.unbind('mousedown');
    productLinks.mousedown(function() {
      var id;
      var link = $(this);
      var title = link.attr('title').replace('_96x96', '').toLowerCase();

      if (!title) return;

      if (link.parents('div.products.mostwanted').length && link.parent().hasClass('mostwanted'))
        id = 'topdevices_' + title;
      else if (link.parents('div.products.thumbnails').length)
        id = 'image_' + title;
      else if (link.parents('div.products.services').length)
        id = 'text_' + link.text().toLowerCase().replace(/[^\w\d]+/g, '_');
      else
        id = 'text_' + title;

      trackLink(id);
    });

    // Top Devices 'Show all' link
    var showAllLink = selector.find('p.showall a');
    showAllLink.unbind('mousedown');
    showAllLink.mousedown(function() {
      var id = 'topdevices_showall';
      trackLink(id);
    });

    // Filter
    var filter = selector.find('div.types-nav');
    var filterOpenClose = filter.find('h3:first');
    filterOpenClose.unbind('mousedown');
    filterOpenClose.mousedown(function() {
      var id = 'filter_' + (($(this).hasClass('expanded')) ? 'close' : 'open');
      trackLink(id);
    });
    filter.find('ul.selection li').each(function() {
      var id = 'filter_' + this.className;
      var link = $(this).find('a');
      link.unbind('mousedown');
      link.mousedown(function() {
        filterSelect = true;
        trackLink(id);
        setTimeout(function() {
          filterSelect = false;
        }, 50);
      });
    });
    var filterCheckboxes = filter.find('ul.types a');
    filterCheckboxes.unbind('mousedown');
    filterCheckboxes.mousedown(function() {
      if (filterSelect) return;

      var li = $(this).parent();
      var id = 'filter_';
      id += li.hasClass('checked') ? 'uncheck_' : 'check_';
      id += li.get(0).className.replace(/^.*type_(\w+).*$/, '$1');
      trackLink(id);
    });

    // Paginator
    var pagAll = selector.find('p.all a');
    pagAll.unbind('mousedown');
    pagAll.mousedown(function() { trackLink('pag_all'); });
    var pagPrev = selector.find('div.paginator li.prev a');
    pagPrev.unbind('mousedown');
    pagAll.mousedown(function() { trackLink('pag_prev'); });
    var pagNext = selector.find('div.paginator li.next a');
    pagNext.unbind('mousedown');
    pagNext.mousedown(function() { trackLink('pag_next'); });

    // Teasers
    $('#s_teasers div.teaser').each(function() {
      var id = 'teaser_' + this.className.replace(/.*teaser (\w+).*/, '$1');
      var link = $(this).find('a');
      link.unbind('mousedown');
      link.mousedown(function() { trackLink(id); });
    });

    var popular = $('#s_most-popular');

    // Most popular Tabs
    var popularTabs = popular.find('ul.nav a');
    popularTabs.unbind('mousedown');
    popularTabs.mousedown(function() {
      var id = this.href.replace(/^.*#popular/, 'pop_tabs');
      trackLink(id);
    });

    // Most popular Links
    popular.find('div.content').each(function() {
      var groupid = this.id.replace('popular_', 'pop_') + '_';
      var link = $(this).find('a');
      link.unbind('mousedown');
      link.mousedown(function() {
        var link = $(this);
        var title = link.find('span.title');
        var id = groupid + title.text().toLowerCase().replace(/[^\w\d]+/g, '_');
        trackLink(id);
        return false;
      });
    });
  });

  var debug = true;
  if (typeof _hbLink == 'function') debug = false;

  var trackLink = function(id) {
    if (debug && (typeof console != "undefined") && (typeof console.log == "function")) console.log('Tracking: '+id);
    else {
      _hbLink(id, '', '');
      _hbHalt();
    }
  };

}(jQuery));
