$().ready(function(){
    var timeout;
    $('.product').each(function(){
        var fadeIn = function(elem) {
            clearTimeout(timeout);
            timeout = setTimeout(function(){ elem.fadeIn() }, 150);
        }
        
        var $this = $(this);
        var $o = $this.children('.overlay');
        
        var pO = $this.offset();
        var wW = $(window).width();
        var left;
        if(pO.left > (wW / 100) * 58) {
            left = -230;
        } else {
            left = 100;
        }
        
        $o.children('.info').css({ top: 50, left: left });
        
        $this.mouseenter(function(){ $o.closest('.product').css('z-index', 10000); fadeIn($o); })
             .mouseleave( function(){ clearTimeout(timeout); $o.fadeOut().closest('.product').css('z-index', 2);  });
        
        $this.find('a.buy').click(function(){
            var data = {
                'product_id': this.id.substr(this.id.lastIndexOf('-')+1),
                'quantity': 1,
                'attributes': []
            }
            
            $.post(Ctrl.Config.baseUrl + '/cart/add/', {data : JSON.stringify(data) }, function(resp) {
                $.flashMessenger('Product added to cart.');
            });
            
            return false;
        });
    });

});
