// when the DOM is ready...
$(document).ready(function () {

    var $panels = $('#slider .scrollContainer > div');
    var $container = $('#slider .scrollContainer');

    // if false, we'll float all the panels left and fix the width 
    // of the container
    var horizontal = true;

    // float the panels left if we're going horizontal
    if (horizontal) {
        $panels.css({
            'float' : 'left',
            'position' : 'relative' // IE fix to ensure overflow is hidden
        });

        // calculate a new width for the container (so it holds all panels)
        $container.css('width', $panels[0].offsetWidth * $panels.length);
    }

    // collect the scroll object, at the same time apply the hidden overflow
    // to remove the default scrollbars that will appear
    var $scroll = $('#slider .scroll').css('overflow', 'hidden');

    // apply our left + right buttons
    $scroll
        .before('<img class="scrollButtons left" src="images/scroll_left.png" />')
        .after('<img class="scrollButtons right" src="images/scroll_right.png" />');

    // handle nav selection
    function selectNav() {
        $(this)
            .parents('ul:first')
                .find('a')
                    .removeClass('selected')
                .end()
            .end()
            .addClass('selected');
    }

    $('#slider .navigation').find('a').click(selectNav);

    // go find the navigation link that has this target and select the nav
    function trigger(data) {
        var el = $('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
        selectNav.call(el);

var currSlide = data.id;
var lastSlide = "";
var captionText ="";

switch(currSlide) {
case "1":
  captionText = "Between June 2008 and June 2009, CGSGI partnered with the NGO Angelitos de Luz to support 30 medical missions that delivered much-needed health services to people in rural areas who lack regular access to health care. Pictured here is one such mission held in September 2008.<br />Photo Credit: Clinton Foundation";
  break;    
case "2":
  captionText = "The Angelitos de Luz medical missions perform small surgeries.  Teams of 10-14 doctors and nurses, as well as equipment and supplies, were flown to isolated regions for 3-4 days, where they treated an average of 200 people per mission.<br />Photo Credit: Clinton Foundation";
  break;
case "3":
  captionText = "Workers at the Capas S.A.C. processing and packaging plant in Ancash, Peru sort through paprika.  Growers receive technical assistance from a CGSGI-sponsored Economic Service Center.<br />Photo Credit: Penelope Chester / Clinton Foundation";
  break;
case "4":
  captionText = "Workers at the Capas S.A.C. processing and packaging plant in Ancash, Peru sort through paprika.  Growers receive technical assistance from a CGSGI-sponsored Economic Service Center.<br />Photo Credit: Penelope Chester / Clinton Foundation";
  break;
case "5":
  captionText = "Students in Barranquilla, Colombia enjoy a nutritious lunch.  Internal conflict in Colombia has displaced over 4 million people, leaving thousands of vulnerable children without access to quality nutrition.  In cooperation with Shakira’s Fundación Pies Descalzos, CGSGI is funding a two-year nutrition program that is providing 4,000 primary school students with two meals a day for 180 days per year.<br />Photo Credit: Clinton Foundation";
  break;
case "6":
  captionText = "A new and improved school lunch menu is posted outside of a Pies Descalzos school.<br />Photo Credit: Clinton Foundation";
  break;
case "7":
  captionText = "Spices grown by TANA, a promising organic spice business, are displayed in both whole and packaged form. CGSGI is helping to expand TANA by helping it connect with markets and obtain international organic certification.<br />Photo Credit: Clinton Foundation";
  break;
case "8":
  captionText = "CGSGI is helping the group of Afro-Colombian women who run the TANA organic spice business improve production and processing.<br />Photo Credit: Clinton Foundation";
  break;
case "9":
  captionText = "Residents of Iquitos, Peru, are screened for cataracts.  In conjunction with Fundación Slim and the Peruvian Ministry of Health, CGSGI is sponsoring a project that will provide the necessary resources to increase the number of cataract surgeries taking place in Peru by 50,000 over four years.  Iquitos will be the first site for these surgeries, which will take place on June 30, 2009.<br />Photo Credit: Clinton Foundation";
  break;
case "10":
  captionText = "With the continued support of the ESC and Mr. Basilio’s determination, it will not be long before his business is able to compete in the market without assistance.";
  break;
default:
  captionText = "Between June 2008 and June 2009, CGSGI partnered with the NGO Angelitos de Luz to support 30 medical missions that delivered much-needed health services to people in rural areas who lack regular access to health care. Pictured here is one such mission held in September 2008.<br />Photo Credit: Clinton Foundation";

}//end switch

captionText = '<strong>Slide ' + currSlide + '</strong><br /><br />' + captionText;
document.getElementById("slideCaption").innerHTML = captionText;

    }

    if (window.location.hash) {
        trigger({ id : window.location.hash.substr(1) });
    } else {
        $('ul.navigation a:first').click();
    }

    // offset is used to move to *exactly* the right place, since I'm using
    // padding on my example, I need to subtract the amount of padding to
    // the offset.  Try removing this to get a good idea of the effect
    var offset = parseInt((horizontal ? 
        $container.css('paddingTop') : 
        $container.css('paddingLeft')) 
        || 0) * -1;


    var scrollOptions = {
        target: $scroll, // the element that has the overflow

        // can be a selector which will be relative to the target
        items: $panels,

        navigation: '.navigation a',

        // selectors are NOT relative to document, i.e. make sure they're unique
        prev: 'img.left', 
        next: 'img.right',

        // allow the scroll effect to run both directions
        axis: 'xy',

        onAfter: trigger, // our final callback

        offset: offset,

        // duration of the sliding effect
        //duration: 500,
        duration: 5,

        // easing - can be used with the easing plugin: 
        // http://gsgd.co.uk/sandbox/jquery/easing/
        easing: 'swing'
    };

    // apply serialScroll to the slider - we chose this plugin because it 
    // supports// the indexed next and previous scroll along with hooking 
    // in to our navigation.
    $('#slider').serialScroll(scrollOptions);

    // now apply localScroll to hook any other arbitrary links to trigger 
    // the effect
    $.localScroll(scrollOptions);

    // finally, if the URL has a hash, move the slider in to position, 
    // setting the duration to 1 because I don't want it to scroll in the
    // very first page load.  We don't always need this, but it ensures
    // the positioning is absolutely spot on when the pages loads.
    scrollOptions.duration = 1;
    $.localScroll.hash(scrollOptions);

});
