/**
 * Special global variable to control seek to disfunctionments.
 */
var seekControl = null;

jQuery(document).ready(function() {
  try
  {
    var fragment = null;
    // Load on window anchor first
    if( window.location.href.indexOf('#') !== -1 )
    {
      fragment = window.location.href.substring( window.location.href.indexOf( '#' ) );
    }

    jQuery('table>tbody').each( function(){
      var rows = jQuery(this).find('tr');
      for( var i = 1, iMax = rows.length; i < iMax; i += 2 )
      {
        jQuery(rows[i]).addClass('distinct');
      }
    } );

    jQuery('form .hint').hint();

    jQuery('form input[type="radio"]').radio();
    jQuery('form input[type="checkbox"]').checkbox();

    jQuery('a[rel*=external]').each( function() {
      jQuery(this).attr('target','_blank');
    } );

    jQuery('a[href$=\'.pdf\']').each( function() {
      jQuery(this).addClass('pdf');
    } );

     // IE 8 overflow and white-space nowrap fix (in compilance with stylesheet)
    if( jQuery.browser.msie && jQuery.browser.version.match( /^8\..*$/ ) )
    {
      jQuery('.ticker li').each( function(){
        var sub = jQuery(this).find('.info:eq(0)');
        jQuery(this).css( 'padding-left' , new String(
          sub.width() + parseInt( sub.css( 'padding-left' ) ) + parseInt( sub.css( 'padding-right' ) )
        ) + 'px' );
      } );
    }
    jQuery('.ticker').ticker();

    if( jQuery('#gallery-thumbnails').length > 0 && jQuery('#gallery-images').length > 0 ) {

      jQuery('.video-container').each( function(){
        flashembed( this , {
          src: this.href ,
          wmode: 'transparent' ,
          play: 'false' ,
          autoplay: 'false' ,
          allowscriptaccess: 'false'
        } );
      } );
      
      jQuery('.video-text').bind( 'click' , function(){
        jQuery( jQuery( this ).parent().find('.video-container') ).flashembed( {
          src: jQuery( this ).parent().find('.video-container').attr('href') ,
          wmode: 'transparent' ,
          play: 'true' ,
          autoplay: 'true' ,
          allowscriptaccess: 'false'
        } );
        jQuery(this).hide();
      } );

      var galleryThumbnails = jQuery('#gallery-thumbnails').scrollable( { size: 4, api: true, clickable: false } );
      var galleryImages = jQuery('#gallery-images').scrollable( { size: 1, api: true, clickable: false } ).onSeek( function(){
        if( seekControl === null )
        {
          galleryThumbnails.seekTo( this.getIndex() );
        }
        else
        {
          galleryThumbnails.seekTo( seekControl );
          seekControl = null;
        }
        jQuery('.gallery-thumbnail').removeClass('active');
        jQuery('.gallery-thumbnail:eq('+this.getIndex()+')').addClass('active');
      } );

      // Load on window anchor first
      if( fragment && jQuery('#gallery-overlay').find(fragment).length > 0 ) {
        jQuery('#gallery-overlay').overlay( {
          expose: {
            color: '#ffffff' ,
            loadSpeed: 100 ,
            opacity: 0.75
          } ,
          left: 'center' ,
          top: 'center' ,
          api: true ,
          onLoad: function() {
            seekControl = jQuery(fragment).index();
            galleryImages.seekTo( jQuery(fragment).index() , 0);
            jQuery('.gallery-thumbnail').removeClass('active');
            jQuery('.gallery-thumbnail:eq('+jQuery(fragment).index()+')').addClass('active');
          } ,
          onClose: function(){
            jQuery('#gallery-overlay .video-text').show();
            jQuery('#gallery-overlay .video-container').each( function(){
                flashembed( this , {
                  src: this.href ,
                  wmode: 'transparent' ,
                  play: 'false' ,
                  autoplay: 'false' ,
                  allowscriptaccess: 'false'
              } );
            } );
            jQuery('.gallery-thumbnail').removeClass('active');
          }
        } ).load();
      }

      // Other overlayer
      jQuery('.gallery-thumbnail').each( function(index) {
        // thumbnail images trigger the overlay
        jQuery(this).overlay( {
          target: '#gallery-overlay',
          expose: {
            color: '#ffffff' ,
            loadSpeed: 100 ,
            opacity: 0.75
          } ,
          left: 'center' ,
          top: 'center' ,
          onLoad: function() {
            seekControl = index;
            galleryImages.seekTo(index, 0);
            jQuery('.gallery-thumbnail').removeClass('active');
            jQuery('.gallery-thumbnail:eq('+index+')').addClass('active');
          } ,
          onClose: function(){
            jQuery('#gallery-overlay .video-text').show();
            jQuery('#gallery-overlay .video-container').each( function(){
                flashembed( this , {
                  src: this.href ,
                  wmode: 'transparent' ,
                  play: 'false' ,
                  autoplay: 'false' ,
                  allowscriptaccess: 'false'
              } );
            } );
            jQuery('.gallery-thumbnail').removeClass('active');
          }
        } );

      } );
    }

    if( jQuery('#send-to-friend-link').length > 0 ) {
      var sendToFriendOverlay = jQuery('#send-to-friend-link').overlay( {
        expose: {
          color: '#ffffff' ,
          loadSpeed: 100 ,
          opacity: 0.75
        } ,
        left: 'center' ,
        top: 200 ,
        target: jQuery('#send-to-friend-link').attr('href') ,
        api: true
      } );

      // Load on window anchor
      if( fragment === jQuery('#send-to-friend-link').attr('href') ) {
        sendToFriendOverlay.load();
      }
    }
   
    // Load on window anchor first
    var splashCounter = jQuery.cookie('youngacl-splashviews');
    if( ! splashCounter ) splashCounter = 0;
    if( splashCounter < 2 && jQuery('#splash-overlay').length > 0 ) {
      jQuery('#splash-overlay').overlay( {
        expose: {
          color: '#ffffff' ,
          loadSpeed: 100 ,
          opacity: 0.75
        } ,
        left: 'center' ,
        top: 'center' ,
        api: true
      } ).load();
      jQuery.cookie('youngacl-splashviews' , ++splashCounter );
    }

  }
  catch( e )
  {
    alert( e );
  }
  
} );
