/**
 * 二级联动JS类
 * @author feifengxlq
 * @since 2008-7-10
 * 使用例子
 <script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="relationSelect.js"></script>
<script type="text/javascript">
var rs = new relationSelect($('#p')[0],$('#c')[0]);
rs.add(new selectNode('1','父节点1',0));
rs.add(new selectNode('5','子节点11',1));
rs.add(new selectNode('6','子节点12',1));
rs.add(new selectNode('7','子节点13',1));

rs.add(new selectNode('2','父节点2',0));
rs.add(new selectNode('8','子节点21',2))
rs.add(new selectNode('9','子节点22',2))
rs.add(new selectNode('10','子节点23',2))

rs.add(new selectNode('3','父节点3',0));
rs.add(new selectNode('11','子节点31',3))
rs.add(new selectNode('12','子节点32',3))
rs.add(new selectNode('13','子节点33',3))

rs.add(new selectNode('4','父节点4',0));
rs.add(new selectNode('14','子节点41',4))
rs.add(new selectNode('15','子节点42',4))
rs.add(new selectNode('16','子节点43',4))

rs.addPre(new selectNode('','请选择'))
rs.addPre(new selectNode('','请选择'),true)

rs.init(15);
$('#p').change(function(){
	rs.change();
})

</script>
 *
 *
 *
 *
 *
*/
function selectNode(_id,_name,_pid)
{
	this.id = _id;
	this.name = _name;
	this.pid = _pid;
}
function relationSelect(_pselectObj,_cselectObj)
{
	this.nodes = [];
	
	this.pselectObj = _pselectObj;
	
	this.cselectObj = _cselectObj;
	
	this.pValue = '';
	
	this.cValue = '';
	
	this.pPreNode;
	
	this.cPreNode;
	
	this.add = function(_node)
	{
		this.nodes.push(_node);
	}
	
	this.addPre = function(_node,_isP)
	{
		if(_isP)
		{
			this.pPreNode = _node;
		}else
		{
			this.cPreNode = _node;
		}
	}
	
	this.init = function(_cValue,_pValue,_pp)
	{
		if(_cValue)
		{
			this.cValue = _cValue;
			//程序获取父节点ID
			this.pValue = this._getPValueByCvalue(_cValue);
		}else{
			this.cValue = '';
			this.cValue = '';
		}
		
		if(!_pp)_pp=0;
		this._initP(_pp);
		this._initC();
	}
	this._getPValueByCvalue = function(_cValue)
	{
		var _nodesNum = this.nodes.length;
		if(_nodesNum>0)
		{
			for(var _i=0;_i<_nodesNum;_i++)
			{				
				if( (this.nodes[_i].id == parseInt(_cValue)) )
				{
					return this.nodes[_i].pid;
				}
			}
		}
		return '';
	}
	/**
	 * _pp 表示第一层的父类ID
	*/
	this._initP = function(_pp)
	{
		if(this.pPreNode)
		{
			this._getNodes2SelectByPid(this.pselectObj.options,_pp,this.pValue,this.pPreNode.name,this.pPreNode.id);
		}else{
			this._getNodes2SelectByPid(this.pselectObj.options,_pp,this.pValue);
		}
	}
	this.change = function()
	{
		this._initC();
	}
	this._initC = function()
	{
		if(this.cPreNode)
		{
			this._getNodes2SelectByPid(this.cselectObj.options,this.pselectObj.options[this.pselectObj.selectedIndex].value,this.cValue,this.cPreNode.name,this.cPreNode.id);
		}else{
			this._getNodes2SelectByPid(this.cselectObj.options,this.pselectObj.options[this.pselectObj.selectedIndex].value,this.cValue);
		}
	}
	
	this._getNodes2SelectByPid = function(_myOptions,_pid,_nowId,_defaultName,_defaultId)
	{
		//初始化，清空
		_myOptions = this._clearOptions(_myOptions);
		var _nodesNum = this.nodes.length;
		if(_defaultName)
		{
			if(!_defaultId) _defaultId = '';
			_myOptions.length++;
			_myOptions[0].text = _defaultName;
			_myOptions[0].value = _defaultId;			
		}			
		if(_nodesNum>0)
		{
			for(var _i=0;_i<_nodesNum;_i++)
			{				
				if( (this.nodes[_i].pid === parseInt(_pid)) )
				{
					_myOptions.length++;
					_myOptions[_myOptions.length-1].text = this.nodes[_i].name;
					_myOptions[_myOptions.length-1].value = this.nodes[_i].id;					
					if( this.nodes[_i].id == _nowId )
					{
						_myOptions[_myOptions.length-1].selected = 'selected';
					}					
				}
			}
		}
		return _myOptions;
	}
	
	this._clearOptions = function (_myOptions)
	{
		_myOptions.length = 0;
		return _myOptions;
	}
}
