var Cart = new Cart();
jQuery(document).ready(function () {

	$.ajaxSetup({cache: false});
	$.get('/orders/cart', function(response){
		Cart.replaceContent(response);
	}, 'json');
	
	jQuery('a.lightbox').attr('rel', 'shadowbox'); Shadowbox.init();

	$('.open-link').click(function(){
		$(this).siblings('ul').toggle();
		var image = $('#changer-' + this.rel);
		if(image.attr('class') == 'plus'){
			image.removeClass('plus').addClass('minus').attr('src', '/img/bg-li-open.png');
		}else{
			image.removeClass('minus').addClass('plus').attr('src', '/img/bg-li-close.png');
		}
		return false;
	});
	
	$('.vendor-selection').click(function(){
		$('.sel-filter-left').toggle();
		return false;
	});
	
	$('#jump-to-page').submit(function(){
		var regexp = /page:[0-9]+/;
		var currentUrl = location.href;
		if(regexp.test(currentUrl)){
			currentUrl = location.href.replace(/(page:)([0-9]+)/, '$1' + $('#jump-page').val());
		}else{
			var page = 'page:' + $('#jump-page').val();
			lastChar = location.href.substr(location.href.length-1, 1);
			if(lastChar != '/'){
				page = '/' + page;
			}
			currentUrl += page;
		}
		document.location.href = currentUrl;
		return false;
	});
	
	$('#limit-form').submit(function(){
		var regexp = /max:[0-9]+/;
		var currentUrl = location.href;
		if(regexp.test(currentUrl)){
			currentUrl = location.href.replace(/(max:)([0-9]+)/, '$1' + $('#limit').val());
		}else{
			var limit = 'max:' + $('#limit').val();
			lastChar = location.href.substr(location.href.length-1, 1);
			if(lastChar != '/'){
				limit = '/' + limit;
			}
			currentUrl += limit;
		}
		document.location.href = currentUrl;
		return false;
	});
	
	$('.add-product-to-basket').click(function(e){
		var form = $('#update-cart');
		if(form.length == 0){
			form = $(this).next('form');
		}
		if((is_numeric($('#item-quantity').val())) && (is_numeric($('#item-id').val()))){
			var data = form.serialize();
			e.preventDefault();
			$('#modal-message').text('Ваш товар добавлен в Корзину.');
			$('#basic-modal-content').modal({overlayClose:true});
			$.post(form.attr('action'), data, function(response){
				Cart.replaceContent(response);
				updateCartWidget();
				//alert('Товар добавлен в корзину');
				
			}, 'json');
		}
		return false;
	});
	
	$('.change-selection').live('click', function(){
		var link = $(this);
		var form = link.next('form');
		var data = form.serialize();
		$.post(form.attr('action'), data, function(response){
			Cart.replaceContent(response);
			var input = form.children('.item-selected');
			var value = input.val();
			if(value == 1){
				link.parent().parent().removeClass('product-hor-restore').addClass('product-hor-basket');
				input.val('0');
				link.text('Удалить');
			}else if(value == 0){
				link.parent().parent().removeClass('product-hor-basket').addClass('product-hor-restore');
				input.val('1');
				link.text('Восстановить');
			}
			var uniqueCount = Cart.uniqueTotal();
			$('#uniqueQuantity').text(uniqueCount);
			$('#totalCost').text(Cart.cartInfo.totalCost);
			$('#word').text(Cart.inflector(uniqueCount));
			updateCartWidget();
		}, 'json');
		return false;
	});
	
	$('#save-changes').live('click', function(e){
		//e.preventDefault();
		var temp = [];
		$.each($('.fake-item-class'), function(index, value){
			var div = $(value);
			var idAttr = div.attr('id');
			var itemId = idAttr.substr(5);
			Cart.content[itemId].quantity = $('.item-quantity', '#' + idAttr).val();		
		})
		Cart.prepareForDeletion();
		//$('#basic-modal-content3').modal({overlayClose:true});
		//$('#simplemodal-container').attr('id', 'simplemodal-container2');
		//$('.login-close').css('right', '70px');
		$.post('/orders/updatecart/', Cart.content, function(response){
			Cart.replaceContent(response);
			e.preventDefault();
			$('#modal-message').text('Данные сохранены, корзина пересчитана.');
			$('#basic-modal-content').modal({overlayClose:true});
			$.get('/orders/cart/rerender', function(response){
				$('#content').html(response);
				updateCartWidget();
				//alert('Данные сохранены, корзина пересчитана');
			});
		}, 'json')
	});

	$('#sendorder').click(function(){
		do_feedback();
	});
	
	$('#unset-delivery').click(function() {
		_this = $(this);
		$.get('/orders/unset_delivery', function() {
			$.get('/orders/cart/rerender', function(response){
				$('#content').html(response);
				updateCartWidget();
				_this.remove();
			});
		}, 'json');
		return false;
	});
	
	$('#unset-delivery2').click(function() {
		_this = $(this);
		$.get('/orders/unset_delivery', function() {
			$.get('/orders/cart/rerender', function(response){
				$('#delivery-block').remove();
				_this.remove();
			});
		}, 'json');
		return false;
	});

});
	function updateCartWidget(){
		var uniqueCount = Cart.uniqueTotal();
		if(uniqueCount > 0){
			$('#cartWidget').html('В вашей <a href="/orders/cart">корзине</a><br> ' + uniqueCount + ' ' + Cart.cartInfo.word);
		}else{
			$('#cartWidget').html('В вашей <a href="/orders/cart">корзине</a><br> нет товаров');
		}
	}
