// initialize the jquery code
$(document).ready(function(){
    // One instance that's re-used to show info for the current item
    var itemBubble = $('<div id="itemBubble" class="itemBubble"></div>');
    $('body').append(itemBubble);
       
    // The set of pushers
    var pusher = $('.pusher'); 
    
    // Change the artist name into the link to the artist home page
    $('.artist').each(function(){
        // this is the anchor containing the artists name
        var artistName = $(this).find('span').html();
        $(this).attr('href','/desktopmodules/ovx_dnnbio/redirectbio.ashx?Artist=' + artistName);
        $(this).attr('title','View more artwork from '+ artistName);
    });

    // Change the enlarge anchor into the base for the thickbox.
    $(pusher).find('#enlarge').each(function(){
        // this is the anchor containing the thickbox
        try {
            var enlargeIcon = $(this);
            var productLink = $(this).siblings('.ArtWorkFrame');
            var productTitle = $(this).siblings('.ArtWorkTitle');
            var productImage = $(productLink).find('img');
            var imageSrc = $(productImage).attr('src').match(/(?:\WID=)([^&]*)/)[1];
            
            $(this).attr('href', $.URLDecode(imageSrc).replace(/\+/g,' '));
            $(enlargeIcon).attr('title', 'Click here or the image to enlarge and zoom');
            $(enlargeIcon).attr('caption', productTitle.html());
            $(productLink).click(function() {return false;});
            $(productTitle).click(function() {return false;});
            $(productImage).click(function() {
                $(enlargeIcon).trigger('click');
            });        
        }catch(e){};
    });

    // toggle the cart buttons and the item bubble
    $(pusher).hover(
            function(e) {
                // find the first anchor href in the product display table
                var pusherHref = $(this).find('a').eq(0).attr('href');
                
                // extract the product id from the url
                var productID = pusherHref.match(/(?:productid|pid)[=|/](\d+)/i)[1];

                // find the product picture for this item
                var productIMG =  $(this).find('.ArtWorkFrame').find('img');

                // position the balloon relative to the picture
                var pos = $(productIMG).offset();
                var width = $(productIMG).width();
                var height = $(productIMG).height();
                var windowWidth = $(window).width();
                
                //$(productIMG).attr('title', 'Click image to view a larger size and zoom into the image');

                // find the enlarge thickbox feature
                var enlargeImg = $(this).find('#enlarge');
                $(enlargeImg).show();

                // change alpha to the rest of the products
                $(this).parent().parent().parent().find('.pusher').animate({opacity:0.5},{duration:300,queue:false});
                $(this).animate({opacity:1},{duration:300,queue:false});

                // find, position and show the cart for this item
                $(this).find('.cart').show();

                var bubbleEdge = pos.left + width + 500;
                var orientation = 'left';
                var bubbleTop = $(window).scrollTop() + 30;//pos.top + window.screenTop + 30;

                if ((pos.top - 30) < 0)
                    bubbleTop = 30;
                else
                    bubbleTop = pos.top - 30;

                if ((bubbleEdge > windowWidth) && (pos.left > 500)) {
                    orientation = 'left';
                    itemBubble.css({
                        left: (pos.left - 500 - 20),
                        top: bubbleTop
                    });
                    $(enlargeImg).css({
                        left: pos.left + width - 10,
                        top: pos.top - 10
                    });
                } else {
                    orientation = 'right';
                    itemBubble.css({
                        left: (pos.left + width + 21),
                        top: bubbleTop
                       
                    });
                    $(enlargeImg).css({
                        left: pos.left - 10,
                        top: pos.top - 10
                    });
                }              

                // Clear any previous results             
                $('#itemBubble').html('&nbsp;');

                // Grab the current time so we can pass it as a dummy parameter to prevent caching.
                var currentDate = new Date();
                var mytime = currentDate.getTime();
                
                // Get the balloon contents and fill it.
                $.ajax({
                    type: 'GET',
                    url: '/desktopmodules/ovxitemdetail/itemdetail.aspx',
                    data: 'p=' + productID + '&o=' + orientation + '&y=' + (e.pageY - bubbleTop) + '&t=' + mytime,
                    success: function(data) {
                        // Verify that we're pointed to a page that returned the expected results.
                        if (data.indexOf('balloonContainer') < 0) {
                            $(itemBubble).html('<span >Product ' + productID + ' not return a valid result.'
                                + ' Please have your administrator check the error log.</span>');
                        }

                        // verify this is the correct request
                        // if (data.indexOf(currentID) > 0) {                  
                            var text = $(data).find('#balloonContainer').html();
                            $(itemBubble).html(text);
                        //}
                    }
                });
                $(itemBubble).show();
            });
            
      
    $(pusher).mouseleave(
            function() { 
                // hide the cart buttons
                $(this).find('.cart').hide();
                
                // hide bubble
                $(itemBubble).hide();
                
                // hide enlarge icon on corner
                $(this).find('#enlarge').hide();

                // make all products full alpha
                $(this).parent().parent().parent().find('.pusher').animate({opacity:1},{duration:300,queue:false});
            });
});