﻿CLNSEARCH = {

	key: null,

	data: {
		row:       'search_proximity2',
		addy:      'search_address2',
		button:    'search_geocode2',
		latitude:  'latitude2',
		longitude: 'longitude2',
		image:     'search_image2',
		form:      'practiceForm',
		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);
		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 + "')";
		}
	}

};

Event.onDOMReady(CLNSEARCH.init, CLNSEARCH, true);

