J.prime = {

    init : function () {
        Shadowbox.init({
            skipSetup : true
        });
        that = this;
        setTimeout(function(){
            that.animate_product_thumbs_position(10, $('#prime-image img').width(), $('#prime-image img').height());
        }, 350);        
    },

    show_image : function(anchor, url, gallery_index, width, height) {

        // if current 
        if($('#product-image img').attr("src") == url) { return false; }

        // clear selected image
        $('#product-thumbs a').removeClass('selected');

        // set new selected
        $(anchor).addClass('selected');
        
        // fade out old image
        $('#product-image img').fadeOut(100);
        
        // set new rel for gallery index        
        $('#product-image img').attr('rel', gallery_index);

        // set new holder size
        $('#product-image').animate({
            height: (width > height) ? height : height - 20
        }, 250, 'swing', function(){
            $('#product-image img').load(function(){
                J.prime.set_orientation_for_image($('#product-image img'));
                $('#product-image img').fadeIn(100);
                $('#product-image img').bind('load', function(){}); // remove load listener
            }).attr('src', url);
        });
        this.animate_product_thumbs_position(400, width, height);
    },
    
    display_product_image_gallery : function () {
        var first = parseInt($('#product-image img').attr('rel'), 0);
        var imgs = [];
        for(var i=0; i<shadowbox_gallery.length; i++) {
            imgs.push(
                {
                    el: document.createElement('img'),
                    player: 'img',
                    content: shadowbox_gallery[i]
                }
            );
        }
        Shadowbox.open(imgs, {counterType:'skip'}, first);
    },
    
    animate_product_thumbs_position : function (speed, width, height) {
        width = width || $('#product-image img').width();
        height = height || $('#product-image img').height();
        var product_image_bottom_y = width > height ? height : height - 20;
        var thumbnail_pane_size = $('#product-thumbs').height();
        var desc_height = $('#product-detail-height-wrapper').height();
        var top = (product_image_bottom_y - desc_height) - thumbnail_pane_size;
        top = top <= 20 ? 20 : top;
        $('#product-thumbs').animate({
            'top' : top
        }, speed, function() {
            $('#product-thumbs').fadeIn(100);
        });
        
    },
    
    set_orientation_for_image : function (img) {
        var c = img.width() > img.height() ? 'landscape' : 'portrait';
        $('#product-image').removeClass('landscape').removeClass('portrait').addClass(c);
    }
    
};