addEvent('load', window, 'Configurator.ini()');
addEvent('resize', window, 'Interface.alignAlertArea()');
addEvent('scroll', window, 'Interface.alignAlertArea()');

var Car = {
	modeID: null,
	engineID: null,
	svID: null,
	sv: null,
	sv_category_id: null,
	gearboxID: null,
	colorID: null,
	colorData: null,
	upholsID: null,
	upholsData: null,
	rimsID: null,
	rims: null,
	rimsData: null,
	mc: null,
	options: [],
	blockedOptions: [],
	blockedOptionsData: [],
	customerID: null,
	customer_type: null
};

/*window.onbeforeunload = function() {
	//alert('1');
}*/
var Pictures = [];

var Configurator = {
	isDealer: true,
	fromVDI: false,
	currentStep: 'pdf',
	//steps: ['dealers', 'models', 'matrix', 'standartOptions', 'interior', 'options', 'accessories', 'summary'],
	//stepsTitles: ['dealers', 'menu_model', 'matrix', 'options', 'menu_c_u_w', 'optional_equipment', 'accessories', 'menu_final_step'],
	steps: ['pdf', 'dealers', 'models', 'matrix', 'standartOptions', 'interior', 'options', 'summary'],
	stepsTitles: ['none', 'dealers', 'menu_model', 'matrix', 'equipment', 'menu_c_u_w', 'options', 'menu_final_step'],
	hash: {'pdf':'#pdf', 'dealers':'#dealers', 'models':'#models', 'matrix':'#matrix', 'standartOptions':'#standart_options', 'interior':'#interior', 'options':'#options', 'summary':'#summary'},
	dealers: null,
	models: null,
	matrix: null,
	standartOptions: null,
	interior: null,
	options: null,
	accessories: null,
	summary: null,
	pdf: null,

	language: null,
	marketID: null,
	market: null,
	regionID: null,
	dealerID: null,
	outletID: null,
	DBnum: null,
	onlyData: false,
	resetDealer: false,
	lastLocationHash: null,
	interval: null,
	completeOrder: false,

	ini: function() {
		Pictures = new ImagePreloader('Pictures');
		Pictures.setOnload(Interface.turnOffLoading);

		$('content').innerHTML = '';
		Interface.loading();

		window.location.hash = this.lastLocationHash = this.hash[this.currentStep];
		if (this.interval === null) this.interval = setInterval('Configurator.checkLocationHash()', 200);

		if (mGLOBALS['db_update'] == '1') Configurator.currentStep = 'pdf';

		switch (Configurator.currentStep) {
			case 'pdf':
				if (this.fromVDI === false) this.outletID = this.delaerID = null;
				this.dealers = this.matrix = this.standartOptions = this.options = this.interior = this.summary = null;
				this.resetCar();

				if (this.pdf === null) this.pdf = new PdfActions();
				Interface.loadInterface(this.pdf);
				this.pdf.getData();

				break;
			case 'dealers':
			case 'pdf':
				if (this.fromVDI === false) this.outletID = this.delaerID = null;
				this.dealers = this.matrix = this.standartOptions = this.options = this.interior = this.summary = null;
				this.resetCar();
				this.dealers = new Dealers();
				Interface.loadInterface(this.dealers);
				this.dealers.getData();
				break;
			case 'models':
				this.matrix = this.standartOptions = this.options = this.interior = this.summary = null;
				this.resetCar();

				this.onlyData = true;
				if (this.dealers === null) {
					this.dealers = new Dealers();
					this.dealers.getData(true);
				}
				this.onlyData = false;

				this.models = new Models();
				Interface.loadInterface(this.models);

				//if (this.fromVDI === false)
				this.models.ini();


				break;
			case 'matrix':
				this.standartOptions = this.options = this.interior = this.summary = null;

				this.onlyData = true;
				if (this.dealers === null) {
					this.dealers = new Dealers();
					this.dealers.getData();
				}
				if (this.models === null) this.models = new Models();
				this.onlyData = false;

				this.resetCar();
				if (this.matrix === null) this.matrix = new Matrix();
				Interface.loadInterface(this.matrix);
				this.matrix.getData();
				break;
			case 'standartOptions':
				//if(Car.modelID == '275' && Car.engineID == '51') Car.mc = 23;
				//else Car.mc = 10;
				if (this.standartOptions === null) this.standartOptions = new StandartOptions();
				Interface.loadInterface(this.standartOptions);
				this.standartOptions.getData();
				break;
			case 'interior':
				if (this.interior === null) this.interior = new Interior();
				Interface.loadInterface(this.interior);

				this.onlyData = true;
				if (this.dealers === null) {
					this.dealers = new Dealers();
					this.dealers.getData();
				}
				if (this.models === null) this.models = new Models();
				if (this.matrix === null) {
					this.matrix = new Matrix();
					this.matrix.getData();
				}
				if (this.standartOptions === null) {
					this.standartOptions = new StandartOptions();
					this.standartOptions.getData();
				}
				if (this.options === null && Configurator.completeOrder === true) {
					this.options = new Options();
					this.options.getData();
				}
				this.onlyData = false;

				this.interior.getData();
				break;
			case 'options':
				if (this.options === null) {
					Price.options = 0;
					this.options = new Options();
				}
				Interface.loadInterface(this.options);

				this.onlyData = true;
				if (this.dealers === null) {
					this.dealers = new Dealers();
					this.dealers.getData();
				}
				if (this.models === null) this.models = new Models();
				if (this.matrix === null) {
					this.matrix = new Matrix();
					this.matrix.getData();
				}
				if (this.standartOptions === null) {
					this.standartOptions = new StandartOptions();
					this.standartOptions.getData();
				}
				if (this.interior === null) {
					this.interior = new Interior();
					this.interior.getData();
				}
				this.onlyData = false;

				this.options.getData();
				break;
			case 'accessories':
				if (this.accessories === null) this.accessories = new Accessories();
				Interface.loadInterface(this.accessories);

				this.onlyData = true;
				if (this.dealers === null) {
					this.dealers = new Dealers();
					this.dealers.getData();
				}
				if (this.models === null) this.models = new Models();
				if (this.matrix === null) {
					this.matrix = new Matrix();
					this.matrix.getData();
				}
				if (this.standartOptions === null) {
					this.standartOptions = new StandartOptions();
					this.standartOptions.getData();
				}
				if (this.interior === null) {
					this.interior = new Interior();
					this.interior.getData();
				}
				this.onlyData = false;

				this.accessories.getData();
				break;
			case 'summary':
				this.summary = new Summary();
				Interface.loadInterface(this.summary);

				if (this.resetDealer) {
					this.onlyData = true;

					/*this.matrix = new Matrix();
					this.matrix.getData();

					this.standartOptions = new StandartOptions();
					this.standartOptions.getData();

					this.interior = new Interior();
					this.interior.getData();*/

					this.onlyData = false;
				}
				this.resetDealer = false;

				this.summary.ini();
				break;
		}
	},

	checkLocationHash: function() {
		if (Configurator.currentStep && this.lastLocationHash !== null && window.location.hash != this.lastLocationHash) {
			var hash = window.location.hash.split('#');
			Configurator.currentStep = hash[1];
			Configurator.ini();
		}
	},

	resetCar: function() {
		Car.options = [];
		Car.blockedOptions = [];
		Car.blockedOptionsData = [];
		Price.color = Price.rims = Price.uphols = Price.MSRP = Price.local_delivery = Price.other = null;
		Interface.flashCache = false;
		$('flashCache').style.display = 'none';
	},

	putCar: function() {
		if (!window.opener) return;
		var options = [];
		var temp = [];
		var so = Car.options.length;
		var so2 = 0;
		for (var i = 0; i < so; i++) temp.push(Car.options[i].id);

		for (el in Configurator.options.ad_options) {
			so2 = Configurator.options.ad_options[el].length;
			for (i = 0; i < so2; i++) {
				if (in_array(Configurator.options.ad_options[el][i][1], temp)) options.push({'id':Configurator.options.ad_options[el][i][1], 'category_id':el});
			}
		}

		//options.push({'id':Car.rimsID, 'category_id':12});
		gearboxData = Configurator.matrix.getGearboxData();
		engineData = Configurator.matrix.getEngineData();


		var configuration = {
			"carMask":{
				"typ":{
					"id": Car.modelID,
					"text": Configurator.models.getModelById()
				},
				"en": {
					"id": Car.engineID,
					"emblem": engineData.emblem,
					"cyl": engineData.cyl,
					"fuel_id": engineData.fuel_id,
					"hp": engineData.hp,
					"nm": engineData.nm,
					"disp": engineData.disp,
					"code": engineData.code
				},
				"sv": {
					"id": Car.svID,
					"text": Car.sv,
					"category_id": Car.sv_category_id
				},
				"b": {"id":"0"},
				"g": {
					"id": Car.gearboxID,
					"type": gearboxData.type,
					"drive": gearboxData.drive,
					"gears": gearboxData.gears
				},
				"s": {"id":"1"},
				"mc": {"id":Car.mc}
			},
			"col": {
				"id": Car.colorID,
				"text": Car.colorData.text
			},
			"uph": {
				"id": Car.upholsID,
				"text": Car.upholsData.text
			},
			"wheels": {
				"id": Car.rimsID,
				"category_id":12
			},
			"options": options,
			"pricelist_id": Configurator.DBnum/*,
			"accessories": window.opener.ot.getDocument().getCarConfiguration().getAccessories(),
			"local_accessories": window.opener.ot.getDocument().getCarConfiguration().getLocalAccessories()*/
		};
		//return;
		if (!window.opener || !window.opener.ot) {
			window.close();
			return;
		}
		window.opener.ot.external_outlet_id = this.outletID;
		window.opener.ot.getDocument().setCarConfiguration(configuration, true);
		window.opener.ot.refreshConfiguration();
		window.close();
	}
};

function getXML() {
	if (ajaxReq.responseText == '') return false;
	try {
		var rs = xml2array(ajaxReq.responseXML.lastChild);
	} catch (e) {
		document.body.innerHTML = ajaxReq.responseText;
		return;
	}
	return rs;
}

function debug(v, clear) {
	if (isset(clear)) $('debug').innerHTML = v;
	else $('debug').innerHTML += v+'<br>';
}


