//
// NOTE: ANY CHANGES MADE TO THIS FILE ALSO NEED TO BE MADE TO js/util/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;
}

// helper method to get footprint without setting a new one if not found.
function footprintGet()
{
  var footprint;
  try {
    footprint = TALSO.get('CookieId');
  } 
  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, null, {"allowScriptAccess":"always", "hidden":"true", "id":"FC"});
    }
    else {
      setTimeout('attemptInject(' + (tries - 1) + ');', 100);
    }
  }
}

function initTalso()
{
	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 {

		  try {
			var divTag = document.createElement("div");
			divTag.id = 'talsoDiv';
			document.body.appendChild(divTag);

			divTag = document.getElementById('talsoDiv');
				
			divTag.innerHTML = '<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" >'
				+ '  <embed src="' + '/TALSO.swf" width="1" height="1" hidden=true style="position:absolute" id="FC" allowScriptAccess="always">'
				+ '  </embed>'
				+ '  <param name="allowScriptAccess" value="always" />'
				+ '  <param name="movie" value="' + '/TALSO.swf" />'
				+ '  <param name="hidden" value="false" />'
				+ '</object>';
			
			
		  } catch (e) {
			  //alert(e);
		  }
	  }
	}
}

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

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


if ((typeof(talsoLoadedWithAjax) != "undefined") && talsoLoadedWithAjax) {

    initTalso();

} else if (window.addEventListener) {
	
    window.addEventListener("load", initTalso, false);
	
} else if (window.attachEvent) {

    window.attachEvent('onload', initTalso);

}

