var Basket = function() {
	this.init();
}

Basket.prototype = {
	init: function() {
		this.basketContainer = $('#basket-container');
		this.basketSummaryContainer = $('#basket-summary');
		this.totalCostContainer = $('#total-cost');
		this.basketHeader = $('#basket-header');
		this.clearBasketButton = $('#clear-basket');
		this.updateBasket();
		this.shown = true;
		
		this.defaultBasketBottom = 110;

		this.updateProductProcessing = false;
		this.updateProductCount = 0;
		
		this.bindBacketHide();
		this.bindBacketClear()
	},
	
	
	bindBacketHide: function(){
		var self = this
		
		var windowWidth = $(window).width();
		if(windowWidth < 1400) {
			this.hide(0);
		}
		
		this.basketHeader.click(function(){
			self.toogleBasket();
		});
	},
	
	bindBacketClear: function(){
		
		var self = this
		
		this.clearBasketButton.click(function(event){
			event.preventDefault();
			if( confirm('Вы действительно хотите очистить коризну?') ) {
				self.clearBasket()
			}
			return false;
		});
	},
	
	toogleBasket: function(){
		if(this.shown == true) {
			this.hide()
		}
		else {
			this.show()
		}
	},
	
	show: function() {
		this.basketContainer.animate({'bottom': this.defaultBasketBottom});
		this.shown = true;
	},
	
	hide: function(time) {
		
		if( time == undefined ) {
			time = 500
		}
		
		var headerHeigth = this.basketHeader.height()
		var containerHeight = this.basketContainer.height()
		this.basketContainer.animate({'bottom': -(containerHeight-headerHeigth)+7}, time);
		this.shown = false;
	},
	
	/**
	 * Adding product to the basket
	 */
	addProduct: function(id) {
		
		this.sendRequest('addproduct',
			{
				'id': id
			}
		);
		
		// update basket view
		this.updateBasket();
		this.show()
	},
	
	/**
	 * Clear basket
	 */
	clearBasket: function() {
		
		this.sendRequest('clearBasket', {});
		
		// update basket view
		this.updateBasket();
		this.show()
	},
	
	/**
	 * Adding product to the basket
	 */
	updateProduct: function(id, count) {
		
		this.sendRequest('updateproduct',
			{
				'id': id,
				'count': count
			}
		);
		
		// update basket view
		this.updateBasket();
	},
	
	changeProductCount: function(id, inputObj) {
		var self = this;
		this.updateProductCount = parseInt(inputObj.value);
		
		if( !this.updateProductProcessing ) {

			this.updateProductProcessing = true;

			setTimeout(
				function(){
					var intCount = self.updateProductCount;
					if( !isNaN(intCount) ) {
						self.updateProduct(id, intCount);
						self.updateTotalCost();
					}

					self.updateProductProcessing = false;
				},
				1000
			);
		}
	},
	/**
	 * Update basket data and redraw it
	 */
	updateBasket: function(){
		
		// basket instance
		var self = this
		
		this.sendRequest(
			'receive',
			{
			},
			function(data) {
				self.basketSummaryContainer.html(data);
				
				if(data == '') {
					self.basketContainer.fadeOut(300)
				}
				else {
					self.basketContainer.fadeIn(300)
				}
			}
		);
	},
	
	updateTotalCost: function(){
		
		var self = this;
		this.sendRequest(
				'getTotalCost',
				{
				},
				function(data) {
					self.totalCostContainer.html(data)
				}
			);
	},
	
	
	/**
	 * Send AJAX request
	 */ 
	sendRequest: function(action, params, callback) {
		
		// default param
		if( !callback ) {
			callback = function(){ };
		}
		
		$.ajax({
			type:	'POST',
			url:	config.siteURL + 'basket/' + action, // url
			data:	params,	//params
			success:callback,
			async:	false
		});
	}
}

var basket;
$(function(){
	basket = new Basket();
});
