if (typeof jQuery != "function") {
	throw new Error("Dealer.js: You must load the jQuery library!");
}

function Dealer(config) {
	this.config = $.extend({}, Dealer.defaultConfig, config || {});
	this.distance = NaN; // stores the distance in meters to destination chosen by user
	this.marker = null; // stores the marker on the map associated with this dealer
	this.gLatLng = (this.config.gLatLng && this.config.gLatLng.constructor == Array) ? new GLatLng(this.config.gLatLng[0], this.config.gLatLng[1]) : null;
	this.index = Dealer.instances.length;
	this.warning = "";
	this.tries = -1;
	this.duplicateIndex = NaN;
	this.initialized = false;
}

Dealer.defaultConfig = {
	prefix: "",
	title: "",
	company: "",
	company2: "",
	firstname: "",
	lastname: "",
	street: "",
	zipcode: "",
	city: "",
	phone: "",
	cellphone: "",
	email: "",
	url: "",
	baseCountryCode: "",
	gLatLng: null, // array containing the coordinates used to instantiate the GLatLng class
	warning: "", // is set if Google is unable to locate the given address - is displayed to the user
	onNotFound: null,
	onFound: null,
	mapIcon: null // must be GIcon object
}

Dealer.instances = [];
Dealer.invalidAddress = [];
Dealer.duplicates = [];

Dealer.geocoder = new GClientGeocoder();

Dealer.newInstance = function(config) {
	
	var instance = new Dealer(config);
	Dealer.instances.push(instance);
	try
	{
		//inst.showDealer(event, Dealer.instances.length-1);
	}
	catch(err){Base.Trace(err);}
	return instance;
}

Dealer.initDealers = function() {
	for (var i = 0, l = Dealer.instances.length; i < l; i++) {
		Dealer.instances[i].init();
	}
}

Dealer.compare = function(dealer1, dealer2) {
	return dealer1.compareTo(dealer2);
}

Dealer.prototype.toString = function() {
	return "[object Dealer] " + this.config.company + ", " + this.getAddress();
}

Dealer.prototype.checkDuplicate = function() {
	// necessary with a try - catch block - stopReading may have been called (the instance array is reset/cleared)
	try {
		for (var i = 0; i < this.index; i++) { // this looping takes a long time
			var instance = Dealer.instances[i];
			if (this.getAddress() == instance.getAddress() || (this.gLatLng && this.gLatLng.equals(instance.gLatLng))) {
				this.duplicateIndex = i;
				Dealer.duplicates.push(this);
				break;
			}
		}
	}
	catch (ex) {
	}
}

Dealer.prototype.init = function() {
	if (!this.initialized) {
		
		if (!(this.gLatLng instanceof GLatLng)) {
			var q = this.getAddress();
			if (this.config.baseCountryCode) {
				q += ", " + this.config.baseCountryCode;
			}
			else if (Dealer.geocoder.getBaseCountryCode()) {
				q += ", " + Dealer.geocoder.getBaseCountryCode();
			}
			Dealer.geocoder.getLocations(q, GEvent.callback(this, this.callback));
		}
		this.initialized = true;
	}
}

Dealer.prototype.getAddress = function() {
	return this.config.street + ", " + this.config.zipcode + " " + this.config.city;
}

Dealer.prototype.getDisplayName = function() {
	return this.config.company;
}

Dealer.prototype.callback = function(response) {
	this.tries++;
	if (response.Status.code != 200) {
		this.warning = this.config.warning;
		if (!Dealer.invalidAddress.includes(this, true)) {
			Dealer.invalidAddress.push(this);
		}
		if (typeof this.config.onNotFound == "function") {
			this.config.onNotFound.apply(this, [response]);
		}
		return;
	}
	var place = response.Placemark[0];
	this.gLatLng = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
	this.checkDuplicate();
	if (typeof this.config.onFound == "function") {
		this.config.onFound.apply(this, [response]);
	}
}

Dealer.prototype.compareTo = function(dealer) {
	if (dealer) {
		if (this.distance < dealer.distance) {
			return -1;
		}
		if (this.distance > dealer.distance) {
			return 1;
		}
	}
	return 0;
}