$(function() { 
	
	popIt();
	orderForm();
	formFieldDefaults();
	if ($('.botmenu')) $('.botmenu').css('zoom','1');
	
});

function formFieldDefaults () { 
	
	$('input[type=checkbox]').clearForm();
	$('input[type=radio]').clearForm();
	$('#car-type').clearForm();
	$('#f_return_place, #f_pickup_place').clearForm();
}
function orderForm() {
	
	var price_tag = 0;
	var now = new Date();
	$('#step2, #step3').css('display', 'none');
	$("#f_start_date").attr('value', now.getFullYear()+'-'+parseInt(now.getMonth()+1)+'-'+now.getDate());
	$("#f_end_date").attr('value', now.getFullYear()+'-'+parseInt(now.getMonth()+1)+'-'+now.getDate());
	
	
	convertData();
	formStep2();
	
	fixIEEvents();
	// Filtering and showing/hidding cars.
	// In general adding property show = true/false for each car
	$('.rent-a-car .car-filter input, .rent-a-car .car-filter select').change(function(){
		//alert('d');
		// Reseting sub steps
		$('#step2, #step3').css('display', 'none');
		$('input[type=radio]').clearForm();
		
		var cps = new Array();
		$('.car-filter input').each(function(i){
		
			if($(this).attr('checked') == true) {
				cps[i] = $(this).attr('id');
			}
		});
		
		$(".car-list li").each(function(){
				
			if ($('#car-type').val() == 'ct_all') { 
				if ( cps.length < 1 ) { 
					$(".car-list li").data('show','true');
				} else { 
						var i;
						for (i in cps) { 
							
							if ($(this).data(cps[i]) == 'true') {
								$(this).data('show','true');
							} else { 
								$(this).data('show','false');
							}
						}
				}
			} else { 
				if ( cps.length < 1 ) { 
					if ($(this).data('ct') == $('#car-type').val()) { 
					 	$(this).data('show','true');
		 			} else { 
						$(this).data('show','false');
					}
				} else { 
					if ($(this).data('ct') == $('#car-type').val()) { 
						
						var i;
						for (i in cps) {
							if ($(this).data(cps[i]) == 'true') {
								$(this).data('show','true');
							} else { 
								$(this).data('show','false');
							}
						}
					 
		 			} else { 
						$(this).data('show','false');
					}
				}
			}
	
		});
		
		// Going trough all cars to show and hide them with css.
		$(".car-list li").each(function() {
			if ($(this).data('show') == 'true') { 
				$(this).css('display','block');
			} else {
				$(this).css('display','none');
			}
		});
	});
}
function convertData() { 
	// Converting data from attr class to data[];
	$(".car-list li").each(function(){
				if($(this).hasClass('ct_sedan')) 	{ $(this).data('ct', 'ct_sedan').removeClass('ct_sedan'); }
				if($(this).hasClass('ct_hb')) 		{ $(this).data('ct', 'ct_hb').removeClass('ct_hb'); }
				if($(this).hasClass('ct_van')) 		{ $(this).data('ct', 'ct_van').removeClass('ct_van'); }
			
				if($(this).hasClass('cp_cond')) 	{ $(this).data('cp_cond', 'true').removeClass('cp_cond'); }
				if($(this).hasClass('cp_auto')) 	{ $(this).data('cp_auto', 'true').removeClass('cp_auto');}
			});
}
function formStep2() { 
		
	
		$('input[name=car-model]').change(function(){
			$('#step2').css('display', 'block');
			$('#book-car').show(); 	// display second step
			feeCalculator();					 	// Calculate days fee			
			goToByScroll('step2');
			zoomIE();
		});
		$('.places select').change(function(){ 
			feeCalculator();
		});
		$("#step2 input").change(function(){			
			feeCalculator();	
		});
		
		$('#book-car').click(function(){
			$('#step3').css('display', 'block');
			$(this).hide();
			goToByScroll('step3');
			return false;
		});	
		$('#f_start_date, #f_end_date').keyup(function(){
			feeCalculator();
		});
	
}

function feeCalculator () {
	if ($('.rent-a-car').width()) { 
		// General fee (days) calculation
		var days = 0;
		var price = 0;
		var stuffFee = 0;
		var pickupFee = 0;
		var startA = $('#f_start_date').val().split("-");
		var endA = $('#f_end_date').val().split("-");
		var dateS = new Date();
		var dateE = new Date();
		dateE.setFullYear(endA[0],endA[1]-1,endA[2]);
		dateS.setFullYear(startA[0],startA[1]-1,startA[2]);
		
		while ( dateS < dateE ) {
			
			dateS.setDate(dateS.getDate() + 1);
			days++;
		}
		days++;
		var ttype = 0;
		// console.log(days);
		if ( days >= 1 && days <= 7 ) { ttype = 0;}
		if ( days >= 8 && days <= 12 ) { ttype = 1;}
		if ( days >= 13 && days <= 16 ) { ttype = 2;}
		if ( days >= 17 && days <= 20 ) { ttype = 3;}
		if ( days > 20 ) { ttype = 4;}
		
		// console.log(i + '//' + ttype);
		
		if ( $('input[name=car-model]:checked').length ) {
		tmp_c = $('input[name=car-model]:checked').attr('class').split("car-");
		} else {
			return false;
		}
		ctype = parseInt(tmp_c[1])-1;
	
		var car_prices = new Array(
								new Array('25.83','22.93','21.19','20.02','19.15'), // 1. Opel Corsa 1.2
								new Array('34.25','30.47','28.44','26.70','25.54'), // 2. Opel Astra 1.6
								new Array('34.25','30.47','28.44','26.70','25.54'), // 3. Opel Astra Caravan 1.6
								new Array('34.25','30.47','28.44','26.70','25.54'), // 4. Opel Astra Sedan 1.8
								new Array('51.96','46.15','43.25','40.64','39.18') 	// 5. Opel Vivaro 2.0 CDTI
								);
								
		days = (days <= 1)? 1 : days-1;					
								
		price = (days)*car_prices[ctype][ttype];
		$('.dailyRate').text(car_prices[ctype][ttype]);

		// Calculating additional stuff fee
		$('#step2 input[type=checkbox]').each(function(){ 
			if ($(this).is(':checked')) { 
				stuffFee = stuffFee + parseFloat($(this).val());
			}
		});
		stuffFee = days*stuffFee;
		
		// Calculating pickup fee	
		$('#step2 .places select').each(function(){
			if (this.selectedIndex >= 2) { 
				pickupFee = pickupFee+ 10;
			}
		});
		
		var result = Math.round((price+stuffFee+pickupFee)*100)/100;
		
		result=result.toString();
		
		if(result.indexOf('.')==-1){
			result+='.00'
		}else{
			if(result.substr(result.indexOf('.')+1).length==1)
				result+='0'
		}
		// gets here allways	
		$(".fee").text(result);
		
		}
}

function popIt() { // Pop-up for car details
	
	$("a[rel].popit").overlay({
		expose: {  // Black bg around box
			color: '#000', 
			opacity: 0.6 
			},
		onBeforeLoad: function() {
			var wrap = this.getContent().find(".contentWrap");
			wrap.load(this.getTrigger().attr("href"));
		}
	});
	
}

function zoomIE () { 
	if(jQuery.browser.msie) {
		$('.check-list label').css('position','static');
		$('.check-list label').css('position','relative');
		$('.check-list label').css('zoom','1');
		if ($('.botmenu')) $('.botmenu').css('zoom','1');
	}
}
function fixIEEvents () { 

	$('.rent-a-car .car-filter input[type=checkbox]').focus(function(){
		$(this).change();
	});
	$('.rent-a-car .car-list label').click(function(){
		$(this).find('input').change();
	});	

}


$.fn.clearForm = function() {
   return this.each(function() {
     var type = this.type, tag = this.tagName.toLowerCase();
     if (tag == 'form')
       return $(':input',this).clearForm();
     if (type == 'text' || type == 'password' || tag == 'textarea')
       this.value = '';
     else if (type == 'checkbox' || type == 'radio')
       this.checked = false;
     else if (tag == 'select')
       this.selectedIndex = 0;
   });
 };

function goToByScroll(id){
     	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow', function(){
		zoomIE();
		
});
}

// TMP STUFF
	// old crap. just in case.
	// if ( cps.length  < 1 && $('#car-type').val() == 'ct_all') { 
	// 			console.log("All cars 0 selects"+cps.length + " " + $('#car-type').val());
	// 			$(".car-list li").data('show','true');
	// 		} else {
	// 			if ( cps.length < 1 ) { 
	// 				if ($(this).data('ct') == $('#car-type').val()) { 
	// 					$(this).data('show','true');
	// 				} else { 
	// 					$(this).data('show','false');
	// 				}
	// 			} else { 
	// 				
	// 				for each (var item in cps) {
	// 					console.log('ww');
	// 					if ($(this).data(item) == 'true' && ($(this).data('ct') == $('#car-type').val() || $(this).data('ct') == 'ct_all')) { 
	// 						$(this).data('show','true');
	// 					} else {
	// 						console.log('false: '+ $(this).data('ct') +"<>" + $('#car-type').val() + ".");
	// 						$(this).data('show','false');
	// 					}
	// 				 }
	// 			}
	// 		
	// 		}



