var NXC = NXC || {};
NXC.RelatedLists = new Class( {
	Implements: [Options, Events],

	options: {
		'parentOptionsSelector': 'li',
		'selectedOptionClass'  : 'selected-option',
		'inactiveOptionClass'  : 'inactive-option',
		'getChildsURL'         : 'index.php/getchildes/',
		'childOptionTag'       : 'li',
		'loaderClass'          : 'loading',
		'selectParentMessage'  : '- Select parent -',
		'childListsToEmpty'    : [],
		'childViewAllLink'     : false
	},
	
    initialize: function( parentListID, childListID, options ) {
    	this.setOptions( options );    	
		
		this.parentList  = document.id( parentListID );
		this.childList   = document.id( childListID );
		
		this.viewAllLink = document.id( this.options.childViewAllLink );
		if( this.viewAllLink ) {
			this.viewAllLink.setStyle( 'visibility', 'hidden' );
		}

		this.loader = ( this.childList.get( 'id' ) != 'navigation-base-profile-view' ) ? this.childList.getParent() : this.childList;
    },

	install: function() {
    	this.parentOptions = this.parentList.getChildren( this.options.parentOptionsSelector );

    	this.installEvents();
	},

	installEvents: function() {
		this.parentOptions.each( function( option ) {
			option.addEvent( 'click', function( e ) {
				e.stop();

				if( option.hasClass( this.options.selectedOptionClass ) || option.hasClass( this.options.inactiveOptionClass ) ) {
					return false;
				}

				this.selectOption( option );
	
				this.showLoader();

				this.emptyChild( false );
				this.emptyChildLists();
				
				if( this.childList.get( 'id' ) != 'navigation-base-profile-view' ) {
					new Request.JSON( {
						'url': this.options.getChildsURL + '/' + option.retrieve( 'node_id' ),
						onSuccess: function( childNodes ) {
							this.hideLoader();

							if( childNodes.length == 0 ) {
								this.addOption( '- No availible objects -', 0 );
								if( this.viewAllLink ) {
									this.viewAllLink.setStyle( 'visibility', 'hidden' );
								}
							} else {
								if( this.viewAllLink ) {
									this.viewAllLink.setStyle( 'visibility', 'visible' );
								}
								childNodes.each( function( el ) {
									this.addOption( el.name, el.value );
								}.bind( this ) );	
							}
							
							this.childList.fireEvent( 'updateRelatedList' );
						}.bind( this )
					} ).send();	
				} else {
					new Request.HTML( {
						'url': this.options.getChildsURL + '/' + option.retrieve( 'node_id' ),
						update: this.childList,
						onSuccess: function( responseTree, responseElements, responseHTML, responseJavaScript ) {
							this.hideLoader();
						}.bind( this )
					} ).send();
				}
			}.bind( this ) );
		}.bind( this ) );
	},

	emptyChildLists: function() {
		this.options.childListsToEmpty.each( function( el ) {
			el.emptyChild( true );
		} );
	},
	
	emptyChild: function( setDefaultValue ) {
		this.childList.empty();
		
		if( setDefaultValue == true ) {
			this.addOption( this.options.selectParentMessage, 0 );
			if( this.viewAllLink ) {
				this.viewAllLink.setStyle( 'visibility', 'hidden' );
			}
		}
	},

	selectOption: function( option ) {
		this.parentOptions.removeClass( this.options.selectedOptionClass );
		option.addClass( this.options.selectedOptionClass );
	},
	
	addOption: function( html, value ) {
    	var option = new Element( this.options.childOptionTag, {
    		'html': html
    	} ).inject( this.childList );
    	
    	if( value == 0 ) {
    		option.addClass( this.options.inactiveOptionClass );
    	} else {
    		option.store( 'node_id', value );
    	}
	},
	
	showLoader: function() {
		this.loader.addClass( this.options.loaderClass );
	},

	hideLoader: function() {
		this.loader.removeClass( this.options.loaderClass );
	}
} );
