// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

var CatalogCart = function(container){
	var me = {
		_data: [],
		list: function(){
			return jQuery(me._data);
		},
		get: function(id){
			var found = undefined;
			jQuery(me._data).each( function(){
				if( this.id == id )
				{
					found = this;
					return false;
				}
			});
			return found;
		},
		has: function(id){
			var found = false;
			jQuery(me._data).each( function(){
				if( this.id == id )
				{
					found = true;
					return false;
				}
			});
			return found;
		},
		add: function(id){
			if( me.has(id) )
				return this;

			jsonPost(SITE.baseurl+'site/cart/add',{
				id:id
			}, function(data){
				if( data && typeof data == 'object' && data.status == 'success' && data.data )
				{
					me._data.push(data.data);
					
					if( jQuery.isFunction(me.onAdd) )
						me.onAdd(data.data);

					if( jQuery.isFunction(me.onChange) )
						me.onChange();
				}
			});

			return this;
		},
		count: function(){
			return jQuery(me._data).size();
		},
		price: function(){
			var p = 0;
			jQuery(me._data).each(function(){
				if( this.price && this.count )
					p += parseInt(this.price,10) * parseInt(this.count,10);
			});
			return p;
		},
		setTotal: function(id,total,callback){
			jsonPost(SITE.baseurl+'site/cart/count',{
				id:id,
				count:total
			}, function(data){
				if( data && typeof data == 'object' && data.status == 'success' )
				{
					if( jQuery.isFunction(callback) )
						callback();

					if( jQuery.isFunction(me.onChange) )
						me.onChange();
				}
			});
		},
		remove: function(id){
			if( me.has(id) )
				jsonPost(SITE.baseurl+'site/cart/remove',{
					id:id
				}, function(data){
					if( data && typeof data == 'object' && data.status == 'success' )
					{
						me.list().each(function(index){
							if( this.id == id )
							{
								me._data.remove(index);
								return false;
							}
						});

						if( jQuery.isFunction(me.onChange) )
							me.onChange();
					}
				});
			return this;
		},
		refresh: function(){
			jsonGet(SITE.baseurl + 'site/cart/list',{},function(data){
				if( data && typeof data == 'object' && data.status == 'success' && data.items && data.items.length > 0 )
				{
					jQuery(data.items).each(function(){
						me._data.push(this);
					});
				}

				if( jQuery.isFunction(me.onLoad) )
					me.onLoad();
			});
			return this;
		}
	};
	return me;
}

function createToCart( id, type )
{
	if( !id )
		return undefined;

	if( !type )
		type = 'cart-small';

	var c = jQuery('#site-templates').tmpl({
		text:'в корзину',
		template: type
	});
	c.data('id',id);
	c.click( function(){
		SITE.cart.add(id);
	});
	return c;
}

function createFromCart( id, type )
{
	if( !id )
		return undefined;

	if( !type )
		type = 'cart-small';

	var c = jQuery('#site-templates').tmpl({
		text:'из корзины',
		template: type
	});
	c.addClass('ui-state-highlight');
	c.data('id',id);
	c.click( function(){
		SITE.cart.remove(id);
	});
	return c;
}

function cartRefresh()
{
	jQuery('.cart-place').each(function(){
		var id = parseInt(jQuery(this).data('id'),10);
		if( !id )
			return;

		var type = jQuery(this).data('cart-type');

		if( SITE.cart.has(id) )
			jQuery(this).empty().append(createFromCart(id,type));
		else
			jQuery(this).empty().append(createToCart(id,type));
	});

	var c = SITE.cart.count();
	var container = jQuery('#cart-cantainer').find('.value');
	var total = jQuery('#cart-cantainer').find('.total');
	
	total.empty();
	container.empty();
	
	if( c < 1 ){
		templateRender('cart-empty').appendTo(container);
		jQuery('#cart-order-btn').hide();
		return;
	}
	
	var max = 20;
	var _m = 0;
	
	SITE.cart.list().each(function(){
		if( _m++ > max )
		{
			templateRender('cart-more',{total:SITE.cart.count()}).appendTo(container);
			return false;
		}
		
		var item = templateRender('cart-list', this);

		item.find('.remove').click(function(){
			if( !confirm('Хотите удалить предмет из корзины?') )
				return;

			var id = item.data('id');
			SITE.cart.remove(id);
		});
		item.appendTo(container);
		
	});
	
	if( c > 0 ){
		jQuery('#cart-order-btn').show();
	}
	
	templateRender('cart-total', {price:SITE.cart.price()}).appendTo(container);
}

function do_animate(count)
{
	var inf = jQuery('#cart-informer');
	
	for(;count!=0;--count)
	{
		inf.animate({'opacity':'1.0'},{duration:150,queue:true});
		inf.animate({'opacity':'0.0'},{duration:50,queue:true});
	}	
}

jQuery(document).ready(function(){
	SITE.cart = new CatalogCart();
	SITE.cart.onChange = cartRefresh;
	SITE.cart.onLoad = cartRefresh;
	SITE.cart.refresh();
	SITE.cart.onAdd = function(){
		jQuery('#cart-informer').css({
			'visibility':'visible',
			'opacity':'0.0'
		});
		do_animate(5);		
	};
});
