﻿PHYSEARCH = {

	key: null,

	data: {
		row:       'search_proximity',
		addy:      'search_address',
		button:    'search_geocode',
		latitude:  'latitude',
		longitude: 'longitude',
		image:     'search_image',
		form:      'PHYSEARCH_form',
		clear:     'PHYSEARCH_clear',
		width:     null,
		height:    null,
		geocoder:  null
	},

	init: function () {
		this.resolve(this.data);

		this.data.geocoder = new PB.Geocode(
			this.data.latitude,
			this.data.longitude);

		this.data.row.style.display = '';

		this.data.width  = Dom.getStyle(this.data.image, 'width');
		this.data.height = Dom.getStyle(this.data.image, 'height');
		this.data.width  = parseInt(this.data.width,  10);
		this.data.height = parseInt(this.data.height, 10);

		this.data.geocoder.onChange.subscribe(this.doChange, this, true);
		Event.addListener(this.data.button, 'click', this.search, this, true);
		Event.addListener(this.data.form, 'reset', this.doReset, this, true);
		this.doChange();
	},

	resolve: function (o) {
		for (var x in o) {
			if (o.hasOwnProperty(x) && Lang.isString(o[x])) {
				o[x] = $(o[x]);
			}
		}
	},

	search: function () {
		var s = this.data.addy.value;
		s = Lang.trim(s);

		if (s.length) {
			this.data.geocoder.search(s);
		} else {
			this.data.geocoder.open();
		}
	},

	doChange: function () {
		var w = this.data.width,
		h     = this.data.height,
		src   = this.data.geocoder.staticURL(w, h, this.key),
		img   = this.data.image;

		if (src === null) {
			img.style.backgroundImage = 'none';
			img.style.display = 'none';
		} else {
			img.style.display = '';
			img.style.backgroundImage = "url('" + src + "')";
		}
	},

	doReset: function (e) {
		Event.stopEvent(e);
		$('familyName').value = '';
		$('specialty').selectedIndex = 0;
		$('condition').selectedIndex = 0;
		$('keyword').value = '';
		$('search_address').value = '';
		$('latitude').value = '';
		$('longitude').value = '';
		this.data.image.style.display = 'none';
		this.data.image.style.backgroundImage = 'none';
	}

};

