// the current audience being displayed
var current_audience = '';
// the current background position being filled (either 0 or 1)
var current_background = -1;
// the current content position being filled (either 0 or 1)
var current_content = -1;
// the pre-loaded images and the associated audience names for
// controlling the pre-load order
var slides_images = { };
var audience_names = [];

function setupSlider() {
    
    // we'll use opacity to cross-fade backgrounds
    $('#slide-one').css('opacity', 0.0);
    $('#slide-two').css('opacity', 0.0);
    
    // we won't cross-fade the content if "real" opacity is not
    // supported, e.g. avoiding IE6/7/8 opacity with PNG bugs :(
    if ($.support.opacity)
        $('.slide-content').css('opacity', 0.0);
    
    // set up roll over and roll out handlers for the nav
    $('.flyout-nav').mouseover(function() {
       
        // clear 'em all out
        $('.flyout').hide();
        
        // show this guy's flyout
        $(this).next().show();
        
        // figure out which audience we're hovering over
        var id_pieces = $(this).next().attr('id').split('-');
        var new_audience = id_pieces[id_pieces.length-1];
        
        // don't do anything else if we're already on the requested slide
        if (new_audience == current_audience)
            return;
        
        // update the slide
        displaySlide(new_audience);
        
        /*
        // get the slide content
        var slide_content = slides_content[audience];
        
        // get the name of the image that needs to be loaded
        var background = slide_content.background;
        
        // get the headline
        var headline = slide_content.headline;
        
        // get the secondary headline
        var secondary_headline = slide_content.secondary_headline;
        
        // do the crossfade
        crossfadeTo(background, audience, headline, secondary_headline);
        */
    });
    
    $('#gateway-control-box ul li').mouseout(function() {
        //$('.flyout').hide();
    });
    
    // start pre-loading the images now
    audience_names = [
        'patients',
        'physicians',
        'payers'
    ];
    for (var i = 0; i < audience_names.length; i++) {
        slides_images[audience_names[i]] = null;
    }
    preLoadImage(0);
}

function preLoadImage(index) {
    
    if (!slides_content[audience_names[index]])
        return;
    
    function handleError(index) {
        
        return function() {
            
            // just move on to the next slide
            if (index < audience_names.length)
                preLoadImage(index + 1);
        }
    }
    
    function handleSuccess(index) {
        
        return function() {
            
            // update the list of images
            slides_images[audience_names[index]] = this;            
            
            // were we waiting for this image?            
            if (current_audience == audience_names[index])
                displayBackground(audience_names[index]);
            
            // move on to the next slide
            if (index < audience_names.length)
                preLoadImage(index + 1);
        }
    }
    
    var img = new Image();
    img.onabort = handleError(index);
    img.onerror = handleError(index);
    img.onload = handleSuccess(index);
    img.src = slides_content[audience_names[index]].background;
}

function displayBackground(audience) {
    
    // do we actually have an image?
    if (!slides_content[audience])
        return;
    
    // get the image for the background
    var image = slides_images[audience];
    
    // update the slide background
    var prev_slide = current_background < 0 || current_background == 1 ? $('#slide-two') : $('#slide-one');
    var next_slide = current_background < 0 || current_background == 1 ? $('#slide-one') : $('#slide-two');
    
    // update the background on the next slide
    // is it avaliable?
    if (image != null)
        $(image).appendTo(next_slide.empty());
    else
        next_slide.empty();
    
    // fade in the next slide
    next_slide.show();
    next_slide.stop(true).animate({ opacity: 1.0 }, 500);
    
    // fade out the prev slide, being sure to hide it and clear it on complete
    prev_slide.stop(true).animate({ opacity: 0.0 }, 500, (function(slide) {
        return function() {
            slide.hide();
            slide.empty();
        }
    })(prev_slide));
    
    // update the current index
    current_background = current_background < 0 || current_background == 1 ? 0 : 1;
}

function displayContent(audience) {
    
    // do we actually have any content?
    if (!slides_content[audience])
        return;
    
    // get content for the slide
    var content = slides_content[audience];
    
    // update the slide content
    var prev_content = current_content < 0 || current_content == 1 ? $($('.slide-content')[1]) : $($('.slide-content')[0]);
    var next_content = current_content < 0 || current_content == 1 ? $($('.slide-content')[0]) : $($('.slide-content')[1]);
    
    // update the headlines
    next_content.find('h3').html(content.headline);
    next_content.find('p').html(content.secondary_headline);
    
    // make sure the next slide will be on top (so <a> tags work correctly)
    next_content.css('z-index', 1);
    prev_content.css('z-index', 0);
    
    // fade in the next content
    if ($.support.opacity) {
    
        next_content.show();
        next_content.stop(true).animate({ opacity: 1.0 }, 500, function(slide) {
            return function() {
                slide.css('opacity', '');
            }
        }(next_content));
    
    } else {
        
        next_content.show();
    }
    
    // fade out the prev content, being sure to hide it on complete
    if ($.support.opacity) {
        
        prev_content.stop(true).animate({ opacity: 0.0 }, 500, (function(slide) {
            return function() {
                slide.hide();
            }
        })(prev_content));
    
    } else {
        
        prev_content.hide();
    }
    
    // Adding cross-browser text shadow.
    $('#site-wrapper-home .slide-content h3').textShadow();
    $('#site-wrapper-home .slide-content p').textShadow();
    
    // update the current content index
    current_content = current_content < 0 || current_content == 1 ? 0 : 1;
}


function displaySlide(audience) {
    
    // update the current feature on display
    current_audience = audience;
    
    // update the background
    displayBackground(audience);
    
    // update the content
    displayContent(audience);
}

