$(function() {
	
	var Krot = function() {
		this.globalFadeTime = 250;
		this.nav = $('div.h-nav');
		this.grandTotalContainer = $('#itogo');
		this.init();
	}
	
	Krot.prototype.addMenuHoverBg = function() {
		var self = this;
		self.nav.find('li').prepend('<div>');
	}
	
	Krot.prototype.addMenuEvents = function() {
		var self = this;
		self.nav.on('mouseenter', 'li:not(.selected)', function() {
			$(this).children('div').stop(true, true).fadeIn( self.globalFadeTime );
		}).on('mouseleave', 'li:not(.selected)', function() {
			$(this).children('div').fadeOut( self.globalFadeTime );
		});
	}
	
	Krot.updateGrandTotal = function() {
		var grandTotal = 0;
		$('span.c-t-num').each(function() {
			var rowTotal = parseInt( $(this).text() );
			if ( isNaN(rowTotal) ) {
				rowTotal = 0;
			}
			
			grandTotal += rowTotal;
		});
		
		$('#itogo').text( grandTotal );
	}
	
	Krot.cartCount = function(element) {
		this.input = element;
		this.priceTable = this.input.closest('div').siblings('table');
		this.totalContainer = this.input.closest('div').siblings('h2').children('span');
		this.price1To = parseInt( this.priceTable.attr('data-price1to') );
		this.price2To = parseInt( this.priceTable.attr('data-price2to') );
		this.price3To = parseInt( this.priceTable.attr('data-price3to') );
		this.selectedPrice = 0;
		this.value = 0;
		this.prevValue = 0;
		this.priceColumn = 0;
		this.init();
	}
	
	Krot.cartCount.prototype.init = function() {
		var self = this;
		self.changeValue();
		self.input.on('keyup input', function() {
			self.changeValue();
		});
	}
	
	Krot.cartCount.prototype.changeValue = function() {
		this.value = parseInt( this.input.val(), 10 );
		
		if ( isNaN( this.value ) ) {
			this.value = 0;
		}
		
		if ( this.value != this.prevValue ) {
			if ( this.value <= this.price1To ) {
				this.priceColumn = 0;
			} else if ( this.value > this.price1To && this.value <= this.price2To ) {
				this.priceColumn = 1;
			} else if ( this.value > this.price2To && this.value <= this.price3To ) {
				this.priceColumn = 2;
			} else {
				this.priceColumn = 3;
			}
			
			this.priceTable.find('th').removeClass('cart-t-selected').eq( this.priceColumn ).addClass('cart-t-selected');
			this.priceTable.find('td').removeClass('cart-t-selected').eq( this.priceColumn ).addClass('cart-t-selected');
			
			this.selectedPrice = parseInt( this.priceTable.find('td').eq( this.priceColumn ).text() );
			this.totalContainer.text( this.value * this.selectedPrice );
			
			
			Krot.updateGrandTotal();
			
			this.prevValue = this.value;
		}
	}
	
	Krot.prototype.addCartEvents = function() {
		$('input.cart-count-value').each(function() {
			var input = new Krot.cartCount( $(this) );
		});
		
		$('a.cart-delete-item').on('click', function(e) {
			del( 'catalog', $(this).attr('data-num') );
			e.preventDefault();
		});
	}
	
	Krot.prototype.showAddedToCartPopup = function( element ) {
		var tooltip = $('<span class="added-to-cart-tooltip">Товар добавлен в корзину</span>');
		tooltip.appendTo(element).fadeIn(100).delay(3000).fadeOut(300, function() {
			$(this).remove();
		});
	}
	
	Krot.prototype.addToCart = function() {
		var self = this;
		$('a.order-btn-list, a.order-btn-prod').on('click', function(e) {
			var count = $(this).siblings('input.order-btn-count').val();
			if ( isNaN(count) ) {
				count = 0;
			}
			basket_add('catalog', $(this).attr('data-uin'), count);
			self.showAddedToCartPopup( $(this) );
			e.preventDefault();
		});
	}
	
	Krot.prototype.showError = function( element, message ) {
		var errorTooltip = element.children('.form-validation-tooltip');
		if ( errorTooltip.length ) {
			errorTooltip.text(message);
		} else {
			var errorTooltip = $('<span class="form-validation-tooltip">' + message + '</span>');
			errorTooltip.appendTo( element );
		}
	}
	
	Krot.prototype.formActions = function() {
		var self = this;
		$('.ch-boxes input[name="customer"]', '#orders').on('change', function() {
			$('#paytype').val( $(this).val() );
			
			if ( $(this).val() == 3 ) {
				$('div.hide-for-pr').hide();
			} else {
				$('div.hide-for-pr').show();
			}
		});
		
		$('#order-go').on('click', function(e) {
			var isValid = true;
			$('div[data-required="text"]').each(function() {
				var value = $(this).find('input').val();
				if ( value.length < 1 ) {
					self.showError( $(this), 'Поле обязательно для заполнения');
					isValid = false;
				}
			});
			
			$('div[data-required="email"]').each(function() {
				var value = $(this).find('input').val();
				var pattern = new RegExp(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/);
				if ( !pattern.test( value ) ) {
					self.showError( $(this), 'E-mail введен неверно');
					isValid = false;
				}
			});
			
			$('div[data-required="checkaddress"]').each(function() {
				var value = $(this).find('textarea').val();
				if ( $('.ch-boxes input[name="delivery"]:checked', '#orders').val() == 2 && value.length < 2 ) {
					self.showError( $(this), 'Введите адрес доставки' );
					isValid = false;
				}
			});
			
			return isValid;
		});
		
		$('input, textarea', 'div[data-required]').on('focus', function() {
			$(this).siblings('.form-validation-tooltip').fadeOut(200, function() {
				$(this).remove();
			});
		});
	}
	
	Krot.prototype.init = function() {
		this.addMenuHoverBg();
		this.addMenuEvents();
		this.addCartEvents();
		this.addToCart();
		this.formActions();
	}
	
	var krot = new Krot();
});


