var total_points = 0; 
var mpg = 0;
var all_points = 58; // Max number of points
var num_ofQuestions = 15;
animatedcollapse.addDiv('question1', 'fade=1,speed=400')
animatedcollapse.init();

var badges = new Array();
//1st param - part of JPG name
//2nd param - title of car style 
badges[0] = new Array('badge_motorcycle','motorcycle');
badges[1] = new Array('badge_hybrid', 'hybrid car');
badges[2] = new Array('badge_sedan', 'mid-sized sedan');
badges[3] = new Array('badge_suv','mid-sized SUV');
badges[4] = new Array('badge_semi', 'semi-truck');

function init() {
  for (i = 2; i < num_ofQuestions+1; i++) {
	  if ('question' + i) {
			animatedcollapse.addDiv('question' + i, 'fade=1,speed=300, hide=1');
		} else {
			break;
		}
  }	
}

function calcMPG(total_points){
	// the range for MPG is 0-50; 
	// we calculate a percentage score
	var user_points = Math.round((total_points/all_points)*100) ;	
	// then apply that score to the MPG range
	user_mpg = (50 * user_points) / 100;
	return user_mpg;
}

function transition(question_id,points) {
	var points;
	animatedcollapse.hide('question' + question_id);
	//$("question" + question_id).fadeOut("slow");
	total_points += points;
	if(question_id < num_ofQuestions){
		question_id++;
		if ('question' + question_id) {
			animatedcollapse.show('question' + question_id);
		} 
	}
	else {
		mpg = calcMPG(total_points);
		$("#results").css("display","block");
		$("#footer").css("display","block");
		if(mpg>0 && mpg<11){
			// motorcycle
			i=4;
		}
		if(mpg>10  && mpg<21){
			// hybrid car
			i=3;
		}
		if(mpg>20  && mpg<31){
			// mid-sized sedan
			i=2;
		}
		if(mpg>30  && mpg<41){
			// mid-sized SUV
			i=1;
		}
		if(mpg>40  && mpg<51){
			// semi-truck
			i=0;
	}
	$("h2").text("Your body gets around "+mpg+" miles per gallon!");
	$("h3#equivalent").text("This is the equivalent of a "+badges[i][1]);
	$("#image_block").html("<a style='display: block;width: 348px;height: 289px;padding-top: 60px;color: #ff6600;font-size: 28px;background: #333 url(img/"+badges[i][0]+".jpg) no-repeat 0 0;text-align: center;'>"+mpg+" miles per gallon</a>");

	$("#htmlCode").val('<a href="http://www.thecarconnection.com/mpg-quiz/" style="display: block; text-decoration: none; width: 348px; height: 289px; padding-top: 60px; color: #ff6600; font-size: 28px; background: #333 url(http://www.thecarconnection.com/mpg-quiz/img/'+badges[i][0]+'.jpg) no-repeat 0 0; text-align: center;">'+mpg+' miles per gallon</a><p style="margin: 5px 0;">Created by <a href="http://www.thecarconnection.com/">The Car Connection</a></p>');
	//	 $("form").submit(function () { alert("submit");return true; }); 
	}
}

init();