/** PEAK PERFORMANCE TEST LICENSE
 * Copyright (c) Nathan Trujillo ntrujillo http://www.mindgruve.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
* @author Nathan Trujillo
*
*/


/**	GRAPH LICENSE
 * Copyright (c) Dragan Bajcic http://dragan.yourtree.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 * @author Dragan Bajcic
 * @link http://dragan.yourtree.org/code/ajax-mgraph/
 * 
 */


function draw(g){

	this.id=g.divID;

	this.htmlString='';

	this.htmlString='<div id="ag-head-'+this.id+'" class="ag-head"></'+'div>';

	this.htmlString+='<div class="ag-graph-key">'+'<' +'/div><div id="ag-gLeft-'+this.id+'" class="ag-gLeft"><div id="ag-graph-'+this.id+'" class="ag-graph"><div id="ag-dump-'+this.id+'" class="ag-dump">No Data to display :]</'+'div></'+'div>';

	this.htmlString+='<div class="x-titles"><div id="x-title-1-'+this.id+'" class="x-title"></'+'div><div id="x-title-2-'+this.id+'" class="x-title"></'+'div><div id="x-title-3-'+this.id+'" class="x-title"></'+'div><div id="x-title-4-'+this.id+'" class="x-title"></'+'div><div id="x-title-5-'+this.id+'" class="x-title"></'+'div><div id="x-title-6-'+this.id+'" class="x-title"></'+'div><div id="x-title-7-'+this.id+'" class="x-title"></'+'div><div id="x-title-8-'+this.id+'" class="x-title"></'+'div><div id="x-title-9-'+this.id+'" class="x-title"></'+'div><div id="x-title-10-'+this.id+'" class="x-title"></'+'div></'+'div></'+'div>';

	this.htmlString+='<div id="ag-gSidebar-'+this.id+'" class="ag-gSidebar"><h3>Stats</'+'h3><div id="ag-max-'+this.id+'"></'+'div><div id="ag-min-'+this.id+'"></'+'div><div id="ag-avg-'+this.id+'"></'+'div><h4 id="note-title-'+this.id+'">Note</'+'h4><div id="note-content-'+this.id+'">Little note about this graph goes here.. </'+'div></'+'div>';

	$(g.divID).setStyle({display:'none'});
	fadein($(g.divID));
	
	$(g.divID).innerHTML=this.htmlString;
	$(g.divID).setStyle({height:'300px'});
	

	
	$('ag-dump-'+this.id).innerHTML='Loading...';
	
	if(g.ShowNote=='yes'){
		
		$('note-title-'+this.id).setStyle({display:'block'});
		$('note-content-'+this.id).setStyle({display:'block'});
		$('note-title-'+this.id).update(g.NoteTitle);
		$('note-content-'+this.id).update(g.NoteText);
	
	}		
	
		var RESP = {	
		
			num:[
			     sectionsObj.vision,
			     sectionsObj.preparation,
			     sectionsObj.will,
			     sectionsObj.positivity,
			     sectionsObj.process,
			     sectionsObj.thoughts,
			     sectionsObj.motivation,
			     sectionsObj.acceptance,
			     sectionsObj.confidence,
			     sectionsObj.fun
			],
			
			xTitle:["Vision", "Preparation", "Will",
				"Positivity", "Process<br/>Focus", "Thoughts<br />Emotions",
				"Motivation", "Acceptance<br/>Ego", "Confidence" ,
				"Fun<br/>Creativity"
			],
						
			xLink:[],
			
			msg:"Success"
			
		};

		$('ag-dump-'+this.id).innerHTML='';

		var gr = new Graph(RESP.num,this.id);

		this.i=1;

		RESP.xTitle.each(function(itm){
			//debug(i);
			$('x-title-'+i+'-'+this.id).update('<a href="#">' + itm +' &raquo;</' + 'a>');
			
			$('x-title-'+i+'-'+this.id).oid = i;
			
			$('x-title-'+i+'-'+this.id).onmouseover = function(){
				
				//$('x-title-'+ this.oid + '-' + g.divID).setStyle({background:'#e1e1e1',color:'#414141'});
				var dataStr =$('key').getElementsByTagName('p')[this.oid - 1].innerHTML + "<br />";
				p = document.createElement('p');
				p.innerHTML = 	dataStr;		
				$('ag-head-'+ g.divID).update(p);
				
			}
			
			$('x-title-'+i+'-'+this.id).onmouseout = function(){
			
				$('x-title-'+ this.oid + '-' + g.divID).setStyle({background:'#ffffff',color:'#898989'});
			}
			
			i=i+1;
		});

	 	$('ag-dump-'+this.id).innerHTML+=gr.returnHTML;
		$('ag-head-'+this.id).update('Move mouse over graph bars to show values')
		$('ag-max-'+this.id).update('Max: '+gr.max);	 
		$('ag-min-'+this.id).update('Min: '+gr.min);
		$('ag-avg-'+this.id).update('Avg: '+gr.avg);
		
}	

		var gShow=function(elmnt,num,cnt,id){

			var dataStr =$('key').getElementsByTagName('p')[cnt - 1].innerHTML + "<br />";
			
			p = document.createElement('p');
			p.innerHTML = 	dataStr;		
			
			$('ag-head-'+id).update(p);
			
			//$('x-title-'+cnt+'-'+id).setStyle({background:'#e1e1e1',color:'#414141'});
			
		}
		
		var gHide=function(elmnt,cnt,id){	

			$('x-title-'+cnt+'-'+id).setStyle({background:'#ffffff',color:'#898989'});
			
		}

		var Graph = Class.create();
		
		Graph.prototype = {
		
			initialize:function(data,id){
				this.data=data;
				this.id=id;
				this.max=Array.max(data);
				this.min=Array.min(data);
				this.avg=0;
				this.returnHTML=this.unfold(data);
			},
			
			unfold: function(da){
			
				this.len= da.length;
				this.tmp='';
				for (i=0;i<this.len;i++){
					this.scaled=Math.round(178/this.max * da[i]);
					this.mtop=178-this.scaled;
					this.counter=i+1;
					this.avg+=da[i];
					this.tmp+='<div style="height:'+this.scaled+'px;margin-top:'+this.mtop+'px;" class="m-bar" onmouseover="gShow(this,'+da[i]+','+this.counter+',\''+this.id+'\');" onmouseout="gHide(this,'+this.counter+',\''+this.id+'\');"></'+'div>';
				}
				
				this.avg=Math.round(this.avg/this.counter);
				this.storeAvg(this.avg);
				return this.tmp;	
			},
			
			storeAvg:function(data){
				this.avg=data;
			}
		}
		
	Array.max = function( array ){
		return Math.max.apply( Math, array );
	};
	Array.min = function( array ){
		return Math.min.apply( Math, array );
	};

	
	function debug(txt){
		$('debug').innerHTML += txt + "<br />\n";
	}
	
	function altColors(){
		$$('div.section > div:nth-child(odd)').each(function(s) {
			s.addClassName('odd');
		});
		$$('div.section > div:nth-child(even)').each(function(s) {
			s.addClassName('even');
		});
	}
		
	function progresso(value,obj){	
		
		question += value;	
		percentage = question / questions;  
		var progressWidth = Math.floor(barWidth * percentage);		
		//debug(stage + " / " + questions + " "  + percentage + "% " + barWidth + " ='" +  progressWidth + "'" );	
		$('progress').style.width = progressWidth + 'px';	
		
		if(question == questions){
			
			fadeout($('score_graph'));
			$('pageInfo').update('');
			fadein($('resultsInfo'));
			draw(graph1);		
			fadeout($('test'));
			fadein($('bookDiv'));
			fadein($('bookImg'));
			
		}else if(question % perSection == 0){// what we do for the next section
			
			fadeout($('introText'));			
			fadeout($('section' + currentPage));
			currentPage++;
			$('section' + currentPage).style.display ="inline";
			$('pageInfo').innerHTML = "PAGE " + currentPage  + " of " + sections;	
		}
	
		fadeout(obj.parentNode.parentNode);
//		obj.parentNode.parentNode.style.display = "none";
		altColors();
	  
	}

	
	var graph1={
		divID:'testResults',
		ShowNote:'no'
	}
	

	
	var sectionsObj = {	
		vision:		1,
		preparation:	1,
		will:		1,
		positivity:	1,
		process:	1,
		thoughts:	1,
		motivation:	1,
		acceptance:	1,
		confidence:	1,
		fun:		1
	}
		
	function fadein(obj){
		new Effect.Appear(obj, {duration:2});
	}
	
	function fadeout(obj){
		new Effect.Fade(obj, {duration:.2});
	}
