$(document).ready(function() {
    
    /* Wish List Notification */
    
    $('#mmpr-addwishlist a').bind('click', function (event) {
        var url = $(this).attr('href');
        $.ajax({
            'url': url,
            'cache': false,
            'success': function (data, status) {
                $.fn.colorbox({width:'598px', inline:true, href:'#mmpr-wishlist-notification'});
            },
            'error': function (xhr, status, error) {
                alert('There was an error adding this item to your wishlist.');
            }
        });
        return false;
    });
    
    /* Product Images */
    
    $('.product img').attr('src', $('.smproduct a:eq(0)').attr('href'));
    product_images = $('.smproduct');
    var current = 0;
    $('#product-slider').easySlider({
        auto: false, 
        continuous: true,
        nextId: "product-slider-next",
        prevId: "product-slider-prev"
    });
    $('#product-slider a').click(function(){
        $('.product img').attr('src', $(this).attr('href'));
        current = parseInt($(this).attr('rel'));
        return false;
    });
    $('.product a, .slideshow a').colorbox({
        href:       '#mmprlb-slideshow',
        inline:     true,
        onOpen:     function(){
                        $('#mmpr-product-image-links img').attr('src', $('.product img').attr('src'));
                        i = $('a', product_images).index($('a[href=' + $('.product img').attr('src') + ']', product_images));
                        $('#cboxNext img').click(function() {
                            i++;
                            if (i == product_images.length) i = 0;
                            $('#mmpr-product-image-links img').attr('src', $('a', product_images[i]).attr('href'));
                        });
                        $('#cboxPrevious img').click(function() {
                            i--;
                            if (i < 0) i = product_images.length - 1;
                            $('#mmpr-product-image-links img').attr('src', $('a', product_images[i]).attr('href'));
                        });
                    },
        onClosed:   function(){
                        $('#cboxNext img, #cboxPrevious img').unbind('click');
                    },
        width:      '600px'
    });
                    
    /* Tabbed Content */
    
    $('#mmpr-producttabs a').click(function() {
        $('#mmpr-producttabs a').removeClass('current');
        $(this).addClass('current');
        $('#mmpr-product-content li').hide();
        $('#mmpr-product-content li:eq(' + $('#mmpr-producttabs a').index(this) + ')').show();
        return false;
    });
    $('#mmpr-producttabs a:first').click();
    
    /* Related Products */
    
    $('#relateditems-slider').easySlider({
        auto: false, 
        continuous: true,
        nextId: "relateditems-slider-next",
        prevId: "relateditems-slider-prev"
    });
    
    /* Back Button */
    
    $('#mmpr-backbutton a').bind('click', function (event) {
        window.history.go(-1);
        return false;
    });
    
});
