function explode( delimiter, string, limit ) {
    // http://kevin.vanzonneveld.net
    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
 
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}


function next(){
	setWidth();

	var c_quest=parseInt($("#c_quest").val());

	if(!$.string($("#ans_"+c_quest).val()).blank()){
		var margin=parseInt($("#scroller").css("marginLeft"));
		var all_w=parseInt($("#scroller").css("width"));
	
		var width=parseInt($(window).width());
		var w_intr=width-25;
		var next_go=margin-w_intr;
	
		var intr=$(".intrebare").length;
		var total_scroll=(intr*w_intr);
		if((margin*-1)<(total_scroll-w_intr))
			$("#scroller").animate({marginLeft: next_go+"px"}, 2000);

		$("#c_quest").val(c_quest+1);

		$("#intrebare_"+$("#c_quest").val()).css({visibility:'visible'});
	}else{
		alert('Mosul te roaga sa alegi un raspuns!');
	}
	
	if(c_quest==intr){
		$(".next").hide();
		sendAnswers();
	}
}

function sendAnswers(){
	var intr=$(".intrebare").length;
	
	var url='ajax/get_results.php';
	var pars='&nr_intr='+intr;
	
	for(i=1;i<=intr;i++){
		pars+='&ans_'+i+'='+$("#ans_"+i).val();
	}

	$.ajax({type: "POST", url: url, data: pars, success: response});	

	function response(data){
		quizResult(data);
	}
	return false;
}

function quizResult(score){
	$("#scroller").hide();
	$("#show_results").show("slow");

	var toys=$('#toys').val();
	var ar_toy=explode(',',toys);
	var nr_won=Math.round(score/6.25);
	
	var code='<div align="middle">';
	
	for(i=0;i<nr_won;i++){
		if(i==2)
			code+='<br/>';
		code+='<img class="cadou_rez" src="resize-image-crop.php?image=jucarii/toy'+ar_toy[i]+'.jpg&amp;w=100&amp;h=100"/>';
	}
	code+='</div>';
	
	$("#box_prez").html(code);
}

function setWidth(){
	var width=parseInt($(window).width());
	var w_intr=width-85;

	var intr=$(".intrebare").length+1;
	var total_scroll=(intr*w_intr);

	$("#scroller").css({width:total_scroll+'px'});
	$("#scroller .intrebare_content").each(function(i){
				   $(this).css({width:w_intr+"px"});
				 });
	var c_quest=parseInt($("#c_quest").val());
	var c=0;
	$(".intrebare").each(function(i){
					c++;
					if(c==(c_quest+2) || (c==2 && c_quest==1))
					$(this).css({visibility:'hidden'});
				 });
	
}

var resizeTimer = null;
$(window).bind('resize', function() {
    if (resizeTimer) clearTimeout(resizeTimer);
    resizeTimer = setTimeout(setWidth, 100);
});

function selectAns(ans,intr){
	$("#intrebare_"+intr+" #answers_"+intr+" .raspuns div").removeClass("ans_wrong ans_right");
	$("#intrebare_"+intr+" #answers_"+intr+" .raspuns div").addClass("ans_wrong");
	$("#intr_"+intr+"_ans_"+ans+" div").removeClass("ans_wrong").addClass("ans_right");
	$("#ans_"+intr).val(ans);
}

function showSel(id){
	$("#sel_"+id).show();
}
function hideSel(id){
	var toys=$('#toys').val();
	var found=false;

	var ar_toy=explode(',',toys);
	for(i=0;i<ar_toy.length;i++){
		if(ar_toy[i]==id)
			found=true;
	}
	if(!found)
		$("#sel_"+id).hide();
}

function addToy(id){
	var toys=$('#toys').val();
	var found=false;

	var ar_toy=explode(',',toys);
	for(i=0;i<ar_toy.length;i++){
		if(ar_toy[i]==id)
			found=true;
	}
	if(!found){
		if(ar_toy.length<4){
			if(toys!='')
				toys+=','+id;
			else
				toys+=id;
			$("#sel_"+id).show();
		}
	}
	if(found){
		if(toys.indexOf(id+',')!='-1')
			toys=toys.replace(id+',','');
		if(toys.indexOf(id)!='-1')
			toys=toys.replace(id,'');
	}
	
	toys=toys.replace(',,',',');
	if(toys==',')
		toys='';
	
	$("#toys").val(toys);
	checkIf4();
}

function checkIf4(){
	var toys=$('#toys').val();
	var ar_toy=explode(',',toys);
	
	var nr=0;
	for(i=0;i<ar_toy.length;i++){
		if(ar_toy[i]!='')
			nr++;
	}

	if(nr==4)
		$("#chose_next").show();
	else
		$("#chose_next").hide();
}

function startQuiz(){
	$("#chose_next").hide();
	$("#quiz_next").show();
	$("#scroller").show("slow");
	$("#chose_pres").hide("fast");
}

$(document).ready(function() {
	setWidth();
	$("#scroller").hide();
	$("#chose_pres").show();
	$("#show_results").hide();
	$("#quiz_next").hide();
	$("#chose_next").hide();
});
