$(document).ready(function() {
	var max_height = 0;
	$('div.product').each( function(){
		if ( $(this).height() > max_height ) {
			max_height = $(this).height();
		}
	});
	$('div.product').height( max_height );
	$('div.buy input.submit').click( function( e ) {
		var $self = $(this);
		var prod_id = parseInt( ($self.parent().find('label input').attr('id') ).substr(3) );
		var count = parseInt( $self.parent().find('label input').val() );
		if ( !isNaN( prod_id ) && !isNaN( count ) ) {
			$.post(
				$self.parent().attr('action'),
				{
					id: prod_id,
					count: count,
					js: true
				},
				function(){
					$self.attr( 'src', '/img/buy_cart_ico_full.png' );
					update_cart();
				}
			);
		}
		e.preventDefault();
	});

	$('table.show_cart tr.order a.clean_cart').click( function( e ) {
		$.post(
				'/ajax.php',
				{
					js: true,
					action: 'clean_cart'
				},
				function( data ) {
					window.location.href = data['href'];
				},
				'json'
		);
		e.preventDefault();
	});


	$('table.show_cart td.count input').change( function() {
		var $self = $(this);
		var $parent = $self.parent().parent();
		var val = parseInt( $self.val() );
		val = !isNaN( val ) ? val : 0;
		$self.val( val );

		$parent.find('td.total').html( val * parseInt( $parent.find('td.price').html() ) + ' руб.' );
	});

	$('table.show_cart tr.order a.calculate').click( function( e ) {
		var cart = {};
		var total_sum = 0;
		var total_count = 0;
		var each_length = $('table.show_cart input[type=text]').length;
		var each_counter = 0;
		$('table.show_cart input[type=text]').each( function() {
			var $self = $(this);
			total_count += parseInt( $self.val() );
			total_sum += parseInt( $self.parent().parent().find('td.total').html() );
			cart[$self.attr('id')] = parseInt( $self.val() );
			each_counter++;
		});

		var interval = setInterval( function() {
			if ( each_counter == each_length ) {
				clearInterval( interval );
				$.post(
						'/ajax.php',
						{
							js: true,
							action: 'calculate',
							cart: cart
						},
						function( data ) {
							$('table.show_cart tr.last td.count').html( total_count );
							$('table.show_cart tr.last td.total').html( total_sum + ' руб.' );
						}
				);

			}
		}, 100 );
		e.preventDefault();
	});
	$('table.show_cart td.delete a').click( function( e ) {
		var $self = $(this);
		var prod_id = parseInt( $self.parent().parent().find('td.count input').attr('id') );
		if ( !isNaN( prod_id ) ) {
			$.post(
				'/ajax.php',
				{
					js: true,
					action: 'delete_from_cart',
					id: prod_id
				},
				function() {
					$self.parent().parent().remove();
				}
			);
		}
		e.preventDefault();
	});

	if ( $('div.hat div.contacts div.phone span').length > 2 ) {
		$('div.hat').addClass('bigger');
	}

	var selector_with_max_witdh = ['div.center div.other_products', 'div.center div.page_navigation'];
	for( var selector in selector_with_max_witdh ) {
		$(selector_with_max_witdh[selector]).width( $('div.center div.content').width() - 10 );
		$(window).resize( function(){
			$(selector_with_max_witdh[selector]).width( $('div.center div.content').width() - 10 );
		});
	}


	//TODO: сделать равномерный отсуп на главной, если мало товаров и растягивается страница
	function update_cart()
	{

		$.post(
				'/ajax.php',
				{
					js: true,
					action: 'update_cart'
				},
				function( data ) {
					$('div.cart div.count span').html( data['count'] );
					$('div.cart div.sum span').html( data['total'] );
				},
				'json'
		)
	}

	( function(){
		var max_height = 0;
		var itemsLength = $('div.day_product a.name').length;
		var itemsCounter = 0;
		$('div.day_product a.name').each( function() {
			if ( max_height < $(this).height() ) {
				max_height = $(this).height();
			}
			itemsCounter++;
		});
		var heightInterval = setInterval(
				function() {
					if( itemsCounter == itemsLength ) {
						clearInterval( heightInterval );
						$('div.day_product a.name').height( max_height );
					}
				}, 100
		);
	})();
	
	$('div.search form input.search')
			.focus( function() {
				if ( $(this).val() == 'Поиск по сайту' ) {
					$(this).val( '' );
				}
			})
			.blur( function() {
				if ( $(this).val() == '' ) {
					$(this).val( 'Поиск по сайту' );
				}
			});

});
