$(document).ready(function(){

	cartCurrentPage = 1;


//Change page
$('.cart_pages a').live('click', function(){
	if ($(this).hasClass('next')) newPage = cartCurrentPage + 1;
	else if ($(this).hasClass('prev')) newPage = cartCurrentPage - 1;
	else newPage = parseInt($(this).text());
	
	cartChangePage(newPage);

   return false;
});


//Change item num
$('#cart_table input.num').livequery('focus', function(){
		oldVal = $(this).val().replace(/^0[0-9]*|[^0-9]+/, '');
		$(this).val('');
	}).livequery('blur', function(){
		if ($(this).val() == '') $(this).val(oldVal);
	}).livequery('keyup', function(){
		var newNum = $(this).val(),
			 oldPrice = $(this).parents('.in_row').children('.t4').children('.price').text();
			 
			 newNum = newNum.replace(/^0[0-9]*|[^0-9]+/, '');
			 $(this).val(newNum);

   if (newNum != '') {
		$(this).parents('.in_row').children('.t4').children('.price').text((oldPrice / oldVal * newNum).toFixed(2));

		$.ajax({
	   	url: '/index.php?ajax=cart',
   		data: 'act=change_num&item_id=' + $(this).parents('.i_row').attr('item_id') + '&num=' + newNum,
   		beforeSend: function(){
//				$('#calc_features_selects .tree').show();
   		},
   		complete: function(){
//      		$('#calc_features_selects .tree').hide();
   		},
   		success: function ($jqueryOutput) {
   		   oldVal = newNum.replace(/^0[0-9]*|[^0-9]+/, '');
				cartTotalCalc();
	   	}
		});
	}

	});


//Delete cart item
$('a.cart_delete').live('click', function(){
	var $item_id = $(this).parents('.i_row').attr('item_id');

		$(this).parents('.i_row').slideUp('',
			function () {
				i = 1;
				$.each($('#cart_table .slide .i_row:visible'), function(){
					$(this).find('.number').text(i);
					if ($(this).find('.number').text().length < 2) $(this).find('.number').prepend('0');
				i++;
				});
				$('div[item_id='+$item_id+']').remove();

		$.ajax({
	   	url: '/index.php?ajax=cart',
   		data: 'act=cart_delete&item_id=' + $item_id,
   		beforeSend: function(){
//				$('#calc_features_selects .tree').show();
   		},
   		complete: function(){
//      		$('#calc_features_selects .tree').hide();
   		},
   		success: function ($jqueryOutput) {
   		   cartShowPages();
				cartTotalCalc();
				cartChangePage(cartCurrentPage);
				$('.cart_count').text(parseInt($('.cart_count#cart_count_main').text()) - 1);
	   	}
		});

			}
		);

   return false;
});


//Edit cart item
$('a.cart_edit').live('click', function(){
	var $item_id = $(this).parents('.i_row').attr('item_id'),
		 $type = $(this).parents('.i_row').find('.t2').text();

   $(".calc_r .in_bl#cart_view").animate({right: '-253px'}, 350,
		function(){
		   $(".calc_r .in_bl#cart").animate({right: '-5px'}, 350);
		}
	);

   $("#cart_bl").animate({marginLeft: '-700px'}, 350,
		function(){
			$("#calc_bl").animate({marginLeft: '0px'}, 350,
				function(){
				$(this).queue('fx', function(){
               $('.radio#calc_types_r a[value='+ $type +']').click();

		$.ajax({
	   	url: '/index.php?ajax=cart',
   		data: 'act=get_cart_details&item_id=' + $item_id,
   		beforeSend: function(){
//				$('#calc_features_selects .tree').show();
   		},
   		complete: function(){
//      		$('#calc_features_selects .tree').hide();
   		},
   		success: function ($jqueryOutput) {
   		   $features = $jqueryOutput.split('-');
	   	}
		});

				$(this).dequeue('fx');
				});
				
	$('#editcart_button').attr('item_id', $item_id);
	$('#editcart_button').show();

	   $("#calc_bl").animate({marginLeft: '0px'}, 500, function(){
	      $('.select#form_product .box .list a[value='+ $features[0] +']').click();
		}).animate({marginLeft: '0px'}, 1500, function(){
		jQuery.fx.off = true;
			for (i=1;i<$features.length;i++) {
				$('.select .box .list a[value='+ $features[i] +']').click();
			}
         $('body').click();
		jQuery.fx.off = false;
		});

				});
		});

   return false;
});


//Place order
$('a.cart_order_button').click(function(){
	var cartNum = parseInt($('.cart_count#cart_count_main').text());
	
	if (cartNum > 0) {
	   $('#order_form .in').show();
	   $('#order_form #thanks').hide();
		$('#page_overlay').fadeIn();
		$('#order_form').slideDown();
	}

   return false;
});
//close order_from
$('#order_form a.close').click(function(){

	$('#page_overlay').fadeOut();
	$('#order_form').slideUp();

   return false;
});
//Place order send
$('#order_form a.submit').click(function(){

   $emailError = false;
   $('#order_form .error_text').hide();
   if (!((/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test($('#order_form form input').eq(6).val()))) $emailError = true;

	if (checkReq('#order_form form') == false || $emailError) {
		$('#order_form .error_text').show();
		if ($emailError) $('#order_form form input').eq(6).focus();
	}
	else {
	
		$.ajax({
	   	url: '/index.php?ajax=cart',
   		data: 'act=place_order&'+ $('#order_form form').serialize(),
   		beforeSend: function(){
				$('#order_form .in').fadeOut('fast', function() { $('#order_form .tree').show(); });
   		},
   		complete: function(){
      		$('#order_form .tree').hide();
   		},
   		success: function ($jqueryOutput) {
   		   $('#order_form #thanks').fadeIn(500);
   		   $('#order_form .tree').animate({marginTop: +0}, 500, function() { $('#order_form a.close').click(); });
   		   formClear('#order_form form');
   		   $('.cart_count').text('0');
   		   $('#top_menu div div[page=page_1]').click();
   $('#calc_load_bl .anim').animate({marginLeft: '0px'}, 350,
		function(){
		  //$("#cart_bl").animate({marginLeft: '700px'}, 350);
		}
	);
	   	}
		});
		
	}
	
   return false;
});


$('#order_form div div form').submit(function(){
   $('#order_form a.submit').click();
   return false;
});

/*
		$.ajax({
	   	url: '/index.php?ajax=cart',
   		data: 'act=place_order&'+ $('#order_form form').serialize(),
   		beforeSend: function(){
				$('#order_form .in').fadeOut('fast', function() { $('#order_form .tree').show(); });
   		},
   		complete: function(){
      		$('#order_form .tree').hide();
   		},
   		success: function ($jqueryOutput) {
   		   $('#order_form #thanks').fadeIn(500);
   		   $('#order_form .tree').animate({marginTop: +0}, 500, function() { $('#order_form a.close').click(); });
	   	}
		});
*/


//Send personal order
$('#personal_order a.submit').click(function(){

   $('#personal_order .error_text').hide();

	if (checkReq('#personal_order form') == false) {
		$('#personal_order .error_text').show();
	}
	else {

		$.ajax({
	   	url: '/index.php?ajax=cart',
   		data: 'act=personal_order&'+ $('#personal_order form').serialize(),
   		beforeSend: function(){
				$('#personal_order .in').hide();
				$('#personal_order .tree').show();
   		},
   		complete: function(){
      		$('#personal_order .tree').hide();
   		},
   		success: function ($jqueryOutput) {
   		   $('#personal_order .thanks').fadeIn(500);
	   	}
		});

	}

   return false;
});


//MAIN!
});


//Load cart items
function loadCart () {
 
$.ajax({
  	url: '/index.php?ajax=cart',
	data: 'act=load_cart',
	beforeSend: function(){
		$('.slide_in .tree').show();
	},
	complete: function(){
		$('.slide_in .tree').hide();
	},
	success: function ($jqueryOutput) {
	   $('.slide_in').html($jqueryOutput);
		cartShowPages();
		cartTotalCalc();
		$('#cart_bl .total_price').fadeIn();
	}
});

}


function cartTotalCalc () {
	var $total = 0;
	
   $.each($('#cart_table .slide .i_row:visible'), function(i, row){
		$total += parseInt($(this).find('.price').text());
//		alert($(this).find('.price').text());
   });

   $('#total_price').text($total.toFixed(2));
   $('#tax_price').text(($total * 0.19).toFixed(2));

}


function cartChangePage (newPage) {

   var pagesNum = Math.ceil($('#cart_table .slide .i_row:visible').length / 2);

	if (newPage > pagesNum) newPage = 1;
	else if (newPage < 1) newPage = pagesNum;
	
	if (pagesNum < cartCurrentPage) newPage = cartCurrentPage - 1;

   $('.cart_pages a').removeClass('sel');
   $('.cart_pages a:eq('+ newPage +')').addClass('sel');

	if (pagesNum == 0) $("#cart_table .slide_in").css('marginTop', 0);
	else {
   $("#cart_table .slide_in").animate({marginTop: 154 * (newPage - 1) * -1}, 700,
		function(){
		}
	);}
   cartCurrentPage = newPage;

}


function cartShowPages () {

	var itemsNum = $('#cart_table .slide .i_row:visible').length;
	
	$('.cart_pages #digits').empty();
	
	if (itemsNum <= 2) {
		$('.cart_pages').fadeOut('fast');
		//
	}
	else $('.cart_pages').fadeIn('fast');
	//
		if (itemsNum == 0) {
		   $('#cart_table .slide').css('height', '0px');
		}
		else if (itemsNum == 1) {
	   	$('#cart_table .slide').css('height', '77px');
		}
		else $('#cart_table .slide').css('height', '154px');
//
	j = 1;
	for (i=1;i<=itemsNum;i=i+2) {
	   thisSel = '';
	   if (j == cartCurrentPage) thisSel = ' class="sel"';
	   
		$('<a'+ thisSel +' href="">'+ j +'</a>').appendTo('.cart_pages #digits');
		
		j++;
	}

}
