var BDHelper = new Class({
	Implements: Options,
	region_selector:  false,
	sector_selector:  false,
	service_selector: false,
	request_object:   false,
	status_feedback:  false,
	options: {
		region_id:  false,
		sector_id:  false,
		service_id: false,
		status_id:  false,
		action_url: false,
		please_select_text: 'Please Select',
		loading_text: 'Working...',
		allowed_sectors : []
	},
    initialize:function(options){
		
		this.setOptions(options);
		// *** Check we have set our options ***
		
		if(typeof this.options.sector_id == 'undefined') {
			throw('Missing Sector ID in Options');
		}
		if(typeof this.options.service_id == 'undefined') {
			throw('Missing Service ID in Options');
		}
		if(typeof this.options.region_id == 'undefined') {
			throw('Missing Region ID in Options');
		}
		if(typeof this.options.action_url == 'undefined') {
			throw('Missing Action URL in Options');
		}
		
		// *** Get internal reference to our <selects> ***
		this.sector_selector = document.id(this.options.sector_id);
		this.service_selector = document.id(this.options.service_id);
		this.region_selector = document.id(this.options.region_id);
		this.status_feedback = document.id(this.options.status_id);
						
		if(typeof this.sector_selector == 'undefined') {
			throw('Missing Sector <select> id');
		}
		if(typeof this.service_selector == 'undefined') {
			throw('Missing Service <select> id');
		}
		if(typeof this.region_selector == 'undefined') {
			throw('Missing Region <select> id');
		}
		
		// *** Check we have the form ***
		this.form = this.region_selector.getParent('form');
		if(typeof this.form == 'undefined') {
			throw('Missing Search Form');
		}
		
		// *** Get a request object ***
		var that = this;
		this.request_object = new Request({
									url: this.options.action_url,
							  		method: 'post',
							  		onRequest:function() {
							  			that.showLoading();
							  		},
							  		onFailure:function() {
							  			that.hideLoading();
							  		},
							  		onComplete:function(response) {
							  			if(typeof response !== 'undefined') {
							  				response = JSON.decode(response);
							  				that.hydrate(response);
							  			}
							  			that.hideLoading();
							  		}
							  });
							  
		if(typeof this.request_object == 'undefined') {
			throw('Missing Request Object');
		}
		if(this.status_feedback) { this.status_feedback.set('html', this.options.loading_text); }										
    },
    // Remove all events
    detatch:function() {
    	this.sector_selector.removeEvents();
    	this.service_selector.removeEvents();
		this.region_selector.removeEvents();
    },
    // Attach all events
    attach:function() {
    	var that = this;

    	this.region_selector.addEvent('change', function(e){
    		that.search();
    	});
    	this.sector_selector.addEvent('change', function(e){
    		that.search();
    	});
    	return this;
    },
    // Get sectors
    search:function() {
    	var form_data = "";
    	if(this.form) {
    		form_data = this.form.toQueryString(); 
    		form_data += "&nocache=1";
    	} else {
    		form_data += "nocache=1";
    	}
    	this.request_object.send({data: form_data});
    	return this;
    },
    // Create the services <options> from response object
    hydrate:function(json_obj) {
		// Remove the <select> events so that our next actions don't trigger the callbacks.
    	this.detatch();
    	
		var region_options  = json_obj.regions; 
    	var sector_options  = json_obj.sectors;
    	var service_options  = json_obj.services;
    				    	
    	var that = this;
    				    	
    	if(this.region_selector) {
    		this.region_selector.empty();
    		var new_region_options = [];	
    		this.region_selector.adopt(new Option(this.options.please_select_text, ''));    	
	    	var ops_string = "<option value=''>Please select</option>";
	    	region_options.each(function(el, ind) {
	    		if(el.selected) {
	    			ops_string += "<option value='" + el.token + "' selected='selected'>" + el.title + "</option>";
	    		} else {
	    			ops_string += "<option value='" + el.token + "'>" + el.title + "</option>";
	    		}
	    	});
	    	this.region_selector.set('html', ops_string);
    	}

    	if(this.sector_selector) {
    		this.sector_selector.empty();
    		var new_sector_options = [];
    		this.sector_selector.adopt(new Option(this.options.please_select_text, ''));
    		var ops_string = "<option value=''>Please select</option>";
	    	sector_options.each(function(el, ind) {
    			if(this.options.allowed_sectors.contains(el.token.toInt())) {
		    		if(el.selected) {
		    			ops_string += "<option value='" + el.token + "' selected='selected'>" + el.title + "</option>";
		    		} else {
		    			ops_string += "<option value='" + el.token + "'>" + el.title + "</option>";
		    		}
	    		}
	    	});
	    	this.sector_selector.set('html', ops_string);
    	}

    	if(this.service_selector) {
    		this.service_selector.empty();
    		var new_service_options = [];
    		this.service_selector.adopt(new Option(this.options.please_select_text, ''));
    		var ops_string = "<option value=''>Please select</option>";
	    	service_options.each(function(el, ind) {
	    		if(el.selected) {
	    			ops_string += "<option value='" + el.token + "' selected='selected'>" + el.title + "</option>";
	    		} else {
	    			ops_string += "<option value='" + el.token + "'>" + el.title + "</option>";
	    		}
	    	});
	    	this.service_selector.set('html', ops_string);
    	}
    	// Re-attach the <select> events.
    	this.attach();
    	return this;
    },
    showLoading:function() {
    	if(!this.status_feedback) { return false; }
    	this.status_feedback.fade('show');
		return this;
    },
    hideLoading:function() {
    	if(!this.status_feedback) { return false; }
    	this.status_feedback.fade('out');
		return this;
	}
});