//home items
$j(document).ready(function(){
	var wrapProductsHeight = $j('.home_items_wrap').height();
	var totalProducts = $j('.td_home_items_cell').length;
	var totalProductsHeight = $j(wrapProductsHeight * totalProducts);
	var currentProduct = 1;
	var activeClick = true;
	
	function slideProductsNext() {
		if(currentProduct == totalProducts){
			return false;
		}else{
			activeClick = false;
			$j('.home_items_wrap').fadeTo(200, 0, function(){ $j('.home_items').animate({ marginTop: '-='+wrapProductsHeight }, 0); });
			setTimeout(function(){
				$j('.home_items_wrap').fadeTo(200, 1);
				currentProduct++;
				if(currentProduct == totalProducts){ $j('#btn_next').addClass('inactive'); }
				$j('#main_right_wrapper_cars .items_navigation .actual_number').text(currentProduct);
				activeClick = true;
			}, 1000);
			$j('#btn_prev').removeClass('inactive');
		}
	}
	
	function slideProductsPrev() {
		if(currentProduct == 1){
			return false;
		}else{
			activeClick = false;
			$j('.home_items_wrap').fadeTo(200, 0, function(){ $j('.home_items').animate({ marginTop: '+='+wrapProductsHeight }, 0); });
			setTimeout(function(){
				$j('.home_items_wrap').fadeTo(200, 1);
				currentProduct--;
				if(currentProduct == 1){ $j('#btn_prev').addClass('inactive'); }
				$j('#main_right_wrapper_cars .items_navigation .actual_number').text(currentProduct);
				activeClick = true;
			}, 1000);
			$j('#btn_next').removeClass('inactive');
		}
	}
	
	function createNavigation() {
		$j('#main_right_wrapper_cars').append('<div class="items_navigation"><div id="btn_prev"><a href="#">Previous</a></div> <span class="actual_number"></span> of <span class="total_numbes"></span> <div id="btn_next"><a href="#">Next</a></div></div>');
		$j('#main_right_wrapper_cars .items_navigation .total_numbes').text(totalProducts);
		$j('#main_right_wrapper_cars .items_navigation .actual_number').text(currentProduct);
		$j('#btn_prev').addClass('inactive');
		
		$j('#btn_next').click(function(){
			if(activeClick == true){
				slideProductsNext();
			}else{
				return false;
			}
		});
		
		$j('#btn_prev').click(function(){
			if(activeClick == true){
				slideProductsPrev();
			}else{
				return false;
			}
		});
	}
	
	if($j('.td_home_items_cell').length > 1){
		createNavigation();
	}else{
		$j('.home_items tr').append('<td style="text-align: center; font-weight: bold; padding-top: 95px;">Out of stock</td>');
	}
});
