

$(function(){

	$.datepicker.setDefaults($.datepicker.regional['ru'] );

	$('#header .rotator, #header .banners').swap(5000, 1000);
	
	cars_swap();
	
	
	
	$('button.reset').click(function(){	
		$(':input', $(this).closest('form'))
		 	.not(':button, :submit, :reset, :hidden')
			.val('')
			.removeAttr('checked')
			.removeAttr('selected');
	})
	
	
	//$('#header .login').traktor();
});



$.fn.traktor = function(){
	var trigger = $('.trigger', this),
		fieldset = $('fieldset', this);
		
	trigger.one('mouseover', function(){
		trigger.hide();
		fieldset.fadeIn(700);
	})
}


$.fn.swap = function(loopTime, fadeTime) {
	var box = this;
	var items = $('.item', box);

	var length = items.length;
	var current = 0; 
	
	function loop() {
		var next = current < length-1 ? next = current + 1 : 0;
		$(items[current]).fadeOut(fadeTime);
		$(items[next]).fadeIn(fadeTime);
		current = next
		setTimeout(loop, loopTime)
	}	
	
	setTimeout(loop, loopTime)
	
}



function cars_swap() {

	var big = $('.big img'),
		smalls = $('.smalls a');

	smalls.click(function(e){
		e.preventDefault();
		var url = this.href;
		big.attr('src', url);
		})
	
	
}


function cart_recount() {
	
	var inputs = $('.quantity input'),
		buttons = $('.quantity span'),
		item_totals = $('.total strong'),
		order_total = $('.order_sum'),
		form = order_total.closest('form');
		

	buttons.click(function(e){
		form.submit();
		
		/*
		var total = 0,
			error = false;
		buttons.each(function(i, n){
			var input = $('input', $(n).closest('td')),
				val = parseInt(input.val()),
				item_price = $('.price strong', $(n).closest('tr')).text(),
				item_total = $('.total strong', $(n).closest('tr'));
				
				if (!isNaN(val)) {
					
					var item_total_num = parseInt(val) * parseInt(item_price);
					item_total.text(item_total_num);
					total = total + item_total_num;
				} else {
					error = true;
				}
		});
		if (!error) {
			order_total.text(total);
		}
		*/
		
	})
	
}
