function slider_initCallback(carousel) {
    $('#slider-controls span').bind('click', function() {
        var ix = $(this).index() + 1;
        var newState = 'state' + ix;
        $('#slider-controls').removeAttr('class').addClass(newState);
        carousel.scroll($.jcarousel.intval(ix));
        carousel.stopAuto();
    });
}

function slider_itemFirstInCallback(carousel, item, idx, state) {
    var newState = 'state' + idx;
    $('#slider-controls').removeAttr('class').addClass(newState);
}

$(function() {
    // Nav drop down
    $('#nav > ul > li').hover(function() {
        $(this).find('a:eq(0)').toggleClass('hover');
        $(this).find('.dd').stop(true, true).slideToggle('fast');
    });

    // Home page slider
    $('#slider ul').jcarousel({
        scroll : 1,
        visible : 1,
        auto : 5,
        initCallback : slider_initCallback,
        itemFirstInCallback : slider_itemFirstInCallback,
        buttonPrevHTML : null,
        buttonNextHTML : null
    });
    
    // Sliding boxes
    $('.box-details-l a').click(function() {
        $(this).parent().prev().slideToggle();
        $(this).toggleClass('open');
        return false;
    });
    
    // Home page flip images
    var flip_length = $('.box-flips li').length-1;
    var i = 0;
    window.setInterval(function() {
        if($.browser.msie) {
            $('.box-flips li').eq(i).css({
                'z-index' : i
            });
            $('.box-flips li').eq(i).next().css({
                'z-index' : flip_length+1
            });
            if(i<flip_length) {
                i++;
            } else {
                i=0;
                $('.box-flips li:last').css({
                    'z-index' : 1
                });
                $('.box-flips li:first').css({
                    'z-index' : flip_length+1
                });
            }
        } else {
            $('.box-flips li').eq(i).animate({
                opacity : 0.5
            });
            $('.box-flips li').eq(i).next().animate({
                opacity : 0.5
            }, {
                complete : function() {
                    $(this).css({
                        'z-index' : flip_length
                    }).animate({
                        opacity : 1
                    });
                    $(this).prev().css({
                        'z-index' : $(this).index()-1
                    }).animate({
                        opacity : 1
                    });
                }
            });
            if(i<flip_length) {
                i++;
            } else {
                i=0;
                $('.box-flips li:last').animate({
                    opacity : 0.5
                });
                $('.box-flips li:first').animate({
                    opacity : 0.5
                }, {
                    complete : function() {
                        $(this).css({
                            'z-index' : 3
                        }).animate({
                            opacity : 1
                        });
                        $('.box-flips li:last').css({
                            'z-index' : 1
                        }).animate({
                            opacity : 1
                        });
                    }
                });
            }
        }
    }, 4000);
    
    // PNG fixes
    if($.browser.msie && $.browser.version.substr(0,1) == 6) {
        DD_belatedPNG.fix('#logo a, .border-t, .border-b, #nav .dd-t, #nav .dd-m, #nav .dd-b, .paperclip, .btn-blue, .btn-grey, .qty-toggle');
    }
});
