"use strict";

var ldg = {};

ldg.search = function () {
    var sub = 'O que você deseja comprar?',
        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);
        }
    });
};

ldg.deps = {};

ldg.deps.init = function () {
    ldg.deps.checkParents();
    ldg.deps.bulletsEvents();
};

ldg.deps.checkParents = function () {
    $('#cat-box li').each(function () {
        var children = $(this).find('.cat-child:first');

        if (children.length > 0) {
            $(this).children('a.action').addClass('minus');
        }
    });
};

ldg.deps.bulletsEvents = function () {
    $('#cat-box a.action').click(function (e) {
        var elm = $(this),
            parent = elm.parent(),
            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');
        }

        return false;
    });
};

ldg.prodlist = function () {
    var maxImgHeight = 0,
        imgs = $('.products-list .image'),
        productsList = $('.products-list ul li'),
        prevTop = 0,
        currentRow = -1,
        rows = [],
        newTop,
        elmSize;

    imgs.each(function () {
        var elm = $(this),
            image = elm.find('img'),
            realImgHeight = image.height();

        if (realImgHeight > maxImgHeight) {
            maxImgHeight = realImgHeight;
        }
    });

    imgs.height(maxImgHeight);

    productsList.each(function (index, element) {
        newTop = $(element).offset().top;

        if ((newTop !== prevTop) || (index === 0)) {
            currentRow += 1;
            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);
        });
    });
};

ldg.zoom = function () {
    if ($('#zoom-pic').length > 0) {
        var options = {
            zoomWidth: 200,
            zoomHeight: 200,
            xOffset: 20,
            yOffset: 0,
            position: "right",
            preloadText: 'Carregando zoom...',
            title: false
        };

        $('#zoom-pic').jqzoom(options);

        $('#images-list a').click(function () {
            var elm = $(this),
                parent = elm.parent(),
                href = elm.attr('href'),
                img = elm.children('.cache').children('img').attr('src'),
                destLink = $('#zoom-pic'),
                destImg = $('#zoom-pic img');

            destLink.attr('href', href);
            destImg.attr('src', img);

            $('#images-list li.current').removeClass('current');
            parent.addClass('current');

            return false;
        });
    }
};

ldg.personals = {
    init: function () {
        ldg.personals.disable();
        ldg.personals.defaultValues();

        $('#personalization').show();

        $('#personal-choice').change(function () {
            var elm = $(this),
                value = elm.attr('value'),
                id = elm.find('option[value=' + value + ']').attr('rel'),
                child = $('#personal-' + id);

            ldg.personals.disable();

            if (child.length > 0) {
                $('#personal-' + id).show().find('input, select').removeAttr('disabled');
            }
        });

        $('#personal-opts select').change(function () {
            var elm = $(this),
                value = elm.val(),
                id = elm.find('option[value=' + value + ']').attr('rel'),
                ids = [],
                child = $('#personal-node-' + id);

            elm.find('option').each(function () {
                if ($(this).val() !== '') {
                    ids.push($(this).attr('rel'));
                }
            });

            ldg.personals.disableNodes(ids);

            if (child.length > 0) {
                child.show().find('input, select').removeAttr('disabled');
            }
        });
    },

    disable: function () {
        $('#personal-opts').find('fieldset').hide().end()
            .find('input, select').attr('disabled', 'disabled').end()
            .find('select').val('');
    },

    disableNodes: function (ids) {
        $.each(ids, function (index, value) {
            var node = $('#personal-node-' + value);

            if (node.length > 0) {
                node.hide().find('input, value').attr('disabled', 'disabled');
            }
        });
    },

    defaultValues: function () {
        $('#personal-opts input').each(function () {
            var elm = $(this),
                defaultValue = elm.val();

            elm.focus(function () {
                if (elm.val() === defaultValue) {
                    elm.val('');
                }
            }).blur(function () {
                if (elm.val() === '') {
                    elm.val(defaultValue);
                }
            });
        });
    }
};

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

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