<!--
  Copyright 2011-2014 Marvell Semiconductor, Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<module xmlns="http://www.kinoma.com/kpr/1">

	<!-- 
		OptionsBehavior
		
		The optionsFields field's value may provide a comma separated list of field names.
		Each named field's default value may optionally be overriden by the data passed into onCreate
	-->

	<behavior id="OptionsBehavior">
		<field id="optionFields"/>

		<method id="onCreate" params="container, data"><![CDATA[
  			this.applyOptions(data);
         ]]></method>
		 <method id="applyOptions" params="options"><![CDATA[
		 	if ( undefined != options ) {
		 		var optionFields = this.optionFields;
		 		optionFields = optionFields.split(",");
		 		for ( var i=0; i < optionFields.length; i++)	
					this.applyFieldOption(options, optionFields[i]);     
			}       
		 ]]></method>
		 <method id="applyFieldOption" params="options, fieldName"><![CDATA[
		 	if (fieldName in options)
		 		this[fieldName] = options[fieldName];
		 ]]></method>
	</behavior>
	
	<!-- 
		ControlBehavior
		
		Provides a mecanism for child objects in compound objects to send events to the root
	-->
	
	<behavior id="ControlBehavior" like="OptionsBehavior">
		<method id="onCreate" params="container, data"><![CDATA[
			OptionsBehavior.prototype.onCreate.call(this, container, data);
			this.container = container
			this.eventRoot = container
         ]]></method>
         
  		<method id="setEventRoot" params="eventRoot"><![CDATA[
 			this.eventRoot = eventRoot;
         ]]></method>
		<method id="sendEvent" params="event"><![CDATA[
			this.eventRoot.delegate(event, this.container);
         ]]></method>
	</behavior>

	<!-- 
		ButtonBehavior
		
		Push button beahvior
	-->
	
	<behavior id="ButtonBehavior" like="ControlBehavior">
		<field id="optionFields" value="'name'"/>
		<field id="name" value="Button"/>

		<method id="onCreate" params="container, data"><![CDATA[
			ControlBehavior.prototype.onCreate.call(this, container, data);
			this.button = container
			this.data = data;
         ]]></method>
		<method id="onDisplaying" params="container"><![CDATA[
			this.onStateChanged(container);
         ]]></method>
		<method id="onStateChanged" params="container"><![CDATA[
			this.changeState(container, container.active ? 1 : 0);
         ]]></method>
		<method id="onTouchBegan" params="container, id, x, y, ticks"><![CDATA[
 			this.changeState(container, 2);
         ]]></method>
		<method id="onTouchCancelled" params="container, id, x, y, ticks"><![CDATA[
 			this.changeState(container, 1);
         ]]></method>
		<method id="onTouchEnded" params="container, id, x, y, ticks"><![CDATA[
 			this.changeState(container, 1);
			this.sendEvent("onTap");
         ]]></method>
		<method id="changeState" params="container, state"><![CDATA[
			container.state = state;
			if (false == ("length" in container))
				debugger
			if (container.length > 0) {
				var content = container.first;
				while (content) {
					content.state = state;
					content = content.next;
				}
			}
         ]]></method>
        
		<method id="getButton"><![CDATA[
 			return this.button;
         ]]></method>
                         
         <!-- public -->
		<method id="onTap" params="container"><![CDATA[
         ]]></method>
	</behavior>

	<container id="Button" active="true" style="THEME.buttonStyle" skin="THEME.buttonSkin" behavior="ButtonBehavior"/>
	
	<!-- 
		ToggleButtonBehavior
		
		Toggle button beahvior
	-->
	
	<behavior id="ToggleButtonBehavior" like="ButtonBehavior">
		<field id="selectedVariant"/>
		<field id="unselectedVariant"/>
		<method id="onTouchBegan" params="container, id, x, y, ticks"><![CDATA[
 			ButtonBehavior.prototype.onTouchBegan.call(this, container, id, x, y, ticks);
			this.doToggle();			           
        ]]></method>
        
        <!-- public -->
        
		<method id="onSelected" params="container"/>
		<method id="onUnselected" params="container"/>
		 
		<method id="doToggle" params="silent"><![CDATA[
			var button = this.button
			if (button.state != 0)
				button.variant = (button.variant == this.selectedVariant) ? this.unselectedVariant : this.selectedVariant
			if (! silent) {
				if (button.variant == this.selectedVariant) 
					this.sendEvent("onSelected");
				else
					this.sendEvent("onUnselected");	
			}	           
        ]]></method>
		<method id="isSelected" params=""><![CDATA[
			return this.button.variant == this.selectedVariant;
        ]]></method>
		<method id="setSelected" params="selected, silent"><![CDATA[
			if (selected != this.isSelected())
				this.doToggle(silent);
        ]]></method>
	</behavior>

	<behavior id="RadioBehavior" like="ToggleButtonBehavior">
		<field id="name" value="'RadioButton'"/>
		<field id="selectedVariant" value="THEME.RADIO_SELECTED"/>
		<field id="unselectedVariant" value="THEME.RADIO_UNSELECTED"/>
	</behavior>

	<container id="Radio" active="true" variant="THEME.RADIO_UNSELECTED" skin="THEME.glyphSkin" behavior="RadioBehavior"/>

	<behavior id="CheckboxBehavior" like="ToggleButtonBehavior">
		<field id="name" value="'Checkbox'"/>
		<field id="selectedVariant" value="THEME.CHECKBOX_SELECTED"/>
		<field id="unselectedVariant" value="THEME.CHECKBOX_UNSELECTED"/>
	</behavior>

	<container id="Checkbox" active="true" variant="THEME.CHECKBOX_UNSELECTED" skin="THEME.glyphSkin" behavior="CheckboxBehavior"/>

	<behavior id="LabeledToggleBehavior" like="ControlBehavior">
		<field id="optionFields" value="'name'"/>
		<field id="name" value="Button"/>
		
		<method id="onCreate" params="container, data"> <![CDATA[
			OptionsBehavior.prototype.onCreate.call(this, container, data);
			container.buttonLabel.string = this.name;
			this.labeledButton = container;
			this.setEventRoot(this.labeledButton);
         ]]></method>
         
   		<method id="setEventRoot" params="eventRoot"><![CDATA[
 			this.eventRoot = eventRoot;
 			this.labeledButton.button.behavior.setEventRoot(eventRoot);
         ]]></method>
         
        <!-- public -->
      
		<method id="onTap" params="root, control"/>
		<method id="onSelected" params="root, control"/>
		<method id="onUnselected" params="root, control"/>
		
		<method id="getButton"><![CDATA[
 			return this.labeledButton.button;
         ]]></method>
		<method id="doToggle" params=""><![CDATA[
			this.getButton().behavior.doToggle();		           
        ]]></method>
		<method id="isSelected" params=""><![CDATA[
			return this.getButton().behavior.isSelected();
        ]]></method>
		<method id="setSelected" params="selected, silent"><![CDATA[
			this.getButton().behavior.setSelected(selected, silent);
        ]]></method>
	</behavior>

	<behavior id="LabeledButtonBehavior" like="LabeledToggleBehavior"/>

	<container id="LabeledButton" style="THEME.buttonStyle" behavior="LabeledButtonBehavior">
		<container name="button" left="0" right="0" top="0" bottom="0" like="Button"/>
		<label name="buttonLabel" left="0" right="0"/>
	</container>
	
	<behavior id="LabeledRadioBehavior" like="LabeledToggleBehavior"/>

	<line id="LabeledRadio" style="THEME.labeledButtonStyle" behavior="LabeledRadioBehavior">
		<container name="button" left="0" like="Radio"/>
		<label name="buttonLabel" left="0"/>
	</line>
	
	<behavior id="LabeledCheckboxBehavior" like="LabeledToggleBehavior"/>

	<line id="LabeledCheckbox" style="THEME.labeledButtonStyle" behavior="LabeledCheckboxBehavior">
		<container name="button" left="0" like="Checkbox"/>
		<label name="buttonLabel" left="0"/>
	</line>
	
	<behavior id="RadioGroupBehavior" like="ControlBehavior">>
		<field id="optionFields" value="'buttonNames'"/>
		<field id="buttonNames" value="'One,Two,Three,Four,Five'"/>
		
		<method id="onCreate" params="container, data"> <![CDATA[
  			OptionsBehavior.prototype.onCreate.call(this, container, data);
			var buttonNames = this.buttonNames.split(",");
			for (var i=0; i < buttonNames.length; i++) {
				var buttonName = buttonNames[i];
				var button = new LabeledRadio( { name : buttonName } );
				button.coordinates = { left : 0, top : undefined, right : undefined, bottom : undefined };
				button.behavior.setEventRoot(container)
				container.add( button );
			}
         ]]></method>
         
         <method id="onSelected" params="radioGroup, labeledButton"><![CDATA[
         	var container = radioGroup.first;
         	while (container) {
         		var aButton = container.behavior.getButton();
         		if (aButton === labeledButton)
         			aButton.behavior.setSelected(true, true);
         		else
         			aButton.behavior.setSelected(false, true);
         		container = container.next;
         	}
     		this.onRadioButtonSelected(labeledButton.behavior.name);
          ]]></method>  
          
		<method id="onTap" params="root, control"/>
		<method id="onUnselected" params="root, control"/>
   
        <method id="onRadioButtonSelected" params="buttonName"/>
	</behavior>
	
	<column id="RadioGroup" active="true" behavior="RadioGroupBehavior"/>

</module>
