$().ready(function(){
    var timeout;
    $('#products .product').each(function(){
        var fadeIn = function(elem) {
            clearTimeout(timeout);
            timeout = setTimeout(function(){ elem.fadeIn(100); elem.parent().css('z-index', 2); elem.css('z-index', 1); }, 200);
        }
        
        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 = -240;
        } else {
            left = 110;
        }
        
        /* colin@brainbits.ca */
        if (pO.top > $('#content').height() * .66) {
        	var top = -100;
        } else {
        	var top = 40;
        }
        
        $o.children('.info').css({ top: top, left: left });
        
        $this.mouseenter(function(){ fadeIn($o); })
             .mouseleave(function(){ clearTimeout(timeout); $o.fadeOut(100, function(){ $o.parent().css('z-index', 0); }); });
        
/*        $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;
        });
*/
    });

    $('#search-shop :text').tipify();
});

