// Copyright 2009 Google Inc.  All Rights Reserved.

/**
 * @fileoverview Provides some functions for the Latitude intro page.
 * Some functions need jQuery.
 * @author jeremydw@google.com (Jeremy Weinstein)
 */

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


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


/**
 * Controls the screenshot tour.
 * @param {object} config An object containing configuration properties.
 * @constructor
 */
gweb.latitude.screenSwapper = function(config) {
  this.selectedScreen = config.selectedScreenNumber;
  this.screenshotEl = document.getElementById(config.screenshotId);
  this.showScreen(this.selectedScreen);
};


/**
 * Displays the specified screenshot and hides the currently selected one.
 * @param {number} sNumber The number-part of the screenshot's ID to show.
 */
gweb.latitude.screenSwapper.prototype.showScreen = function(sNumber) {
  document.getElementById('screen' + this.selectedScreen + 'Thumb').className =
      '';
  document.getElementById('screen' + sNumber + 'Thumb').className = 'selected';
  document.getElementById('screen' + this.selectedScreen + 'Caption').style.
      display = 'none';
  document.getElementById('screen' + sNumber + 'Caption').style.display =
      'block';
  document.getElementById('screenshotContainer' + this.selectedScreen).style.
      display = 'none';
  document.getElementById('screenshotContainer' + sNumber).style.display =
      'block';
  this.selectedScreen = sNumber;
};


/**
 * Gets a DC from cookie.
 * @return {string} Dc in cookie if present.
 */
gweb.latitude.getDcFromCookie = function() {
  var allcookies = document.cookie;
  var dcParamIndex = allcookies.indexOf('dc=');

  if (dcParamIndex != -1) {
    var dcValueIndex = dcParamIndex + 3;
    var dcValueEnd = allcookies.indexOf(';', dcValueIndex);

    if (dcValueEnd == -1) {
      dcValueEnd = allcookies.length;
    }
    return allcookies.substring(dcValueIndex, dcValueEnd);
  }
};


/**
 * Sets the DC in the cookie if it finds it in the hash.
 * Returns the DC from either the hash or the cookie.
 * @return {string} The DC from the hash or cookie or null.
 */
gweb.latitude.getDcFromHashOrCookie = function() {
  var hash = document.location.hash;

  if (this.getDcFromCookie()) {
    return this.getDcFromCookie();
  } else if (hash.length > 0) {
    var regex = new RegExp('dc=([^&]*)');
    var results = regex.exec(hash);

    if (results != null) {
      this.setDcInCookie(results[1]);
      return results[1];
    }
  } else {
    return null;
  }
};


/**
 * Sets the DC in the cookie.
 * @param {string} dcToSet The value of the DC to set.
 */
gweb.latitude.setDcInCookie = function(dcToSet) {
  var expiry = new Date();

  expiry.setTime(expiry.getTime() + (24 * 60 * 60 * 1000));
  document.cookie = 'dc=' + dcToSet + ';expires=' + expiry.toGMTString();
};


/**
 * Returns the DC from the cookie.
 * @return {string} The value of the DC from the cookie or null.
 */
gweb.latitude.getDcFromCookie = function() {
  var allcookies = document.cookie;
  var dcParamIndex = allcookies.indexOf('dc=');

  if (dcParamIndex != -1) {
    var dcValueIndex = dcParamIndex + 3;
    var dcValueEnd = allcookies.indexOf(';', dcValueIndex);

    if (dcValueEnd == -1) {
      dcValueEnd = allcookies.length;
    }
    return allcookies.substring(dcValueIndex, dcValueEnd);
  }
};



/**
 * Initializes the map on the Latitude landing page.
 * @param {number} goolatlong A comma-separated latlong.
 */
gweb.latitude.initializeMap = function(goolatlong) {
  var gooLat = goolatlong.split(', ')[0];
  var gooLong = goolatlong.split(', ')[1];
  var zoomLevel = 11;

  // Overwrite the default Lat, Long with geo-location if possible.
  if (google.loader.ClientLocation) {
    gooLat = google.loader.ClientLocation.latitude;
    gooLong = google.loader.ClientLocation.longitude;
  }
  else {
    zoomLevel = 12;
  }

  var apiKey = 'ABQIAAAAKZTsxo4ULxH5LT713MqXuhSsTL4WIgxhMZ0ZK_kHjwHeQuOD4xSP8_lZOQp2ev97Rezrm-WI_JWn2g';

  // Show the map.
  document.getElementById('screenBackdrop').style.backgroundImage =
    'url(\'' + 'http://maps.google.com/staticmap?' +
        'center=' + gooLat + ',' + gooLong +
        '&zoom=' + zoomLevel + '&size=290x320' +
        '&key=' + apiKey +
        '&sensor=true_or_false' +
        '\')';

  // Args: div object, how far down, how far left
  function setPos(objId, topNum, leftNum) {
    var markerId = document.getElementById(objId);
    markerId.style.top = topNum + 'px';
    markerId.style.left = leftNum + 'px';
  }

  var randTop = 20 + Math.ceil( Math.random() * 80 );
  var randLeft = 10 + Math.ceil( Math.random() * 210 );
  setPos('mark1', randTop, randLeft);

  var randTop = 60 + Math.ceil( Math.random() * 80 );
  var randLeft = 10 + Math.ceil( Math.random() * 50 );
  setPos('mark2', randTop, randLeft);

  var randTop = 60 + Math.ceil( Math.random() * 80 );
  var randLeft = 100 + Math.ceil( Math.random() * 90 );
  setPos('mark3', randTop, randLeft);
};


/**
 * Initializes the DCs on the page links and in the stp.
 */
gweb.latitude.initializeDCs = function() {
  jQuery.each(jQuery('a'), function() {
    if (gweb.latitude.getDcFromHashOrCookie()) {
      var dc = gweb.latitude.getDcFromCookie();
    } else {
      var dc = 'lato';
    }
    this.href = this.href.replace('dc=[dc]', 'dc=' + dc);
    var stpval = jQuery('#stp-form input[name=text]').val();
    jQuery('#stp-form input[name=text]').val(stpval.replace('[dc]', dc));
  });
};

