/*
// 2007-09-14
// Copyright (c) Art. Lebedev | http://www.artlebedev.ru/
// Author - Vladimir Tokmakov
*/

var XList = {

	Object: function( oParams ){
		this.aoChild = [];
		this.create( oParams );
		return this;
	}

	, Child: function( oBranch, oParent, oTop ){
		this.create( oBranch, oParent, oTop );
		return this;
	}

};

XList.Object.prototype.create = function( oParams ){
	var oThis = this;
	oThis.oParams = oParams;

	oThis.oSelected = new Map.Selected( oParams.eSelected );

//	var aeOptgroup = Common.Dom.getElementsByClassName( oParams.eList,  'optgroup' );
//	if( aeOptgroup ){
//	}else{
		var aeInput = oParams.eList.getElementsByTagName( 'input' );
//	}

	for( var i = 0 ; i < aeInput.length ; i++ ){
		new XList.Child( aeInput[i], oThis );
	}

	if( !oThis.oSelected.iCount ){
		Common.Class.add( oParams.eSelected.parentNode, 'empty' );
		Common.Class.add( oParams.eSelected.parentNode.parentNode, 'empty' );
	}
}

XList.Child.prototype.create = function( eInput, oParent, oTop ){
	var oThis = this;
	oThis.oParent = oParent;
	oThis.oTop = oTop ? oTop : oParent;
	oThis.eInput = eInput;
	oThis.oData = {
		sID: eInput.value
		, sName: eInput.nextSibling.innerHTML.replace( /<\/?label[^>]*>/g, '' )
		, oWidget: xForm.getWidgetById( eInput.id )
	};

       

	Common.Event.add( eInput, 'click', function( oEvent ){
		oThis.toggle();
	} );

         

	if( oThis.oTop.oParams.asSelected.indexOf( oThis.oData.sID ) >= 0 ){	
		
                oThis.toggle();
                oThis.toggle();
	}
}

XList.Child.prototype.pre_toggle = function(){
	this.oData.oWidget.uncheck();
	this.toggle();
}

XList.Child.prototype.toggle = function(){
	
	if( this.eInput.checked ){
		this.oTop.oSelected.append( this );
	}else{
		this.oTop.oSelected.remove( this );
	}
	if( !this.oTop.oSelected.iCount ){
		Common.Class.add( this.oTop.oParams.eSelected.parentNode, 'empty' );
		Common.Class.add( this.oTop.oParams.eSelected.parentNode.parentNode, 'empty' );
	}else{
		Common.Class.remove( this.oTop.oParams.eSelected.parentNode, 'empty' );
		Common.Class.remove( this.oTop.oParams.eSelected.parentNode.parentNode, 'empty' );
	}
}

