﻿function recordOutboundLink(link, category, action) {
  /*try {
  var pageTracker = _gat._getTracker("UA-2001365-2");
  pageTracker._trackEvent(category, action);
  setTimeout('document.location = "' + link.href + '"', 100)
  } catch (err) { }*/
}
function getQueryString(url) {
  return url.substring(url.indexOf('?'));
}
function decode(s) {
  try {
    return decodeURIComponent(s).replace(/\r\n|\r|\n/g, "\r\n");
  } catch (e) {
    return "";
  }
}
function getQueryStringAsJSON(url) {
  var qs = getQueryString(url);
  var multimap = {};
  if (qs.length > 1) {
    qs = qs.substr(1);
    qs.replace(/([^=&]+)=([^&]*)/g, function(match, hfname, hfvalue) {
      var name = decode(hfname);
      var value = decode(hfvalue);
      if (name.length > 0) {
        if (!multimap.hasOwnProperty(name)) {
          multimap[name] = [];
        }
        multimap[name].push(value);
      }
    });
  }
  return multimap;
}

function callFloodlight(category, type, isUnique) {
  type = type || 'whirl781';
  isUnique = isUnique || false;
  var seed = Math.random() + "";
  var ord = (isUnique) ? 1 : seed * 10000000000000;
  var tag_url = "http://fls.doubleclick.net/activityi;src=2625291;type=" + type + ";cat=" + category + ";ord=" + ord + ";num=" + seed + "?";
  if ($('#DCLK_FLDiv').length == 0) {
    $('<div></div>')
            .attr('id', 'DCLK_FLDiv')
            .css('display', 'none')
            .appendTo('body');
  }
  $('<iframe></iframe>')
        .attr('id', 'DCLK_FLIframe_' + Math.floor(Math.random() * 999999))
        .attr('src', tag_url)
        .appendTo('#DCLK_FLDiv');
}

function outboundDC(event, url) {
  event.preventDefault();
  event.stopImmediatePropagation();
  var qs = getQueryStringAsJSON(url);
  var category = qs['category'];
  var type = (qs['type']) ? qs['type'] : null;
  var unique = (qs['unique']) ? qs['unique'] : false;
  var redirect = (qs['out']) ? qs['out'] : url;
  callFloodlight(category, type, unique);
  setTimeout('(function() { document.location = \'' + redirect + '\'; })();', 500);
}

$(document).ready(function() {
  var tabContainers = $('#tabs > div');
  tabContainers.hide().filter(':first').show();
  $('#tabs ul li a:not(#support_tab a)').click(function() {
    tabContainers.hide();
    tabContainers.filter(this.hash).show();
    $('#tabs ul li a').removeClass('selected');
    $(this).addClass('selected');
    return false;
  }).filter(':first').click();

  $('a[href^="http"]:not([href*="' + document.domain + '"]),a[href^="/redirecting.aspx"]').click(function(e) {
    e.preventDefault();
    category = 'Outbound Links';
    if ($(this).hasClass('installation')) category = 'Installation';
    _gaq.push(['_trackEvent', category, 'Click', $(this).attr('href')]);

    var qs = getQueryStringAsJSON($(this).attr('href'));
    var url = (qs['out']) ? qs['out'] : $(this).attr('href');
    if (!$(this).hasClass('dc_track') &&
        !$(this).hasClass('buy_it_now') &&
        !$(this).hasClass('professional-installation')) {
      setTimeout('(function() { document.location = \'' + url + '\'; })();', 100);
    }
  });

  $('.dc_track').click(function(e) { outboundDC(e, $(this).attr('href')); });

  $('ul.printemail li.print a').click(function(e) {
    e.preventDefault();
    callFloodlight('whpwa913', 'whirl480');
    window.print();
  });

  $('ul.printemail li.email a').click(function() { callFloodlight('whpwa470', 'whirl480'); });

  $('.registration-write-a-review').click(function() {
    _gaq.push(['_trackEvent', 'Review Product Link', 'Click', 'Warranty Registration Thank You Page']);
    setTimeout('(function() { document.location = \'' + $(this).attr('href') + '\'; })();', 100);
  });
});
