$(function(){
    if($('#slide-body').length) rm.scroll_init();
    $(window).bind('resize', function() {
    	var liwidth = Math.round($('#slide-body').width()/2)-1;
    	var licount = $('#slide-body ul li').length;
    	rm.liwidth = liwidth;
    	rm.licount = licount;
    	var width = liwidth * licount;
    	$('#slide-body ul li').width(rm.liwidth);
    	$('#slide-body ul').width(rm.width+1);
    });

	
	$('#gccalc input[name="startdate"]').DatePicker({
		format:'m/d/Y',//'n/j/Y'
		date: $('#gccalc input[name="startdate"]').val(),
		current: $('#gccalc input[name="startdate"]').val(),
		starts: 1,
		position: 'r',
		onBeforeShow: function(){
			$('#gccalc input[name="startdate"]').DatePickerSetDate($('#gccalc input[name="startdate"]').val(), true);
		},
		onChange: function(formated, dates){
			$('#gccalc input[name="startdate"]').val(formated);
			
			rm.calcdate(formated);
		}
	});
	
	$('#gccalc input[name="placetype"]').change(function(){
		rm.greencard_calc();
		var option_idx = 53 + parseInt($(this).val());
		$('select[name="frm_82"] option[value="'+option_idx+'"]').attr('selected','selected');
	});
	$('#gccalc select[name="cartype"]').change(function(){
		rm.greencard_calc();
		var option_idx = 46 + parseInt($(this).val());
		$('select[name="frm_77"] option[value="'+option_idx+'"]').attr('selected','selected');
	});
	

	$('#gccalc select[name="term"]').change(function(){
		rm.greencard_calc();
		//var option_idx = 46 + parseInt($(this).val());
		//$('input[name="frm_81"]').val($('"#gccalc select[name="term"] option:selected"').text());
		rm.calcdate($('#gccalc input[name="startdate"]').val());
	});
	
	
	$('#dsagocalc input[name="cat"]').change(function(){
		rm.dsago_calc();
		var option_idx = $.trim($(this).parent('td').next('td').find('label').html()).replace(/\&nbsp\;/gi, '');
		$('select[name="frm_192"]>option:contains("'+option_idx+'")').attr('selected','selected');
	});
	$('#dsagocalc input[name="price"]').change(function(){
		rm.dsago_calc();
		var option_idx = $.trim($(this).parent('td').next('td').find('label').html()).replace(/\&nbsp\;/gi, '');
		$('select[name="frm_193"]>option:contains("'+option_idx+'")').attr('selected','selected');
	});
	

	/*
	$('#gccalc input[name="startdate"]').blur(function(){
		var start = $(this).val();
		var term = $('"#gccalc select[name="term"] option:selected"').val();
		var enddate = new Date(start);
		//enddate.setTime(Date.parse(start));
		enddate.setMonth(+term);
		var end = (enddate.getMonth()+1)+'/'+enddate.getDay()+'/'+enddate.getFullYear();
		
		$('input[name="frm_81"]').val(start+' - '+end);
	});*/
		
	$('#osagocalc select[name="powertype"]').parents('.el').css('display','none');
	
	$('.form input[type="hidden"]').parents('.el').css('display','none');

	
	
	$('#osagocalc select').change(function(){
		
		$('input[name="frm_131"]').val('');
		
		rm.osago_calc();
		
		$('input[name="frm_130"]').val($('"#osagocalc select[name="ocartype"] option:selected"').text());
		//if($('#osagocalc select[name="powertype"]').parents('.el').css('display') == 'block')
		$('input[name="frm_131"]').val($('"#osagocalc select[name="powertype"] option:selected"').text());
		$('input[name="frm_133"]').val($('"#osagocalc select[name="region"] option:selected"').text());
		$('input[name="frm_126"]').val($('"#osagocalc select[name="periodtype"] option:selected"').text());
		$('input[name="frm_136"]').val($('"#osagocalc select[name="bonustype"] option:selected"').text());
		
		
	});
	$('#osagocalc input').change(function(){
		rm.osago_calc();
		$idx = $('#osagocalc input[name="regtype"]:checked').val();
		$('input[name="frm_129"]').val($('#osagocalc label[for="regtype_option_'+$idx+'_regtype"]').text());
		$idx = $('#osagocalc input[name="carowner"]:checked').val();
		$('input[name="frm_132"]').val($('#osagocalc label[for="carowner_option_'+$idx+'_carowner"]').text());
		
		$idx = $('#osagocalc input[name="drivertype"]:checked').attr('id');
		$('input[name="frm_134"]').val($('#osagocalc label[for="'+$idx+'"]').text());
	});

	if($('#osagocalc').length) rm.osago_calc();

	//rm.greencard_calc();
	
	$('.calc a.tab3').click(function(){$('#viewgc').hide();$('#viewosago').hide();$('#viewdsago').show(); return false;});
	$('.calc a.tab2').click(function(){$('#viewgc').hide();$('#viewdsago').hide();$('#viewosago').show(); return false;});
	$('.calc a.tab1').click(function(){$('#viewdsago').hide();$('#viewosago').hide();$('#viewgc').show(); return false;}).click();
	
	
	new swfobject.embedSWF("/src/img/ban_h.swf", "ban_h", "680", "100", "9.0.0", false, {}, {});
	new swfobject.embedSWF("/src/img/ban_v.swf", "ban_v", "240", "350", "9.0.0", false, {}, {});
	
	
});

rm = {
	moving: false,
	scrolling: false
};

rm.calcdate = function(formated) {
	var startdate = new Date(formated);
	var term = $('"#gccalc select[name="term"] option:selected"').val();
	var enddate = new Date(formated);
	if(term > 0) enddate.setMonth(parseInt(startdate.getMonth())+parseInt(term));
	else enddate.setDate(startdate.getDate()+14);
	var end = (enddate.getMonth()+1)+'/'+enddate.getDate()+'/'+enddate.getFullYear();
	var start = (startdate.getMonth()+1)+'/'+startdate.getDate()+'/'+startdate.getFullYear(); 
	$('input[name="frm_81"]').val(start+' - '+end);
};

rm.scroll_init = function() {
//rm.init_slimbox();
var liwidth = $('#slide-body ul li:first').width()+1;
var licount = $('#slide-body ul li').length;
rm.liwidth = liwidth;
rm.licount = licount;
var width = rm.liwidth * rm.licount;
$('#slide-body ul li').width(liwidth);
$('#slide-body ul').width(width+1);
$('.rightbtn').show();
	$('.leftbtn').click(function() {
		if (rm.scrolling) return false;
		rm.scrolling = true;
		$('#slide-body').scrollTo('-='+rm.liwidth+'px', 800, {
			axis: 'x',
			onAfter: function() {
				rm.scroll_finish();
			}
		});
		return false;
	});
	$('.rightbtn').click(function() {
		if (rm.scrolling) return false;
		rm.scrolling = true;
		$('#slide-body').scrollTo('+='+rm.liwidth+'px', 800, {
			axis: 'x',
			onAfter: function() {
				rm.scroll_finish();
			}
		});
		return false;
	});
	rm.scroll_update();
}

rm.scroll_finish = function() {
	rm.scrolling = false;
	rm.scroll_update();
}


rm.scroll_update = function() {
	var s = $('#slide-body').get(0).scrollLeft;
	if (s == 0) $('.leftbtn').css('visibility', 'hidden');
	else $('.leftbtn').css('visibility', 'visible');
	if (s >= ($('#slide-body li').length-2) * rm.liwidth)
	    $('.rightbtn').css('visibility', 'hidden');
	else 
	    $('.rightbtn').css('visibility', 'visible');
}

rm.init_slimbox = function() {
	var e = $("a[rel^='lightbox']");
	if (e.slimbox) e.slimbox({
		loop: true
	}, null, function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});
}

rm.osago_calc = function() {
	var regtype = $('#osagocalc input[name="regtype"]:checked').val()-1;
	var cartype = $('#osagocalc select[name="ocartype"]').val().split('|');
	var powertype = $('#osagocalc select[name="powertype"]').val();
	var carowner = $('#osagocalc input[name="carowner"]:checked').val()-1;
	var region = $('#osagocalc select[name="region"]').val().split('|');
	var drivertype = $('#osagocalc input[name="drivertype"]:checked').val().split('|'); drivertype = drivertype[0];
	var periodtype = $('#osagocalc select[name="periodtype"]').val();
	var bonustype = $('#osagocalc select[name="bonustype"]').val();
	var violation = $('#osagocalc input[name="violation[0]"]:checked').val();
	
	var regdiv = $('#osagocalc select[name="region"]').parents('.el');
	var drivertypediv = $('#osagocalc input[name="drivertype"]').parents('.el');
	var powertypediv = $('#osagocalc select[name="powertype"]').parents('.el');
	var periodtypediv = $('#osagocalc select[name="periodtype"]').parents('.el');
	var bonustypediv = $('#osagocalc select[name="bonustype"]').parents('.el');
	var violationdiv = $('#osagocalc input[name="violation[0]"]').parents('.el');
	
	var result;
	
	if(!cartype) cartype = $('#osagocalc select[name="ocartype"] option:first').val().split('|');
	
	//alert(drivertypediv.html());
	
	//if(regtype == 0 && !regdiv.attr('display')) {
	//	regdiv.slideDown('fast');	
	//}
	
	//anim
	if(cartype[2] == 1) { 
		powertypediv.slideDown('fast');
	}else{
		powertypediv.slideUp('fast');
		powertype = 1;
	}
	
	if(cartype[4] == 1) {
		bonustypediv.slideUp('fast');
		violationdiv.slideUp('fast');
		drivertypediv.slideUp('fast'); 
	}else if(regtype != 2){
		
		 if(regtype != 1) bonustypediv.slideDown('fast');
		violationdiv.slideDown('fast');
		//alert(violationdiv.css('display'));		
	}
	//alert(regtype);
	//alert(violationdiv.html());
	
	if(carowner == 1){ 
		drivertypediv.slideUp('fast'); 
		drivertype = 1.7;
	}

	
	//precalc
	result = cartype[carowner];
	if(violation) violation = 1.5;	
	else violation = 1;
		
	//alert(cartype[4]);
	
	switch(regtype) {
		case 0: 
				//animation
				periodtypediv.slideDown('fast');
				regdiv.slideDown('fast');
				if(carowner == 0 && cartype[4] == 0) drivertypediv.slideDown('fast');
				
				//calc
				//if(cartype[2] == 1) powertype = 1;
				//if(drivertypediv.attr('display') == 'none') drivertype = 1.7;
				
				//alert(result+'*'+region[cartype[3]]+'*'+bonustype+'*'+drivertype+'*'+powertype+'*'+periodtype+'*'+violation);
				if(cartype[4] == 1) {
					result = result*region[cartype[3]]*periodtype;	
				}else{
					result = result*region[cartype[3]]*bonustype*drivertype*powertype*periodtype*violation;
				}
				
				break;
		case 1:
				//animation
				periodtypediv.slideDown('fast');
				regdiv.slideUp('fast');
				drivertypediv.slideUp('fast');
				bonustypediv.slideUp('fast');
				
				//calc
				if(cartype[4] == 1) {
					result = result*1.6*periodtype;
				}else{
					if(carowner == 1) drivertype = 1.7; else drivertype = 1.5;
					result = result*1.6*drivertype*powertype*periodtype*violation;
				}
				
				break;
		case 2:
				//anim
				periodtypediv.slideUp('fast');
				regdiv.slideUp('fast');
				if(carowner == 0 && cartype[4] == 0) drivertypediv.slideDown('fast');
				violationdiv.slideUp('fast');
				bonustypediv.slideUp('fast');
				
				//calc
				//if(powertypediv.attr('display') == 'none') powertype = 1;
				periodtype = 0.2;
				
				//alert(result+'*'+region[cartype[3]]+'*'+drivertype+'*'+powertype+'*'+periodtype);
				
				if(cartype[4] == 1) {
					result = result*periodtype;
				}else{
					result = result*drivertype*powertype*periodtype;
				}
				
				
				break;
	}
	
	
	$('#osagocalc .result').html('');
	$('#osagocalc .result').html('Cтоимость полиса: '+result.toFixed(2)+' руб.');
	$('input[name="frm_137"]').val(result.toFixed(2)+' руб.');
	
	//'+result.toFixed(2)+'
	/*
	var placetype = $('.form form select[name="placetype"]').val();
	var cartype = $('.form form select[name="cartype"]').val();
	var term = $('.form form select[name="term"]').val();
	
	alert(greencard_data);
	
	//$('.form').append(greencard_data[placetype][cartype][term]);
	*/
	
	//alert(cartype[0]);
}

