$(document).ready(function(){
	$(".jQuiz-slide:gt(0)").hide();
	points = new Object();
	speed = 500;

	$(".maxPoints").html(getMaxPoints());
	/*$('form').jForms({imagePath:'default/'});*/


	$(".jQuiz-goNext").activateButtonNext();

	$(".jQuiz-slide input:radio, .jQuiz-slide .radioArea").bind('click',function(){
		$(".jQuiz-goNext").activateButtonNext();
	});

	$(".jQuiz-imageblock label").click(function(){
		$(".jQuiz-imageblock label img").css({opacity:'0.68',borderColor:'#cccccc'});
		$(this).find("img").css({opacity:'1',borderColor:'#2E9A31'});
		$('input#'+$(this).attr('for')).click();
	});
});

function nextSlide()
{
	$(".jQuiz-slide:visible").fadeOut('normal',function(){
		$obj = $(this);
		$(".jQuiz-goNext").lockButtonNext();
		if($obj.next().length > 0){
			$obj.next().fadeIn('normal',function(){
				if($(this).hasClass("result")){
					setTimeout('gameOver()',200);
				}
			});
		}else{
			$(".jQuiz-slide:eq(0)").fadeIn('normal');
		}
	});
}

function endTurn()
{
	setPoints();
	$(".totalPoints").updateScore();
	$(".totalPercent").updatePercent();
	$("#outerBar").updateBar(speed);
}

function gameOver()
{
	if(getScore() >= getMaxPoints()-3){
		$(".jQuiz-resultGood").slideDown();
	}else if(getScore() >= Math.round(getMaxPoints()/2)){
		$(".jQuiz-resultMedium").slideDown();
	}else{
		$(".jQuiz-resultBad").slideDown();
	}
	$("#pointsBar").animate({opacity:'0'}, 300, function(){
		$(this).css({color:'yellow',fontSize:'0px'}).animate({fontSize:'15px',opacity:'1'},300,function(){
			animateScore_good();
		});
	});
}

function animateScore_good(){
	var date = new Date();
	var uniqueClass = 'ani_'+date.getTime()
	$("#pointsBar").css({fontSize:'14px'}).clone().addClass(uniqueClass).insertBefore("#pointsBar").css({color: 'yellow'})
	.animate({fontSize:'40px',opacity:'0'},speed,function(){
		$('.'+uniqueClass).remove()
	});
}

function animateScore_bad(){
	var speed = Math.round(speed/2);
	$("#pointsBar").animate({opacity:'0.05'},speed).animate({opacity:'1'},speed)
}

$.fn.lockButtonNext = function()
{
	$(this).unbind('click').css({cursor:'default',opacity:'0.2'}).click(function(e){e.preventDefault();});
}
$.fn.activateButtonNext = function()
{
	$(this).css({cursor:'pointer'}).animate({opacity:'1'},speed).click(function(){
		if($(this).hasClass('noQuestion')){
			nextSlide();
		}else{
			endTurn();
			setTimeout(function(){nextSlide()},speed);
		}
	});
}

$.fn.updateScore = function()
{
	$(this).html(getScore());
}
$.fn.updatePercent = function()
{
	var score = getScore();
	if(score <= 0){
		currentPercent = 0;
	}else{
		currentPercent = Math.round(getScore()/(getMaxPoints()/100));
	}
	$(this).updateCycler(currentPercent);
}

$.fn.updateCycler = function(newVal)
{
	var $obj = $(this);
	var oldVal = parseInt($obj.html(),10);
	var delay = Math.round(speed/(newVal-oldVal));
	function update(val){
		$obj.html(val+'')
		if(val < newVal){
			setTimeout(function(){update(val+1)},delay);
		}
	}
	update(oldVal);
	if(newVal > oldVal){
		setTimeout('animateScore_good()',speed+350);
	}else{
		setTimeout('animateScore_bad()',speed+350);
	}

}

$.fn.updateBar = function()
{
	lengthPerPoint = ($(this).height()-50) / getMaxPoints();
	currentLength = Math.round(lengthPerPoint * getScore())+50;
	$(this).find("div").animate({height: currentLength+'px'}, speed);
}

function setPoints(){
	if($obj = $(".jQuiz-slide:visible input:radio:checked")){
		if($obj.val() >= 0){
			points[$obj.attr('name')] = parseInt($obj.val());
		}
	}
}

function getMaxPoints(){
	if(typeof totalPoints == 'undefined'){
		setName = "";
		totalPoints = 0;
		$('.jQuiz-slide input:radio').each(function(){
			currentName = $(this).attr('name');
			if(setName != currentName){
				setName = currentName;
				setMax = 0;
				$(".jQuiz-slide input:radio[name='"+currentName+"']").each(function(){
					v = parseInt($(this).val());
					if(v > setMax){
						setMax = v;
					}
				});
				totalPoints += setMax;
			}
		});
	}
	return totalPoints;
}

function getScore(){
	score = 0;
	$.each(points, function(i, val) {
		score += val;
	});
	if(score===0){
		return "0";
	}
	return score;
}