/*
* ProductFinder 0.5
* 05-12-20 12:33
* Dirk Ginader für Bartenbach & Co. Agentur für Kommunikation AG & Co. KG
* www.bartenbach.de www.ginader.de
*/
ProductFinder = function(){
	this.lastFormElementID = null;
	this.rows = new Array();
	this.cols = new Array();
	this.formElements = new Array();
	this.productContainer = null;
	this.tooltip = null;
}
o = ProductFinder.prototype;
o.trace = function(msg){	
	if(window.traceWindow){
		window.traceWindow.trace(msg);
	}
}
o.flushTrace = function(){	
	if(window.traceWindow){
		window.traceWindow.flush();
	}
}
o.init = function(){
	//this.trace('init()<br />');
	// Set up the activity of all depending Formfields
	for (var id in this.formElements){		
		if(typeof this.formElements[id] != 'function'){// Bugfix for prototype-lib-compatibility
			this.toggleActive(this.formElements[id][0],false);
		}
	}
	// create all visual representations of the Products
	var markup = "";
	for(var i=0;i<this.rows.length;i++){
		markup += '<div id="product_'+i+'" title="'+this.rows[i][17]+'"><a href="'+this.rows[i][16]+'" target="_blank"><img src="'+this.rows[i][15]+'" /><p>'+this.rows[i][0]+'</p></a></div>';
	}
	this.productContainer.innerHTML = markup;
	for(var i=0;i<this.rows.length;i++){
		// get Objects of the created Divs
		obj = document.getElementById('product_'+i);
		if(!obj){
			this.trace('object ID:"'+id+'" not found<br />');
			return;
		}
		this.rows[i][this.rows[i].length] = obj;
		
		// change Links zu opener if available
		if(window.opener){
			var a = obj.getElementsByTagName("a")[0];
			a.page = a.href;
			a.href = "#";
			a.target = "";
			a.onclick = function(){
				window.opener.location.href = this.page;
				return false;
			}
		}
		
		// setup Tooltip if available
		if(this.tooltip){
			obj.tooltip = this.tooltip;
			obj.tooltiptext = obj.title
			obj.title = "";
			obj.onmouseover = function(){
				this.tooltip.show(this.tooltiptext);
			}
			obj.onmouseout = function(){
				this.tooltip.hide();
			}
		}
	}
	this.update();
}
o.addCols = function(){
	//this.trace('addCols()');
	this.cols = arguments;
}
o.addRow = function(){
	//this.trace('addRow()');
	this.rows[this.rows.length] = arguments;
}
o.addProductContainer = function(id){
	obj = document.getElementById(id);
	if(!obj){
		this.trace('object ID:"'+id+'" not found<br />');
		return;
	}
	this.productContainer = obj;
}
o.inspect = function(){
	this.trace('inspect()<br />');
	for(var i=0;i<this.rows.length;i++){
		this.trace("row: "+i+"<br>");
		for(var j=0;j<this.cols.length;j++){
			this.trace(this.cols[j]+": "+this.rows[i][j]+"<br />");
		}
		this.trace("<br>");
	}	
}
o.dumpObj = function(obj){
	
}
o.update = function(){
	//this.trace('update()<br />');
	this.getMatch();
}
// This Method cannot be abstraced - should move it to the instance
o.getMatch = function(){
	//this.trace('getMatch()<br />');
	// get Index of the Product Menu ==> Producttype
	var showOnlyProduct = this.formElements['types'][0][this.formElements['types'][0].selectedIndex].value;
	//this.trace('showOnlyProduct: '+showOnlyProduct+'<br />');
	// Hide extended Handles when they are not needed (not type 1)
	var extendedHandles = [						   
		document.getElementById('compset'),
		this.formElements['usepressureset'][0],
		document.getElementById('pressureset'),
		this.formElements['usevolumeset'][0],
		document.getElementById('volumeset'),
		this.formElements['usepowerset'][0],
		document.getElementById('powerset'),
		this.formElements['usespecialsset'][0],
		document.getElementById('specialsset'),
		this.formElements['usedrivemodeset'][0],
		document.getElementById('drivemodeset'),
		this.formElements['usespeedcontrolledset'][0],
		document.getElementById('speedcontrolledset')
	];	
	for(var id in extendedHandles){		
		if(typeof this.formElements[id] != 'function'){// Bugfix for prototype-lib-compatibility
			if(showOnlyProduct == 1){
				extendedHandles[id].style.visibility = "visible";
			}else{
				extendedHandles[id].style.visibility = "hidden";
				this.formElements['usepressureset'][0].checked = false;
				this.formElements['usevolumeset'][0].checked = false;
				this.formElements['usepowerset'][0].checked = false;
				this.formElements['usespecialsset'][0].checked = false;
				this.formElements['usedrivemodeset'][0].checked = false;
				this.formElements['usespeedcontrolledset'][0].checked = false;
			}
		}
	}
	// set up resultset
	var resultSet = new Array();
	//add matches to resultset
	for(var i=0;i<this.rows.length;i++){
		if(this.rows[i][1] == showOnlyProduct){			
			resultSet[resultSet.length] = this.rows[i];
		}
	}
	
	// check the comptypes
	var comptypes = this.formElements['comptypes'][0].options[this.formElements['comptypes'][0].selectedIndex].value;
	if(comptypes != 0){
		var tmp = new Array();
		for(var i in resultSet){
			if(resultSet[i][2] == comptypes){
				//this.trace('match '+i+': '+resultSet[i][2]+'<br />');
				tmp[tmp.length] = resultSet[i];
			}
		}
		resultSet = tmp;		
	}
	
	
	// find out what is used from the extended handles
	var usePressure = this.formElements['usepressureset'][0].checked;
	//this.trace('usePressure: '+usePressure+'<br />');
	var useVolume = this.formElements['usevolumeset'][0].checked;
	//this.trace('useVolume: '+useVolume+'<br />');
	var usePower = this.formElements['usepowerset'][0].checked;
	//this.trace('usePower: '+usePower+'<br />');
	var useSpecials = this.formElements['usespecialsset'][0].checked;
	//this.trace('useSpecials: '+useSpecials+'<br />');
	var useDrivemode = this.formElements['usedrivemodeset'][0].checked;
	//this.trace('useDrivemode: '+useDrivemode+'<br />');
	var useSpeedcontrolled = this.formElements['usespeedcontrolledset'][0].checked;
	
	// run the filters
	if(usePressure){
		//var pressure = this.formElements['usepressureset'][1].value;
		var pressure = Math.round(pressureSlider.values[0]);
		var pressureoutput = document.getElementById('pressureoutput');
		pressureoutput.innerHTML = pressure+' bar';
		//this.trace('pressure: '+pressure+'<br />');
		var tmp = new Array();
		for(var i in resultSet){
			if(pressure >= resultSet[i][3] && resultSet[i][4] >= pressure){
				//this.trace('match '+i+': '+resultSet[i][4]+'<br />');
				tmp[tmp.length] = resultSet[i];
			}
		}
		resultSet = tmp;
	}
	if(useVolume){
		//volumeSlider.sliderValue
		//this.trace(volumeSlider.values+'<br>');
		//var volumenmin = this.formElements['usevolumeset'][1].value;
		//var volumenmax = this.formElements['usevolumeset'][2].value;
		var volumenmin = Math.round(volumeSlider.values[0]);
		var volumenmax = Math.round(volumeSlider.values[1]);
		var volumeoutput = document.getElementById('volumeoutput');
		volumeoutput.innerHTML = volumenmin+'-'+volumenmax+' m3/min';
		//volumeoutput.innerHTML = volumenmin+'-'+volumenmax+' m3/min';
		this.flushTrace();
		var tmp = new Array();
		for(var i in resultSet){
			//if(resultSet[i][5] >= volumenmin && resultSet[i][6] <= volumenmax){
			if(volumenmin >= resultSet[i][5] && resultSet[i][6] >= volumenmax){
				this.trace(volumenmin+' >= '+resultSet[i][5]+' && '+resultSet[i][6]+' <= '+volumenmax+'<br />');
				tmp[tmp.length] = resultSet[i];
			}
		}
		resultSet = tmp;
	}
	if(usePower){
		//var power = this.formElements['usepowerset'][1].value;
		var power = Math.round(powerSlider.values[0]);
		var poweroutput = document.getElementById('poweroutput');
		poweroutput.innerHTML = power+' kW';
		//this.trace('power: '+power+'<br />');
		var tmp = new Array();
		this.flushTrace();
		for(var i=0;i<resultSet.length;i++){
			//if(resultSet[i][7] >= power){ //DG new column minpower!
			if(power >= resultSet[i][7] && resultSet[i][8] >= power){
				this.trace(power +" >= "+ resultSet[i][7] +" && "+ resultSet[i][8] +" >= "+ power+'<br />');
				tmp[tmp.length] = resultSet[i];
			}else{
				this.trace("<b>"+power +" >= "+ resultSet[i][7] +" && "+ resultSet[i][8] +" >= "+ power+'</b><br />');				
			}
		}
		resultSet = tmp;
	}
	if(useSpecials){
		var oiled = this.formElements['usespecialsset'][1].checked;
		var oilfree = this.formElements['usespecialsset'][2].checked;
		//this.trace('oiled: '+oiled+'<br />');		
		//this.trace('oilfree: '+oilfree+'<br />');
		if(oiled){
			var tmp = new Array();
			for(var i in resultSet){
				if(resultSet[i][9] == 1){
					//this.trace('match '+i+': '+resultSet[i][7]+'<br />');
					tmp[tmp.length] = resultSet[i];
				}
			}
			resultSet = tmp;			
		}else if(oilfree){
			var tmp = new Array();
			for(var i in resultSet){
				if(resultSet[i][10] == 1){
					//this.trace('match '+i+': '+resultSet[i][7]+'<br />');
					tmp[tmp.length] = resultSet[i];
				}
			}
			resultSet = tmp;			
		}	
	}
	if(useDrivemode){
		var belt = this.formElements['usedrivemodeset'][1].checked;
		var gear = this.formElements['usedrivemodeset'][2].checked;
		var direct = this.formElements['usedrivemodeset'][3].checked;
		//var drehzahl = this.formElements['usedrivemodeset'][4].checked;
		if(belt){
			var tmp = new Array();
			for(var i in resultSet){
				if(resultSet[i][11] == 1){
					//this.trace('match '+i+': '+resultSet[i][7]+'<br />');
					tmp[tmp.length] = resultSet[i];
				}
			}
			resultSet = tmp;		
		}else if(gear){
			var tmp = new Array();
			for(var i in resultSet){
				if(resultSet[i][12] == 1){
					//this.trace('match '+i+': '+resultSet[i][7]+'<br />');
					tmp[tmp.length] = resultSet[i];
				}
			}
			resultSet = tmp;		
		}else if(direct){
			var tmp = new Array();
			for(var i in resultSet){
				if(resultSet[i][13] == 1){
					//this.trace('match '+i+': '+resultSet[i][7]+'<br />');
					tmp[tmp.length] = resultSet[i];
				}
			}
			resultSet = tmp;		
		}
	}
	if(useSpeedcontrolled){
		var speedcontrolled = this.formElements['usespeedcontrolledset'][1].checked;
		var notspeedcontrolled = this.formElements['usespeedcontrolledset'][2].checked;
		var tmp = new Array();
		if(speedcontrolled){
			for(var i in resultSet){
				if(resultSet[i][14] == 1){
					//this.trace('match '+i+': '+resultSet[i][7]+'<br />');
					tmp[tmp.length] = resultSet[i];
				}
			}
			resultSet = tmp;
		}else if(notspeedcontrolled){
			for(var i in resultSet){
				if(resultSet[i][14] == 0){
					//this.trace('match '+i+': '+resultSet[i][7]+'<br />');
					tmp[tmp.length] = resultSet[i];
				}
			}
			resultSet = tmp;
		}
	}
	
	// Hide all Products
	for(var i=0;i<this.rows.length;i++){
		var container = this.rows[i][this.rows[i].length];
		container.className = "unmatched";
	}
	// Show Products that are in the resultset
	for(var i=0;i<resultSet.length;i++){
		var container = resultSet[i][resultSet[i].length];
		if(resultSet[i][1] == showOnlyProduct){
			container.className = "matched";
		}
	}
	this.trace('results: '+resultSet.length+'<br />');
	
}

o.toggleActive = function(obj,updateAfter){
	//this.trace('toggleActive('+obj.checked+')<br />');
	var d = this.formElements[obj.id];
	for (var i=1;i<d.length;i++){
		d[i].disabled = !obj.checked;
		if(!obj.checked){
			d[i].className = "disabled";
		}else{
			d[i].className = "";
		}
	}
	if(updateAfter){
		this.update();
	}
}
o.addMainFormElement = function(id){
	//this.trace('add:'+id+'<br>');
	obj = document.getElementById(id);
	if(!obj){
		this.trace('object ID:"'+id+'" not found<br />');
		return;
	}
	this.lastFormElementID = id;
	this.formElements[id] = new Array(obj);
	obj.ref = this;
	if(obj.className == "depending"){
		obj.onclick = function(){
			this.ref.toggleActive(this,true);
		}
	}else{
		obj.onchange = function(){
			this.ref.update();
		}
	}
}
o.addDependingFormElement = function(id){
	obj = document.getElementById(id);
	if(!obj){
		this.trace('object ID:"'+id+'" not found<br />');
		return;
	}
	this.formElements[this.lastFormElementID][this.formElements[this.lastFormElementID].length] = obj;
	obj.ref = this;
	obj.onclick = function(){
		this.ref.update();
	}
	obj.onchange = function(){
		this.ref.update();
	}
}
o.addDependingSlider = function(obj){
	//this.trace(obj+'<br>');
}
o.addTooltipInstance = function(tooltip){
	this.tooltip = tooltip;
}
