var netcomFlashManagerCount = 0;

function NetcomFlashManager()
{
  // Increment global count of how many instances have been created
  netcomFlashManagerCount++;

  // Set up class properties
  this.flashMovies = new Array();
  this.repeatDefault = 0;
  this.divForFlashMovie = "tmpDivId";
  this.cookieName = "reliantNetcomVisits" + netcomFlashManagerCount;
  this.daysCookieShouldLive = 1000;
  this.debug = false;
}

/* Function: AddMovie - adds a movie to the list of movies to play. */
/* Repeat argument is optional - if not specified, repeatDefault will be used. */
NetcomFlashManager.prototype.AddMovie = function(name, width, height, scale, wmode, repeat)
{
  if(!repeat) repeat = this.repeatDefault;
  var flashMovie = {'name':name, 'width':width, 'height':height, 'scale':scale, 'wmode':wmode, 'repeat':repeat};
  this.flashMovies.push(flashMovie);
}

/* Function: RunFlash - finds which flash movie to play and plays it */
NetcomFlashManager.prototype.RunFlash = function()
{
  var playMovie = null;

  /* Get the current value of the counter - set it to zero if it's not set */
  currentCookieValue = readCookie(this.cookieName);
  if(currentCookieValue != null) // We already have at least one visit
  {
    /* Split the cookie into individual values */
    currentCookieValues = currentCookieValue.split("|");
    currentCounterValue = parseInt(currentCookieValues[0]);
    currentMovieValue = parseInt(currentCookieValues[1]);

    /* Increment counter */
    currentCounterValue++;

    /* Reset the counter if deemed necessary by the repeat value */
    if(this.flashMovies[currentMovieValue]['repeat'] > 0)
    {
      if(currentCounterValue >= this.flashMovies[currentMovieValue]['repeat'])
      {
        currentCounterValue = 0;
      }
    }

    /* Decide which movie to play based on the counter value */
    if(currentCounterValue == 0 && this.flashMovies[currentMovieValue]['repeat'] > 0)
    {
      currentMovieValue++;
      if(currentMovieValue >= (this.flashMovies.length))
      {
        currentMovieValue = 0;
      }
    }
  }
  else // There was no cookie, so start with no count and the first movie
  {
    currentCounterValue = 0;
    currentMovieValue = 0;
  }
  
  /* Get the movie from the array */
  playMovie = this.flashMovies[currentMovieValue];
  
  /* Print debug information if debug is true */
  if(this.debug) document.writeln("Count: " + currentCounterValue + "; Movie: " + playMovie["name"] + "; Width: " + playMovie["width"] + "; Height: " + playMovie["height"] + "; Scale: " + playMovie["scale"] + "; WMode: " + playMovie["wmode"] + "; Repeat: " + playMovie["repeat"] + "<br />");

  /* Set the cookie for the current counter value plus one */
  var date = new Date();
  date.setTime(date.getTime()+(this.daysCookieShouldLive*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();
  document.cookie = this.cookieName+"="+currentCounterValue+"|"+currentMovieValue+expires+"; path=/";

  /* Instruct UFO to create a new flash movie */
  var reliantFO = { movie:playMovie["name"], width:playMovie["width"], height:playMovie["height"], scale:playMovie["scale"], majorversion:"8", build:"0", xi:"true", wmode:playMovie["wmode"] };
  reliantUFO.create(reliantFO, this.divForFlashMovie);
}

/* A function that returns the value of a specific cookie, or null if that cookie is not set */
function readCookie(name) {
	name = name + "=";
	var cookieArray = document.cookie.split(';');
	for(var i=0;i < cookieArray.length;i++) {
		var cookie = cookieArray[i];
		while (cookie.charAt(0)==' ') cookie = cookie.substring(1,cookie.length);
		if (cookie.indexOf(name) == 0) return cookie.substring(name.length,cookie.length);
	}
	return null;
}


