var adTemplate = 255 ;


// Define all possible ad positions
var BANNER_TOP    = 1 << 0;
var BANNER_BOTTOM = 1 << 1;
var TOWER_LEFT    = 1 << 2;
var TOWER_RIGHT   = 1 << 3;
var BIGBOX        = 1 << 4;
var SUPER_BANNER  = 1 << 5;
var SUPER_BANNER_BOTTOM = 1 << 6;
var TILE_ONE      = 1 << 7;
var TILE_TWO      = 1 << 8;
var TOWER_RIGHT_160=1 << 9;
var OVERTURE_LINKS   =1 << 10;
var HALFPAGE_RIGHT =1 << 11;
var HALFPAGE_LEFT =1 << 12;
var GOOGLE_LINKS = 1 << 11;

// Define all possible ad templates
var BESTBETS_TOWER  = 0;
var BESTBETS_BIGBOX = 1;
var DEFAULT_TEMPLATE = '';

// Allow for IE User to get google links


if (typeof thisNode == 'undefined') thisNode = 'news';
//if (thisNode != 'travel') TILE_ONE = null;

// Define contents of ad templates
var templateConfigs = new Array();
templateConfigs[ BESTBETS_BIGBOX ]   = SUPER_BANNER + BIGBOX + GOOGLE_LINKS;
templateConfigs[ BESTBETS_TOWER ]    = SUPER_BANNER + TOWER_RIGHT_160  + GOOGLE_LINKS;



// Make node-to-template assignments
var nodeConfigs = new AdConfigurations();

nodeConfigs.addConfiguration( 'artsandliving/entertainmentguide/bestbets' )
.addTemplateAssignment ( new TemplateAssignment( BESTBETS_TOWER,   .7) )
.addTemplateAssignment ( new TemplateAssignment( BESTBETS_BIGBOX,   .3) );



// For this node, select a template
adTemplate = nodeConfigs.getTemplate( thisNode );
if (document.location.href.indexOf("debugAdCode") != -1) document.writeln("Ad Template: " + adTemplate);

// Define objects
// TemplateAssignment associates a template with a frequency ( 0 <= frq <= 1 )
function TemplateAssignment( tmpl, frq )
{
  this.template = tmpl;
  this.frequency = ( ( frq >= 0 ) && ( frq <= 1 ) ) ? frq : 0 ;
}

// NodeTemplateAssignment collects TemplateAssignment objects for a specific node
function NodeTemplateAssignment ( node )
{
  this.node = node;
  this.templates = new Array();
  this.templateCount = 0;
  
  this.addTemplateAssignment = _addAdTemplateAssignment;
  this.selectTemplate = _adSelectTemplate;
}

// AdConfigurations collects NodeTemplateAssignments for the site
function AdConfigurations ()
{
  this.configs = new Array();
  this.length = 0;
  
  this.addConfiguration = _addAdConfiguration;
  this.getTemplate = _adGetTemplate;
}

function _adSelectTemplate()
{
  var base = 0;
  var rNum = Math.random();
  var rTemplate = 0;
  
  for (var count = 0; count < this.templates.length; count++)
  {
    if ( rNum < this.templates[count].frequency + base ) 
    {
      rTemplate = this.templates[count].template;
      break;
      //return this.templates[count].template;
    }
    else
    {
      base += this.templates[count].frequency;
    }
  }
  return rTemplate;
}

function _adGetTemplate ( node )
{
  adTemplate = DEFAULT_TEMPLATE;
  var base = 0;
  var bestMatch = 0;
  for ( var count = 0; count < this.length; count++ )
  {
    if ( this.configs[count].node == node )
    {
      adTemplate = this.configs[count].selectTemplate();
      break;
    }
    else if (( node.indexOf(this.configs[count].node) == 0 ) && ( this.configs[count].node.length > bestMatch ))
    {
      adTemplate = this.configs[count].selectTemplate();
      bestMatch = this.configs[count].node.length;
    }
  }
  return templateConfigs[adTemplate];
}

function _addAdTemplateAssignment( templateAssignment )
{
  this.templates[this.templateCount++] = templateAssignment;
  return this;
}

function _addAdConfiguration ( n )
{
  this.configs[this.length++] = new NodeTemplateAssignment (n);
  return this.configs[this.length - 1];
}