/*
	Author: Jens Arps, http://jensarps.de/
	
	Available via Academic Free License >= 2.1 OR the modified BSD license.

*/
dojo.provide("app.Fieldset");

dojo.require("dijit.TitlePane");

dojo.declare("app.Fieldset",
[dijit.TitlePane],
{   
	// summary:
	//		A fieldset that can be expanded or collapsed.
	//
	// description:
	//		An accessible fieldset that can be expanded or collapsed via 
	//		it's legend. Fieldset extends `dijit.TitlePane`.
	//
	//
	// example:
	// |	<!-- markup href example: -->
	// |	<div dojoType="app.Fieldset" href="foobar.html" legend="The Legend"></div>
	// 
	// example:
	// |	<!-- fieldset markup -->
	// | 	<fieldset dojoType="app.Fieldset">
	// |        <legend>The Legend</legend>
	// |		<p>Some content</p>
	// |	</div>
	//
	// example:
	// |	// programmatic
	// |	var fieldset = new app.Fieldset({legend:'dojo fieldset 4'},dojo.create('div',{id: 'someId'},dojo.body()));
	// |	fieldset.attr('content','<p>I was created programmatically!</p>');
	

	// baseClass: [protected] String
	//		The root className to use for the various states of this widget
    baseClass: 'appFieldset',
    
	// legend: String
	//		Content of the legend tag. Overrides <legend> tag, if present in markup.
    legend: '',

	// open: Boolean
	//		Whether fieldset is opened or closed.
    open: false,
    
    templateString: '',
    templatePath: dojo.moduleUrl("app", "templates/Fieldset.html"),
    
    postCreate: function() {
	
        this.setupLegend();
        
        // provide a titleNode property, dijit.TitlePane expects it to be present.
        // In this case, our legendTextNode does the job.
        this.titleNode = this.legendTextNode;
        
		this.inherited(arguments);
    },
    
    setupLegend: function() {
		// summary:
		//		Sets up the content of the <legend> tag.
		// 		Will take the legend argument, if given, or search for given legend
		// 		node in markup.
    	
    	
    	// did we receive a legend?
    	if(this.legend !== '') {
            this.attr('legend',this.legend);
    	} else { // try and find legend tag
            var legends = dojo.query('legend',this.containerNode),
            	fieldsets = dojo.query('fieldset',this.containerNode);
            if(!legends.length) { // oops, no legend?
                return;
            }
            // copy text
            this.attr('legend',legends[0].innerHTML);
            // remove
            if(legends.length == (fieldsets.length + 1)) {
            	legends[0].parentNode.removeChild(legends[0]);
            }
    	}

    },
    
	_handleHover: function(e) {
    	// summary:
    	//		Handle hover states for the legend
    	// tags:
    	//		private
    	
		dojo.toggleClass(this.focusNode, this.baseClass + "Legend-hover", e.type === 'mouseover');
	},
	
	_setLegendAttr: function(/* String */ legend) {
		// summary:
		//		Hook to set the legend via attr("legend", legend).
		// legend: String
		//		The text that should appear in the legend.
		
		this.legendTextNode.innerHTML = legend;
	},
	
	_setTitleAttr: function(/* String */ title) {
		// summary:
		//		Alias for attr("legend", legend), so that existing TitlePane code
		//		works with this Fieldset.
		// title: String
		//		The text that should appear in the legend.
		
		this.attr('legend',title);
	}
}
);