/*
	name			: ClassBehaviours, the javascript framework based on class-name parsing
	update			: 9.11.9
	author			: Maurice van Creij
	dependencies	: jquery.classbehaviours.js
	info			: http://www.classbehaviours.com/

    This file is part of jQuery.classBehaviours.

    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2008  Maurice van Creij

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.
*/

	// create the jQuery object if it doesn't already exist
	if(typeof(jQuery)=='undefined') jQuery = function(){};

	// create the root classbehaviours object if it doesn't already exist
	if(typeof(jQuery.classBehaviours)=='undefined') jQuery.classBehaviours = function(){};

	// create the handlers child object if it doesn't already exist
	if(typeof(jQuery.classBehaviours.handlers)=='undefined') jQuery.classBehaviours.handlers = function(){}

	// we don't want this running twice
	if(typeof(jQuery.classBehaviours.handlers.setFormValue)=='undefined'){
		// runs through a sequence of class names
	    jQuery.classBehaviours.handlers.setFormValue = {
	        // properties
	        name: 'setFormValue',
	        // methods
	        start: function (node) {
	            // set the click event for this link
	            node.onmousedown = this.clicked;
	            // TODO: synchronise the initial value
	        },
	        clicked: function (that) {
	            var node = (typeof (this.nodeName) == 'undefined') ? that : this;
	            var sfv = jQuery.classBehaviours.handlers.setFormValue;
	            // get the target node
	            targetNode = document.getElementById(node.href.split('#')[1]);
	            // transfer the value to the target node
	            if (targetNode.nodeName == 'INPUT') {
	                if (targetNode.type == 'text') {
	                    targetNode.value = objNode.innerHTML;
	                }
	                else if (targetNode.type == 'checkbox' || targetNode.type == 'radio') {
	                    targetNode.checked = true;
	                }
	            }
	            else if (targetNode.nodeName == 'SELECT') {
	                // find the correct option
	                selectedIndex = 0;
	                allOptions = targetNode.getElementsByTagName('OPTION');
	                for (var a = 0; a < allOptions.length; a++) {
	                    if (allOptions[a].innerHTML == node.innerHTML) selectedIndex = a;
	                }
	                // make it the selected option
	                targetNode.selectedIndex = selectedIndex;
	            }
	            else if (node.nodeName == 'TEXTAREA') {
	                targetNode.value = node.innerHTML;
	            }
	            // transfer the value to the display node
	            displayId = jQuery.classBehaviours.utilities.getClassParameter(node, 'id', null);
	            if (displayId != null) document.getElementById(displayId).innerHTML = '<strong>' + node.innerHTML + '<strong>';
	            // trigger a virtual event handler defined outside this object
	            if (document.getElementById(displayId).className.indexOf('postback') > -1)
	                __doPostBack(displayId, '');
	        }
	    }

		// add this addon to the jQuery object
		if(typeof(jQuery.fn)!='undefined'){
			// extend jQuery with this method
			jQuery.fn.setFormValue = function(){
				return this.each(
					function(){
						jQuery.classBehaviours.handlers.setFormValue.start(this);
					}
				);
			};
			// set the event handler for this jQuery method
			$(document).ready(
				function(){
					$(".setFormValue").setFormValue();
				}
			);
		}
	}


