

var Connected_Selectboxes = new Class({
	
	getOptions: function(){
		return {
			childSelectId: '',
			ajaxOptions: '',
			updater_controller: ''
		};
	},

	initialize: function(className, options){
		this.setOptions(this.getOptions(), options);
		
		
		if(this.options.useOverlay){
			this.overlay = new Overlay({container: this.options.container, onClick:this.close.bind(this), colour: this.options.overlayColor});
		}
		
		this.parentSelectbox = $$('.'+className);
		
		this.setParent();
		
	},
	
	setParent: function() {
		this.parentSelectbox.addEvent('change',this.setChild.bind(this));
	},
	
	setChild: function() {
		
		if ( $('corners_cont') )
			$('corners_cont').empty();
		
		if ( $('city-adder') )
			$('city-adder').style.display = '';
		
		var selected_opt = this.parentSelectbox.getSelected();
		selected_opt.each(function(el) {
			selected_id = el.getProperty('value');			  
		});
		
		this.selected_id = selected_id;
		
		if ( $(this.options.childSelectId) )
			this.updateChild();
			
	},
	
	updateChild: function() {		
		
		$(this.options.childSelectId).removeProperty('disabled');
		
		this.request = new Request.HTML($merge({
			'url': this.options.updater_controller,
			'update': $(this.options.childSelectId),
			'method': 'post'
		}, this.options.ajaxOptions));
		
		
		// esborrable els atributs de send per la versió standard
		this.request.send('parent_id='+ this.selected_id);
		
		if ( $('corners_cont') )
			this.setCorners.bind(this).delay(500);
			
	},
	
	setCorners: function() {
		$(this.options.childSelectId).addEvent('change',this.updateCorners.bind(this));
	},
	
	updateCorners: function() {
		
		var selected_opt = $(this.options.childSelectId).getSelected();
		selected_opt.each(function(el) {
			selected_id = el.getProperty('value');			  
		});
		
		this.selected_id = selected_id;
		
		if ( $('corners_cont') ) {
		
			this.request = new Request.HTML($merge({
				'url': '/public/vaho_gallery/corners',
				'update': $('corners_cont'),
				'method': 'post'
			}, this.options.ajaxOptions));
		
		}		
		
		// esborrable els atributs de send per la versió standard
		this.request.send('child_id='+ this.selected_id);
	}
	
});

Connected_Selectboxes.implement(new Options);
Connected_Selectboxes.implement(new Events);