﻿/* ------------------------------------------------------------------------------------------------------------ */

CheckList = function( id ) {
	if( !this.getEl( id ) ) return false;
	CheckList.superClass.apply( this, arguments );
	
	this.expanded = false;
	this.options = new Array();
	this.temp = false;

	this.element = this.getEl( id );

	this.list = this.element.getElementsByTagName( 'UL' )[ 0 ];
	this.length = this.list.getElementsByTagName( 'LI' )[ 'length' ];
	
	for( var i = this.element.childNodes.length; i; i-- ) {
		var child = this.element.childNodes[ i - 1 ];
		if( child.nodeName == 'SPAN' ) {
			this.setAllSwitcher( child );
			break;
		}
	}

	this.setClassName( 'checklist', true );
	this.addHandler( this.element, 'click', this.toggleExpand );
	this.addHandler( this.list, 'click', function( params, ev ) {
		this.stopPropagation( ev );
	});

	this.getStartOptions();
}
CheckList.inheritsFrom( Glyph );

/* ------------------------------------------------------------------------------------------------------------ */

CheckList.prototype.setAllSwitcher = function( child ) {
	this.setClassName( 'switcher', true, child );
    this.switcher_name = child.innerHTML;
	this.switcher = this.createNode( 'input', child, { 'type': 'checkbox' });
	this.switcher.checked = true;
	this.addHandler( this.switcher, 'click', this.toggleAll );
}

/* ------------------------------------------------------------------------------------------------------------ */

CheckList.prototype.toggleAll = function( params, ev ) {
	var flag = this.switcher.checked;
	for( var c in this.options ) {
		var option = this.options[ c ];
		if(
			option &&
			option[ 'element' ]
		) {
			option[ 'element' ].checked = flag;
		}
	}
	this.stopPropagation( ev );
	if( !this.expanded ) {
		this.notify( 'Changed' );
	}
}

/* ------------------------------------------------------------------------------------------------------------ */

CheckList.prototype.getOptions = function() {
	return this.options;
}

/* ------------------------------------------------------------------------------------------------------------ */

CheckList.prototype.getStartOptions = function() {
	var options = this.list.getElementsByTagName( 'INPUT' );
	for( var i = 0; i < options.length; i++ ) {
		var option = options[ i ];
		var label = option.parentNode.getElementsByTagName( 'LABEL' )[ 0 ];
		this.applyOption( option, label.innerHTML );
	}
}

/* ------------------------------------------------------------------------------------------------------------ */

CheckList.prototype.setOptions = function( data ) {
	this.removeOptions();
	for( var c in data ) {
		this.addOption( data[ c ] );
	}
}

/* ------------------------------------------------------------------------------------------------------------ */

CheckList.prototype.applyOption = function( option, label ) {
	if(
		option &&
		option[ 'id' ]
	) {
		this.options[ option[ 'id' ] ] = {
			'element': option,
			'data': {
				'id': option[ 'id' ],
				'name': option[ 'name' ],
				'value': option[ 'value' ],
				'label': label
			}
		}
		//this.addHandler( option, 'change', this.notify, 'Changed' );
	}
}

/* ------------------------------------------------------------------------------------------------------------ */

CheckList.prototype.addOption = function( data ) {
	var row = this.createNode( 'li', this.list );
	var option = this.createNode( 'input', row, {
		'type': 'checkbox',
		'id': data[ 'id' ] || '',
		'name': data[ 'name' ] || '',
		'value': data[ 'value' ] || ''
	});

	if( data[ 'selected' ] ) {
		option[ 'checked' ] = 'checked';
	}

	var label = data[ 'label' ] || data[ 'name' ]
	this.createNode( 'label', row, { 'for': data[ 'id' ] }, label );

	this.applyOption( option, label );
}

/* ------------------------------------------------------------------------------------------------------------ */

CheckList.prototype.removeOptions = function() {
	for( var c in this.options ) {
		var option = this.options[ c ];
		this.removeOption( option[ 'data' ][ 'id' ] );
	}
}

/* ------------------------------------------------------------------------------------------------------------ */

CheckList.prototype.removeOption = function( id ) {
	var element = this.options[ id ][ 'element' ];
	element.parentNode.parentNode.removeChild( element.parentNode );
	delete this.options[ id ];
}

/* ------------------------------------------------------------------------------------------------------------ */

CheckList.prototype.toggleExpand = function( params, ev ) {
	this.expanded = !this.expanded;
	this.setClassName( 'expand', this.expanded );

    var value = this.getValue();
	
	if( this.expanded ) {
		this.temp = this.getValue().join( ',' );

        if( value.length == 1 && value != this.switcher_name ) {
            this.switcher.parentNode.childNodes[0].nodeValue = this.switcher_name;
            this.switcher.checked = false;
        }

		this.addHandler( document, 'click', this.toggleExpand );
	} else {        
		if(
			this.temp !== false &&
			this.temp !== value.join( ',' )
		) {
			this.temp = false;
			this.notify( 'Changed' );
		}

        if( value.length == 1 ) {
            this.switcher.parentNode.childNodes[0].nodeValue = value[0];
            this.switcher.checked = true;
        }

		this.removeHandler( document, 'click', this.toggleExpand );
	}
	this.stopPropagation( ev );
	this.notify( 'Expanded', this.expanded );
}

/* ------------------------------------------------------------------------------------------------------------ */

CheckList.prototype.getValue = function() {
	var result = new Array();
	for( var c in this.options ) {
		var option = this.options[ c ];
		if( option[ 'element' ].checked ) {
			result[ result.length ] = option[ 'data' ][ 'value' ];
		}
	}
	return result;
}
