var captionHeight = 0;

function jQueryCycleHack(){
    jQuery("#loading").hide();
	  if (jQuery("#pics").children().length < 2) {
		  jQuery("#pics").css("text-align", "center").show();
		} else {
			if (jQuery("#pics").hasClass("numbered")) {
				jQuery('#pics').show().after('<div id="ss_control"><span id="ss_pause"><a href="#">pause</a></span><span id="ss_play"><a href="#">play</a></span><span id="nav"></span></div><div id="caption_wrapper"><div id="caption"></div></div>').cycle({
					fx: 'fade',
					timeout: 8000,
					delay: -2000,
					pager: '#nav',
					before: onBefore,
					after: onAfter
				});
				jQuery("#ss_pause").click(function(){
					jQuery("#pics").cycle("pause");
					jQuery("#ss_pause").fadeOut("slow", function(){
						jQuery("#ss_play").fadeIn("fast");
					});
				});
				jQuery("#ss_play").click(function(){
					jQuery("#pics").cycle("resume");
					jQuery("#ss_play").fadeOut("slow", function(){
						jQuery("#ss_pause").fadeIn("fast");
					});
				});
			} else {
				jQuery('#pics').show().cycle({
					fx: 'fade',
					timeout: 5000,
					delay: -2000,
					before: onBefore
				});
			}
			jQuery("a.thickbox").click(function(){
				ssPause();
			});
		}
}

function ssThickboxCallback(){
    if (jQuery("#ss_play").length == 0 || jQuery("#ss_play").css("display") == "none") {
        ssResume();
    }
}

function ssPause(){
    jQuery("#pics").cycle("pause");
}

function ssResume(){
    jQuery("#pics").cycle("resume");
}

// have to have this called for the onLoad event because it does not 
// work on the first page load when using jQuery's document ready.  strange.
window.onload = jQueryCycleHack;

// used to center images in the slideshow
function onBefore(cur, next, opts, flag){
    jQuery('#caption').slideUp("slow");
    var x = jQuery(next);
    var w = x.outerWidth();
		if (jQuery("#slideshow").hasClass("small")) {
		  x.css({
        marginLeft: (250 - w) / 2
      });
		} else {
			x.css({
				marginLeft: (500 - w) / 2
			});
		}
}

function onAfter(cur, next, opts, flag){
    jQuery('#caption').html(jQuery(".ss_info", this).html());
    if (jQuery('#caption').height() > captionHeight) {
        captionHeight = jQuery('#caption').height();// + 18;
        jQuery("#caption_wrapper").height(captionHeight);
    }
    jQuery('#caption').slideDown("slow");
}
