ldg = {
    search: function() {
        var sub = 'O que você deseja comprar?';
        var elm = $('#ProductSearchTitle');
        
        if(elm.val() == '') {
            elm.val(sub);
        }
        
        elm.focus(function() {
            if(elm.val() == sub) {
                elm.val('');
            }
        }).blur(function() {
            if(elm.val() == '') {
                elm.val(sub);
            }
        });
    },
    
    deps: {
        init: function() {
            ldg.deps.checkParents();
            ldg.deps.bulletsEvents();
        },
        
        checkParents: function() {
            $('#cat-box li').each(function() {
                var children = $(this).find('.cat-child:first');
                if(children.length > 0) {
                    //children.hide();
                    $(this).children('a.action').addClass('minus');
                }
            });
        },
        
        bulletsEvents: function() {
            $('#cat-box a.action').click(function(e) {
                e.preventDefault();
                
                var elm = $(this);
                var parent = elm.parent();
                var children = parent.find('.cat-child:first');
                
                if(elm.hasClass('plus')) {
                    children.show();
                    elm.removeClass('plus').addClass('minus');
                } else if(elm.hasClass('minus')) {
                    children.hide();
                    elm.removeClass('minus').addClass('plus');
                }
            });
        }
    },
    
    prodlist: function() {
        var maxImgHeight = 0;
        var imgs = $('.products-list .image');
        imgs.each(function() {
            var elm = $(this);
			var image = elm.find('img');
			var realImgHeight = image.height();
            if(realImgHeight > maxImgHeight) {
                maxImgHeight = realImgHeight;
            }
        });
		imgs.height(maxImgHeight);
        
        var productsList = $('.products-list ul li');
        var prevTop = 0;
        var currentRow = -1;
        var rows = [];
        var newTop;
        var elmSize;
        
        productsList.each(function(index, element) {
            newTop = $(element).offset().top;
            
            if((newTop != prevTop) || (index == 0)) {
                currentRow++;
                rows[currentRow] = {maxHeight:0, elements: []};
            }
            
            elmSize = $(element).height();
            
            rows[currentRow].maxHeight = (rows[currentRow].maxHeight < elmSize) ? elmSize : rows[currentRow].maxHeight;
            rows[currentRow].elements.push($(element));
            
            prevTop = newTop;
        });
        
        $.each(rows, function(index, obj) {
            $.each(obj.elements, function(i, elm) {
                elm.height(obj.maxHeight);
            });
        });
    },
    
    gallery: function() {
        var gallery = $('#image-container a.gal-img');
        if(gallery.length > 0) {
            gallery.fancybox();
        }
    }
}

$(window).ready(function() {
    ldg.search();
    ldg.deps.init();
    ldg.gallery();
});

$(window).load(function() {
    ldg.prodlist();
});