$(function()
{
  // code for horizontal scroll
  // move main images
  var api = $(".scroll").scrollable(
  {
    size: 1,
    clickable: false,
    loop: true,
    speed: 100
  }).autoscroll(
  {
    autoplay: true,
    steps:1,
    interval: 8500,
    api: true
  });

  // set api for fade in/fade out
  api.onBeforeSeek(function(event,index){

  // lowest fade value
    var fadeMin = .01;

// alert ("index: " + index );
  // hide caption box
    capHide();

    /* Use a recursive* function approach here, if the image is faded out 
     * we will allow advancing to the next slide, which will trigger the 
     * onSeek event to fade the slides back in */ 
    var element = $("div.scroll div.item"); 
    if (element.css('opacity') == fadeMin) { 
      return true; 
    } 
    
    /* The slide has not yet started fading out, so we will fade the item 
     * out */ 
    element.fadeTo("slow", fadeMin, function(){ 
      /* Once the fade is complete we will call api.seekTo() to trigger 
       * the onBeforeSeek event again, which will now advance the slide 
       * because we will be returning true instead of false */
      api.seekTo(index);
    }); 

    // Prevent advancing the slide on the first call to onBeforeSeek 
    return false; 
 
  }).onSeek(function(){ 
 
    // Fade the item back in 
    $("div.scroll div.item").fadeTo('slow', 1);
    setTimeout( "capReveal()",100); 
  });
  
  // attach hover function to complete banner
  $("div.item").hover(
    function()
    {
      // over function
      $("div.item div.captionbkg").css("background-color", "#036");
    },
    function()
    {
      // off function
      $("div.item div.captionbkg").css("background-color", "transparent");
    }
  );
  
  $("div.item").click(function()
  {
    window.open($(this).find("a").attr("href"));
  });
  
  // attach click controllers
  $("div.item a").attr("target","_blank");

  // show first caption after delayed load
  setTimeout( "capReveal()", 750);


  // form handler
  var frm = $("#frmemail");
  var frmDefVal = "Your e-mail address";
  frm.focus(
    function()
    {
      frm.toggleClass("emaildefault");
      if( frm.val() == frmDefVal )
      {
        frm.val("");
      }
    });
  frm.blur(
    function()
    {
      frm.toggleClass("emaildefault");
      if( frm.val() == "" )
      {
        frm.val(frmDefVal);
      }
    });
  if( frm.hasClass("emaildefault") )
  {
    frm.val(frmDefVal);
  }
}); /* close "document ready" function */


// callback function to reveal captions
var capHide = function(){
  $("div.item div.caption").animate({width:"0"},"slow");
}

var capReveal = function(){
  $("div.item div.caption").animate({width:"200px"},"slow");
}
