
// Code adapted from EngineeringController ($/EngineeringController.root/EngineeringController_1/webstats/dcstats.js)


// BrowserDetector
// ------------------------------------------------------------------------------------------------

var BrowserDetector = {
  isIE: function(userAgent)
  {
    var isOpera = /Opera/.test(userAgent);
    return ( /MSIE/.test(userAgent) && !isOpera );
  }
}


// Trendy
// ------------------------------------------------------------------------------------------------

var Trendy = {
  hit: function(imageBaseUrl) 
  {
    this.runTests();
    this.sendStatsToServer(imageBaseUrl, this.params);    
  },
  
  runTests: function()
  {
    for ( test in Trendy.Tests ) 
    {
      if ( typeof(Trendy.Tests[test].run) != 'undefined' )
        Trendy.Tests[test].run();
    }
  },
  
  sendStatsToServer: function(imageBaseUrl, params)
  {
    var imageUrl = this.getImageUrl(imageBaseUrl, params);
    
    if ( !this.urlTooLong(imageUrl) )
    {
	    var img = new Image();
	    img.src = imageUrl;
	  }
  },
  
  urlTooLong: function(url)
  {
    var maxIeUrlLength = 2083;   // http://support.microsoft.com/kb/208427
    
    var isIE = BrowserDetector.isIE(navigator.userAgent);
    return ( isIE && url.length > maxIeUrlLength );
  },
  
  getImageUrl: function(baseUrl, params)
  {
    return baseUrl + "?" + params.join('&');
  },

  params: [],
  
  result: function(k, v) 
  {
    this.params.push(encodeURIComponent(k) + '=' + encodeURIComponent(v));
  }
}

Trendy.Tests = {}

Trendy.Tests.Basics = {
  title: function() 
  {
    return ( document.title == null || document.title == "" ) ? "(Untitled)" : document.title;
  },
  
  run: function() 
  {
    Trendy.result('RequestUrl',  document.URL);
    Trendy.result('PageTitle',   this.title());
    Trendy.result('ReferrerUrl', document.referrer);
  }
}
  
Trendy.Tests.Resolution = {
  run: function() 
  {
    Trendy.result('ScreenWidth',      screen.width);
    Trendy.result('ScreenHeight',     screen.height);
    Trendy.result('ScreenColorDepth', screen.colorDepth);
  }
}

Trendy.Tests.Java = {
  run: function() 
  {
    Trendy.result('Java', navigator.javaEnabled());
  }
}

Trendy.Tests.Flash = {
  progIdPrefix: "ShockwaveFlash.ShockwaveFlash",  // COM ProgID for IE

  detect: function() 
  {
    if ( BrowserDetector.isIE(navigator.userAgent) ) 
      return this.detectForIE();
    else
      return this.detectForNonIE();    
  },
  
  detectForIE: function()
  {
    var activexCreator = function(objectName) { return new ActiveXObject(objectName); };
    return this.detectForIeWithCreator(activexCreator);
  },
  
  detectForIeWithCreator: function(objectCreator)
  {
    // need to increase this as new versions come out:
    var maxFlashVersion = 10;  
  
    for ( var i = maxFlashVersion; i > 0; i-- ) 
    {
      try
      { 
        var f = objectCreator("ShockwaveFlash.ShockwaveFlash." + i); 
        return i; 
      }
      catch(e) {}
    }
    
    return false;
  },
  
  detectForNonIE: function()
  {
    return this.detectForNonIeWithNavigator(window.navigator);
  },
  
  detectForNonIeWithNavigator: function(nav)
  {
    if ( nav.plugins && nav.plugins.length ) 
    {
      var f = nav.plugins["Shockwave Flash"];
      
      if ( f && (d = f.description) ) 
        return parseInt(d.charAt(d.indexOf('.') - 1));
      
      if ( nav.plugins["Shockwave Flash 2.0"] ) 
        return 2;
    }
    
    if ( nav.mimeTypes && nav.mimeTypes.length ) 
    {
       var f = nav.mimeTypes['application/x-shockwave-flash'];
       
       if ( f && f.enabledPlugin ) 
         return 2;
    }
    
    return false;
  },
  
  run: function()
  {
    var flashVersion = this.detect();
    
    if ( flashVersion )
      Trendy.result('FlashVersion', this.detect());
  }
}


// SiteTracker
// ------------------------------------------------------------------------------------------------

var SiteTracker = {
  defaultImageBaseUrl: "_tracker/TrackerImage.ashx",

  track: function(imageBaseUrl) 
  {
    if ( typeof imageBaseUrl == 'undefined' )
      imageBaseUrl = this.defaultImageBaseUrl;
  
    if ( typeof window.onload != 'function' )
      window.onload = function() { Trendy.hit(imageBaseUrl); }
    else 
    {
      var oldOnLoad = window.onload;
      window.onload = function() { oldOnLoad(); Trendy.hit(imageBaseUrl); };
    }
  }
}
