var Dealers = function() {
	this.title = true;
	this.logo = true;
	this.showSeparator = true;
	this.footer = true;
	this.languages = true;
	this.countries = true;

	this.html = [];
	this.data = null;
	this.interval = null;
	this.coords = {
		"10":{"x":180, "y":405}, "17":{"x":180, "y":245}, // Latvia
		"31":{"x":175, "y":260}, "41":{"x":295, "y":140}, "42":{"x":175, "y":400}, "43":{"x":305, "y":340}, // Belarus
		"12":{"x":60, "y":250}, "13":{"x":220, "y":420}, "14":{"x":210, "y":250}, // Estonia
		"15":{"x":250, "y":388}, "16":{"x":230, "y":260}, "18":{"x":123, "y":72}, "19":{"x":80, "y":225}, "37":{"x":125, "y":310}, // Lithuania
		"20":{"x":110, "y":255}, "23":{"x":150, "y":60}, "24":{"x":232, "y":340}, "26":{"x":130, "y":365},
			"27":{"x":170, "y":510}, "28":{"x":265, "y":310}, "29":{"x":160, "y":370}, "30":{"x":250, "y":350}, "39":{"x":115, "y":160}, "40":{"x":95, "y":440} // Ukraine
	};
}

Dealers.prototype.getData = function(loadModels) {
	var params = [];
	params.push('DBnum='+ Configurator.DBnum);
	params.push('lng='+Configurator.language);
	params.push('marketID='+Configurator.marketID);
	ajax('/en/daction/dealers/', 'Configurator.dealers.getDataHandler('+ Configurator.onlyData +', '+ loadModels +')', params.join('&'));
}

Dealers.prototype.getDataHandler = function(onlyData, loadModels) {
//	document.body.innerHTML = '<pre>'+ajaxReq.responseText;
//	return;
	try {
		this.data = eval('('+ ajaxReq.responseText +')');
	} catch(e) {
//		document.body.innerHTML = '<pre>'+ajaxReq.responseText;
//		return;
		alert('Can\'t load Dealer data.');
		throw e;
	}
	if (onlyData === false) this.ini();
	else if(isset(loadModels)) Configurator.models.ini();
}

Dealers.prototype.ini = function() {
	this.interval = null;
	this.html = [];
	this.html.push('<table align="center" class="content"><tr><td class="content_cell">');
	this.html.push(Interface.showStepButtons(true));

	var i = 0;
	if (Configurator.fromVDI === false || !Configurator.isDealer) {
		for (el in this.data) {
			if (!i) {
				this.html.push('<div class="country_place"><img src="/images/maps/map_'+ Configurator.market +'.gif" alt="'+ Configurator.marketName +'" />');
				this.html.push('<div class="country_name">'+ this.data[el]['country'] +'</div>');
			}
			if (this.coords[el]) {
				this.html.push('<div class="outlet_place" style="top: '+ this.coords[el].x +'px; left:  '+ this.coords[el].y +'px;"><table');
				this.html.push(' onmouseout="this.className=\'inactive_outlet_place\'; Configurator.dealers.interval = setInterval(\'Configurator.dealers.closePopup();\', 200);"');
				this.html.push(' onmouseover="this.className=\'active_outlet_place\'; clearInterval(Configurator.dealers.interval); Configurator.dealers.interval = null;">');
				this.html.push('<tr class="h20"><td onmouseover="Configurator.dealers.showPopup(\''+ this.data[el]['city'] +'\');" ><div class="inactive_outlet_place"');
				this.html.push((this.getDealerCount(this.data[el]['city']) < 2 ?
					' onmouseout="this.className=\'inactive_outlet_place\';" onmouseover="this.className=\'active_outlet_place\';" style="cursor: pointer;"'+
					' onclick="Configurator.dealers.set(\''+ el +'\'); Configurator.currentStep = \'models\'; Configurator.dealers.closePopup(); Configurator.ini();"' : ''));
				this.html.push('></div></td>');
				this.html.push('<td valign="middle"><nobr>'+ this.data[el]['city'] +'</nobr></td></tr></table></div>');
			}
			i++;
		}
		this.html.push('<div class="dealers_popup" id="dealers_popup"');
			this.html.push(' onmouseout="Configurator.dealers.interval = setInterval(\'Configurator.dealers.closePopup();\', 200);"');
			this.html.push(' onmouseover="clearInterval(Configurator.dealers.interval); Configurator.dealers.interval = null;">');
		this.html.push('</div>');
		this.html.push('</div>');
	} else {
		this.html.push('<br>'+ Interface.translate('dealer_information') +': '+ Configurator.dealers.getName() +' - ');
		this.html.push('<a href="" onclick="Configurator.currentStep=\'models\'; Configurator.ini(); return false;" class="href2">'+ Interface.translate('next_step') +'</a>');
	}
	//this.html.push('<div class="relative"><div class="country_name">'+ Configurator.marketName +'</div></div>');

	this.html.push('</td>');
	this.html.push(Interface.showRightMenu());
	this.html.push('</tr></table>');

	$('content').innerHTML = this.html.join('');
	Interface.loading(true);
	//alert(Interface.alert('gg', true));
}

Dealers.prototype.getDealerCount = function(city) {
	var co = 0;
	for (el2 in this.data) {
		if (this.data[el2]['city'] == city) {
			co++;
		}
	}
	return co;
}

Dealers.prototype.showPopup = function(city) {
	var o = $('dealers_popup');
	var html = [];
	var x = y = 0;
	html.push('<table>');
	for (el in this.data) {
		if (this.data[el]['city'] == city) {
			html.push('<tr class="matrix_sv_cell"');
			html.push(' onmouseover="this.style.color=\'#fff\'; this.style.backgroundColor=\'#6D7E8E\';"');
			html.push(' onmouseout="this.style.color=\'#000\'; this.style.backgroundColor=\'#fff\';"');
			html.push(' onclick="Configurator.dealers.set(\''+ el +'\');');
			html.push('Configurator.currentStep = \'models\'; Configurator.dealers.closePopup(); Configurator.ini();"');
			html.push('><td><nobr><b>&nbsp;&nbsp;'+ this.data[el]['name'] +'&nbsp;&nbsp;&nbsp;</b></nobr><br>');
			html.push('<nobr>&nbsp;&nbsp;&nbsp;&nbsp;'+this.data[el]['street'] +'&nbsp;&nbsp;</nobr></td></tr>');
			if (this.coords[el]) {
				x = this.coords[el].x;
				y = this.coords[el].y;
			}
		}
	}
	html.push('</table>');
	o.innerHTML = html.join('');
	o.style.visibility = 'hidden';
	o.style.display = 'block';


	o.style.left = (y + 25) +'px';
	o.style.top = (x - 11) +'px';
	o.style.visibility = 'visible';
}

Dealers.prototype.closePopup = function() {
	if (this.interval !== null && $('dealers_popup')) $('dealers_popup').style.display = 'none';
}

Dealers.prototype.set = function(id) {
	for (el in this.data) {
		if (el == id) {
			Configurator.outletID = el;
			Configurator.dealerID = this.data[el]['dealer_id'];
			break;
		}
	}
}

Dealers.prototype.getName = function() {
	for (el in this.data) {
		if (el == Configurator.outletID) return this.data[el]['name'];
	}
	return Interface.translate('select_dealer');
}

Dealers.prototype.getStreet = function() {
	for (el in this.data) {
		if (el == Configurator.outletID) return this.data[el]['street'];
	}
}

Dealers.prototype.getCity = function() {
	for (el in this.data) {
		if (el == Configurator.outletID) return this.data[el]['city'];
	}
}

Dealers.prototype.getCountry = function() {
	for (el in this.data) {
		if (el == Configurator.outletID) return this.data[el]['country'];
	}
}

Dealers.prototype.getPhone = function() {
	for (el in this.data) {
		if (el == Configurator.outletID) return this.data[el]['phone'];
	}
}

Dealers.prototype.getPostalCode = function() {
	for (el in this.data) {
		if (el == Configurator.outletID) return this.data[el]['postal_code'];
	}
}

Dealers.prototype.genSelect = function() {
	var html = [];
	html.push('<select width="100%" onchange="Configurator.dealers.set(this.value); Configurator.resetDealer = true; Configurator.ini();" id="dealers_list">');
	for (el in this.data) {
		html.push('<option value="'+ el +'"');
		html.push((Configurator.outletID == el ? ' selected="selected"' : ''));
		html.push('>'+ this.data[el]['name'] +'</option>');
	}
	html.push('</select>');
	return html.join('');
}

