﻿/// <reference path="site.js" />

site.shoppingcart = function (settings) {
    var _settings = {
        $form: $('#addtoshoppingcart'),
        $updateshoppingcart: $('#updateshoppingcart'),
        addUrl: '/shoppingcart/add',
        shoppingCartTypeId: 2,
        gotoCart: true,
        imageSrc: '/content/images/indicator.gif'
    };

    if (settings) {
        $.extend(_settings, settings)
    }

    return {
        initialize: function () {
            this.bindSubmitShoppingCart();
            this.bindSubmitUpdateShoppingCart();
            this.bindSubmitUpdateShoppingCartUrl();
            this.bindDeleteShoppingCartItem();
        },

        bindDonateClick: function () {
            var self = this;
            $('.donateclick').click(function (e) {
                self.getItemId();
            });
        },

        bindDonateChange: function () {
            var self = this;
            $('.donatechange').change(function () {
                self.getItemId();
            });
        },

        getItemId: function () {
            $.post('/shoppingcart/GetDonationItem', $('#addtoshoppingcart').serialize(), function (d) {
                $('#itemid').attr('name', 'ItemId-' + d.ItemId);
            })
        },

        bindSubmitShoppingCart: function () {
            var self = this;
            _settings.$form.submit(function (e) {
                e.preventDefault();
                switch (_settings.shoppingCartTypeId) {
                    case site.enums.shoppingCart.multiple:
                        self.addMultiple();
                        break;

                    case site.enums.shoppingCart.single:
                        self.addSingle();
                        break;
                }
            })
        },

        addMultiple: function () {
            var indicator = spider.indicator(null, _settings.$form, { minDuration: 1000, imageSrc: _settings.imageSrc });
            indicator.start();
            $.post(_settings.addUrl, _settings.$form.serialize(), function (d) {
                //google analytics
                try {
                    _gaq = _gaq || [];
                    _gaq.push(['_trackEvent',
                               'ShoppingCart', // category
                               d.Action, // action
                               d.OrderNumber, // opt_label - OrderNumber
                               0 // opt_value
                    ]);
                } catch (ex) {
                    console.log(ex);
                }
                if (d.Success) {
                    if (_settings.gotoCart) {
                        document.location = '/shoppingcart/'
                    } else {
                        indicator.stop();
                    }
                } else {
                    indicator.stop();
                    $('#addtocartmessage').show();
                    $('#addtocartmessage').html(d.Message);
                }

            })
        },

        getSettings: function () {
            return _settings
        },

        addSingle: function () {

        },

        bindSubmitUpdateShoppingCart: function () {
            var self = this;
            _settings.$updateshoppingcart.submit(function (e) {
                e.preventDefault();
                var indicator = spider.indicator(null, $('#updateshoppingcarturl'), { minDuration: 1000, imageSrc: '/content/images/indicator.gif' });
                indicator.start();
                $.post('/shoppingcart/update', _settings.$updateshoppingcart.serialize(), function (d) {
                    //google analytics
                    try {
                        _gaq = _gaq || [];
                        _gaq.push(['_trackEvent',
                               'ShoppingCart', // category
                               d.Action, // action
                               d.OrderNumber, // opt_label - OrderNumber
                               0 // opt_value
                    ]);
                    } catch (ex) {
                        console.log(ex);
                    }
                    indicator.stop(function () {
                        self.refreshShoppingCart();
                    });
                })
            })
        },

        refreshShoppingCart: function () {
            $.get('/shoppingcart/shoppingcartitems/' + _settings.$updateshoppingcart.data('orderid') + '?r=' + new Date().getMilliseconds(), function (data) {
                _settings.$updateshoppingcart.html(data)
            })
        },

        bindSubmitUpdateShoppingCartUrl: function () {
            _settings.$updateshoppingcart.delegate('#updateshoppingcarturl', 'click', function (e) {
                e.preventDefault();
                _settings.$updateshoppingcart.submit();
            })
        },

        bindDeleteShoppingCartItem: function () {
            var self = this;
            _settings.$updateshoppingcart.delegate('.delete', 'click', function (e) {
                e.preventDefault();
                var element = $(this)
                element.closest('tr').remove();
                var itemId = 'ItemId-' + element.data('remove');
                $.post('/shoppingcart/remove/', itemId + '=1', function () {
                    self.refreshShoppingCart();
                })
            })
        }
    }
}
