$(document).ready(function() {
    $('#slideshow1 img').bind('load', function(e) {
        imagesRemaining--;
        if (imagesRemaining == 0) {
            // I'm doing some other stuff when initializing cycle
            startCycle();
            // My images all start with visibility:hidden so they don't show
            // before cycle hides them in a 'stack', so ...
            $('#slideshow1 img').css('visibility', 'visible');
        }
    });
});

var switchWaiting=false,
    switchWaitingPrev =true,
    bSkipOnAfter=false,
    options1 = {
          fx:     'scrollHorz',
          speed:  350,        
          prev:   '#prev1',
          next:   '#next1',
          timeout: 0,
		  
          after: function(curr,next,opts) {onAfter(curr,next,opts,"1");}
        },
    options2 = {
          fx:     'scrollHorz',
          speed:  350,        
          prev:   '#prev1',
          next:   '#next1',
          timeout: 0,
          after: function(curr,next,opts) {onAfter(curr,next,opts,"1");}
        },
    options3 = {
          fx:     'scrollHorz',
          speed:  350,        
          prev:   '#prev3',
          next:   '#next3',
          timeout: 0,
          after: function(curr,next,opts) {onAfter(curr,next,opts,"1");}
        },

    switchTo = function(stopMe,startMe,bPrev){      
      $('#slideshow'+stopMe).cycle('pause');
      if (bPrev) {
        //     alert("1");
        $('#slideshow'+startMe).data("cycleStarted","no");
        // alert("2");
        $('#slideshow'+startMe).cycle('stop');
        //alert("3");
      }
      if ($('#slideshow'+startMe).data("cycleStarted") === undefined ||
          $('#slideshow'+startMe).data("cycleStarted") == "no") {
            // alert('switchTo'+startMe);
            $('#slideshow'+startMe).data("cycleStarted","yes");
            $('#slideshow'+startMe).cycle(getOptions(startMe));  
          } else {
            $('#slideshow'+startMe).cycle('resume');
          }
      $('.nav'+stopMe).hide();
      $('#slideshow'+stopMe).hide();
      $('.nav'+startMe).show();
      if (bPrev) {
        bSkipOnAfter=true;
        $('#slideshow'+startMe).cycle('prev');
      }
      $('#slideshow'+startMe).show();
    },
    
    //image counter output
    onAfter = function(curr,next,opts,gal) {
      var caption = (opts.currSlide + 1) ;//+ opts.slideCount;
      $('#counter'+gal).html(caption +"/"+opts.slideCount);
      //  if (gal == "1") {
      if (bSkipOnAfter) {
        bSkipOnAfter=false;
      } else {
        (gal == "1") ? $("#slideshow2").show(): $("#slideshow1").show();
      }
    };

function startCycle() {
  $(function() {
      $('#slideshow1').cycle(options1);
      $('#slideshow1').data("cycleStarted","yes");
      $('#slideshow2').cycle(options2);
      $('#slideshow2').data("cycleStarted","yes");
      $('#slideshow3').cycle(options3);
      $('#slideshow3').data("cycleStarted","yes");
  });
}