
// make sure that the required includes are there
if (typeof NC == 'undefined') {
	alert("gmap.js requires the NC JavaScript framework");
}


(function () {
	
NC.GaussAppManager = function() {
	/* private variables and methods */
	var baseDivName = null;
	var baseDiv = null;
	var notesDiv = null;
	var scores = null;
	var maxBars = 12;
	var xBarOffset=365;
	var barDistance=30;
	var bars = new Array(12); // 70 to 180
	var curScore = 0;
	var baseDivExtent;
	var autoNote = true;
	var noteTimeout = -1;
	var skinnyNotes = false;
	var tileHeight = 5;
	var starimage = new Array( '',
		'<img src="../imagesv2/badges/one_star_2.png">',
		'<img src="../imagesv2/badges/two_stars_2.png">',
		'<img src="../imagesv2/badges/three_stars_2.png">',
		'<img src="../imagesv2/badges/four_stars_2.png">',
		'<img src="../imagesv2/badges/five_stars_2.png">');

  	
 	 	return {
 	 		init: function( background, _baseDivName, _notesDivName, _xBarOffset, _barDistance, _tileHeight, _skinnyNotes  ) {
				baseDivName = _baseDivName;
				baseDiv = document.getElementById( baseDivName);
				notesDiv = document.getElementById( _notesDivName);
				xBarOffset = _xBarOffset;
				barDistance = _barDistance;
				skinnyNotes = _skinnyNotes;
				tileHeight = _tileHeight;
				baseDivExtent = NC.util.Region.getRegion( baseDiv);
				if (scores == null) {
					// test data
					/*
					scores = [
					    {
					        "categoryname": "Technical Support",
					        "date": "2002-12-22 00:00:00.0",
					        "highestdegree": "",
					        "mgrcategory": 2,
					        "mgrstars": 1,
					        "ncscore": 94,
					        "resumeid": 35818,
					        "yearsexperience": 38
					    },
					    {
					        "categoryname": "Product Management",
					        "date": "2004-02-17 00:00:00.0",
					        "highestdegree": "bachelors",
					        "mgrcategory": 2,
					        "mgrstars": 3,
					        "ncscore": 120,
					        "resumeid": 87945,
					        "yearsexperience": 30
					    },
					    {
					        "categoryname": "Software Engineering",
					        "date": "2002-06-19 00:00:00.0",
					        "highestdegree": "bachelors",
					        "mgrcategory": 1,
					        "mgrstars": 0,
					        "ncscore": 78,
					        "resumeid": 17322,
					        "yearsexperience": 7
					    }
					];
					*/
				}
				// size the baseDiv to what we need
				// initialize the bars
				this.initBars();
				// set the background array
				// call animate with some time delay for all the scores
				this.getScores();
  	 		},
			
			initBars: function() {
				var curLeft = xBarOffset;
				var i = 0;
				for (i = 0; i < maxBars; i++) {
					bars[i] = {
							top: baseDivExtent.bottom - baseDivExtent.top - 35,
							left: curLeft
						};
					curLeft += barDistance;
				}
			},
			
			getScores: function() {
				var callback =
				{
	  				success:function( o) {
						scores = NC.lang.JSON.parse(o.responseText);
						for (var i = 0; i < scores.length; i++) {
							scores[i].classname = 'gaussbox';
						}
						this.animate();
	  				},
	  				failure:function( o) {
//						alert( "failed to get scores");
						return null;
	  				},
					scope: this    				
				};
				var updateUrl = 'getCandidateData.php';
				var connection = new NC.util.AjaxConnection( 'GET', updateUrl, null);
				connection.executeMethod( callback);
			},
			
			insertScore: function( categoryname, date, highestdegree, mgrcategory, mgrstars, ncscore, resumeid, yearsexperience, classname) {
				var myNotesDiv = document.getElementById('MyNotes');
				if (myNotesDiv != null && scores != null) {
					var oScore = {};
					oScore.categoryname = categoryname;
					oScore.date = date;
					oScore.highestdegree = highestdegree;
					oScore.mgrcategory = mgrcategory;
					oScore.mgrstars = mgrstars;
					oScore.ncscore = ncscore;
					oScore.resumeid = resumeid;
					oScore.yearsexperience = yearsexperience;
					oScore.classname = classname;
					oScore.location = 'Fremont, CA';
					scores.splice(curScore + 1, 0, oScore);
					var note = this.formatNote(oScore);
					note = '<h1>Your score</h1>' + note;
					myNotesDiv.innerHTML = note;
				}
				
			},
			
			animate: function() {
				curScore = 0; // kick off the process
				autoNote = true;
				this.floatAScore( scores[curScore], "score" + curScore);
			},
			
			onComplete: function() {
				// increment curScore
				curScore += 1;
				if (curScore < scores.length) {
					var randtime = Math.floor(Math.random()*1000);
					window.setTimeout(
						function() {
							NC.GaussAppManager.floatAScore( scores[curScore], "score" + curScore);
						}, 
						randtime);
				}
				else {
					// remove all children
					window.setTimeout(
						function() {
							while (baseDiv.childNodes.length >= 1) {
								baseDiv.removeChild(baseDiv.firstChild);
							}
							notesDiv.innerHTML = "Initializing...";
							NC.GaussAppManager.initBars();
							NC.GaussAppManager.getScores();
						}, 
						10000);
				}
			},
			
			formatNote: function(attr) {
				var note;
				if (skinnyNotes) {
					note = '<p>' +
					'<img src="../imagesv2/paw_14px.png">&nbsp;' +
					'<span style="font-size:16px;font-weight:bold;font-family: Arial, Helvetica, sans-serif;">' + attr.ncscore + 
					'</span> ' +
					attr.categoryname + ', ';
					if (attr.mgrcategory > 1 && attr.mgrstars >= 1) {
						note += 'Management ' + starimage[attr.mgrstars];
					}
					note += '<br>';
					if (attr.location.length > 1) {
						note += attr.location + ', ';
					}
					note += 'since ' + attr.date +
					'</p>';
				}
				else {
					note = '<h2>' +
					'<img src="../imagesv2/paw_14px.png">&nbsp;' +
					attr.ncscore +
					'</h2>' +
					'<h3>' +
					attr.categoryname;
					if (attr.mgrcategory > 1 && attr.mgrstars >= 1) {
						note += '<br>' + 'Management ' + starimage[attr.mgrstars];
					}
					
					note += '</h3>';
					note += '<p>';
					if (attr.location.length > 1) {
						note += attr.location + '<br>';
					}
					if (attr.yearsexperience > 0 && attr.yearsexperience <= 25) {
						note += attr.yearsexperience + ' years in workforce<br>';
					}
					if (attr.highestdegree.length > 0) {
						note += 'Earned ' + attr.highestdegree + ' degree<br>';
					}
					note += 'Since ' + attr.date +
					'</p>';
				}
				return note;
			},
			
			setAutoNote: function( value){
				autoNote = value;
			},
			
			getNoteTimeoutId: function() {
				return noteTimeout;
			},
			
			setNoteTimeoutId: function( id) {
				noteTimeout = id;
			},

 	 		floatAScore: function( oScore, label) {
				// round the score to figure out which bar
				var iBar = Math.floor((oScore.ncscore - 70) / 10);
				iBar = Math.max(iBar, 0);
				iBar = Math.min(iBar, maxBars -1);
				// create a score div
				var elDiv = document.createElement("div");
				elDiv.id = label;
				var xOffset = bars[iBar].left;
				NC.util.Dom.addClass( elDiv, oScore.classname);
				baseDiv.appendChild(elDiv);
				elDiv.style.left= xOffset + "px";
				elDiv.attr = oScore;
				if (autoNote) {
					notesDiv.innerHTML = this.formatNote(elDiv.attr);
				}
				NC.util.Event.addListener(elDiv, "click", function (e) { 
						autoNote = false;
						notesDiv.innerHTML = NC.GaussAppManager.formatNote( elDiv.attr);
						var lastTimeout = NC.GaussAppManager.getNoteTimeoutId();
						if ( lastTimeout != -1) {
							window.clearTimeout(lastTimeout)
						}
						lastTimeout = window.setTimeout( function() {
							NC.GaussAppManager.setAutoNote( true);
						}, 5000);
					    NC.GaussAppManager.setNoteTimeoutId( lastTimeout); 
					}); 				
//				elDiv.style.top = baseDivExtent.top + "px";
				// animate it down to the top of the bar
				    var a1 = {
				        top: {
//							from: baseDivExtent.top, 
//							to: bars[iBar].top 
							by: bars[iBar].top
							}
				    };
    			var anim = new NC.util.Anim(label, a1, 1, NC.util.Easing.bounceOut);
				anim.onComplete.subscribe(
					function() { NC.GaussAppManager.onComplete(); } );
				anim.animate();

				// adjust the top of the bar
				bars[iBar].top -= tileHeight;
 	 		}
  	 	}
  }	
();

})(); 	
 

