$(document).ready(function() { 
  //init scrollable
  $(".scrollable").scrollable({ vertical: true, mousewheel: false, speed:1000 });
  $(".scrollableNutrition").scrollable({ vertical: false, mousewheel: false, speed:500, circular: false });
  
  // get access to the nutrition API
  var nutritionApi = $(".scrollableNutrition").data("scrollable");
  
  SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
});

//-------------------------------------------------------------------------
//-------------------------------------------- SHOW/HIDE GALLEY BUTTON ----
//-------------------------------------------------------------------------
function showHideGallery(){
  
   /* $('a.next').click(function() {
      var historyApi = $(".scrollable").data("scrollable");
      $('.historyButton').fadeIn(500);
    }); 
    $('a.prev').click(function() {
      var historyApi = $(".scrollable").data("scrollable");
      if(historyApi.getIndex() <=1)
      {
        $('.historyButton').fadeOut(500);
      }
    });  */     

}

//-------------------------------------------------------------------------
//-------------------------------------------- PRODUCT SUB NAV ------------
//-------------------------------------------------------------------------
function productNav(slideNum, productCat){
  
  // get access to the API
  var api = $(".scrollable").data("scrollable");
  
  // slide the scroller
  api.seekTo(slideNum, 1000);
  
  // hide and then show active state
  $('.productNav').each( function(){
    $(this).removeClass('active');
  });
  
  $('.'+productCat).addClass('active');
    
}

//-------------------------------------------------------------------------
//-------------------------------------------- SHOW/HIDE GALLERY BUTTON ----
//-------------------------------------------------------------------------
function historyNav(isNext){
  //setup the api
  var historyApi = $(".scrollable").data("scrollable");
  
  // if index is 0 (eg. first panel moving to second panel), show gallery and back/forward, hide 'learn more'
  if(historyApi.getIndex() == 0)
  {
    $('.button.historyMoreButton').fadeOut(500);
    $('.button.historyGalleryButton').fadeIn(500)                                   

    $('.button.historyMoreButton').removeClass('next');
    $('.sideNav.history a.nextHere').addClass('next'); 
    $('.sideNav.history').fadeIn();
  }
  
  // if index is 1 (eg. 2nd panel moving to first panel), hide gallery and back/forward, show 'learn more'
  if(isNext == 'prev')
  { 
    $('.sideNav.history a.nextHere').removeClass('disabled');
    if(historyApi.getIndex() == 1)
    {
      $('.button.historyMoreButton').fadeIn(500);
      $('.button.historyGalleryButton').fadeOut(500);
      
      $('.sideNav.history').fadeOut(500,
        function(){
          $('.button.historyMoreButton').addClass('next');
          $('.sideNav.history a.nextHere').removeClass('next');          
        }
      );
    }
  }
  
  // make next button go to next... 
  // had to do it this way due to problem of having two '.next' classes... 
  // dunno why,,, just wouldn't work otherwise
  if (isNext == 'next')
  {
    historyApi.next(1000);
    if (historyApi.getIndex() == (historyApi.getSize() - 1))
    {
      $('.sideNav.history a.nextHere').addClass('disabled'); 
    }
  }  
}






// ************************************************************
// handle the change
// ************************************************************
function handleChange(event) 
{
  swfValue = SWFAddress.getValue();
  
  //var str = swfValue.substring(10);  // removes the /projects/ prefix.
  //str = str.replace(/-+/g, ' ');     // removes the dashes.

  if (swfValue != "/") 
  {
    getContent(getAspxPage(swfValue), swfValue);
  }
}


// ************************************************************
// get the document based on the swf address value
// ************************************************************
function getAspxPage(swfAddressValue) 
{
  result = "/"
  if (swfAddressValue != "/")
    result = swfAddressValue;

  return result;
}

// ************************************************************
// get some content based on the swf address
// ************************************************************
function getContent(url,swfValue) 
{
  //get swf address value and take off the /
  var str = swfValue.substring(1);
  
  //setup the scroller
  var historyApi = $(".scrollable").data("scrollable");
  var nutritionApi = $(".scrollableNutrition").data("scrollable");
  
  if (pageType == 'products')
  {
      //search the array of products to match a number value with product name
      var numVal = jQuery.inArray(str, productsArray)
      if (numVal > 10) {
        numVal = numVal;
      }
  
      //divide product value by the number of existing product panels
      var goToPanel = numVal/4

      //seek to appropriate product panel
      historyApi.seekTo(goToPanel, 500)
  }

       
  if (pageType == 'nutrition')
  {
      //search the array of products to match a number value with product name
      var numVal = jQuery.inArray(str, nutritionArray)
      var goToPanel = numVal;
  
      nutritionApi.seekTo(goToPanel, 500)
  }

}

