
$(function() {

    // set banner images to rotate

    var header = $( '#header-image' );
    var headerIndex = 0;
    var headerInterval = 5000;
    var rotateBanner = function() {
        headerIndex = ++headerIndex % BANNER_HEADERS.length;
        var next = $( '<img></img>' )
                    .attr({ src: WEB_ROOT + 'image/upload/' +BANNER_HEADERS[headerIndex]+ '?w=487' })
                    .insertBefore( header )
                    .load(function() {
                        header.animate({ opacity: 0 }, undefined, undefined, function() {
                            header.remove();
                            header = next;
                        });
                    });
    };

    setInterval( rotateBanner, headerInterval );
    rotateBanner();

    // add confirm to deletes

    $( 'form.delete' ).submit(function() {        
        return confirm( 'Are you sure you want to delete this?' );
    });

    var lightboxOpts = {
        fixedNavigation: true,
        imageLoading: WEB_ROOT + 'images/lightbox/lightbox-ico-loading.gif',
        imageBtnPrev: WEB_ROOT + 'images/lightbox/lightbox-btn-prev.gif',
        imageBtnNext: WEB_ROOT + 'images/lightbox/lightbox-btn-next.gif',
        imageBtnClose: WEB_ROOT + 'images/lightbox/lightbox-btn-close.gif'
    };

    $( '.carousel' )
        .jcarousel( {} )
        .find( 'a' )
        .lightBox( lightboxOpts );

    $( '.component' ).each(function() {
        $( '.image a', this )
            .lightBox( lightboxOpts );
    });

    // add confirm to checkout page

    $( '.checkouts' ).each(function() {

        var link = $( '<a></a>' )
                        .attr({ href: WEB_ROOT+ 'page/delivery_terms.html' })
                        .html( 'delivery terms and conditions' );

        var label = $( '<label></label>' )
                        .html( 'I have read the ' )
                        .append( link );
                        
        var checkbox = $( '<input type="checkbox" />' );
        
        $( '<div></div>' )
            .addClass( 'checkout-terms' )
            .append( checkbox )
            .append( label )
            .insertBefore( '.checkouts' );

        $( 'form', this ).submit(function() {
            return checkbox.attr( 'checked' )
                || confirm( 'You have not checked the box to say you have read ' +
                            'the delivery terms and conditions, would you like ' +
                            'to continue anyway?' );
        });

    });


});
