var ResourceDir = {
	sizeX: $(window).getSize().x,
	sizeY: $(window).getSize().y,
	
	currentElem: null,
	currentFilters: {},
	currentRecords: {},
	
	locationFilter: Array(),
	serviceFilter: Array(),
	
	resourceFrame: null,
	filterFrame: null,
	ajaxFrame: null,
	ajaxMask: null,
	
	
	start: function(){
		var ajaxInputs = $$('textarea.resource-dir-ajax');
		
		if ( ajaxInputs.length ) {
			ajaxInputs.each(function(input, idx) {
				input.addEvent('click', function() {
					ResourceDir.showResourceFrame(this);
				});
			});
		}
	},
	
	
	doAjaxLookup: function() {
		var request = new Request.JSON({	'method': 'post',
											'url': '/CareCoordinators/f/ServiceDirectory/Ajax/',
											'data': {	'filter_resourcedir': '1',
														'filter': {		'search_term': $('search_term').value,
																		'service_type': $('service_type').options[$('service_type').selectedIndex].value,
																		'location': $('location').options[$('location').selectedIndex].value,
																		
																		'provides_transport': ($('provides_transport').checked) ? 1 : 0,
																		'provides_bulk_billing': ($('provides_bulk_billing').checked) ? 1 : 0,
																		'provides_no_gaps': ($('provides_no_gaps').checked) ? 1 : 0,
																		'provides_home_visits': ($('provides_home_visits').checked) ? 1 : 0,
																		
																		'accepts_medicare': ($('accepts_medicare').checked) ? 1 : 0,
																		'accepts_epc_referrals': ($('accepts_epc_referrals').checked) ? 1 : 0
															
																	}
													},
											'onRequest': function() {
												// Do something
											},
											'onComplete': function(response) {
												ResourceDir.currentRecords = response;
												//alert(response.length);
												ResourceDir.showResponseDetails();
											}
										}).send();
		
		
//		elem.set('value', )
	},
	
	
	showResourceFrame: function(elem) {
		if ( !$('resourceFrame') ) {
			this.createAjaxMask();
			this.createResourceFrame();
			this.createFilterFrame();
			this.createAjaxFrame();
		}
		
			
		this.resourceFrame.setStyle('left', ((this.sizeX - parseInt(this.resourceFrame.getStyle('width')) ) / 2) );
		this.resourceFrame.setStyle('top', ((this.sizeY - parseInt(this.resourceFrame.getStyle('height')) ) / 2) );
		
		this.resourceFrame.fx.start({'opacity': 1});
		this.ajaxMask.fx.start({'opacity': 0.9});
		
		ResourceDir.currentElem = elem;
	},
	
	
	hideResourceFrame: function() {
		this.resourceFrame.fx.start({'opacity': 0});
		this.ajaxMask.fx.start({'opacity': 0});
	},
	
	
	populateDetails: function(num) {
		this.hideResourceFrame();
		
		if ( this.currentElem ) {
			if ( this.currentRecords[num] ) {
				var concatValue = this.currentRecords[num].first_name + ' ' + this.currentRecords[num].surname;
				
				if ( this.currentRecords[num].company ) {
					concatValue += ' (' + this.currentRecords[num].company + ') ';
				}
				
				
				if ( this.currentRecords[num].address ) {
					concatValue += '\nAddress: ' + this.currentRecords[num].address;
					
					if ( this.currentRecords[num].suburb ) {
						concatValue += ', ' + this.currentRecords[num].suburb;
					}
					
					if ( this.currentRecords[num].city ) {
						concatValue += ', ' + this.currentRecords[num].city;
					}
				}
				
				
				if ( this.currentRecords[num].phone ) {
					concatValue += '\nPhone: ' + this.currentRecords[num].phone + ') ';
				}
				
				this.currentElem.value = concatValue;
			}
		}
	},
	
	
	createResourceFrame: function() {
		if ( $('resourceFrame') ) {
			this.resourceFrame = $('resourceFrame');
		} else {
			this.resourceFrame = new Element('div', {	'id':	'resourceFrame'	
											});
			this.resourceFrame.fx = new Fx.Morph(this.resourceFrame, {	'duration': 1000, 
																		'transition': Fx.Transitions.Quad.easeIn
																	});
			this.resourceFrame.setStyle('opacity', 0);
			
			$('level2').adopt(this.resourceFrame);
		}
	},
	
	
	createFilterFrame: function() {
		if ( $('filterFrame') ) {
			this.filterFrame = $('filterFrame');
		} else {
			this.filterFrame = new Element('div', {	'id':	'filterFrame'	
											});
											
			var heading = new Element('div', 	{	'class': 'resource-search-h4' 	});
			heading.adopt(new Element('h4', 	{	'text':	'Find Service Provider'	}));
											
			this.filterFrame.adopt(heading);

									
			// Text based search filter
			
			var tmpColRow = new Element('div', {	'class': 'form_column_row'	});
			var tmpCol = new Element('div', {		'class': 'form_column'	});
			
			var tmp = new Element('div', {			'class': 'form_row'	});
			var tmpLabel = new Element('div', {		'class': 'form_label',	'text': 'Search for: '	});
			var tmpField = new Element('div', {		'class': 'form_field input-normal'	});
			
			tmpField.adopt(new Element('input', {	'type': 'text',
													'name': 'search_term',
													'id': 'search_term'
												}));
			
			tmp.adopt(tmpLabel, tmpField);
			tmpCol.adopt(tmp);
												
			
															
											
											
											
			// Service Type filter
			var tmp = new Element('div', {			'class': 'form_row'	});
			var tmpLabel = new Element('div', {		'class': 'form_label',	'text': 'Service Type: '	});
			var tmpField = new Element('div', {		'class': 'form_field select-normal'	});
												
			var serviceSelect = new Element('select', {		'name': 'service_type',
															'id': 'service_type'
														});
															
			serviceSelect.adopt(new Element('option', {		'value': '',
															'text': ''
														}));
														
			if ( this.serviceFilter.length ) {
				this.serviceFilter.each(function(service, idx) {
					if ( service ) {
						serviceSelect.adopt(new Element('option', {		'value': service,
																		'text': service
																	}));
					}
				});
			}
			
			tmpField.adopt(serviceSelect);
			
			tmp.adopt(tmpLabel, tmpField);
			tmpCol.adopt(tmp);
			
			
			
										
			// Location filter
			var tmp = new Element('div', {			'class': 'form_row'	});
			var tmpLabel = new Element('div', {		'class': 'form_label',	'text': 'Location: '	});
			var tmpField = new Element('div', {		'class': 'form_field select-normal'	});
			
			var locationSelect = new Element('select', {	'name': 'location',
															'id': 'location'
														});
															
			locationSelect.adopt(new Element('option', {	'value': '',
															'text': ''
														}));
														
			if ( this.locationFilter.length ) {
				this.locationFilter.each(function(location, idx) {
					if ( location ) {
						locationSelect.adopt(new Element('option', {	'value': location,
																		'text': location
																	}));
					}
				});
			}
			
			tmpField.adopt(locationSelect);
			
			tmp.adopt(tmpLabel, tmpField);
			tmpCol.adopt(tmp);
			
			

			
			tmpColRow.adopt(tmpCol);
			var tmpCol = new Element('div', {		'class': 'form_column'	});
			
			
			
											
											
			// Provider 'Feature' Filters
			var tmp = new Element('div', {			'class': 'form_row'	});
			var tmpLabel = new Element('div', {		'class': 'form_label checkboxes-normal'	});
			var tmpField = new Element('div', {		'class': 'form_field checkboxes-normal'	});
			
			tmpLabel.adopt(new Element('input', {		'type': 'checkbox',
														'name': 'provides_transport',
														'id': 'provides_transport'
													}));
											
			tmpLabel.adopt(new Element('label', {		'for': 'provides_transport',
														'html': '<img src="/images/interface/icon_dir_transport.gif" alt="Transport Available" title="Transport Available" /> Provides Transport'
													}));
											
			tmpLabel.adopt(new Element('br'));
										
			tmpLabel.adopt(new Element('input', {		'type': 'checkbox',
														'name': 'provides_bulk_billing',
														'id': 'provides_bulk_billing'
													}));
											
			tmpLabel.adopt(new Element('label', {		'for': 'provides_bulk_billing',
														'html': '<img src="/images/interface/icon_dir_bulk.gif" alt="Bulk Billing Available" title="Bulk Billing Available" /> Provides Bulk Billing'
													}));
											
			tmpLabel.adopt(new Element('br'));
											
			tmpLabel.adopt(new Element('input', {		'type': 'checkbox',
														'name': 'provides_no_gaps',
														'id': 'provides_no_gaps'
													}));
											
			tmpLabel.adopt(new Element('label', {		'for': 'provides_no_gaps',
														'html': '<img src="/images/interface/icon_dir_gap.gif" alt="No Gap Charges" title="No Gap Charges" /> Provides No Gaps'
													}));
											
			tmpLabel.adopt(new Element('br'));
											
			tmpField.adopt(new Element('input', {		'type': 'checkbox',
														'name': 'provides_home_visits',
														'id': 'provides_home_visits'
													}));
											
			tmpField.adopt(new Element('label', {		'for': 'provides_home_visits',
														'html': '<img src="/images/interface/icon_dir_home.gif" alt="Home Visits Available" title="Home Visits Available" /> Provides Home Visits'
													}));
											
			tmpField.adopt(new Element('br'));
											
			tmpField.adopt(new Element('input', {		'type': 'checkbox',
														'name': 'accepts_medicare',
														'id': 'accepts_medicare'
													}));
											
			tmpField.adopt(new Element('label', {		'for': 'accepts_medicare',
														'html': '<img src="/images/interface/icon_dir_medicare.gif" alt="Accepts Medicare Plus" title="Accepts Medicare Plus" /> Accepts Medicare Plus'
													}));
											
			tmpField.adopt(new Element('br'));
											
			tmpField.adopt(new Element('input', {		'type': 'checkbox',
														'name': 'accepts_epc_referrals',
														'id': 'accepts_epc_referrals'
													}));
											
			tmpField.adopt(new Element('label', {		'for': 'accepts_epc_referrals',
														'html': '<img src="/images/interface/icon_dir_epc.gif" alt="Accepts EPC Referrals" title="Accepts EPC Referrals" /> Accepts EPC Referrals'
													}));
															
			tmp.adopt(tmpLabel, tmpField);
			tmpCol.adopt(tmp);
			tmpColRow.adopt(tmpCol);
			this.filterFrame.adopt(tmpColRow);
			
			
															
															
			// Submit button
			var filterButton = new Element('input', {			'type': 'button',
																'name': 'filterDirectory',
																'id': 'filterDirectory',
																'value': 'Search'
															});
			filterButton.addEvent('click', function() {
				ResourceDir.doAjaxLookup();
			});
			
			
			var closeButton = new Element('input', {			'type': 'button',
																'name': 'closeDirectory',
																'id': 'closeDirectory',
																'value': 'Close'
															});
			closeButton.addEvent('click', function() {
				ResourceDir.hideResourceFrame();
			});
			
															
			this.filterFrame.adopt(closeButton, filterButton);
											
			this.resourceFrame.adopt(this.filterFrame);
		}
	},
	
	
	createAjaxFrame: function() {
		if ( $('ajaxFrame') ) {
			this.ajaxFrame = $('ajaxFrame');
			this.ajaxFrame.empty();
		} else {
			this.ajaxFrame = new Element('div', {	'id': 'ajaxFrame'
														});
														
			this.resourceFrame.adopt(this.ajaxFrame);
		}
	},
	
	
	createAjaxMask: function() {
		if ( $('ajaxMask') ) {
			this.ajaxMask = $('ajaxMask');
			this.ajaxMask.empty();
		} else {
			this.ajaxMask = new Element('div', {	'id':	'ajaxMask'	});
			this.ajaxMask.fx = new Fx.Morph(this.ajaxMask, {	'duration': 1000, 
																'transition': Fx.Transitions.Quad.easeIn
															});
			this.ajaxMask.setStyle('opacity', 0);
			
			this.ajaxMask.addEvent('click', function() {	ResourceDir.hideResourceFrame();	});
			
			$('level2').adopt(this.ajaxMask);
		}
	},
	
	
	showResponseDetails: function() {
		var ajaxTable = new MooTable(this.ajaxFrame, {	'rowsPerPage': 100,
														'rowInactiveState': '.mooTable tr.rowInactive',
														'rowActiveState': '.mooTable tr.rowActive'
													});
		
		ajaxTable.addHeader('Provider', true, 'company');
		ajaxTable.addHeader('Service', true, 'service');
		ajaxTable.addHeader('Location', true, 'location');
				
		ajaxTable.addHeader('<img src="/images/interface/icon_dir_transport.gif" alt="Transport Available" title="Transport Available" />', true, 'transport');
		ajaxTable.addHeader('<img src="/images/interface/icon_dir_bulk.gif" alt="Bulk Billing Available" title="Bulk Billing Available" />', true, 'bulk_billing');
		ajaxTable.addHeader('<img src="/images/interface/icon_dir_home.gif" alt="Home Visits Available" title="Home Visits Available" />', true, 'home_visits');
		ajaxTable.addHeader('<img src="/images/interface/icon_dir_gap.gif" alt="No Gap Charges" title="No Gap Charges" />', true, 'no_gaps');
		
		ajaxTable.addHeader('<img src="/images/interface/icon_dir_medicare.gif" alt="Accepts Medicare Plus" title="Accepts Medicare Plus" />', true, 'accepts_medicare');
		ajaxTable.addHeader('<img src="/images/interface/icon_dir_epc.gif" alt="Accepts EPC Referrals" title="Accepts EPC Referrals" />', true, 'accepts_epc');

		if ( this.currentRecords ) {
			this.currentRecords.each(function(record, idx) {
				if ( idx < 100 ) {
					ajaxTable.addRow({
								'company':		record.first_name + ' ' + record.surname + ' (' + record.company + ')',
								'service':		record.provider_type,
								'location':		record.city,
								
								'transport':	(parseInt(record.provides_transport) == 1) ? '<img src="/images/interface/icon_dir_transport.gif" alt="Transport Available" title="Transport Available" />' : '<img src="/images/interface/icon_dir_transport.gif" alt="No Transport Provided" title="No Transport Provided" class="faded" />',
								'bulk_billing':	(parseInt(record.provides_bulk_billing) == 1) ? '<img src="/images/interface/icon_dir_bulk.gif" alt="Bulk Billing Available" title="Bulk Billing Available" />' : '<img src="/images/interface/icon_dir_bulk.gif" alt="No Bulk Billing" title="No Bulk Billing" class="faded" />',
								'home_visits':	(parseInt(record.provides_home_visits) == 1) ? '<img src="/images/interface/icon_dir_home.gif" alt="Home Visits Available" title="Home Visits Available" />' : '<img src="/images/interface/icon_dir_home.gif" alt="No Home Visits Provided" title="No Home Visits Provided" class="faded" />',
								'no_gaps':		(parseInt(record.provides_no_gaps) == 1) ? '<img src="/images/interface/icon_dir_gap.gif" alt="No Gap Charges" title="No Gap Charges" />' : '<img src="/images/interface/icon_dir_gap.gif" alt="Gap Charges" title="Gap Charges" class="faded" />',
								
								'accepts_medicare':		(parseInt(record.accepts_medicare) == 1) ? '<img src="/images/interface/icon_dir_medicare.gif" alt="Accepts Medicare Plus" title="Accepts Medicare Plus" />' : '<img src="/images/interface/icon_dir_medicare.gif" alt="No Medicare Plus" title="No Medicare Plus" class="faded" />',
								'accepts_epc':			(parseInt(record.accepts_epc_referrals) == 1) ? '<img src="/images/interface/icon_dir_epc.gif" alt="Accepts EPC Referrals" title="Accepts EPC Referrals" />' : '<img src="/images/interface/icon_dir_epc.gif" alt="No EPC Referrals" title="No EPC Referrals" class="faded" />'
							},
							{
								'click': function() {	ResourceDir.populateDetails(idx);	}
							}
						);
				}
			});
		}

		ajaxTable.render();
	},
	
	
	
	
	
	addServiceType: function(str) {
		this.serviceFilter.push(str);
		return true;
	},
	
	
	addLocation: function(str) {
		this.locationFilter.push(str);
		return true;
	}
	
};


// Do stuff on load
window.addEvent('load', ResourceDir.start);