//
// Notify Dev team when updated
//
Voy.BrowserCheck = Class.create();

Voy.BrowserCheckRequirements = Class.create();
Voy.BrowserCheckRequirements.Browser = Class.create();
Object.extend( Voy.BrowserCheckRequirements.Browser.prototype,
  {
    initialize: function( name, requirements ) {
      this.name = name;
      this.requirements = {
        versionMajor: null,
        versionMinor: null,
        os: null
      };
      this.requirements = Object.extend( this.requirements, requirements );
      if ( Voy.isNull( this.requirements.versionMajor ) ) {
        throw( "you must define the versionMajor" );
      }
    },
    validate: function() {
      var valid = true;
      if ( this.name != Voy.Env.Browser.browserName() ) {
        valid = false;
      } else {
        if ( this.requirements.os != null ) {
          if ( Voy.Env.OS.osName() == this.requirements.os ) {
            if ( this.requirements.versionMajor > Voy.Env.Browser.version().major ) {
              valid = false;
            } else if ( this.requirements.versionMajor == Voy.Env.Browser.version().major ) {
              if ( ! Voy.isNull( this.requirements.versionMinor ) && this.requirements.versionMinor > Voy.Env.Browser.version().minor ) {
                valid = false;
              }
            } 
          } else {
            valid = false;
          }
        } else {
          if ( this.requirements.versionMajor > Voy.Env.Browser.version().major ) {
            valid = false;
          } else if ( this.requirements.versionMajor == Voy.Env.Browser.version().major ) {
            if ( ! Voy.isNull( this.requirements.versionMinor ) && this.requirements.versionMinor > Voy.Env.Browser.version().minor ) {
              valid = false;
            }
          } 
        }
      }
      return valid;
    }
  }
);

Voy.BrowserCheckRequirements.JavaScript = Class.create();
Object.extend( Voy.BrowserCheckRequirements.JavaScript.prototype, 
  {
    initialize: function( name, requirements ) {
      this.name = name;
      this.requirements = {
        enabled: true
      };
    },
    validate: function() {
      return true;
    }
  }
);

Voy.BrowserCheckRequirements.OS = Class.create();
Object.extend( Voy.BrowserCheckRequirements.OS.prototype, 
  {
    initialize: function( name, requirements ) {
      this.name = name;
      this.requirements = {
        version: null
      };
      this.requirements = Object.extend( this.requirements, requirements );
      if ( Voy.isNull( this.requirements.version ) ) {
        throw( "you must define the version" );
      }
    },
    validate: function() {
      var valid = false;
      this.requirements.version = $A( this.requirements.version );
      
      if ( this.name == Voy.Env.OS.osName() ) {
        var osVersion = Voy.Env.OS.version();
        this.requirements.version.each( function( ver ) {
          if ( ver == osVersion ) {
            valid = true;
            throw $break;
          }
        });
      }
      return valid;
    }
  }
);
Voy.BrowserCheckRequirements.Resolution = Class.create();
Object.extend( Voy.BrowserCheckRequirements.Resolution.prototype, 
  {
    initialize: function( requirements ) {
      this.requirements = {
        width: null,
        height: null
      };
      this.requirements = Object.extend( this.requirements, requirements );
      if ( Voy.isNull( this.requirements.height ) ) {
        throw( "you must define the height" );
      }
      if ( Voy.isNull( this.requirements.width ) ) {
        throw( "you must define the width" );
      }
    },
    validate: function() {
      var valid = false;
      if ( Voy.Env.Resolution.width >= this.requirements.width 
           && Voy.Env.Resolution.height >= this.requirements.height ) {
        valid = true;    
      }
      return valid;
    }
  }
);
Voy.BrowserCheckRequirements.Flash = Class.create();
Object.extend( Voy.BrowserCheckRequirements.Flash.prototype, 
  {
    initialize: function( requirements ) {
      this.requirements = {
        versionMajor: null,
        versionMinor: null
      };
      this.requirements = Object.extend( this.requirements, requirements );
      if ( Voy.isNull( this.requirements.versionMajor ) ) {
        throw( "you must define the versionMajor" );
      }
      //if ( !Voy.Env.Browser.DetectFlash() ) {
      if ( !Voy.Env.Browser.flashEnabled() ) {
      	 throw( "you must download Flash" );
      }
    },
    validate: function() {
      var valid = true;
      watchit = (typeof(watchit) == 'undefined') ? false : true;
      // Added global watchit flag b/c if Flash isn't installed, this function is called twice (not sure why)
      if ( Voy.Env.Browser.flashEnabled() ) {
        if ( 
            ( ( window.location.href.indexOf('voyageru') >= 0 ) || ( window.location.href.indexOf('7200') >= 0 ) || ( window.location.href.indexOf('6200') >= 0 ) ) || 
            ( ( window.location.href.indexOf('ticket') >= 0 ) || ( window.location.href.indexOf('7600') >= 0 ) || ( window.location.href.indexOf('6600') >= 0 ) ) ||
            ( ( window.location.href.indexOf('ticket') >= 0 ) || ( window.location.href.indexOf('7600') >= 0 ) || ( window.location.href.indexOf('6800') >= 0 ) ) || 
            ( ( window.location.href.indexOf('vmath') >= 0 ) || ( window.location.href.indexOf('7700') >= 0 ) || ( window.location.href.indexOf('6700') >= 0 ) )
            ) {
          if ( this.requirements.versionMajor > Voy.Env.Browser.flashVersion().major ) {
            valid = false;
          } else if ( this.requirements.versionMajor == Voy.Env.Browser.flashVersion().major ) {
            if ( ! Voy.isNull( this.requirements.versionMinor ) && this.requirements.versionMinor > Voy.Env.Browser.flashVersion().rev ) {
              valid = false;
            }
          }
        } else {
          if ( (this.requirements.versionMajor-1) > Voy.Env.Browser.flashVersion().major ) {
            valid = false;
          } else if ( (this.requirements.versionMajor-1) == Voy.Env.Browser.flashVersion().major ) {
            if ( ! Voy.isNull( this.requirements.versionMinor ) && this.requirements.versionMinor > Voy.Env.Browser.flashVersion().rev ) {
              valid = false;
            }
          }
        }

      } else {
        valid = false;
      }
      if (valid == false && watchit == false) { 
        watchit = true;
        if( window.location.href.indexOf('bcMinimums.jsp') < 0 ) { 
          if ( (window.location.href.indexOf('voyageru') >= 0) || (window.location.href.indexOf('7200') >= 0) || (window.location.href.indexOf('6200') >= 0) ) {
            bcMinimums_flash_error = 'vu';
          }
          else if ( (window.location.href.indexOf('solo') >= 0) || (window.location.href.indexOf('7400') >= 0) || (window.location.href.indexOf('6400') >= 0) ) {
            bcMinimums_flash_error = 'solo';
          }
          else if ( window.location.href.indexOf('/vip') >= 0 ) {
            bcMinimums_flash_error = 'vip';
          }
          else if ( (window.location.href.indexOf('ticket') >= 0) || (window.location.href.indexOf('7600') >= 0) || (window.location.href.indexOf('6600') >= 0) ) {
            bcMinimums_flash_error = 'ttr';
          }
          else if ( (window.location.href.indexOf('vmath') >= 0) || (window.location.href.indexOf('7700') >= 0) || (window.location.href.indexOf('6700') >= 0) ) {
            bcMinimums_flash_error = 'vmath';
          }
          else { bcMinimums_flash_error = ''; }
        }
      }
      return valid;
    }
  }
);
Voy.BrowserCheckRequirements.Cookie = Class.create();
Object.extend( Voy.BrowserCheckRequirements.Cookie.prototype, 
  {
    initialize: function() {},
    validate: function() {
      return Voy.Env.Browser.cookiesEnabled();
    }
  }
);
Voy.BrowserCheckRequirements.Acrobat = Class.create();
Object.extend( Voy.BrowserCheckRequirements.Acrobat.prototype, 
  {
    initialize: function( requirements ) {  
      this.requirements = {
        versionMajor: null,
        versionMinor: null
      };
      this.requirements = Object.extend( this.requirements, requirements );
      if ( Voy.isNull( this.requirements.versionMajor ) ) {
        throw( "you must define the versionMajor" );
      }
    },
    validate: function() {
      var valid = true;
      if ( Voy.Env.Acrobat.enabled() ) {
        if ( Voy.Env.OS.version() == 'Mac OS X' ) {
          valid = true;
        } else if ( this.requirements.versionMajor > Voy.Env.Acrobat.version().major ) {
          valid = false;
        } else if ( this.requirements.versionMajor == Voy.Env.Acrobat.version().major ) {
          if ( ! Voy.isNull( this.requirements.versionMinor ) && this.requirements.versionMinor > Voy.Env.Acrobat.version().minor ) {
            valid = false;
          }
        }
      } else {
        valid = false;
      }
      return valid;
    }
  }
);

Object.extend( Voy.BrowserCheck.prototype,
  {
    initialize: function( options ) {
      this.options = {
        domId: Voy.BrowserCheck.DEFAULT_DOMID,
        fId: Voy.BrowserCheck.DEFAULT_FID,
        template: Voy.BrowserCheck.DEFAULT_JST,
        imagePath: Voy.BrowserCheck.DEFAULT_IMG_PATH,
        orientation: Voy.BrowserCheck.DEFAULT_ORIENTATION,
        requirements: Voy.BrowserCheck.DEFAULT_REQS,
        displayOrder: Voy.BrowserCheck.DEFAULT_ORDER,
        recommend: Voy.BrowserCheck.DEFAULT_RECOMMEND,
        display: true
      };
      this.valid = true;
      this.options = Object.extend( this.options, options );
      this.options.displayOrder = $A( this.options.displayOrder );
      var self = this;

      try { // test for functionality
        var re = new RegExp( '(\${.*?})' ); 
        if ( this.options.display ) {
          this.show();
        }
      } catch ( e ) {
        this.showInvalidBrowser();
      }      
    },
    show: function() {
      Voy.Template.transform( this.options.template, 'bc', this, { div: this.options.domId } );
      // this adjusts for the 'validating' image
      if ( this.options.orientation == 'top' ) {
        $( this.options.domId ).style.top = ( parseInt( $( this.options.domId ).style.top ) + 50 ) + 'px';
      }
      // need to give the browser a chance to recognize the new DOM elements
      // setTimeout is broken on Safari < 1.2 
      if ( Voy.Env.Browser.browserName() == 'Safari' && parseFloat( Voy.Env.Browser.version().asString() ) < 1.2 ) {
        this.showInvalidBrowser();
      } else {
        window.setTimeout( this.configureBC.bind( this ), 3000 );
      }
    },
    showInvalidBrowser: function() {
      $( this.options.domId ).innerHTML = '<a href=\'/common/bcMinimums.jsp\' target=\'_blank\'><img src=\'' + this.options.imagePath + 'invalid_environment.png\' height=\'80\' width=\'196\' border=\'0\' alt=\'Browser Check Failed\' title=\'Browser Check Failed\'/></a>'
    },
    configureBC: function() {
      if ( this.options.orientation == 'top' ) {
        $( 'bc-details' ).style.top = ( - $( 'bc-details' ).getHeight() + 1 ) + 'px';
      } else {
        $( 'bc-details' ).style.top = ( $( 'bc-bar' ).getHeight() + 1 ) + 'px';
      }
      // Automatic direct drop down should appear if validation fails.
      if ( !this.valid ) {
	    $( 'bc-details' ).toggle();
	    $( 'bc-openclose' ).src = this.options.imagePath + 'bc-minus.png';
	    $( 'bc-bar' ).onclick = this.bcBarOnclick.bindAsEventListener( this );
      } else {
	    $( 'bc-bar' ).onclick = this.bcBarOnclick.bindAsEventListener( this );
      }
      var self = this;
      this.options.displayOrder.each( function( property ) {
        new Voy.Tooltip( $( 'bc-' + property ), self.getTooltipHTML( property ), {effect: 'appear', viewport:true} );
      });
      if ( typeof( bcMinimums_flash_error ) != "undefined" && bcMinimums_flash_error != '' && bcMinimums_flash_error != 'vip' ) {
        if ( bcMinimums_flash_error == 'vu' ) {
          var flashErrorImgSrc = 'vu';
          var flashErrorTop = '-95px';
          var flashErrorLeft = '+220px';
          var flashErrorImgWidth = '308';
          var flashErrorImgHeight = '148';
        }
        else if ( bcMinimums_flash_error == 'solo' ) {
          var flashErrorImgSrc = 'solo';
          var flashErrorTop = '-120px';
          var flashErrorLeft = '+210px';
          var flashErrorImgWidth = '318';
          var flashErrorImgHeight = '157';
        }
        else if ( bcMinimums_flash_error == 'vip' ) {
          var flashErrorImgSrc = 'vport';
          var flashErrorTop = '+80px';
          var flashErrorLeft = '-530px';
          var flashErrorImgWidth = '304';
          var flashErrorImgHeight = '143';
        }
        else if ( bcMinimums_flash_error == 'ttr' ) {
          var flashErrorImgSrc = 'ttr';
          var flashErrorTop = '-410px';
          var flashErrorLeft = '-320px';
          var flashErrorImgWidth = '309';
          var flashErrorImgHeight = '170';
        }
        else if ( bcMinimums_flash_error == 'vmath' ) {
          var flashErrorImgSrc = 'vmath';
          var flashErrorTop = '-110px';
          var flashErrorLeft = '+230px';
          var flashErrorImgWidth = '305';
          var flashErrorImgHeight = '145';
        }
        var flashError = document.createElement("div");
        flashError.setAttribute('id', 'flashError');
        flashError.style.position = 'relative';
        flashError.style.top = flashErrorTop;
        flashError.style.left = flashErrorLeft;
        $('bc-details').appendChild(flashError);
        var flashErrorImgLink = document.createElement("a");
        flashErrorImgLink.setAttribute('href', 'http://www.adobe.com/products/flashplayer/');
        flashErrorImgLink.setAttribute('target', '_blank');
        flashError.appendChild(flashErrorImgLink);
        var flashErrorImg = document.createElement("img");
        flashErrorImg.setAttribute('src', '/common/assets/images/browserCheck/NoFlashError_'+flashErrorImgSrc+'.jpg');
        flashErrorImg.setAttribute('border', '0');
        flashErrorImg.setAttribute('width', flashErrorImgWidth);
        flashErrorImg.setAttribute('height', flashErrorImgHeight);
        flashErrorImgLink.appendChild(flashErrorImg);
        // <div id=\'flashError\' style=\'display:block; width:150px; height:75px; border:1px solid #FF0000; position:relative; z-index:593;\'></div>
        // $( this.options.fId ).style.top = ( parseInt( $( this.options.domId ).style.top ) + 250 ) + 'px';
        // $( this.options.fId ).style.left = ( parseInt( $( this.options.domId ).style.left ) - 1050 ) + 'px';
        // $( flashError ).innerHTML = '<img src=\'/common/assets/images/browserCheck/NoFlashError_vport.jpg\'>';
        // alert('Warning:\n\nYou must use Adobe Flash version 8 or higher to use this site.\n\nYou can update your Flash version at:\n\nhttp://www.adobe.com\n ');
      }
    },
    getTooltipHTML: function( property ) {
      var recommendation = this.options.recommend.get( property );
      var html = '<table border=\'0\' cellpadding=\'0\' cellspacing=\'0\'><col width=\'125\'/><col width=\'125\'/><col width=\'125\'/><tr><th>Category</th><th>PC</th><th>Mac</th></tr>';
      html += '<tr><td class=\'description\'>';
      html += recommendation.category;
      html += '</td><td class=\'value\'>';
      html += recommendation.pc;
      html += '</td><td class=\'value\'>';
      html += recommendation.mac;
      html += '</td></tr></table>';
      return html;
    },
    bcBarOnclick: function( event ) {
      $( 'bc-details' ).toggle();
      if ( $( 'bc-details' ).visible() ) {
        $( 'bc-openclose' ).src = this.options.imagePath + 'bc-minus.png';
      } else {
        $( 'bc-openclose' ).src = this.options.imagePath + 'bc-plus.png';
      }
    },
    getIcon: function( type ) {
      var icon = this.options.imagePath;
      switch ( type ) {
        case 'Browser':
          icon += 'Browser-' + Voy.Env.Browser.browserName().replace( ' ', '' ) + '.gif';
          break;
        case 'OS':
          icon += 'OS-' + Voy.Env.OS.osName().replace( ' ', '' ) + '.gif';
          break;
        case 'Resolution':
          icon += 'Resolution.gif';
          break;
        case 'Cookie':
          icon += 'Cookie.gif';
          break;
        case 'Flash':
          icon += 'Flash.gif';
          break;
        case 'Acrobat':
          icon += 'Acrobat.gif';
          break;
      }
      return icon;
    },
    getName: function( type ) {
      var name = 'unsupported';
      switch ( type ) {
        case 'Browser':
          name = Voy.Env.Browser.browserName() + ' ' + Voy.Env.Browser.version().asString();
          break;
        case 'OS':
          name = Voy.Env.OS.version();
          break;
        case 'Resolution':
          name = Voy.Env.Resolution.asString();
          break;
        case 'Cookie':
          name = 'Cookies Enabled';
          break;
        case 'Flash':
          name = 'Flash ' + Voy.Env.Browser.flashVersion().asString();
          break;
        case 'Acrobat':
          name = 'PDF Reader ' + Voy.Env.Acrobat.version().asString();
          break;
      }
      return name;
    },
    validate: function( type ) {
      var icon = this.options.imagePath;
      var valid = false;
      if ( Voy.isDefined( type ) ) {
        this.options.requirements.each( function( requirement ) {
          if ( requirement.type == type ) {
            var passed = requirement.rules.invoke( 'validate' ).find( function( validated ) { return validated; } );
            valid = Voy.isDefined( passed );
          }
        });
        if ( valid ) {
          icon += 'bc-pass.gif';
        } else {
          icon += 'bc-critical.gif';
          this.valid = false;
        }
      } else {
        // check everything
        var img;
        for ( var i = 0; i < this.options.displayOrder.length; i++ ) {
          img += this.validate( this.options.displayOrder[ i ] );
          if ( img.indexOf( 'critical' ) >= 0 ) {
            this.valid = false;
            break;
          }
        }
        icon += ( this.valid ) ? 'bc-pass.gif' : 'bc-critical.gif';
      }
      return icon;
    }
  }
);

Object.extend( Voy.BrowserCheck, 
  {
    functional: false,
    DEFAULT_DOMID: 'browserCheck',
    DEFAULT_FID: 'flashError',
    DEFAULT_JST: '/common/assets/scripts/BrowserCheck.jst',
    DEFAULT_IMG_PATH: '/common/assets/images/browserCheck/',
    DEFAULT_ORIENTATION: 'top',
    DEFAULT_REQS: $A( [
      {
        type: 'Browser', 
        rules: $A( [ new Voy.BrowserCheckRequirements.Browser( 'Internet Explorer', {versionMajor: 7 } ),
                     new Voy.BrowserCheckRequirements.Browser( 'Safari', { versionMajor: 3, versionMinor: 1, os: "Mac" } ),
                     new Voy.BrowserCheckRequirements.Browser( 'Firefox', { versionMajor: 3} ) ] )
      },
      {
        type: 'OS', 
        rules: $A( [ new Voy.BrowserCheckRequirements.OS( 'Windows', { version: [ 'Windows XP', 'Windows Vista' ] } ),
                     new Voy.BrowserCheckRequirements.OS( 'Mac', { version: [ 'Mac OS X' ] } ),
                     new Voy.BrowserCheckRequirements.OS( 'Linux', { version: [ 'Ubuntu' ]   } ) ] )
      },
      {
        type: 'Resolution', 
        rules: $A( [ new Voy.BrowserCheckRequirements.Resolution( { height:768, width:1024 } ) ] )
      },
      {
        type: 'Cookie', 
        rules: $A( [ new Voy.BrowserCheckRequirements.Cookie() ] )
      },
      {
        type: 'Flash', 
        rules: $A( [ new Voy.BrowserCheckRequirements.Flash( { versionMajor: 10 } ) ] )
      },
      {
        type: 'JavaScript', 
        rules: $A( [ new Voy.BrowserCheckRequirements.JavaScript( ) ] )
      },
      {
        type: 'Acrobat', 
        rules: $A( [ new Voy.BrowserCheckRequirements.Acrobat( {versionMajor: 8 } ) ] )
      }
    ] ),
    DEFAULT_ORDER: $A( ['Browser', 'OS', 'Resolution', 'Cookie', 'Flash'] ),
    DEFAULT_RECOMMEND: $H({
      Browser:    { category: 'Minimum Internet Browser',
                    pc:          'Internet Explorer 7.x<br/>Firefox 3.0',
                    mac:         'Firefox 3.0<br/>Safari 3.1'
                  },
      OS:         { category: 'Minimum Operating System',
                    pc:          'Windows XP/Vista',
                    mac:         'OS 10.4+'
                  },
      Resolution: { category: 'Minimum Screen Resolution',
                    pc:          '1024x768<sup>*</sup>',
                    mac:         '1024x768<sup>*</sup>'
                  },
      Acrobat:    { category: 'PDF Settings',
                    pc:          'Adobe Reader<sup>&reg;</sup> 8+',
                    mac:         'Built-in MAC PDF Reader or Adobe Reader<sup>&reg;</sup> 8+'
                  },
      Cookie:     { category: 'Browser Cookies',
                    pc:          'Cookies must be enabled to experience this application.',
                    mac:         'Cookies must be enabled to experience this application.'
                  },
      Flash:      { category: 'Minimum Flash Version',
                    pc: ( (
                            (
                              ( ( window.location.href.indexOf('ticket') >= 0 ) || ( window.location.href.indexOf('15600') >= 0 ) || ( window.location.href.indexOf('15700') >= 0 ) || ( window.location.href.indexOf('15800') >= 0 ) ) ||  
                              ( ( window.location.href.indexOf('ticket') >= 0 ) || ( window.location.href.indexOf('17600') >= 0 ) || ( window.location.href.indexOf('17700') >= 0 ) || ( window.location.href.indexOf('17800') >= 0 ) ) ||  
                              ( ( window.location.href.indexOf('voyageru') >= 0 ) || ( window.location.href.indexOf('11600') >= 0 ) || ( window.location.href.indexOf('11700') >= 0 ) ) ||  
                              ( ( window.location.href.indexOf('vmath') >= 0 ) || ( window.location.href.indexOf('16600') >= 0 ) || ( window.location.href.indexOf('16700') >= 0 ) ) ||
                              ( ( window.location.href.indexOf('solo') >= 0 ) || ( window.location.href.indexOf('13600') >= 0 ) || ( window.location.href.indexOf('13700') >= 0 ) )
                            )
                        ) ? '10+' : '10+' ),
                    mac: ( (
                            (
                              ( ( window.location.href.indexOf('ticket') >= 0 ) || ( window.location.href.indexOf('15600') >= 0 ) || ( window.location.href.indexOf('15700') >= 0 ) || ( window.location.href.indexOf('15800') >= 0 ) ) ||  
                              ( ( window.location.href.indexOf('ticket') >= 0 ) || ( window.location.href.indexOf('17600') >= 0 ) || ( window.location.href.indexOf('17700') >= 0 ) || ( window.location.href.indexOf('17800') >= 0 ) ) ||  
                              ( ( window.location.href.indexOf('voyageru') >= 0 ) || ( window.location.href.indexOf('11600') >= 0 ) || ( window.location.href.indexOf('11700') >= 0 ) ) ||  
                              ( ( window.location.href.indexOf('vmath') >= 0 ) || ( window.location.href.indexOf('16600') >= 0 ) || ( window.location.href.indexOf('16700') >= 0 ) ) ||
                              ( ( window.location.href.indexOf('solo') >= 0 ) || ( window.location.href.indexOf('13600') >= 0 ) || ( window.location.href.indexOf('13700') >= 0 ) )
                            )
                        ) ? '10+' : '10+' )
                  }
    })
  }
);