$(document).ready(function(){

  $( 'div.retailers-content' ).jScrollPane();
  
  if ( $( 'div#sessionholder' ).length ) {
    setInterval( function(){
      $.ajax({
        url: "/ajax/sessionholder.php",
        success: function(data) {
          $('div#sessionholder').html( data );
        }
      });
    }, 300000 );
  }
  
  $( 'a.rebellion-racing' ).fancybox({
    padding            : 2,
    width              : 894,
    height             : 774, // 674, // was 664
    autoDimensions     : false,
    autoScale          : false,
    type               : 'iframe',
    overlayColor       : '#000000',
    hideOnOverlayClick : false,
    transitionIn       : 'elastic',
    transitionOut      : 'elastic',
    overlayOpacity     : 0.5
  });
  
  /*
  $( 'a.header-open' ).click(function(){
    $( 'a.header-open' ).hide();
    
    $( 'div.header-big' ).css( 'top', '-800px' ).fadeIn( 250 ).animate( { top: '0px' }, { duration: 1000, easing: 'easeOutExpo' } );
    
    setTimeout( "$( 'div.header-big-content' ).fadeIn( 500 );", 1500 );
    
    setTimeout( "$( 'a.header-close' ).show();", 1500 );
    
              var flashvars = {};
              var params = {
                movie : 'flash/video.flv'
              };
              var attributes = {};
              swfobject.embedSWF("flash/video.swf", "video", "720", "405", "9.0.0", "js/swfobject/expressInstall.swf", flashvars, params, attributes );
    
    return false;
  });
  
  $( 'a.header-close' ).click(function(){
    $( 'a.header-close' ).hide();
    
    $( 'div.header-big-content' ).fadeOut( 500 );
    
    setTimeout( "$( 'div.header-big' ).animate( { top: '-800px' }, { duration: 1000, easing: 'easeOutExpo' } ).fadeOut( 250 );", 500 );
    
    setTimeout( "$( 'a.header-open' ).show();", 500 );
    
    return false;
  });
  */
  
  $( 'ul.collection li a' ).hover(function(){
    $( this ).children( 'span' ).stop().animate({ top: '0px' }, { duration: 500, easing: 'easeOutExpo' });
  }, function(){
    $( this ).children( 'span' ).stop().animate({ top: '-20px' }, { duration: 500, easing: 'easeOutExpo' });
  });
  
  $( 'img.reflection' ).reflect({
    height: 0.33,
    opacity: 0.75
  });
  
  $( 'img.adapt' ).load(function() {
    processImage( this, true, true, true );
  });
  
  $( 'img.adaptfull' ).load(function(){
    processImage( this, false, true, true );
  });
  
  /*
  $( 'img.news-home-image-img' ).load(function(){
    processImage( this, false, true, true );
    $( this ).pixastic("desaturate");
  });
  */
  
  function processImage( objImage, force, centerW, centerH ) {
    var maxwidth  = $( objImage ).parent().width();
    var maxheight = $( objImage ).parent().height();
    
    // both width and height have to be definied
    if ( maxwidth > 0 && maxheight > 0 ) {
      var width     = $( objImage ).width();
      var height    = $( objImage ).height();
      
      // if width and/or height is greater than max width/height only...
      // btw, if it is smaller, we only act on the alignement...
      if ( width > maxwidth || height > maxheight ) {
        if ( force ) {
          var rapport = Math.max( ( width / maxwidth ), ( height / maxheight ) );
        } else {
          var rapport = Math.min( ( width / maxwidth ), ( height / maxheight ) );
        }
        
        width  = Math.round( width / rapport );
        height = Math.round( height / rapport );
        
        $( objImage ).width( width );
        $( objImage ).height( height );
      }
      
      // horizontal alignement (center) / crops horizontaly
      if ( centerW ) {
        $( objImage ).css( 'margin-left', Math.round( ( maxwidth - width )  / 2 ) );
      }
      
      // vertical alignement (center) / crops vertically 
      if ( centerH ) {
        $( objImage ).css( 'margin-top', Math.round( ( maxheight - height )  / 2 ) );
      }
    }
  }
  
  $.preloadCssImages();
});

Cufon.replace( 'h1' );
Cufon.replace( 'h2' );
Cufon.replace( 'h3' );
