//
// NOTE: ANY CHANGES MADE TO THIS FILE ALSO NEED TO BE MADE TO js2/TALSO.js
//

var footprint_id  = 'FC';

var TALSO =
{
	init: function(footprint_id)
	{
		this.footprint_id  		= footprint_id;
		this.footprint_able  = false;
		this.footprint  		= null;
		this.flash_is_ready();
		if(!this.is_able())
		{
		   return;
		}
	},

	flash_is_ready: function()
	{	    
		if(!document.getElementById || !document.getElementById(this.footprint_id)) return;
		if(!this.get_movie(this.footprint_id)) return;
		this.footprint_able  = this.footprint.f_cookie_able();
	},

	is_able: function()
	{
		return this.footprint_able;
	},

	del: function(key)
	{
		if(!this.is_able()) return;
		this.footprint.f_delete_cookie(key);
	},

	get: function(key)
	{
		if(!this.is_able()) return '';
		var ret = this.footprint.f_get_cookie(key);
		return ((ret == 'null' || ret == 'undefined') ? '' : ret);
	},

	set: function(key,val)
	{
		if(!this.is_able()) return;
		this.footprint.f_set_cookie(key,val);
	},

	get_movie: function()
	{
    	this.footprint = window[this.footprint_id];
    	if (!this.footprint)
    	{
            this.footprint =  document[this.footprint_id];
    	}
    	return ((this.footprint) ? true : false);
	},

    // try to get value - if it's not set, set it to new value before getting
    // on failure to set, throws error
    get_with_set: function(key, newVal)
    {
        var currentVal = this.get(key);

        // set to new value if not set
        if (currentVal == '')
        {
            this.set(key, newVal);
            currentVal = this.get(key);

            // verify that it got set properly
            if (currentVal != newVal) {
              throw "Failed to set";
            }

        }

        return currentVal;
    }


};

// callback from the flash file to indicate it is ready
function flash_ready()
{
    if (!document.getElementById(footprint_id))
	{
	   footprint_id = 'FCIE';
	   window.attachEvent("onbeforeunload",forceFlashUnload); //only for IE
	}
	TALSO.init(footprint_id);
}

// initialize global variables
var pluginFound = false;

function detectFlash() 
{
    pluginFound = detectPlugin('Shockwave','Flash'); 
    if(!pluginFound) 
    {
    	try {
		  axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.8");
		  pluginFound = true;
	    } catch (e) {}
    }
    return pluginFound;
}

function detectPlugin() 
{
  var daPlugins = detectPlugin.arguments;
  var pluginFound = false;
  if (navigator.plugins && navigator.plugins.length > 0) 
  {
	var pluginsArrayLength = navigator.plugins.length;
	for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) 
	{
	  // loop through all desired names and check each against the current plugin name
	  var numFound = 0;
	  for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) 
	  {
		// if desired plugin name is found in either plugin name or description
		if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
		    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) 
		{
		    // this name was found
		    numFound++;
		}   
	  }
	  // now that we have checked all the required names against this one plugin,
	  // if the number we found matches the total number provided then we were successful
	  if(numFound == daPlugins.length) 
	  {
		pluginFound = true;
		// if we've found the plugin, we can stop looking through at the rest of the plugins
		break;
	  }
	}
  }
  return pluginFound;
} // detectPlugin

function forceFlashUnload() 
{
  var arr = document.all.tags("object");
  if (arr && arr.length) 
  {
    for (var i=arr.length-1; 0<=i; i--) 
	{
      arr[i].removeNode(true);
    }
  }
}

// helper method to get footprint - if isn't set, sets it to newVal
// on failure, returns ''
function footprintGetWithSet(newVal)
{
  var footprint;
  try {
    footprint = TALSO.get_with_set('CookieId', newVal);
  }
  catch(e) {
    footprint = '';
  }
  return footprint;
}


// attempt to inject the flash, with several retries
function attemptInject(tries)
{
  if (tries <= 0) {
    return;
  }
  else {
    if (document.getElementById("FC"))
    {
      swfobject.embedSWF("/TALSO.swf", "FC", "1", "1", "8", null, {"allowScriptAccess":"always", "hidden":"true", "id":"FC"});
    }
    else {
      setTimeout('attemptInject(' + (tries - 1) + ');', 100);
    }
  }
}


if(detectFlash()) 
{
  // on an ajax call, use swfobject to load the flash
  if ((typeof(talsoLoadedWithAjax) != "undefined") && talsoLoadedWithAjax) {

    // create element and add it to the body
    var elementDiv = new Element('div', {'style':'display:none', 'id':'FC'});
    document.body.appendChild(elementDiv);

    // wait for the div to render (after the scripts are evaluated) before actually doing the swf injection, or it doesn't play in ie
    setTimeout('attemptInject(3);', 1);

  }
  // non-ajax call - preserve existing behavior
  else {
    document.writeln('<object width="1" height="1" style="position:absolute" id="FCIE" type="application/x-shockwave-flash" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" >');
    document.writeln('  <param name="allowScriptAccess" value="always" />');
    document.writeln('  <param name="movie" value="/TALSO.swf">');
    document.writeln('  <param name="hidden" value="true">');
    document.writeln('  <embed src="/TALSO.swf" width="1" height="1" hidden=true style="position:absolute" id="FC" allowScriptAccess="always">');
    document.writeln('  </embed>');
    document.writeln('</object>');
  }
}

var manageFootprint = function()
{
  try {
    $('screenWidth').value = screen.width;
    $('screenHeight').value = screen.height;
    
    var FootprintId = TALSO.get('CookieId');

    if( FootprintId == '' ) {
      TALSO.set( 'CookieId', $('footprint').value );
      if( TALSO.get('CookieId') != $('footprint').value ) {
        $('footprint').value = '';
      }
    }
    else $('footprint').value = FootprintId;
  }
  catch (e) {
    if( $('footprint') ) $('footprint').value = '';
  }
}


