// Author: Todd Markelz
//
// Common JavaScript functions for desktop website.

/**
 * Redirect to correct site based on user platform
 */
function platformDetect(){
  if (String(window.location).indexOf("platform=win") != -1) {
    return;
  } else if (String(navigator.platform).toLowerCase().indexOf('mac') == 0) {
    // Mac site
    window.location.pathname = "/mac" + location.pathname;
  }
}

function intlPlatformDetect(){
  if (String(navigator.platform).toLowerCase().indexOf('mac') == 0) {
    if (document.images) {
      window.location.replace("http://desktop.google.com/mac/index.html");
    } else {
      location.href="http://desktop.google.com/mac/index.html";	
    }
  }
}

/**
 * Download the Google Desktop exe or zip from dl server
 * @param hl -- A 2 to 5 character lanuage code followed by a version number
 * where version number is (0 - win2K, 1- release, 2 - beta)
 * @param ent -- Boolean, control download of enterprise edition
*/
function download() {
  var args = download.arguments;
  var hl = args[0];
  var ent = args[1];
  var b = false;
  var win2k = false;

  if (hl.lastIndexOf("2") != -1) {
    b = true;
    hl = hl.substr(0,  hl.lastIndexOf("2"));
  } else if (hl.lastIndexOf("0") != -1) {
    hl = hl.substr(0,  hl.lastIndexOf("0"));
  }

  // We sometimes get an empty hl parameter.
  if (hl == "") {
    hl = "en";
  }

  adwordsTracking(hl);

  // 'zh' is actually 'zh_CN'
  if (hl == "zh") {
    hl = "zh_CN";
  }
  var location='http://dl.google.com/dl/desktop/';  
  if (b) {
    location += 'R/' + hl;
  } else if (win2k){
    location += 'v5.7/' + hl;
  } else {
    location += 'L/' + hl;
  }

  if (ent) {
    location += '/GoogleDesktopEnterprise.zip';
  } else {
    location += '/GoogleDesktopSetup.exe';
  }
  top.location=location;
}

/**
 * Download the Google Desktop exe for Mac
 */
function downloadMac(hl) {
  adwordsTracking(hl);
  setTimeout("top.location='/" + hl + "/mac/thankyou_mac.html'", 4000);
  top.location='https://www.google.com/dl/mac/install/GoogleDesktop.dmg'; 
}


/**
 * Trigger AdWords conversion tracking
 * @param hl -- Language
*/
function adwordsTracking(hl) {
  if (document.images) {
    // Load images for tracking purposes
    var u = "http://www.googleadservices.com/pagead/conversion";
    var u1 = u + "/1070361158/?value=1.0&label=Acquista&script=0&hl=" + hl;
    var u2 = u + "/1070587976/?script=0&hl=" + hl;
    (new Image()).src = u1;
    (new Image()).src = u2;
  }
}

/**
 * Redirect user to target while passing on the full querystring
 * @param target -- URL to redirect user to
*/
function trackingRedirect(target) {
  var qs = '';
  var url = String(window.location);
  var index = url.search(/[\#\?]/);
  if (index > 0) {
    qs = url.substring(index);
  }
  
  if (document.images) {
    location.replace(target+qs);
  } else {
    location.href = target+qs;
  }
}


/**
 * If refer comes from pack, or 'refer=pack' set in querystring hide
 * download button.
 * @param arr -- Array of element ids to hide
 */
function packInit(arr) {
  if (((document.referrer.indexOf("http://pack.google.com") != -1) ||
       (String(window.location).indexOf("refer=pack") != -1)) &&
       (document.getElementById)) {
    var l = arr.length;
    for (var i = 0; i < l; i++) {
      document.getElementById(arr[i]).style.display = 'none';
    }
  }
}

/**
 * Parse URL and return value of parameter 'param' from query string
 */
function getParam(param) {
  var regexStr = "[\\?&]"+param+"=([^&#]*)";
  var regex = new RegExp(regexStr);
  var matches = regex.exec(window.location.href);
  return (!matches)? "" : matches[1];
}
 
 /** 
  * Set a cookie for this site called 'name'  
  * with value 'val' that expires in 'days' days. 
  */
function setCookie(name, val, days) {
  var expDate = new Date();
  expDate.setDate(expDate.getDate() + days);
  document.cookie=name+ "=" +escape(val)+ ((days==null)? "" : ";expires=" + expDate.toGMTString());
}
  
  
/**
 * Parse utm_medium promo code and set corresponding brand code cookie 'gd-brand'
 */
function setBrandCodes() {
  var promoCode = getParam("utm_medium");
  var marketingCodes = {
    hpp:"GZHZ", 
    rpp:"GZRZ", 
    ha:"GZAZ", 
    ysm:"GZYZ", 
    ref:"GZFZ", 
    com:"GZZZ", 
    abt:"GZZZ",
    et:"GZEZ",
    cpp:"GZXZ",
    eml:"GZZZ",
    oa:"GZMZ",
    dm:"GZZZ",
    pa:"GZZZ"
  };
  
  var brandCode = marketingCodes[promoCode];	
  if (brandCode) setCookie("gd-brand", brandCode, 365);
}
													 
  
/**
 * Lists blog entries from the specified JSON feed
 * by creating a new 'ul' element in the DOM.  Each
 * bullet is a linked news item. The "Latest version" 
 * item is parsed and displayed last.
 *
 * @param {JSON} json is the JSON object pulled from the Blogger service.
 */
function listEntries(json) {
  var list = document.createElement('ul')
  list.style.marginLeft = "0px";
  list.style.paddingLeft= "0px";
  
  var version = "";
  
  for (var i = 0; i < json.feed.entry.length; i++) { 
    var entry = json.feed.entry[i];
    var li = document.createElement('li');
	
    // If the current item is the version save it as a txt string
    // Otherwise save and append to list as a link
    if (entry.category && entry.category[0].term == "version") {
      version = document.createTextNode(entry.title.$t);
    } else {
      var a = document.createElement('a');
      a.href = entry.link[1].href;
      var txt = document.createTextNode(entry.title.$t);
      a.appendChild(txt);
      li.appendChild(a);
      li.style.paddingBottom="2px";
      list.appendChild(li);
    }

  }
  
  // Append the version as the last list item
  var li = document.createElement('li');
  li.style.paddingBottom="2px";
  li.appendChild(version);
  list.appendChild(li);
  
  var title = document.createElement('b');
  title.appendChild(document.createTextNode("News"));
  var news = document.getElementById('news');
  news.appendChild(title);
  news.appendChild(list);
  news.className="notice"
}


/**
 * Downloads correct binary for GDLinux based on URL param
 * deb = Debian, rpm=Red Hat/Fedora/Suse/Mandriva
 * Default if no param specified is deb
 */

function downloadLinux() {
   platform=getParam('dl');
   if (platform=="deb") {
      top.location="http://dl.google.com/linux/deb/pool/non-free/g/google-desktop-linux/google-desktop-linux_current_i386.deb";
   } else if (platform=="rpm") {
      top.location="http://dl.google.com/linux/rpm/stable/i386/google-desktop-linux-current.rpm";
   } else if (platform=="deb64") {
      top.location="http://dl.google.com/linux/deb/pool/non-free/g/google-desktop-linux/google-desktop-linux_current_amd64.deb";
   } else if (platform=="rpm64") {
      top.location="http://dl.google.com/linux/rpm/stable/x86_64/google-desktop-linux-current.rpm";
   } else {
      top.location="http://dl.google.com/linux/deb/pool/non-free/g/google-desktop-linux/google-desktop-linux_current_i386.deb";
   }
}
