/*
 
<div id="question4">
	<div class="question-colapsed">
		<h4>4.</h4>
		<div class="question-titleblock">
			<h2>Imagine your KiwiSaver savings have built up to $10,000 and over the next year they drop in value to $9,000, What would you do?</h2>
			<h3>The choosen answer will be showing here.</h3>
		</div>
		<div class="clear"></div>
	</div>
	<div class="question-expanded">
		<h4>4.</h4>
		<div class="question-titleblock">
			<h2>Imagine your KiwiSaver savings have built up to $10,000 and over the next year they drop in value to $9,000, What would you do?</h2>
		</div>
		<div class="clear"></div>
		<div class="hr"></div>
	    <ul>
    	    <li><a href="javascript:void(0)" score="0">Consider switching to a lower risk investment fund.</a></li>
			<li><a href="javascript:void(0)" score="5">Monitor my investment closely.</a></li>
			<li><a href="javascript:void(0)" score="10">Do nothing and expect that my investment will recover at some time in the next one to two years.</a></li>
			<li><a href="javascript:void(0)" score="15">Consider increasing the amount I am saving.</a></li>
		</ul>
        <div class="nextcontrols">
			<div class="hr"></div>
			<div class="nextbutton">
				<a href="javascript:void(0)">
					<img src="../css/images/content/gfx-form-profiler-next-question.jpg" width="108" height="27" alt="Next Question" />
					<span>Next Question</span>
				</a>
			</div>
		</div>
	</div>
</div>
 
 
 * 
 */



function ProfileQuestionPanel(args){
	var arguments = (typeof args == 'object') ? args : {};
	// defaults for arguments
	arguments.questionNumber 	= (typeof arguments.questionNumber == 'undefined') 	? 0 : arguments.questionNumber;
	arguments.mode 	= (typeof arguments.mode == 'undefined') 	? 'open' : arguments.mode;
						
	var self 					= this;	// Pointer this so we can access 'this' in callback functions
	
	this.score 					= 0;
	this.answered 				= false;
	this.questionNumber 		= arguments.questionNumber;
	this.containerID 			= 'question' + arguments.questionNumber.toString();
	this.containerIDWithHash 	= '#' + this.containerID;
	this.oContainer				= $(this.containerIDWithHash);
	this.colapsed = new Object();
	this.colapsed.div = $(this.containerIDWithHash + ' .question-colapsed')[0];
	this.colapsed.h3 = $(this.containerIDWithHash + ' h3');
	this.expanded = new Object();
	this.expanded.div = $(this.containerIDWithHash + ' .question-expanded')[0];
	this.options = $(this.containerIDWithHash + ' .question-expanded ul a');
	this.nextcontrols = new Object();
	this.nextcontrols.exists = false;
	this.nextcontrols.div = $(this.containerIDWithHash + ' .nextcontrols')[0];
	
	if(typeof this.nextcontrols.div != 'undefined'){
		this.nextcontrols.exists = true;
		this.nextcontrols.img = $(this.containerIDWithHash + ' .nextbutton img');
		this.nextcontrols.a = $(this.containerIDWithHash + ' .nextbutton a');
	}else{
		
		
	}

	
	// Private Functions
	var _who = function(){ alert(this.containerID);	}
	
	// Event management
	this.dispatchEvent = function(eventname,data){
		if (data.constructor.toString().indexOf("Array") == -1){
			alert('Your call to dispatch the event ' + eventname + ' must have data passed to it as an array: typeof: ' + data.constructor.toString());
		}else{
			this.oContainer.trigger(eventname, data);
		}
	}
	this.addEventListener = function(eventname,method){
		this.oContainer.bind(eventname,method);
	}
	
	this.setPublicVar = function(name,value){
		this[name] = value;
	}
	
	this.unLoad = function() {
		$(this.containerIDWithHash).hide();
		$(this.containerIDWithHash).html('');
		this.loaded = false;
		this.dispatchEvent('unload',[]);
	}
	
	this.colapse = function() {
		$(this.expanded.div).hide();
		$(this.colapsed.div).show();
	}
	
	this.expand = function() {
		$(this.colapsed.div).hide();
		$(this.expanded.div).show();
	}
	
	this.validateNextButton = function(){
		if(self.nextcontrols.exists){
			if(self.answered){
				self.nextcontrols.img.css('opacity',1);
			}else{
				self.nextcontrols.img.css('opacity',0.3);
			}
		}
		
	}
	
	
	
	
	/*	Localise Event Bindings
	 * 
	 * 	Globalise standard events to custom events against this object for clarity and debugging hooks
	 * 
	 */
	this.closedQuestionClick = function(event,object) {
		/* 	I'm not sure why, but doing any sort of visual update throught this function
			causes Mozilla to lock up all form controls. Very strange even just having an alert()
			in this method causes this problem. Clasping at staws I discovered that using the
			setTimeout() function can avoid the issue. so that is what I have done.
		*/
		var self = this;
		setTimeout ( function(){self.dispatchEvent('closedquestionclick',[event,object,self]);}, 40 );
	}
	
	this.answerClick = function(event,object) {
		/* 	I'm not sure why, but doing any sort of visual update throught this function
			causes Mozilla to lock up all form controls. Very strange even just having an alert()
			in this method causes this problem. Clasping at staws I discovered that using the
			setTimeout() function can avoid the issue. so that is what I have done.
		*/
		var self = this;
		setTimeout ( function(){self.dispatchEvent('answerclick',[event,object,self]);}, 40 );
	}
	
	this.nextClick = function(event,object) {
		/* 	I'm not sure why, but doing any sort of visual update throught this function
			causes Mozilla to lock up all form controls. Very strange even just having an alert()
			in this method causes this problem. Clasping at staws I discovered that using the
			setTimeout() function can avoid the issue. so that is what I have done.
		*/
		var self = this;
		setTimeout ( function(){self.dispatchEvent('nextclick',[event,object,self]);}, 40 );
	}
	
	// event bindings
	$(this.colapsed.div).bind('click',function(event){self.closedQuestionClick(event,this);});
	this.options.bind('click',function(event){self.answerClick(event,this);});
	if(self.nextcontrols.exists) this.nextcontrols.a.bind('click',function(event){self.nextClick(event,this);});
	
	
	
	
	
	// Run initalisation code
	
	if(arguments.mode == 'closed'){
		this.colapse();
	}else{
		this.expand();
	}
	
	this.validateNextButton();
	
	
	/*	Event Bindings
	 * 
	 * 	Globalise standard events to custom events against this object
	 * 
	 */
	
	this.addEventListener('closedquestionclick',function(event,object,profileQuestionPanel){
		if(profileQuestionPanel.answered){
			oProfileQuestions.selectQuestion(profileQuestionPanel.questionNumber);
		}
	});
	
	this.addEventListener('answerclick',function(event,object,profileQuestionPanel){
		var score = parseInt($(object).attr('score'));
		
		profileQuestionPanel.options.removeClass('selected');
		
		if(profileQuestionPanel.answered && profileQuestionPanel.score == score){
			// untick
			profileQuestionPanel.score 					= 0;
			profileQuestionPanel.answered 				= false;
			
			profileQuestionPanel.colapsed.h3.html('');
			profileQuestionPanel.colapsed.h3.hide();
			
			
			
		}else{
			
			$(object).addClass('selected');
			profileQuestionPanel.score 					= score;
			profileQuestionPanel.answered 				= true;
			
			profileQuestionPanel.colapsed.h3.html($(object).html());
			profileQuestionPanel.colapsed.h3.show();
		}
		
		
		$(object).blur();
		
		profileQuestionPanel.validateNextButton();
		oProfileQuestions.validateSubmitButton();

		
	});
	
	this.addEventListener('nextclick',function(event,object,profileQuestionPanel){
		if(profileQuestionPanel.answered){
			oProfileQuestions.selectQuestion(profileQuestionPanel.questionNumber+1);
		}
	});
	
	

	
	
}


function ProfileQuestions(){
	var arguments = (typeof args == 'object') ? args : {};
	// defaults for arguments
	//arguments.questionNumber 	= (typeof arguments.questionNumber == 'undefined') 	? 0 : arguments.questionNumber;
					
	var self = this;	// Pointer this so we can access 'this' in callback functions
	
	this.submitcontrols = new Object();
	this.submitcontrols.div = $('#form-submit-control');
	this.submitcontrols.img = $('#form-submit-control img');
	this.submitcontrols.a = $('#form-submit-control a');
	
	
	this.questions = [
			new ProfileQuestionPanel({questionNumber:1,mode:'open'})
		,	new ProfileQuestionPanel({questionNumber:2,mode:'closed'})
		,	new ProfileQuestionPanel({questionNumber:3,mode:'closed'})
		,	new ProfileQuestionPanel({questionNumber:4,mode:'closed'})
		,	new ProfileQuestionPanel({questionNumber:5,mode:'closed'})
	];
	
	this.selectQuestion = function(questionNumber){
		// close all
		this.questions[0].colapse();
		this.questions[1].colapse();
		this.questions[2].colapse();
		this.questions[3].colapse();
		this.questions[4].colapse();
		
		if(questionNumber-1 < this.questions.length && questionNumber-1 >= 0){
			this.questions[questionNumber-1].expand();
		}
	}
	
	
	this.okToSubmit = function(){
		var oPQP = null;
		var bAllAnswered = true;
		for (var i = 0; i < this.questions.length; i++) {
    		oPQP = this.questions[i];
    		if(!oPQP.answered){
    			bAllAnswered = false;
    			break;
    		}
		}
		return bAllAnswered;
	}
	
	
	this.getScore = function(){
		var score = 0;
		for (var i = 0; i < this.questions.length; i++) {
    		oPQP = this.questions[i];
    		if(oPQP.answered){
    			score = score + parseInt(oPQP.score);
    		}
		}
		return score;
	}
	
	
	this.validateSubmitButton = function(){
		if(this.okToSubmit()){
			this.submitcontrols.img.css('opacity',1);
		}else{
			this.submitcontrols.img.css('opacity',0.3);
		}
	}
	
	this.validateSubmitButton();
	
	this.submitcontrols.a.bind('click',function(){
		var score = parseInt(self.getScore());
		
		if(self.okToSubmit()){
			
					
			
			oProfileQuestions.selectQuestion(-1);
			
			/*var currLoc = window.location;
			var path = location.pathname;
			var pA = path.split("/");*/
			var newPath = "";
			
	
			if (score >= 82) {
				//growth investor
				newPath += "compared-growth-investor.aspx";
			} else if (score >= 65) {
				//balanced investor
				newPath += "compared-balanced-investor.aspx";
			} else if (score >= 46) {
				//moderate investor
				newPath += "compared-moderate-investor.aspx";
			} else if (score >= 29) {
				//conservative investor
				newPath += "compared-conservative-investor.aspx";
			} else {
				//defensive investor
				newPath += "compared-defensive-investor.aspx";
				
			}
			
			setTimeout(function(){
				window.location.href = newPath;
			}, 500);
			
			
		}
	});
	
}


var oProfileQuestions = null;

$(document).ready(function(){
		
	oProfileQuestions = new ProfileQuestions();
	
});








