// Use initCallback to assign control funcitonality to the carousel
function slideshow_initCallback(carousel) {
    // Use the specified next/prev buttons for the carousel
    $('#carousel-next').bind('click',
    function() {
        carousel.next();
        return false;
    });
    $('#carousel-previous').bind('click',
    function() {
        carousel.prev();
        return false;
    });
}

function Nielsen_Event() {
    var d = new Image(1, 1);
    d.src = ["//secure-us.imrworldwide.com/cgi-bin/m?ci=us-400338h&cg=0&cc=1&si=",
    escape(window.location.href), "&rp=", escape(document.referrer),
    "&c0=usergen,1&rnd=", (new Date()).getTime()].join('');
}

$(document).ready(function() {
    $('.gallery-item').bind('slidechange',
    function(event, idx) {
        $('.ad-placement').each(function() {
            if ($(this).children('iframe')) {
                var ord = Math.random() * 10000000000000000;
                var isrc = $(this).children('iframe').attr("src").replace(/ord=.*$/, 'ord=' + ord + '?');
                $(this).children('iframe').attr("src", isrc);
            }
            s.prop37 = "Photo " + idx;
            s.events = "";
            void(s.t());

            if (typeof reload_flyerboard == 'function') {
                reload_flyerboard();
            }
            if (typeof Nielsen_Event == 'function') {
                Nielsen_Event();
            }
        });
        return false;
    });
    $('.gallery-nav #next a').click(function() {
        var next = $('ul#more-in-this-gallery-inner li.selected').next().find('a');
        next.trigger('click');
        return false;
    });
    $('.gallery-nav #previous a').click(function() {
        var prev = $('ul#more-in-this-gallery-inner li.selected').prev().find('a');
        prev.trigger('click');
        return false;
    });
    $('ul#more-in-this-gallery-inner').jcarousel({
        scroll: 6,
        initCallback: slideshow_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
    $('ul#more-in-this-gallery-inner li a').click(function() {
        var a = $(this).attr('href');
        if ($(this).parent().hasClass('selected')) return false;
        var t = $(this).attr('alt');
        var c = $(this).attr('title');
        var img = '<img class="slide next" src="' + a + '" alt="' + t + '" />';
        $('.gallery-slide').append(img);
        $('.gallery-slide img.current').fadeOut('fast',
        function() {
            $(this).remove();
        });
        $('.gallery-slide img.next').fadeIn('fast',
        function() {
            $(this).removeClass('next').addClass('current');
        });
        $('h4.image-title').html(t);
        $('.image-caption').html(c);
        $('ul#more-in-this-gallery-inner li.selected').removeClass('selected');
        var i = $(this).parent().addClass('selected').attr('jcarouselindex');
        $('#photo-number .current').html(i);
        $(this).parent().parent().parent().parent().parent().parent().parent().trigger('slidechange', [i]);
        return false;
    });
});
