$.fn.cycle.defaults.timeout = 6000;
setTimeout($(function() {
//   $('#s5').before('<ul id="photo-gallery-nav">').cycle({
   var cycle = $('#s5').cycle({
    fx:     'fade',
    sync:   1,
    timeout: 0,
//    prev:   '#prev', 
    next:   '#s5 div img',
//    before:  onBefore
    after:   onAfter,
    pager: '#photo-gallery-nav',
    activePagerClass: 'activeSlide',
//    containerResize: 0,
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#photo-gallery-nav li:eq(' + idx + ') a'; 
    } 



    // callback fn that creates a thumbnail to use as pager anchor 
//    pagerAnchorBuilder: function(idx, slide) { 
//	$('#photo-gallery-nav').width($('#photo-gallery-nav').width()+56);
//        return '<li><a href="#"><img src="' + slide.children[0].src + '" width="50" height="50" /></a></li>'; 
//    } 

 });

 if (window.location.hash) {
 	$('#s5').cycle(parseInt(window.location.hash.replace(/^#/, ''), 10) - 1);
 } 
//    $('#s4').before('<div id="nav" class="nav">').cycle({
//        fx:     'turnDown',
//        speed:  'fast',
//        timeout: 0,
//        pager:  '#nav'
//    });
}),200);

var ran_once = false;
function onAfter(curr, next, opts) {
	if (ran_once) {
		var current_slide = opts.currSlide + 1;
		window.location.hash = '#' + current_slide;
		_gaq.push(['_trackEvent', 'chosen_images', 'new_image']);

		$('iframe.reload').each(function(i, el) {
			el = $(el);
			var src = el.attr('src');
			if (src.match(/random/)) {
				src.replace(/random=(.*?)&?/, 'random=' + Math.floor(89999999*Math.random()+10000000));
			} else {
				src += '&random=' + Math.floor(89999999*Math.random()+10000000);
			}

			if (src.match(/millis/)) {
			        src.replace(/millis=(.*?)&?/, 'millis=' + Math.floor(89999999*Math.random()+10000000));
			} else {
			        src += '&millis=' + Math.floor(89999999*Math.random()+10000000);
			}

			el.attr('src', src);
		});
	}
	ran_once = true;
}






/*
$('#s5').swipeleft(function(e){
    $('#s5').cycle('prev'); 
    e.preventDefault();
  }); 

$('#s5').swiperight(function(e){
    $('#s5').cycle('next'); 
    e.preventDefault();
  }); 
*/

$('#s5').bind( 'swipeleft', function( e ) {
    $('#s5').cycle('prev'); 
    e.stopImmediatePropagation();
    return false;
  } );  

/*
$('body').bind( 'swipe', function( e ) {
    alert( 'You swiped!' );
    e.stopImmediatePropagation();
    return false;
  } );  
*/

/*
function onAfter(curr, next, opts) {
    var index = opts.currSlide;
    $('#prev')[index == 0 ? 'hide' : 'show']();
    $('#next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
}

function onAfter(curr, next, opts, fwd) {
var $ht = $(this).height();

//set the container's height to that of the current slide
$(this).parent().animate({height: $ht});
}

function onBefore() {
//    $('#output').html("Scrolling image:<br>" + this.src);
    //window.console.log(  $(this).parent().children().index(this) );
}
function onAfterOLD() {
    $('#output').html('<h3>' + this.alt + '</h3>');
//    $('#output').html("Scroll complete for:<br>" + this.src)
//        .append('<h3>' + this.alt + '</h3>');
}

*/

