// Copyright 2010 Google Inc. All Rights Reserved.

/**
 * @fileoverview DoubleClick pixel inclusion to track GA users on homepage.
 * @author mrigoli@google.com (Mike Rigoli)
 */

/**
 * The readCookie function iterates through the users cookies and tries to find
 * a match to what is supplied in the function call.
 * @param {string} cookieName is supplied by the function call, it is the
 *     cookie name we are hoping to match.
 * @return {string} if cookie is found to match then the cookie name is
 *     returned.
 */
function readCookie(cookieName) {
  var nameEqual = cookieName + '=';
  var cookieArr = document.cookie.split(';');
  for (var i = 0; i < cookieArr.length; i++) {
    var gaCookie = cookieArr[i];
    while (gaCookie.charAt(0) == ' ') {
      gaCookie = gaCookie.substring(1, gaCookie.length);
      if (gaCookie.indexOf(nameEqual) == 0) {
        return gaCookie.substring(nameEqual.length, gaCookie.length);
      }
    }
  }
  return null;
}

/**
 * The showDoubleClickIframe uses readCookie function to see if the user has a
 * Google Analytics cookie. If the user does not have a GA cookie the
 * function creates a random number and builds HTML for an iframe to be
 * displayed in the Analytics home page.
 */
function showDoubleClickIframe() {
  if (!readCookie('AnalyticsUserLocale') && !readCookie('SID')) {
    var axel = Math.random() + '';
    var a = axel * 10000000000000;
    var dcIframe = '<iframe src="http://fls.doubleclick.net/activityi';
    dcIframe += 'src=2507573;type=analy454;cat=analy301;ord=' + a;
    dcIframe += '?" class="ga-doubleclick-iframe" frameborder="0">';
    dcIframe += '</iframe>';
    document.getElementById('dc-iframe-container').innerHTML = dcIframe;
  }
}

