// This requires hoverintents to be sourced beforehand.

var faded = 0;
var current_slide = 0;
var num_slides = 0;
var autoRotate = true;
var pauseRotation = false;

var current_shade = -1;
var shade_up = false;


j(document).ready(function() {
   
   registerHomeRotator();
   
   registerShade();
   
});

function registerShade() {
    
   j('#left_nav ul li a.box').each(function(index) {
      
      hoverIntentConfig = {
         over: function() {
            if(current_shade !== index || !shade_up) {
               slideShadeUp(index);
            }
         },
         out: function() {
            slideShadeDown();
         }
      }
      
      j(this).hoverIntent(hoverIntentConfig);
       
   });
   
   j('#left_nav ul').hover(function() {
      // pause auto-rotation
      pauseRotation = true;
   }, function() {
      // resume auto-rotation
      pauseRotation = false;
   });
}

function slideShadeUp(index) {
    
    current_shade = index;
    
    label = shades[index].label;
    title = shades[index].title;
    text = shades[index].text;
    image = shades[index].image;
    
    j('#shade .content_left h2').html(label);
    j('#shade .content_left h1').html(title);
    j('#shade .content_left .shade_text').html(text);
    j('#shade .content_right img').attr('src', image);
    
    j('#shade').fadeIn(0);
    
    j('#shade').stop().animate({
        "top": "214px"
    }, 0, 'easeOutQuad', function() {
        j(this).animate({
            "top": "0px"
        }, 300, 'easeOutQuad', function() {
            shade_up = true;
        });
    });
    
}

function slideShadeDown() {
    
    j('#shade').stop().animate({
        "top": "214px"
    }, 300, 'easeInQuad', function() {
        shade_up = false;
    });
    
}


function autoRotateSlide() {
    // Only auto-rotate if they haven't clicked anything yet.
    if (autoRotate) {
        // Only auto-rotate if they aren't hovered over it.
        if (!pauseRotation) {
            // Advance to the next slide.
            slideLeft(getNextSlide());
        }
        // Set up the next auto-rotate.
        setTimeout(autoRotateSlide, 6250);
    }
}

function registerHomeRotator() {
    
    // prepare the arrows
    registerArrowHovers();
    registerArrowClicks();
    
    // Set up the auto-rotate.
    setTimeout(autoRotateSlide, 6250);
    
}

function registerArrowHovers() {
    
    // preload the images
    j('#rotator img.arrow').load(function(){
        
        // fade out on first load
        if(faded < 2) {
            
            j(this).fadeTo(2000, 0);
            
            faded++;
        }
      
    }).each(function(){
        
        // trigger events for images that have loaded,
        // other images will trigger the event once they load
        if ( this.complete && this.naturalWidth !== 0 ) {

            j( this ).trigger('load');

        }
      
    });
      
      
    // set up the hover states on the arrows
    j('#rotator .left-arrow').hover(function() {

        j(this).attr('src', '/elements/img/rotator-left-arrow-hover.png'); 
        
    }, function() {

        j(this).attr('src', '/elements/img/rotator-left-arrow.png');
        
    });
    
    j('#rotator .right-arrow').hover(function() {

        j(this).attr('src', '/elements/img/rotator-right-arrow-hover.png'); 
        
    }, function() {

        j(this).attr('src', '/elements/img/rotator-right-arrow.png');
        
    });
    
    // only show the arrows when hovering over the banner
    j('#rotator').hover(function() {
       
       // pause rotation
       pauseRotation = true;
       
       // fade in the arrows 
       j('img.arrow').stop(true, true).fadeTo(300, 0.99);
        
    }, function() {
        
       // fade out the arrows
       j('img.arrow').stop(true, true).fadeTo(300, 0);
        
       // unpause rotation
       pauseRotation = false;
       
    });
    
    
}

function getPreviousSlide() {
   
   // If we're at the first slide, go to the last.
   if (current_slide == 0)
      current_slide = num_slides - 1;
   // Otherwise, just decrease.
   else
      current_slide--;
   
   // Return the slide itself.
   return slides[current_slide];
   
}

function getNextSlide() {
   
   // Increase the slide index.
   current_slide = (current_slide + 1) % num_slides;
   
   // Return the slide itself.
   return slides[current_slide];
   
}

function registerArrowClicks() {
   
   num_slides = slides.length;
   
   // the meat and potatoes
   j('#rotator .left-arrow').click(function() {
    
      // Disable automatic rotating from here on out.
      autoRotate = false;
      
      // slide to the left
      slideRight(getPreviousSlide());
      
      // Track this change.
      _gaq.push(['pageTracker._trackEvent', 'Homepage Slide', 'Change', 'Previous', 0, true]);
      // Track the slide being seen.
      _gaq.push(['pageTracker._trackEvent', 'Homepage Slide', 'View', slides[current_slide].learnMoreURL, 0, true]);
      
   });
   
   j('#rotator .right-arrow').click(function() {
      
      // Disable automatic rotating from here on out.
      autoRotate = false;

      // slide to the right
      slideLeft(getNextSlide());
      
      // Track this movement.
      _gaq.push(['pageTracker._trackEvent', 'Homepage Slide', 'Change', 'Next', 0, true]);
      // Track the slide being seen.
      _gaq.push(['pageTracker._trackEvent', 'Homepage Slide', 'View', slides[current_slide].learnMoreURL, 0, true]);
      
   });
   
   /*
   // the meat and potatoes
   j('#rotator .left-arrow').click(function() {

      // slide to the left
      slideRight(slides[current_slide]);
      
      // If we're at the first slide, go to the last.
      if (current_slide == 0)
         current_slide = num_slides - 1;
      // Otherwise, just decrease.
      else
         current_slide--;
      
   });
   
   j('#rotator .right-arrow').click(function() {
      
      // slide to the right
      slideLeft(slides[current_slide]);
      
      // Increase the slide index.
      current_slide = (current_slide + 1) % num_slides;
       
   });
   */
    
}

function unregisterArrowClicks() {
    
    // remove dem click handlers
    j('#rotator .left-arrow').unbind().click(function() { return false; });
    j('#rotator .right-arrow').unbind().click(function() { return false; });
    
}

function slideLeft(slide) {
    
    // prevent multiple slides really quickly
    unregisterArrowClicks();
    
    // generate the HTML we need for the new slide
    staging_slide = getStagingSlide(slide.title, slide.text, slide.twitter, slide.linkedin, slide.email, slide.learnMoreText, slide.learnMoreURL, slide.image, 'right');
    
    // insert the slide into the DOM
    j('#rotator .stage .primary').after(staging_slide);
    
    // animate the stage containing the two slides being transitioned
    j('#rotator .stage').stop().animate({

       left: '-=757' 
       
       }, 900,'easeInQuad', function() {
            
        // remove the old slide
        j('#rotator .stage .primary').remove();
        
        // reset the stage
        j('#rotator .stage').css('left', '-757px');
        
        // make the new slide the current slide
        j('#rotator .stage .staging-right').removeClass('staging-right').addClass('primary');
        
        // allow clicking again
        registerArrowClicks();
        
    });
    
    return false;
    
}

function slideRight(slide) {
    
    // prevent multiple slides really quickly
    unregisterArrowClicks();
    
    // generate the HTML we need for the new slide
    staging_slide = getStagingSlide(slide.title, slide.text, slide.twitter, slide.linkedin, slide.email, slide.learnMoreText, slide.learnMoreURL, slide.image, 'left');
    
    // insert the slide into the DOM
    j('#rotator .stage .primary').before(staging_slide);
    
    // animate the stage containing the two slides being transitioned
    j('#rotator .stage').stop().animate({

       left: '+=757' 
       
       }, 900,'easeInQuad', function() {
        
        // remove the old slide
        j('#rotator .stage .primary').remove();
        
        // reset the stage
        j('#rotator .stage').css('left', '-757px');
        
        // make the new slide the current slide
        j('#rotator .stage .staging-left').removeClass('staging-left').addClass('primary');
        
        // allow clicking again
        registerArrowClicks();
        
    });
    
    return false;
    
}

function getStagingSlide(title, text, twitter, linkedin, email, learnMoreText, learnMoreURL, image, dir) {
    
    return '<div class="rotator-slide staging-'+dir+'">'+
        
        '<div class="content_left">'+
            
            '<h1 class="slide-title">'+title+'</h1>'+
            '<div class="slide-text">'+text+
            '</div>'+
            '<div class="toolbox">'+

                 '<div class="social_icons">'+
                     '<a href="'+twitter+'" id="twitter" class="social_icon" target="_blank"><img src="/elements/img/twitter-icon.jpg" alt="Twitter" /></a>'+
                     '<a href="'+linkedin+'" id="linkedin" class="social_icon" target="_blank"><img src="/elements/img/linked-in-icon.jpg" alt="LinkedIn" /></a>'+
                     '<a href="'+email+'" id="email" class="social_icon"><img src="/elements/img/email-icon.jpg" alt="Email" /></a>'+
                     
                     '<div class="clearfix">&nbsp;</div>'+
                 '</div>'+

                 '<div class="learn_more">'+
                     '<p class="more">'+
                         '<a href="'+learnMoreURL+'" onclick="_gaq.push([\'pageTracker._trackEvent\', \'Homepage Slide\', \'Click\', \''+learnMoreURL+'\']);">'+
                             learnMoreText+
                         '</a>'+
                     '</p>'+
                 '</div>'+

                 '<div class="clearfix">&nbsp;</div>'+

            '</div>'+
            
        '</div>'+
        
        '<div class="content_right">'+
            
            '<!--<div class="content_right_shadow"></div>-->'+
            
            '<img src="'+image+'" class="rotator-image" alt="none"/>'+
            
        '</div>'+
        
        '<div class="clearfix">&nbsp;</div>'+
        
    '</div>';
    
}

function getShade(label, title, text, image) {
   return '<div class="content_left">'+
         '<h2>'+label+'</h2>'+
         '<h1 class="slide-title">'+title+'</h1>'+
         text+
      '</div>'+
      '<div class="content_right">'+
         '<img src="'+image+'" class="rotator-image" alt="none"/>'+
      '</div>'+
      '<div class="clearfix">&nbsp;</div>';
}




