// 0 - A can't be with B
// 1 - A only with B
// 2 - A with one of B,C,D...
// 3 - B can't be without A

function cloneObj(obj) {
	if(obj == null || typeof(obj) != 'object') return obj;
    var temp = obj.constructor();
    for (var key in obj) temp[key] = cloneObj(obj[key]);
    return temp;
}

var Rules = {
	stack: [],

	searchForRules: function(id, checked) {
		if (checked === true) this.addOption(id);
		else {
			
			// options with upholstery
			for (el in Car.blockedOptionsData) {
				if (Car.blockedOptionsData[el].id == id && Car.blockedOptionsData[el].rule == 2) {
					this.showUpholsAlert(id, Car.upholsID);
					$(id).checked = true;					
					return;
				}
			}
			
			
			this.removeOption(id);
		}

		
		for (el in Configurator.options.data['rules']) {			
			if (el == id) {						
				this.addToStack(id, checked);				
				this.followStack();
				break;
			}
		}	
	},

	followStack: function() {		
		var stop = false;
		var data = [];
		var original_data = [];
		var checked = false;
		var id = null;
		
		for (var s_num = this.stack.length - 1; s_num >= 0; s_num--) {
			if (!this.stack[s_num]) continue;
			checked = this.stack[s_num]['checked'];
			id = this.stack[s_num]['id'];

			
			// getting rules data for this options
			for (my_el in Configurator.options.data['rules']) {				
				if (my_el == this.stack[s_num]['id']) {	
					data = Configurator.options.data['rules'][my_el];	
					original_data = cloneObj(data);			    
							
					//this.removeNotExistsOptions(data);
					break;
				}
			}			
			
			// every rule - one array element
			for (var n = data.length - 1; n >= 0; n--) {			
				// every rule have many options
				for (var i = data[n]['options'].length - 1; i >= 0; i--) {					
					stop = false;
					if (!this.optionExists(data[n]['options'][i])) {
						/*var temp = [];
						var temp_id = data[n]['options'][i];
						alert(data[n]['options']);
						for (var i = data[n]['options'].length - 1; i >= 0; i--) {
							if (data[n]['options'][i] != temp_id) temp.push(data[n]['options'][i]);
						}
						data[n]['options'] = temp;
						alert(data[n]['options']);
						i--;*/
						continue;
					}
					//alert(data[n]['options']);				
					// checking						
					switch(data[n]['rule']) {						
						case 0:					
							if (checked === true && this.isOptionSelected(data[n]['options'][i])) {
								// if old option is blocked - deselect new option
								// else if ok old option deselect if cancel new option deselect
								
								/*if (in_array(data[n]['options'][i], Car.blockedOptions)) {
									this.showAlert0(id, data[n]['options'][i]);
									this.addToStack(id, false);
									this.removeOption(id);
								} else*/ this.showRule0(id, data[n]['options'][i]);
								stop = true;
							}
							break;
						case 1:
							if (checked === true && !this.isOptionSelected(data[n]['options'][i])) {
								// if ok both options select, cancel - both options deselect
								this.showRule1(id, data[n]['options'][i]);
								stop = true;
							}							
							break;
						case 2:
							// if no needed options cchecked, uncheck clicked option							
							if (checked !== true) continue;
							var isChecked = false;
							
							
							
							//alert(original_options);
							// find checked options, if false neew to add last option
							for (var k = data[n]['options'].length - 1; k >= 0; k--) {
								if (this.isOptionSelected(data[n]['options'][k])) {
									isChecked = true;
									break;
								}
							}

							if (!isChecked && !this.haveStandartOptions(original_data[n]['options'])) {								
								this.showRule2(id, data[n]['options']);
								stop = true;
							}
							break;
						case 3:
							// if old option selected ask for deselect it, if ok - deselect							
							if (checked === false && this.isOptionSelected(data[n]['options'][i])) {
								this.showRule3(id, data[n]['options'][i]);
								stop = true;
							}
							break;
					}
					if (stop === true) return;
				}				
			}			
			// if stop is false remove from stack
			this.removeFromStack(id);
		}
	},

	addOption: function(id) {
		if (!this.optionExists(id)) return;

		Car.options.push({'id':id});
		if ($(id)) $(id).checked = true;
		Configurator.options.getPrice();
	},

	addToStack: function(id, checked) {		
		this.stack.push({'id':id, 'checked':checked});		
	},

	removeOption: function(id) {		
		var so = Car.options.length;
		var temp = [];
		for (var i = 0; i < so; i++) {
			if (Car.options[i]['id'] != id) temp.push(Car.options[i]);
		}
		$(id).checked = false;
		Car.options = temp;
		Configurator.options.getPrice();
	},

	removeFromStack: function (id) {
		var so = this.stack.length;
		var temp = [];
		for (var i = 0; i < so; i++) {
			if (id != this.stack[i]['id']) temp.push(this.stack[i]);
		}
		this.stack = temp;		
	},

	optionExists: function(id) {
		var so = 0;
		for (el in Configurator.options.data['options']) {
			so = Configurator.options.data['options'][el].length
			for(var i = 0; i < so; ++i) {
				if (Configurator.options.data['options'][el][i][1] == id) return true;
			}
		}
		return false;
	},
	
	isOptionSelected: function(id) {
		var so = Car.options.length;
		for (var i = 0; i < so; i++) {
			//if (id == '000057') alert('gg');
			if (Car.options[i]['id'] == id) return true;
		}
		return false;
	},

	showRule0: function(new_id, old_id) {		
		var html = [];
		html.push('\"('+ new_id +') '+ Interface.translate('option_'+ Car.modelID +'_'+ new_id) +'\"<br>'+ Interface.translate('cant_be') +'<br>');
		html.push('\"('+ old_id +') '+ Interface.translate('option_'+ Car.modelID +'_'+ old_id) +'\"<br><br>');
		html.push('<b>'+ Interface.translate('deselect') +' \"('+ old_id +') '+ Interface.translate('option_'+ Car.modelID +'_'+ old_id) +'\"?</b>');

		Interface.alert(html.join(''), true, new_id, old_id, 0);
	},

	showAlert0: function(new_id, old_id) {		
		var html = [];
		html.push('\"('+ new_id +') '+ Interface.translate('option_'+ Car.modelID +'_'+ new_id) +'\"<br>'+ Interface.translate('cant_be') +'<br>');
		html.push('\"('+ old_id +') '+ Interface.translate('option_'+ Car.modelID +'_'+ old_id) +'\"');

		Interface.alert(html.join(''), false, new_id, old_id);
	},
	
	showUpholsAlert: function(opt_id, uphols_id) {
		var html = [];
		html.push('\"('+ uphols_id +') '+ Interface.translate('uphols_'+ uphols_id) +'\"<br>'+ Interface.translate('can_be') +'<br>');
		html.push('\"('+ opt_id +') '+ Interface.translate('option_'+ Car.modelID +'_'+ opt_id) +'\"');

		Interface.alert(html.join(''), false, opt_id, uphols_id);
	},

	showRule1: function(new_id, old_id) {
		var html = [];
		html.push('\"('+new_id+') '+ Interface.translate('option_'+ Car.modelID +'_'+ new_id) +'\"<br>'+Interface.translate('can_be')+'<br>');
		html.push('\"('+ old_id +') '+ Interface.translate('option_'+ Car.modelID +'_'+ old_id) +'\"'+'<br><br>');
		html.push('<b>'+Interface.translate('select')+'?</b>');

		Interface.alert(html.join(''), true, new_id, old_id, 1);
	},

	showRule2: function(new_id, ids) {
		var html = [];
		html.push('\"('+new_id+') '+ Interface.translate('option_'+ Car.modelID +'_'+ new_id) +'\"<br>'+ Interface.translate('can_be_one') +'<br>');
		
		var so = ids.length;
		for (var i = 0; i < so; i++) html.push('\"('+ ids[i] +') '+ Interface.translate('option_'+ Car.modelID +'_'+ ids[i]) +'\"<br>');
		
		html.push('<br><br><b>\"('+ ids[(so - 1)] +') '+Interface.translate('option_'+ Car.modelID +'_'+ ids[(so - 1)]) + '\"');
		html.push(Interface.translate('will_be')+'.</b>');

		Interface.alert(html.join(''), true, new_id, ids[(so - 1)], 2);
	},

	showRule3: function(new_id, old_id) {
		var html = [];
		html.push('\"('+ old_id +') '+ Interface.translate('option_'+ Car.modelID +'_'+ old_id) +'\"<br>'+Interface.translate('can_be')+'<br>');
		html.push('\"('+ new_id +') '+ Interface.translate('option_'+ Car.modelID +'_'+ new_id) +'\"');
		html.push('<br><br><b>'+Interface.translate('deselect_both')+'?</b>');

		Interface.alert(html.join(''), true, new_id, old_id, 3);
	},

	removeNotExistsOptions: function(data) {
		var so = data.length;
		var so2 = null;
		var temp = [];		
		for (var i = 0; i < so; i++) {
			so2 = data[i]['options'].length;
			temp = [];
			for (var n = 0; n < so2; n++) {
				if (this.optionExists(data[i]['options'][n])) temp.push(data[i]['options'][n]);
			}
			
			data[i]['options'] = temp;
		}		
	},
		
	haveStandartOptions: function(val) {
		var so = val.length;
		for(var i = 0; i < so; i++) {
			if (in_array(val[i], Configurator.standartOptions.ids)) return true;
		}			
		return false;
	}
}




