var AjaxBasket = Class.create();
AjaxBasket.prototype = {
    initialize: function(options)
    {
        this.options = options;
		var _this = this;
		$j(document).ready(function() { _this.bindEvents(); });
    },
	bindEvents: function()
	{
		var _this = this;
		this.dropdown = $j('#' + this.options.dropdownId);
		this.dropdownToggle = $j(this.options.dropdownToggle);
		this.dropdownToggle.click(function()
		{
			_this.toggleDropDown();
		});
	},
	toggleDropDown: function()
	{
		var _this = this;
		if (this.dropdown.css('display') == 'none')
		{
			this.showDropDown();
		}
		else
		{
			this.hideDropDown();
		}
	},
	showDropDown: function()
	{
		var _this = this;
		if (this.dropdown.css('display') == 'none')
		{
			this.dropdown.slideDown('slow', function()
			{
				_this.dropdownToggle.text(_this.options.collapseText);
				_this.dropdownToggle.addClass('collapsed');
				$j('body').click(function(event)
				{
					if (!$j(event.target).closest(_this.dropdown).length)
					{
						_this.hideDropDown();
					};
				});
			});
		}
	},
	hideDropDown: function()
	{
		var _this = this;
		if (this.dropdown.css('display') != 'none')
		{
			this.dropdown.slideUp('fast', function()
			{
				_this.dropdownToggle.text(_this.options.expandText);
				_this.dropdownToggle.removeClass('collapsed');
				$j('body').unbind('click');
			});
		}
	},
	addSet: function (id, btn, related_products)
	{
		this.related_products = related_products;
		this.add(id, 1, btn);
		this.related_products = null;
	},
    add: function(id, qty, btn, overlay_id)
    {
        var _this = this;
		var parameters = '';
		
		var qtyBox = $('qty_' + id);
		
		var formVar = 'productAddToCartForm';
		
		if (window[formVar] === undefined)
		{
			parameters = 'product='+id+'&qty='+qty;
		}
		else
		{
			parameters = window[formVar].form.serialize();
		}
		
		if (this.related_products)
		{
			parameters += '&related_product=' + this.related_products;
		}
		
        new Ajax.Request(
            //_this.options.addUrl + 'id/' + id + '/qty/' + qty,
			_this.options.addUrl,
            {
                method: 'post',
				parameters: parameters,
                onSuccess: function(transport)
                {		
                    if (transport && transport.responseText)
                    {
                        try {
                            response = eval('(' + transport.responseText + ')');
                        }
                        catch (e) {
                            response = {};
                        }
                        var overlay = $j('#' + overlay_id);
                        if (response.success)
                        {
                            overlay.addClass('ajaxbasket-message-success');
                        }
                        else
                        {
                            overlay.addClass('ajaxbasket-message-failure');
                        }
						_this.refresh();
						if (qtyBox)
						{
							qtyBox.value = 1;
						}
                        overlay.html(response.message);
                        overlay.fadeIn('fast', function()
                        {
                            setTimeout(function()
                            {
                                overlay.fadeOut('slow', function()
                                {
                                    
                                });
                            }, 2000);
                        });
                    }
                }
            }
        );
    },
    remove: function(itemId)
    {
        var _this = this;
        new Ajax.Request(
            _this.options.removeUrl + 'id/' + itemId,
            {
                method: 'get',
                onSuccess: function(transport)
                {
                    if (transport && transport.responseText)
                    {
                        try {
                            response = eval('(' + transport.responseText + ')');
                        }
                        catch (e) {
                            response = {};
                        }
                        _this.refresh();
                    }
                }
            }
        );
    },
    refresh: function()
    {
        var _this = this;
		$j('html, body').animate({scrollTop:0}, 'slow');
        new Ajax.Request(
            _this.options.sidebarUrl,
            {
                method: 'get',
                onSuccess: function(transport)
                {
                    var html = $j(transport.responseText);
                    $j('div.block-cart').html(html.html());
					$j('div.block-cart').addClass('item-added');
					setTimeout(function()
					{
						$j('div.block-cart').removeClass('item-added');
					}, 3000);
					_this.bindEvents();
					_this.showDropDown();
                }
            }
        );
    }
};
