$(document).ready(function() {
		// Services round grey block carousel
		$('#services_carousel').jcarousel( {
			visible : 1,
			scroll : 1,
			initCallback : services_carousel_initCallback
		});

		/// services
		var loaded = {k1:1};
		
		function addButton(obj) {
			obj.append('<p><a href="javascript:void(0);" onclick="$(\'#send\').modal();"><img src="/i/index_blocks/send_request.png" alt="" /></a></p>');
		}
		
		addButton($('#k1'));
		
		function services_carousel_initCallback(carousel) {
			$('#services .menu a').bind('click', function() {
				$('#services .menu a').removeClass('active');
				$(this).addClass('active');
				
				var href = $(this).attr('href');
				var id = jQuery.jcarousel.intval($(this).attr('rel'));
				carousel.scroll(id);
				
				var item = $('#services_carousel li[jcarouselindex='+id+']').each(function(){
					var thisLi = $(this);
					if (!loaded[thisLi.attr('id')]) {
						$.post( href, {}, function (data) {
							thisLi.html(data.page.introtext);
							loaded[thisLi.attr('id')] = 1;
							addButton(thisLi);
						}, "json" );
					}
				});
				return false;
			});
		}
		
		$('#count_btn').click(function(){
			var f = document.forms['calc-form'];
			var koef =  (f.elements['city'].value=='Москва')?0.8:0.7;
			var total = parseInt(f.elements['cost'].value) * koef;
			if (isNaN(total)) {
				alert('Укажите примерную стоимость автомобиля');
			} else {
				alert('Примерная сумма '+Math.floor(total)+' руб.');
			}
			
			return false;
		});
		
		//////// calc
		$('.colorpicker a').click(function() {
			$('.colorpicker a').removeClass('active');
			$(this).addClass('active');
			console.log(this.style.backgroundColor, this.style);
			$('#calc-form input[name=color]').attr('value', this.title?this.title:this.style.backgroundColor);
			return false;
		});

		$('#calc-form input[name=phone]').focus(function(){
			if ($(this).attr('value')=='+x xxx xxx xx xx') {
				$(this).attr('value', '');
			}
		}).blur(function(){
			if ($(this).attr('value')=='') {
				$(this).attr('value', '+x xxx xxx xx xx');
			}
		});	
		
		$('#header .indexmenu .descr').click(function(){
			var c = $('p', this).attr('class').replace('srve', 'serv');
			show_block(c);
		});
	});

function updateCalc(type, value) {
	
	$.post('/service/'+type+'/'+value, {}, function(e){
		if (e.success) {
			
			var m = document.forms['calc-form'].elements[type];
			for(var i=m.options.length-2; i>0; --i) {
				m.remove(i);
			}
			var k=0;
			for (var i in e.result) {
				var opt = document.createElement('OPTION');
				opt.value = i;
				opt.text  = e.result[i];
				++k;
				m.add(opt, m.options[k]);
			}
			$("#calc-form select[name="+type+"]").parents('.jquery-selectbox').unselectbox();
			$("#calc-form select[name="+type+"]").selectbox().bind('change', calcChange);
		}
	}, 'json');				
}

function calcChange() {
	if (this.name=='type' || this.name=='auto') {
		updateCalc(this.name=='type'?'auto':'model', this.value);
	}	
}

function show_block(block) {
	
	var blocks = new Array('services', 'documents', 'sale', 'calculator');
	for ( var i = 0; i < blocks.length; i++) {
		$('#' + blocks[i]).hide();
	}
	$('#' + block).show();

	$('body').scrollTo( {
		top : 386,
		left : 0
	}, 800);
	
	if (block=='calculator') {
		$('.cs').selectbox().bind('change', calcChange);
	}	
}
