﻿//CAT Call - meow.js
//version 1.0

$(document).ready(function() //once document is ready

   {
   	
   var thisrecord = document.location.href;
   if(thisrecord == "http://www.thegateway.org/browse/dcrecord.2011-04-24.0693875836"){
   $("td:eq(24)").text("Facing History and Ourselves");
 		}

$("#catSearch").click(function(){ //on click of suggest standards button do this
	
	var urlJurisdiction = $('#jurisdd').val(); //get the value of the jurisdiction drop down
	var urlSubject = $('#subjectdd').val(); //get the value of the subject drop down
	var ddGrades = $('#gradedd').val(); //get the value of the grades drop down

	if(urlJurisdiction==0 || urlSubject==0 || ddGrades==0){ //if any of the values in the dropdowns are zero create a jquery ui dialog box alerting user of this
		var $dialog = $('<div></div>')
		.html("You must select a <em>Jurisdiction</em>, <em>Subject</em> and <em>Grade</em>.")
		.dialog({
			draggable: false,
			resizable: false,
			width: 375,
			autoOpen: false,
			title: 'Cannot Suggest Standards - Missing Value ',
			modal: true,
			buttons: { "OK": function() { $(this).dialog("close"); }}
		});//close dialog
		
	$dialog.dialog('open');

	}else{ //if the values in the dropdowns contained valid data do this

		$('.catTextResults').empty(); //empty text result class
		$('.benchmark').empty(); //empty benchmark class

		var urlDescription = $('td').slice(6,7).text(); //get the description from the screen
		urlDescription = encodeURIComponent(urlDescription); //uri encode the description
		
		urlDescription = urlDescription.substr(0,324); //get the first 324 characters of description to sumbit to CAT
		
		var urlKeyword = $('td').slice(16,17).text(); //get the keyword from the screen
		urlKeyword = encodeURIComponent(urlKeyword); //uri encode the keyword
		
		urlKeyword = urlKeyword.substr(0,324); //get the first 324 characters of keywords to submit to CAT
				
		var grades = new Array(); //create a new array for grade values that come over from the drop down
		grades = ddGrades.split(','); //split the return at the comma level
		var urlGradeStart = grades[0]; //part 1 is the begining grade
		var urlGradeEnd = grades[1]; //part 2 is the end grade
		
		//make the JSONP request:
	
		$.getJSON("http://jesandco.info/cat_request.php?method=suggestStandards&username=jesandco&password=a.s.n&query=" + urlDescription + "&author=" + urlJurisdiction + "&topic=" + urlSubject + "&startGrade=" + urlGradeStart + "&endGrade=" + urlGradeEnd + "&maxResults=2&keywords=" + urlKeyword + "&callback=?", 
		function(JSONcat){
			if (JSONcat.SuggestedStandards.Results.Result.NumResults == "0"){
				$("ol").append("No Suggestions<br/><br/>");
				//$("ol").append("<em><strong>We'd like to hear from you about this new feature.  Click <a href='http://www.surveymonkey.com/s/DBVXZNZ'>here</a> to take a brief survey and leave your feedback about this pilot program.</strong></em>"); //add survey to end of list
			}// end num = 0
			else if(JSONcat.SuggestedStandards.Results.Result.NumResults == "1"){
					var CATtaxon = JSONcat.SuggestedStandards.Results.Result.Standard.Text; //get the taxon
					var CATgrade = JSONcat.SuggestedStandards.Results.Result.Standard.GradeLevels; //get the grade
					var CATbenchmark = JSONcat.SuggestedStandards.Results.Result.Standard.Benchmark; //get the benchmark
					
					if (CATtaxon == "[object Object]"){ //if no taxon path do this
					$("ol").append("<li><div class='benchmark'>&raquo; " + CATbenchmark + " (Grade: " + CATgrade + ")</div></li><br/>"); //write out the items minus taxon variable
					}else{ //taxon exists do this			
					$("ol").append("<li><div class='taxon'>" + CATtaxon + "</div></li>" + "<div class='benchmark'><ul>&raquo; " + CATbenchmark + " (Grade: " + CATgrade + ")</ul></div><br/>"); //write out the items to the ol
					} //close taxon check if
					//$("ol").append("<em><strong>We'd like to hear from you about this new feature.  Click <a href='http://www.surveymonkey.com/s/DBVXZNZ'>here</a> to take a brief survey and leave your feedback about this pilot program.</strong></em>"); //add survey to end of list
					
			}  //end num = 1
			else {
				for (i=0; i < JSONcat.SuggestedStandards.Results.Result.Standard.length; i++){ //for every result
					var CATtaxon = JSONcat.SuggestedStandards.Results.Result.Standard[i].Text; //get the taxon
					var CATgrade = JSONcat.SuggestedStandards.Results.Result.Standard[i].GradeLevels; //get the grade
					var CATbenchmark = JSONcat.SuggestedStandards.Results.Result.Standard[i].Benchmark; //get the benchmark
					
					if (CATtaxon == "[object Object]"){ //if no taxon path do this
					$("ol").append("<li><div class='benchmark'>&raquo; " + CATbenchmark + " (Grade: " + CATgrade + ")</div></li><br/>"); //write out the items minus taxon variable
					}else{ //taxon exists do this
					$("ol").append("<li><div class ='taxon'>" + CATtaxon + "</div></li>" + "<div class='benchmark'><ul>&raquo; " + CATbenchmark + " (Grade: " + CATgrade + ")</ul></div><br/>"); //write out the items to the ol
					} // close taxon check if

				}//endfor
					//$("ol").append("<em><strong>We'd like to hear from you about this new feature.  Click <a href='http://www.surveymonkey.com/s/DBVXZNZ'>here</a> to take a brief survey and leave your feedback about this pilot program.</strong></em>"); //add survey to end of list
			}//end else if
		
		});//end function JSONcat

	}//close if dialogs are zero

}); //close onclick

}); //close docReady
