var FT = window.FT || {};

FT.env =
{
   'formats':   {
      'banlb':    '468x60,728x90;dcopt=ist',
      'mpu':      '300x250',
      'sky':      '120x600',
      'button1':  '120x60,120x90,120x120',
      'button2':  '120x60,120x90,120x120',
      'button3':  '120x60,120x90,120x120',
      'button4':  '120x60,120x90,120x120'
   },
   'audSciLimit': 20,
   'server':    '',
   'method':    '/adj/',
   'ord':       Math.floor(Math.random() * 1E16),
   'tile':      0,
   'Requests':  {},
   'getAudSci': function (cookie)
   {
      var ft = this;
      ft.cookie = cookie === undefined ? document.cookie : cookie;
      ft.audSci = '';

      if (ft.audSciLimit)
      {
         var rsiSegs = ft.cookie.replace(/^.*\brsi_segs=([^;]*).*$/, '$1');
         if (rsiSegs !== ft.cookie)
         {
            var Segments = rsiSegs.split(/\|/);
            var Found = [];
            for (var idx = 0; Found.length < ft.audSciLimit && idx < Segments.length; ++idx)
            {
               var segment = Segments[idx];
               if (segment.match(/^J07717_/))
               {
                  segment = segment.replace(/^J07717_/, '');
                  segment = ';a=z' + (parseInt(segment, 10) - 10000);
                  Found.push(segment);
               }
            }
            ft.audSci = Found.join('');
         }
      }
      return ft.audSci;
   },
   'getURL':    function (pos)
   {
      var ft = this;
      ft.url = undefined;
      ft.URLParts = undefined;
      ft.pos = pos;
      ft.sz = ft.formats[ft.pos];
      if (ft.audSci === undefined)
      {
         ft.audSci = ft.getAudSci(ft.cookie);
      }
      if (ft.sz)
      {
         ft.URLParts = ['http://ad.', ft.server, 'doubleclick.net', ft.method, ft.dfp_site, '/', ft.dfp_zone, ';sz=', ft.sz, ';pos=', ft.pos, ft.targeting, ft.audSci, ';tile=', ++ft.tile, ';ord=', ft.ord, '?'];
         ft.url = ft.URLParts.join('');
      }
      ft.Requests[ft.pos] = ft.url;
      return ft.url;
   },
   'getTag':    function (pos)
   {
      var ft = this;
      ft.tag = undefined;
      ft.Parts = undefined;
      var url = ft.getURL(pos);
      if (url)
      {
         ft.Parts = ['<', 'script type="text/javascript" src="', url, '"><', '/script>'];
         ft.tag = ft.Parts.join('');
      }
      return ft.tag;
   },
   'adCall':    function (pos)
   {
      var tag = this.getTag(pos);
      if (tag)
      {
         document.write(tag);
      }
   },
   '_': '_'
};
