var BrowserAdvisor = {
  setup: function ()
           { 
             // these attributes will be available after BrowserAdvisor is init'd
             this.discoveredBrowser = null;
             this.browserVendor = null;
             this.browserVersion = null;
             this.link = null;

             // browsers known to the advisor
             this.profiles = new Array(
                 new this.BrowserProfile( "Internet Explorer", "MSIE", "MSIE",
                                          "<a target=\"_blank\" href=\"http://www.microsoft.com/windows/ie\">http://www.microsoft.com/windows/ie</a>" ),
                 new this.BrowserProfile( "Firefox", "Firefox", "Firefox", 
                                          "<a target=\"_blank\" href=\"http://www.mozilla.com/firefox\">http://www.mozilla.com/firefox</a>" ),
                 // chrome must be listed before safari
                 new this.BrowserProfile( "Chrome", "Chrome", "Chrome",
                                          "<a target=\"_blank\" href=\"http://www.google.com/chrome\">http://www.google.com/chrome</a>" ), 
                 new this.BrowserProfile( "Safari", "Safari", "Version",
                                          "<a target=\"_blank\" href=\"http://www.apple.com/safari\">http://www.apple.com/safari</a>" ),
                 new this.BrowserProfile( "Opera", "Opera", "Opera",
                                          "<a target=\"_blank\" href=\"http://www.opera.com/\">http://www.opera.com/</a>" ),
                 // if listed, netscape must be last 
                 new this.BrowserProfile( "Netscape", "Mozilla", "Mozilla",
                                          "<a target=\"_blank\" href=\"http://browser.netscape.com/\">http://browser.netscape.com/</a>" )
             );

             this.currentBrowser = document.getElementById( 'currentBrowser' );
             this.browserAdvice = document.getElementById( 'browserAdvice' );
           },
  BrowserProfile: function ( displayName, vendorPattern, versionPattern, vendorLink )
           {
             this.displayName = displayName;
             this.vendorPattern = vendorPattern;
             this.versionPattern = versionPattern;
             this.vendorLink = vendorLink;
           },
  getBrowser: function ()
           {
             var b = 0;
             var vendor = null;
             while( b < this.profiles.length && vendor == null )
             {
               if( new RegExp( ".*" + this.profiles[b].vendorPattern + ".*" ).test( this.userAgent ) )
               {
                 vendor = this.profiles[b];
               }
               b++;
             }
             return vendor;
           },
   getVersion: function ( browser )
           {
             var b = 0;
             var version = null;
             while( b < this.profiles.length && version == null )
             {
               if( this.profiles[b] == browser )
               {
                 index = this.userAgent.indexOf( this.profiles[b].versionPattern );
                 version = this.userAgent.substring( index + this.profiles[b].versionPattern.length + 1 ).replace( /[;\s].*/, '' );
               }
               b++;
             }
             return version;
           },
   discover: function ()
           { 
             this.userAgent = navigator.userAgent;
             this.discoveredBrowser = this.getBrowser();
             if( ! this.discoveredBrowser )
             {
               return;
             }

             this.browserVendor = this.discoveredBrowser.displayName;
             this.browserVersion = this.getVersion( this.discoveredBrowser );
           }, 
   advise: function ()
           { 
             if( this.discoveredBrowser == null  ) // unknown
             {
                return;
             }
             else // current
             {
                this.link = this.discoveredBrowser.vendorLink;
             }

             this.currentBrowser.innerHTML = this.browserVendor + " " + this.browserVersion;
             this.browserAdvice.innerHTML = this.link;
           },
   main: function ()
           { 
             this.setup();
             this.discover();
             this.advise();
           }
};
