/**
 * User: maurogadaleta
 * Date: 06/10/11
 * Time: 20:35
 */
jQuery(document).ready(function () {
    var $shop = $('#cart');


    var _openShop = function () {
        $('.extended .more_info').slideDown();
        $.ajax({url:'?service=shop&ajax=1&action=setShopOpen&open=1'});
        $('#open_shop').hide();
        $('#close_shop').show();
    }

    var _closeShop = function () {
        $('.extended .more_info').slideUp();
        $.ajax({url:'?service=shop&ajax=1&action=setShopOpen&open=0'});
        $('#open_shop').show();
        $('#close_shop').hide();
    }

    var _sendQuantityToAjax = function (id, value) {
        $.ajax({
            url:'?service=shop&ajax=1&action=updateQuantityItem&params=' + id + ',' + value,
            success:function (data) {
                $shop.html(data);
                _listeners();
                _openShop();
            }
        });
    }

    var _cleanAll = function () {
        $('.quantity').each(function () {
            var id = $(this).attr('rel');
            _sendQuantityToAjax(id, 0);
        });
    }

    var _listeners = function () {
        $('#open_shop').click(_openShop);
        $('#close_shop').click(_closeShop);
        $('#clean_all').click(_cleanAll);

        $('#submit').click(function () {
            $.ajax({
                url:''
            });
        });

        $('.quantity').change(function () {
            var id = $(this).attr('rel');
            var value = $(this).val();
            _sendQuantityToAjax(id, value);

        })
    }

    $('.toCart').click(function () {
        var that = $(this);
        $shop.html('').addClass('loading');
        $.scrollTo(0, 500, {
            onAfter:function () {
                _closeShop();
                _addItem(that);
            }
        });

        return false;
    });

    var _addItem = function (el) {
        var type = '';
        var id, option_id = undefined;
        if (el.attr('rel') === 'form') {
            type = 'product';
            id = $('#product_id').val();
            option_id = $('#option_id').val();
        }
        else {
            type = 'book';
            id = el.attr('rel');
        }

        $.ajax({
            url:'?service=shop&ajax=1&action=addItemToCart&params=' + type + ',' + id + ',' + option_id,
            success:function (data) {
                $shop.removeClass('loading').html(data);
                _listeners();
                _openShop();
            }
        });
    }

    $.ajax({
        url:'?service=shop&ajax=1',
        success:function (data) {
            $shop.removeClass('loading').html(data);
            _listeners();
            $.ajax({
                url:'?service=shop&action=getShopOpen&ajax=1',
                success:function (data) {
                    console.log(data);
                    if (data === '1')
                        _openShop();
                    else
                        _closeShop();
                }
            })
        }
    });
});
