$j(document).ready(function() {
    $j('body').append($j('<span>', {
        'class': 'actual-popup'
    }));
    
	initProductGridPopup();
});

function initProductGridPopup() {
    $j('.products-grid li.item img.product-grid-image').each(function() {
        //$j(this).attr('alt', '');
        $j(this).parent().attr('title', '');
    }).mousemove(function(e) {
        var left_offset = e.pageX - 170;
        var top_offset = e.pageY + 50;
        $j('.actual-popup').css({
            'top': top_offset + 'px',
            'left': left_offset + 'px'
        }).data('left', left_offset).data('top', top_offset);
    }).hoverIntent({
        sensitivity: 30,
        interval: 120,
        over: function(e) {
            var element_parent = $j(this).parent();
            var existing_data = element_parent.data('popup');

            if (existing_data == null) {
                var data = $j('<div>', {
                    'class': 'popup-wrapper'
                }).append($j('<span>', {
                    'class': 'name-wrapper',
                    'html': element_parent.siblings('.product-name').html()
                })).append($j('<span>', {
                    'class': 'free-delivery-wrapper',
                    'html': "Free Delivery"
                })).append($j('<span>', {
                    'class': 'ratings-wrapper',
                    'html': element_parent.siblings('.ratings-wrapper').html()
                })).append($j('<span>', {
                    'class': 'description-wrapper',
                    'html': element_parent.siblings('.product-description').html()
                }));

                element_parent.data('popup', data);
                updatePopup(data);
            } else {
                updatePopup(existing_data);
            }
        },
        timeout: 250,
        out: function() {
            // pass
        }
    }).hover(function() {
        // pass
    }, function() {
        $j('.actual-popup').hide();
        $j('.actual-shadow').hide();
    });
}

function updatePopup(data) {
    if (data != '') {
        $j('.actual-popup').html(data).prepend($j('<span>', {
            'class': 'actual-popup-arrow'
        })).show();
        $j('.actual-shadow').remove();

        /* Drop shadow code: */
        left_offset = $j('.actual-popup').data('left');
        top_offset = $j('.actual-popup').data('top');

			i = 1;
            var shadow_left_offset = 0 - i;
            var shadow_top_offset = 0 - i + 13;
            var shadow_width = $j('.actual-popup .popup-wrapper')[0].scrollWidth + (2 * i);
            var shadow_height = $j('.actual-popup .popup-wrapper')[0].scrollHeight + (2 * i);
            
            // IE fixing code
            if( jQuery.browser.msie )
            {
                shadow_width += 2;
                shadow_height += 2;
            }
            // end IE fixing code

            $j('.actual-popup').prepend($j('<div>', {
                'class': 'actual-shadow',
                'style': 'left: ' + shadow_left_offset + 'px; top: ' + shadow_top_offset + 'px; width: ' + shadow_width + 'px; height: ' + shadow_height + 'px;'
            }));

    }
}

