// CONTROLLING EVENTS IN jQuery

var currentQuestion = 0;

var answers = [];

function checkDisplay() {
    if ( currentQuestion < 1 ) {
    	$('#prev').hide();
    } else if ( currentQuestion == 6 ) {
    	$('#next').hide();
    } else {
    	$('#prev').show();
    	$('#next').show();
    }
}

		
function sorting(resul){
    var sortnum = true;
    var pos = 0;
    tot = resul.length;
    while(sortnum == true) {
        for(var j=(pos+1); j<tot; j++) {
            var rd = parseFloat(resul[pos]);
 	    var fg = parseFloat(resul[j]);
 	    if(rd > fg) {
 		resul[pos] = fg;
 		resul[j] = rd;
 	    }
 	}
        pos = pos+1;
        if(pos >= tot) {
 	    sortnum=false;
        }
    }
    return resul;
}



function getValues() {
    answers[0] = $("input[name='question1']:checked").val();
    answers[1] = $("input[name='question2']:checked").val();
    answers[2] = $("input[name='question3']:checked").val();
    answers[3] = $("input[name='question4']:checked").val();
    answers[4] = $("input[name='question5']:checked").val();
    answers[5] = $("input[name='question6']:checked").val();
}


function setAdventurer(adventurerId) {
    var h3Text = "";
    var pText = "";
    var textareaText = "<p style='line-height: normal; font-family: sans-serif;'><a href='http://www.tripbase.com/quiz/whichadventurer/' style='display:block; font-size:0px; width:211px; border: 0px none;'><img src='http://www.tripbase.com/quiz/whichadventurer/images/badge_"+adventurerId+".gif' alt='Which Adventurer Are You?' style='border: 0px none; float:none; margin:0px; padding:0px;'></a><span style='text-align:center; font-size: 10px !important; line-height: normal; text-align: center; background: url(http://www.tripbase.com/quiz/whichadventurer/images/badgebottom.gif) no-repeat #efefef; width: 211px; height: 33px; display: block; color: white; '>Quiz brought to you by<br/> <a href='http://www.tripbase.com' style='color: white;'>Tripbase - Vacation Ideas</a></span></p>";

    switch(parseInt(adventurerId)) {
        case 1:
            h3Text = "You are Allan Quatermain!";
            pText = "As the protagonist of King Solomon's Mines and member of the League of Extraordinary Gentlemen, you are the quintessential outdoorsman. A big game hunter working mainly in Africa, your marksmanship is second to none. In possession of a map which leads to a fabled treasure trove, you embark on many an epic adventure with your companions.";
            break;
        case 2:
            h3Text = "You are Indiana Jones!";
            pText = "Always eager to plunder from ancient tombs and temples, you are the cinematic action hero with a penchant for putting things in museums. You also don't like snakes.";
            break;
        case 3:
            h3Text = "You are Conan the Barbarian!";
            pText = "One of the most recognizable figures in fantasy, you wish for nothing more than to crush your enemies, see them driven before you, and to hear the lamentation of their women.";
            break;
        case 4:
            h3Text = "You are MacGyver!";
            pText = "The highly intelligent and resourceful secret agent even had his own TV show. Your quick wittedness has allowed you to escape from many a perilous situation, all without casualties.";
            break;
        case 5:
            h3Text = "You are Frodo Baggins!";
            pText = "You are short and rather hairy, but you don't let that hold you back when it comes to adventuring. As the bearer of the Ring, you understand the importance of the task at hand and will stop at nothing to complete it, even if it does mean crying a lot.";
            break;
    }
    $('#badge').attr("src","images/badge_"+adventurerId+".gif");
    $('#badgeText > h3').html(h3Text);
    $('#badgeText > p').html(pText);
    $('#tArea').val(textareaText);

}

function checkAdventurer() {
    if ( answers[5] > 0 ) {
    	var tot = answers.length;
	var modeval=0;
	var maxval=0;
	var mode,curval;
	resal = sorting(answers);
	curval = resal[0];
	for(var z=0; z<=tot; z++) {
	    var zval = resal[z];
	    if(zval == curval) {
//  		alert(zval+"--"+curval);
		modeval=modeval+1;
		if(modeval > maxval) {
 		    mode = curval;	
		    maxval = modeval;
		}
	    } else {
// 		alert(curval+"--"+modeval+"--"+maxval);
 		if(modeval == maxval && mode != curval) {
 		    mode = mode+","+curval;
 		}
		curval = zval;
 		modeval = 1;
 	    }
 	}

	if ( mode[0] != undefined ) {
	    setAdventurer(mode[0]);
	} else { 
	    setAdventurer(mode);
	}		
    }
}


function preSlide( direction ){
    getValues();
    if ( direction == "next" && ( answers[currentQuestion] > 0 || currentQuestion > 5 ) || direction == "prev" ) {
        if ( direction == "next" ) {
	    checkAdventurer();
	}	

	$('#questions').trigger( direction );
    } else {
        return false;
    }
}


$(document).ready( function() {
      $.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 = -1;
        });
      };


    $('#prev').hide();
    $( "#next" ).bind( "click", function() { preSlide('next'); } );
    $( "#prev" ).bind( "click", function() { preSlide('prev'); } );
    $( "#retake" ).bind( "click", 
    	function() { 
    	    $("input[name='question1']").clearForm();
    	    $("input[name='question2']").clearForm();
    	    $("input[name='question3']").clearForm();
    	    $("input[name='question4']").clearForm();
    	    $("input[name='question5']").clearForm();
    	    $("input[name='question6']").clearForm();
    	    answers = [];
    	    $('#questions').trigger( 'goto', [ 0 ] ); 
    	}
    );
    $('#questions').serialScroll({
	items: 'li', 
	axis: 'xy',
	force: true,
	cycle: false,
	onBefore: function(e, elem, $pane, $items, pos) { 
	    currentQuestion = pos;
	    $('#prev,#next').show();

	    if( pos == 0 ) {
   	        $('#prev').hide();
	    } else if( pos == $items.length-1 ) {
	        $('#next').hide();
            }
	}
    });
    
});