﻿if(!AV.MAPS) {AV.MAPS = {}; }
AV.MAPS.Log = function() {
  
  var arTiles = [];
  var stopLogging = false;
  var used_token = 0;         //bit value recording whether or not a token was used
  
  
  function GetUniqueValues(ar) {  
    var d = [], e = {};
    
    //FF (Opera and Safari) records the following World Map tiles when via the DOMNodeInserted event
    //IE does not.  So to get a consistent tile count across browsers, The images below will be exluded from the tile count.
    //When the Bing Reports begin displaying data, we may need to adjust this and possible add 4 to
    //the IE tile logging to account for these tiles (or find a way to load the map without using LoadMap())
    var excludeTiles = ['http://ecn.t0.tiles.virtualearth.net/tiles/r0.png?g=321&mkt=en-us',
                        'http://ecn.t1.tiles.virtualearth.net/tiles/r1.png?g=321&mkt=en-us',
                        'http://ecn.t2.tiles.virtualearth.net/tiles/r2.png?g=321&mkt=en-us',
                        'http://ecn.t3.tiles.virtualearth.net/tiles/r3.png?g=321&mkt=en-us'];
                        
    Ext.each(ar, function (f) {
                    var skip = false;
                    for(var i = 0; i < excludeTiles.length; i++) {
                      if(excludeTiles[i] === f) { skip = true; break; }
                    }
                    
                    if(skip === false) {
                      if (!e[f]) {
                        d.push(f);
                      }
                      e[f] = true;
                    }
                  });    
    return d;
  }
  
  function WriteLogTiles(e) {
    Ext.Ajax.request({
      url: 'WriteLogTiles.asp?tile_count=' +  GetTileCount() + '&used_token=' + used_token  
    });     
  }
  
  function WriteLogSearchTransaction(count, search_value) {
    Ext.Ajax.request({
      url: 'WriteLogTiles.asp?search_transaction_count=' + count + '&used_token=' + used_token + '&search_value=' + search_value  
    });     
  }
  
  //IE Logging function
  function GetTiles(e) {
    LogTiles();
    stopLogging = false;
    //Get Tiles and compare to previously loaded tiles
    //Tiles are not fully loaded when the onchangeview event fires
    setTimeout(LogTiles, 3000);        
  }
  
  //IE Logging function
  function LogTiles() {
    //get main map tiles       
    var arTiles_temp =  Ext.DomQuery.select('img[class*=MSVE_ImageTile]', Ext.getDom('myMap'));
    //get minmimap tiles
    //var arTiles_temp2 =  Ext.DomQuery.select('img[class*=MSVE_ImageTile]', Ext.getDom('MSVE_minimap_content'));
   
    if(arTiles_temp.length > 0) {
      for(var i = 0; i < arTiles_temp.length; i++) { arTiles.push(arTiles_temp[i].src); }      
    }
      
    //Check twice....allow time for tiles to be loaded.
    if(stopLogging !== true) { 
      setTimeout(LogTiles, 3000); 
      stopLogging = true;
    }
  }
  
  function GetTileCount() { 
    //FOR DEBUGGING/COUNTING TILES
    //Ext.Msg.alert(arTiles.length.toString(), arTiles.join(String.fromCharCode(13)));
    arTiles = GetUniqueValues(arTiles);
    return arTiles.length; 
  } 
  
  function AttachLogEvents(map) {
    //Tile Logging    
    if((typeof(Ext) !== "undefined")) {   
      //FF TILE LOGGING  (also Opera and Safari 1.3 and higher)     DOMNodeInserted DOES NOT WORK FOR IE!
      // utilize event delegation to register just one handler on the container element to capture Tiles loading       
      Ext.get('myMap').addListener('DOMNodeInserted', function(evt, el, o) {            
        if(el.src) {
          arTiles.push(el.src);  
        }
      }, this, 
        {
          // filter the target element to be a descendant with the class 'MSVE_ImageTile'
          delegate: '.MSVE_ImageTile' 
        }
      );        
      
      //IE TILE LOGGING 
      if(Ext.isIE) {  map.AttachEvent("onchangeview", GetTiles); }               
    }
  
    window.onunload = WriteLogTiles;
  }
  
  
  return {    
    writeLogSearchTransaction: function(count, search_value) { WriteLogSearchTransaction(count, search_value); },
    attachLogEvents:  function(map) {AttachLogEvents(map);},     
    setUsedToken: function(token_used) { used_token = token_used;}
  }
  
}();

