/** 
* Copyright 2008 massimocorner.com
* @author      Massimo Foti (massimo@massimocorner.com)
* @version     0.5.5, 2008-06-10
* @require     tmt_core.js
* @require     tmt_form.js
* @require     tmt_validator.js
* @require     SpryAccordion.js
*/

if(typeof(tmt) == "undefined"){
	tmt = {};
}

if(typeof(tmt.spry) == "undefined"){
	tmt.spry = {};
}

if(typeof(tmt.spry.widget) == "undefined"){
	tmt.spry.widget = {};
}

tmt.spry.widget.AccordionForm = {};

// Constructor
tmt.spry.widget.AccordionForm = function(element, opts){
	if(typeof(tmt.validator) == "undefined"){
		alert("Error: TMT Validator JavaScript library missing");
	}
	this.formNode = tmt.form.getParentForm(this.getElement(element));
	if(!this.formNode){
		alert("Error: Missing <form> tag around Accordion");
	}
	this.visitedIndex = {};
	if(!this.callback){
		this.callback = tmt.validator.DEFAULT_CALLBACK_MULTISECTION;
	}
	Spry.Widget.Accordion.call(this, element, opts);
}

// Import all methods
for(var x in Spry.Widget.Accordion.prototype){
	tmt.spry.widget.AccordionForm.prototype[x] = Spry.Widget.Accordion.prototype[x];
}
tmt.spry.widget.AccordionForm.prototype.constructor = tmt.spry.widget.AccordionForm;
tmt.spry.widget.AccordionForm.prototype.ERROR_CLASS = "tmtAccordionError";
tmt.spry.widget.AccordionForm.prototype.VALID_CLASS = "tmtAccordionValid";

/* New and overwritten methods */

tmt.spry.widget.AccordionForm.prototype.onPanelTabClick = function(e, panel){
	this.setVisited();
	if(panel != this.currentPanel){
		this.openPanel(panel);
	}	
	else{
		this.closePanel();
	}	
	this.validateVisited();
		
	if(this.enableKeyboardNavigation){
		this.focus();
	}
	if(e.preventDefault){
		e.preventDefault();
	}
	else {
		e.returnValue = false;
	}
	if(e.stopPropagation){
		e.stopPropagation();
	} 
	else {
		e.cancelBubble = true;
	}
	return false;
}

tmt.spry.widget.AccordionForm.prototype.getPanelValidators = function(panel){
	var fields = tmt.form.getChildFields(panel);
	var validators = [];
	for(var i=0; i<fields.length; i++){
		validators.push(tmt.validator.fieldValidatorFactory(fields[i]));
	}
	return validators;
}

tmt.spry.widget.AccordionForm.prototype.getVisitedPanels = function(){
	var panels = this.getPanels();
	var visitedPanels = [];
	for(var i=0; i<panels.length; i++){
		if(this.isVisited(i)){
			visitedPanels.push(panels[i]);
		}
	}
	return visitedPanels;
}

tmt.spry.widget.AccordionForm.prototype.flagInValid = function(panel){
	var tab = this.getPanelTab(panel);
	this.removeClassName(tab, this.VALID_CLASS);
	this.addClassName(tab, this.ERROR_CLASS);
}

tmt.spry.widget.AccordionForm.prototype.flagValid = function(panel){
	var tab = this.getPanelTab(panel);
	this.removeClassName(tab, this.ERROR_CLASS);
	this.addClassName(tab, this.VALID_CLASS);
}

tmt.spry.widget.AccordionForm.prototype.isVisited = function(panelIndex){
	if(this.visitedIndex[panelIndex]){
		return true;
	}
	return false;
}

tmt.spry.widget.AccordionForm.prototype.setVisited = function(){
	this.visitedIndex[this.getCurrentPanelIndex()] = true;
}

tmt.spry.widget.AccordionForm.prototype.validate = function(){
	var results = this.validatePanels(this.getPanels());
	// Forward results to the callback
	eval(this.callback + "(this.formNode, results.hasErrors, results.sectionResults)");
	return results.hasErrors == false;	
}

tmt.spry.widget.AccordionForm.prototype.validateVisited = function(){
	var results = this.validatePanels(this.getVisitedPanels());
	// Forward results to the callback
	eval(this.callback + "(this.formNode, results.hasErrors, results.sectionResults)");
	return results.hasErrors == false;
}

tmt.spry.widget.AccordionForm.prototype.validatePanels = function(panels){
	var sectionResults = [];
	var hasErrors = false;	
	for(var i=0; i<panels.length; i++){
		var panelResults = {};
		panelResults.label = this.getPanelTab(panels[i]).innerHTML;
		panelResults.validators = [];
		var validators = this.getPanelValidators(panels[i]);
		var activeValidators = tmt.validator.executeValidators(validators);
		// If errors, attach active validators
		if(activeValidators.length > 0){
			panelResults.validators = activeValidators;
			this.flagInValid(panels[i]);
			hasErrors = true;
		}
		else{
			this.flagValid(panels[i]);
		}
		sectionResults.push(panelResults);
	}
	return {sectionResults: sectionResults, hasErrors: hasErrors};
}