$(document).ready(function(){
	$("#jqfeedback").html('[:)]')


	$('.controls').addClass('clickable')
	$('.prv').click(function() {getnextpic($(this),'prv') })

	$('.nxt').click(function() {getnextpic($(this),'nxt') }).hide()

	$('.resp').click(function() { markoption($(this))})
	.addClass('clickable')

// first pic is selected in login.js

	$('.getreptbtn').click(function() { getrept() })
	.addClass('clickable')

	$('#jqfeedback').click(function() {
		getrept()
		$('#getrept').show()
	})
	.addClass('clickable')


});
//============================================
function getnextpic(thispic,direction){
	var thispicname=$(thispic).parents('.strippanel').find('img').attr('src')
	var poststr= "thispic=" + thispicname+"&dir="+direction+"&resp="+$('.marked').attr('title')+"&goto="+$('input[@name=goto]').val()

	$('input[@name=goto]').val('0')
	$(".prholder").html('<img class="progress" src="progress.gif">')
			$.ajax({
   			type: "POST",
   			url: "ajq/ajq_fetchpic.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$(".progress").remove()
				if (jr.respz.outfile=='done') {
					$(thispic).parents('.strippanel').find('img').attr('src','images/done.jpg')
					$('#getrept').show()
					$('#showstrips').hide()
					getrept()
				}
				else {
					$(thispic).parents('.strippanel').find('img').attr('src','strips/'+jr.respz.outfile)
                                                                 .attr('title',jr.respz.outfile)
					if (jr.captions!=undefined) {
	//alert(jr.captions[1]['w'])
	// 					var w=jr.captions[1]['w']
						$('#cap1').width(parseInt(jr.captions[1]['w'],10))
						$('#cap1').html(jr.captions[1]['text'])

						$('#cap2').width(parseInt(jr.captions[2]['w'],10))
	// 					$('#cap2').css('width',jr.captions[2]['w'])
						$('#cap2').html(jr.captions[2]['text'])

						$('#cap3').width(parseInt(jr.captions[3]['w'],10))
						$('#cap3').html(jr.captions[3]['text'])

						$('#cap4').width(parseInt(jr.captions[4]['w'],10))
						$('#cap4').html(jr.captions[4]['text'])
					}
					$('#getrept').hide()
					$('#showstrips').show()
                 }

				$('input[@name=username]').val(jr.respz.username)
//alert(jr.respz.testid)
				$('input[@name=testid]').val(jr.respz.testid)
//alert($('input[@name=testid]').val())
				$('.nxt').hide()
  				$('.resp').removeClass('marked')

				$("#feedback").html("<hr>"+jr.respz.html)

//				$("#fscores").html(jr.respz.htmlscore)

   			}
 		});
}

//============================================
function markoption(obj) {

  $('.resp').removeClass('marked')
  $(obj).addClass('marked')
  $('.nxt').show()
   .click() // comment out when not debugging to allow user to reconsider
}
//============================================
function getrept() {
	var testid=$('input[@name=testid]').val()
	if (!testid) $('#therept').html('Returning results for first test done')
	else $('#therept').html('<img class="progress" src="9-0.gif">')

	var poststr="&testid="+$('input[@name=testid]').val()+"&username="+$('input[@name=username]').val()

//	alert('getrept:'+poststr)

	$.ajax({
   			type: "POST",
   			url: "ajq/ajq_fetchrept.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$(".progress").remove()
				$('#joblist').remove()
				$('#therept').html(jr.respz.rept)
				$('#therept').after(jr.jobs)
			}
		})
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

