// Copyright 2009 Google Inc.  All Rights Reserved.

/**
 * @fileoverview  Provides object that checks to see if current user agent
 * is a mobile device. Also provides two helper methods that will help handle
 * common functions for client-side mobile device detection. You can auto-
 * redirect for certain mobile devices, and insert a clickable link at the top
 * of the page for possible mobile devices.
 * @author  Jeremy Weinstein
 */


/**
 * Initializes gweb namespace.
 */
var gweb = gweb || {};


/**
 * Initializes gweb.mobile namespace.
 */
gweb.mobile = gweb.mobile || {};


/**
 * Initializes gweb.mobile.userAgent object.
 * @constructor
 */
gweb.mobile.userAgent = function() {
  this.userAgent = navigator.userAgent.toLowerCase();
  this.agentStatus = this.isMobileDevice(this.userAgent);
  this.isHTML5Device = this.detectForHTML5Device(this.userAgent);
};


/**
 * A list of HTML5 user agents.
 * @type {list}
 */
gweb.mobile.userAgent.HTML5_MOBILE_AGENTS = [
  'iphone',
  'android'
];


/**
 * A list of certain mobile user agents.
 * @type {list}
 */
gweb.mobile.userAgent.CERTAIN_MOBILE_AGENTS = [
  'midp',
  '240x320',
  'blackberry',
  'netfront',
  'nokia',
  'panasonic',
  'portalmmm',
  'sharp',
  'sie-',
  'sonyericsson',
  'symbian',
  'windows ce',
  'benq',
  'mda',
  'mot-',
  'opera mini',
  'philips',
  'pocket pc',
  'sagem',
  'samsung',
  'sda',
  'sgh-',
  'vodafone',
  'xda',
  'iphone',
  'android'
];


/**
 * A list of possible mobile user agents.
 * @type {list}
 */
gweb.mobile.userAgent.POSSIBLE_MOBILE_AGENTS = [
  'opera'
];


/**
 * Returns a status code depending on the user agent.
 * @return {number} 0 if not a mobile device. 1 if certain mobile device. 2 if
 *     possible mobile device.
 */
gweb.mobile.userAgent.prototype.isMobileDevice = function() {
  for (var i = 0; i < gweb.mobile.userAgent.CERTAIN_MOBILE_AGENTS.length;
      i++) {
    if (this.userAgent.indexOf(gweb.mobile.userAgent.
        CERTAIN_MOBILE_AGENTS[i]) != -1) {
      return 1;
    }
  }

  for (var i = 0; i < gweb.mobile.userAgent.POSSIBLE_MOBILE_AGENTS.length;
      i++) {
    if (this.userAgent.indexOf(gweb.mobile.userAgent.
        POSSIBLE_MOBILE_AGENTS[i]) != -1) {
      return 2;
    }
  }

  return 0;
};


/**
 * Checks to see if the device is HTML5-capable.
 * @return {boolean} Whether or not the user agent is HTML5-capable.
 */
gweb.mobile.userAgent.prototype.detectForHTML5Device = function() {
  for (var i = 0; i < gweb.mobile.userAgent.HTML5_MOBILE_AGENTS.length;
      i++) {
    if (this.userAgent.indexOf(gweb.mobile.userAgent.
        HTML5_MOBILE_AGENTS[i]) != -1) {
      return true;
    }
  }

  return false;
};


/**
 * Redirects the page if it's a certain mobile device.
 * @param {string} url The url to redirect to.
 */
gweb.mobile.userAgent.prototype.redirectForCertainMobileDevice =
    function(url) {
  if (this.agentStatus == 1) {
    window.location = url;
  }
};


/**
 * Redirects the page if it's a certain mobile device.
 * @param {string} url The url to redirect to.
 */
gweb.mobile.userAgent.prototype.redirectForHTML5Device = function(url) {
  if (this.isHTML5Device) {
    window.location = url;
  }
};


/**
 * Adds some HTML to the top of the page if it's a possible mobile device.
 * Requires body tag.
 * @param {string} html HTML to add to the top of the page.
 */
gweb.mobile.userAgent.prototype.prefixPageForPossibleMobileDevice =
    function(html) {
  if (this.agentStatus == 2) {
    document.body.innerHTML = html + document.body.innerHTML;
  }
};


/**
 * Adds some HTML to the top of the page if it's a certain or possible
 * mobile device.
 * Requires body tag.
 * @param {string} html HTML to add to the top of the page.
 */
gweb.mobile.userAgent.prototype.prefixPageForCertainMobileDevice =
    function(html) {
  if (this.agentStatus == 1 || this.agentStatus == 2) {
    document.body.innerHTML = html + document.body.innerHTML;
  }
};
