function wEvent(name, params)
{
    var e = new WoopraEvent(name);
    for (i in params) {
        e.addProperty(i, params[i]);
    }
    e.fire();
}

function validateForm(form, options) {
    function animate(el, type) {
        el.animate({'width':type, 'opacity':type}, 'slow');
    }

    options = options || {};

    options.errorPlacement = function(error, element) {
        var div = $(document.createElement('div')).addClass('error-container')
            .append($(document.createElement('div')).addClass('error')
                .append($(document.createElement('em')).addClass('arrow'))
                .append(error)
            ).hide();
        element.after(div);
        animate(div, 'show');
    }

    options.showErrors = function(errorMap, errorList) {
        for (var i = 0; errorList[i]; i++) {
            var error = errorList[i];
            var label = validator.errorsFor(error.element);
            if (label.length) {
                animate(label.parent().parent(), 'show');
                label.html(error.message);
                validator.toShow = validator.toShow.add(label);
            } else {
                validator.showLabel( error.element, error.message );
            }
        }
        validator.toHide = validator.toHide.not( this.toShow );
        for (i = 0; validator.toHide[i]; i++) {
            label = $(validator.toHide[i]);
            animate(label.parent().parent(), 'hide');
        }
    }

    var validator = form.validate(options);
}

function UnirgyPricing(config) {
    var checkboxes = $('.product-checkbox'), products = 0, totals = {}, form = $('#addtocart');

    function calc() {
        var i;
        for (i in config.prices) {
            totals[i] = 0;
        }
        products = 0;
        checkboxes.each(function() {
            if (!this.checked) return;
            products++;
            for (i in config.prices) {
                if (config.prices[i]['products'][this.value]) {
                    totals[i] += config.prices[i]['products'][this.value]['price'];
                }
            }
        });
        for (i in config.prices) {
            $('#total-'+i).html(totals[i]);
        }
    }

    checkboxes.each(function() {
        this.checked = false;
    });
    checkboxes.click(calc);

    form.submit(function (e) {
        e.preventDefault();
    });

    $('.group-addtocart').click(function (e) {
        e.preventDefault();
        if (!products) {
            alert('Please add at least one product to the cart');
            return false;
        }
        var params = form.serializeArray();
        params.push({name:'bundle', value:this.value});
        cart.startUpdate();
        $.post(form.get(0).action, params, cart.completeUpdate, 'json');

        var wps = {}, i;
        for (i in params) {
            wps[i] = params[i].value;
        }
        wEvent && wEvent('Add To Cart', wps);
    });
}

function UnirgyCart(config) {
    var isOpen;

    function open() {
        //wEvent && wEvent('Cart', {action:'Open'});
        $('.mini-cart').removeClass('cart-closed').addClass('cart-expanded');
        $('.jScrollPaneContainer, .jScrollCap, .jScrollPaneTrack').show();
        $('.cart-details').jScrollPane({scrollbarWidth:10, scrollbarMargin:3});
        isOpen = true;
    }

    function close() {
        //wEvent && wEvent('Cart', {action:'Close'});
        $('.jScrollPaneContainer, .jScrollCap, .jScrollPaneTrack').hide();
        $('.mini-cart').removeClass('cart-expanded').addClass('cart-closed');
        isOpen = false;
    }

    function init() {
        if (location.href.match(/checkout\/cart/)) return;

        $('.cart-count').click(function(e) {
            e.preventDefault();
            if (!isOpen) open(); else close();
        });

        $('.mini-cart').click(function(e) {
            e.preventDefault();
            e.stopPropagation();
        });

        $('.checkout-button, .mini-cart .product-link').click(function (e) {
            //wEvent && wEvent('Checkout', {});
            location.href = this.href;
        });

        $('.remove-from-cart').click(function (e) {
            e.preventDefault();
            cart.startUpdate();
            var id = this.id.replace(/^remove-/, '');
            $.post(BASE_LINK+'checkout/process.php', {'do':'remove', id:id}, cart.completeUpdate, 'json');
            wEvent && wEvent('Remove From Cart', {});
        });
    }
    init();

    $(document).click(function(e) {
        if (isOpen) close();
    });

    return {
        startUpdate: function() {
            open();
            //$('.cart-loader').show();
        },
        completeUpdate: function(result, status) {
            if (result.error) {
                alert(result.error);
                return;
            }
            $('.mini-cart-container').html(result.carthtml);
            open();
            init();
        }
    }
}