/**************************************************************
** INTERFACE
**************************************************************/
var search_interface = new Class({
	initialize: function(wrapper, headers) {
		$(wrapper).style.position = 'relative';
		var im = $(wrapper).getFirst();
		while(im = im.getNext())
			if(im.hasClass('boxBottom'))
				im.src += '&greycenter=1';
		
		var h1 = new Element('div').injectInside(wrapper).addClass('boxhead').setStyles({'background': 'url("/lib/img.boxheader.php?width=960&height=30&b=5&bg=FBFBFB&fg=e4e4e4&top=true")', 'height': '30px', 'line-height': '30px', 'position': 'relative', 'top': '-3px'});
		for(var i=0;i<headers.length;i++)
			new Element('img').inject(new Element('div').injectInside(h1).appendText(headers[i]).addClass('numb3rs_'+i), 'top').set('src', '/src/img/symbol/'+(i+1)+'.gif');

		this.leftSide = new Element('div').injectInside(wrapper).addClass('leftSide');
		this.rightSide = new Element('div').injectInside(wrapper).addClass('rightSide');
		this.centerSide = new Element('div').injectInside(wrapper).addClass('centerSide');
		
		this.leftElements = [];
		this.centerElements = [];
		this.rightElements = [];
		
		new Element('br').set({'clear': 'both'}).injectInside(wrapper);
	},
	addLeft: function(o) {
		o.injectInside(this.leftSide);
		this.leftElements.push(o);
	},
	addCenter: function(o) {
		o.injectInside(this.centerSide);
		this.centerElements.push(o);
	},
	addRight: function(o) {
		o.injectInside(this.rightSide);
		this.rightElements.push(o);
	}
});
/**************************************************************
** PREVIEW
**************************************************************/
var search_preview = new Class({
	initialize: function(titel, form) {
		form.addEvent('update', this.update.bind(this));
		/*this.element = new Element('div').set({'class': 'grouper'});
		new Element('h2').set({'class': 'bar_wc'}).appendText(titel).injectInside(this.element);
		new Element('table').set({'class': 'topten'}).injectInside(this.element);
		new Element('tr').injectInside(this.element.getLast());
		new Element('th').set({rowspan: 2}).injectInside(this.element.getLast().getLast());
		new Element('img').set({src: '/img/showcase/icon_top10.jpg'}).injectInside(this.element.getLast().getLast().getLast());
		this.products = [];
		for(var i=0; i<5; i++)
			this.products.push(new Element('a').set({'class': 'productThumb'}).injectInside(new Element('td').injectInside(this.element.getLast().getLast())));
		new Element('tr').injectInside(this.element.getLast());
		for(var i=0; i<5; i++)
			this.products.push(new Element('a').set({'class': 'productThumb'}).injectInside(new Element('td').injectInside(this.element.getLast().getLast())));
		$each(this.products, function(v){
			new Element('img').set({src: '/lib/img/productThumb.php?id=0'}).injectInside(v);
		});
		form.addEvent('update', this.update.bind(this));
		new Element('div').set({'class': 'end'}).injectInside(this.element);
		new Element('img').set({src: '/img/shadow/shadow_inv.gif'}).injectInside(this.element.getLast());*/
	},
	injectInside: function(o) {
		/*this.element.injectInside(o);*/
		return this;
	},
	update: function(num, div) {
		$('left').empty().innerHTML = div;
	}
	/*update: function(num, ids, links) {
		for(var i=0; i<10; i++) {
			if(ids.length > 0) {
				var id = ids.pop();
				var link = links.pop();
				this.products[i].removeEvents('mouseover');
				this.products[i].removeEvents('mouseout');
				this.products[i].addEvent('mouseover', function() {PreviewIn(this);}.bind(id));
				this.products[i].addEvent('mouseout', function() {PreviewOut();});
				this.products[i].href = link;
				this.products[i].getFirst().src = '/lib/img/productThumb.php?id='+id;
			} else {
				this.products[i].removeEvents('mouseover');
				this.products[i].removeEvents('mouseout');
				this.products[i].href = "";
				this.products[i].getFirst().src = '/lib/img/productThumb.php?id=0';
			}
		}
	}*/
});
var search_controls = new Class({
	initialize: function(titel, form, texte) {
		this.element = new Element('div').set({'class': 'grouper', style: 'position: relative;'});
		new Element('a').set({'class': 'reset_btn'}).appendText(texte[1]).injectInside(this.element).addEvent('click', function(){
			$each(this.searchElements, function(v){
				v.reset();
			});
			this.doUpdate();
		}.bind(form));
		new Element('h2').addClass('nomargin').appendText(titel).injectInside(this.element);
		new Element('img').set({src: '/src/img/lupe/search.gif'}).injectInside(this.element);
		this.count = new Element('div').set({'class': 'search_count'}).injectInside(this.element).appendText('0');
		/*form.injectInside(this.element).form.set({'class': 'search_controlform'});
		new Element('a').set({'class': 'reset_btn'}).appendText(texte[1]).injectInside(form.form).addEvent('click', function(){
			$each(this.searchElements, function(v){
				v.reset();
			});
			this.doUpdate();
		}.bind(form));*/
		/*new Element('a').set({'class': 'submit_btn'}).appendText(texte[0]).injectInside(form.form).addEvent('click', function(){
			var sort = this.form.getLast();
			var first;
			while(first = this.form.getFirst())
				first.dispose();
			$each(this.searchElements, function(v){
				var e=v.postVal();
				$each(e, function(i){i.injectInside(this.form);}, this);
			}, this);
			if(this.form.getChildren().length == 0)
				new Element('input').set({type: 'hidden', name: 'current_search', value: 'empty'}).injectInside(this.form);
			new Element('input').set({type: 'hidden', name: 'site', value: 1}).injectInside(this.form);
			sort.injectInside(this.form);
			this.form.submit();
		}.bind(form));*/
		if(texte[3]) {
			new Element('div').set({'class': 'order_btn'}).appendText(texte[2]).injectInside(form.form);
			var sel = new Element('select').set({name: 'sortBy'}).injectInside(form.form.getLast());
			$each(texte[3], function(v,k){
				new Element('option', {value: k}).appendText(v).injectInside(this);
			}, sel);
			sel.addEvent('change', form.doUpdate.bind(form));
		}
		form.addEvent('update', this.update.bind(this));
	},
	injectInside: function(o) {
		this.element.injectInside(o);
		return this;
	},
	/*update: function(num, ids, links) {*/
	update: function(num, ids) {
		this.count.innerHTML = num;
	}
});
var search_summary = new Class({
	initialize: function(title, form) {
		this.element = new Element('div').set({'class': 'grouper', style: 'position: relative;'});
		new Element('h2').appendText(title).injectInside(this.element);
		new Element('div').set({style: 'height: 100px; width: 298px; overflow-y: scroll;'}).injectInside(this.element).addEvent('mousedown', function(){if(this.tween){window.clearInterval(this.tween);this.tween=false;}}.bind(this));
		this.table = new Element('table').set({'class': 'summary'}).injectInside(this.element.getLast());
		this.form = form;
		this.form.addEvent('update', this.update.bind(this));
		this.elems = {};
		/*new Element('div').set({'class': 'end'}).injectInside(this.element);
		new Element('img').set({src: '/img/shadow/shadow_inv.gif'}).injectInside(this.element.getLast());*/
	},
	injectInside: function(o) {
		this.element.injectInside(o);
		return this;
	},
	/*update: function(num, ids, links) {*/
	update: function(num, ids) {
		$each(this.form.searchElements, function(v){
			if(!this.elems[v.id]) {
				this.elems[v.id] = new Element('tr').injectInside(this.table).set({style: 'cursor: pointer;'}).addEvent('click', v.opt.activate.bind(v.opt));
				v.opt.addEvent('activate', function(){if(search_summary.actOpt) search_summary.actOpt.style.color = ''; search_summary.actOpt = this; this.style.color = '#B90624';}.bind(this.elems[v.id]));
				if(!this.elems[v.id].previousSibling) v.opt.activate();
				new Element('th').appendText(v.name).injectInside(this.elems[v.id]);
				new Element('td').injectInside(this.elems[v.id]);
			}
			var t = v.humanReadable();
			if(!t)
				this.elems[v.id].injectInside(this.table).style.display = 'none';
			else {
				this.elems[v.id].getLast().innerHTML = t;
				this.elems[v.id].style.display = '';
				/*alert(v.opt.headLine.getChildren().length);
				if(v.opt.headLine.getChildren().length == 0)
					new Element('img').injectInside(v.opt.headLine).set({src: '/img/search/check.png', style: 'float: right;'});*/
			}
			if(!this.tween)
				this.tween = window.setInterval(this.doTween.bind(this), 20);
		}, this);
	},
	doTween: function() {
		if(!search_summary.actOpt) return true;
		var e = search_summary.actOpt;
		if(e.style.display == 'none') return true;
		var p = e.parentNode.parentNode;
		var s = p.getScroll().y;
		var y = e.getPosition(p).y;
		if(y < 0)		p.scrollTo(0, s-2);
		var eh = e.offsetHeight;
		var ph = p.offsetHeight;
		if(eh < ph)
		if(y+eh > ph)	p.scrollTo(0, s+2);
	}
});
search_summary.actOpt = false;
/**************************************************************
** FORM
**************************************************************/
var search_form = new Class({
	Implements: Events,
	initialize: function(formName) {
		this.updateRequest = new Request({url: '/lib/ajax.search.php'}).addEvent('success', this.update.bind(this));
		this.form = new Element('form').set({'method': 'post'});
		this.searchElements = [];
	},
	setPg: function(pg) {
		this.pg = pg;
		return this;
	},
	update: function(r,t) {
		var e = r.split('|');
        /*this.fireEvent('update', [e[0], e[1].split(','), e[2].split(',')]);*/
        this.fireEvent('update', [e[0], e[1]]);
    },
    injectInside: function(e) {
    	this.form.injectInside(e);
    	return this;
    },
    sendUpdate: function(str) {
    	if(str) str = 'pg='+this.pg+'&ds='+str.replace(/\+/g, "%2B").replace(/\-/g, "%2D");
    	else str = 'pg='+this.pg;
    	
    	var hiddens = $('ds_hiddens').getChildren()[0].getChildren('input');
    	hiddens.each(function(v){
    		str += '&'+v.name+'='+v.value;
    	});
    	
    	this.updateRequest.send(str);
    },
    doUpdate: function() {
    	var str = '';
    	$each(this.searchElements, function(v){str += v.sql();});
    	var sel = this.form.getLast();
    	if(sel != null)
    		sel = sel.getLast();
    	/*if(sel != null)
    		str += ' ORDER BY '+sel.value;*/
    	this.sendUpdate(str);
    },
    addInteraction: function(e) {
    	this.searchElements.push(e);
    },
    set: function(fid, f, v) {
    	for(var i=0;i<this.searchElements.length;i++)
    		if(this.searchElements[i].id == fid) break;
    	this.searchElements[i].set(f,v);
    },
    sortBy: function(name) {
		var form = this.form.getLast();
		if(form != null) form = form.getLast();
		if(form != null) {
			var kids = form.getChildren();
			$each(kids, function(v){
				if(v.value == name) v.selected=true;
			});
		}
    }
});
search_form.defaultForm = false;
search_form.registeredForms = {};
search_form.get = function(name) {
	if(!name && search_form.defaultForm)
		return search_form.get(search_form.defaultForm);
	if(!name) {
		do {
			name = String.fromCharCode(randomChar(),randomChar(),randomChar(),randomChar(),randomChar(),randomChar(),randomChar(),randomChar(),randomChar(),randomChar());
		} while(search_form.registeredForms[name]);
		search_form.defaultForm = name;
		return search_form.get(name);
	}
	if(!search_form.registeredForms[name])
		search_form.registeredForms[name] = new search_form(name);
	return search_form.registeredForms[name];
};
/**************************************************************
** OPTIONS
**************************************************************/
var search_option = new Class({
	Implements: Events,
	initialize: function(name) {
		this.marketstrip = false;
		if(name)
			this.name = new Element('h2').appendText(name).set({'class': 'bar_rl'});
		this.marketstripLoader = new Request({url: '/lib/ajax.glossary.php'}).addEvent('success', this.receiveMarketstrip.bind(this));
	},
	activate: function(c) {
		if(this.group) return this.group.activate(this);
		var first;
		while(first = search_option.interaction.getFirst())
			first.dispose();
		while(first = search_option.marketstrip.getFirst())
			first.dispose();
		this.name.injectInside(search_option.interaction);
		this.interaction.injectInside(search_option.interaction);
		if(this.marketstripOptions)
			this.loadMarketstrip(c);
		this.fireEvent('activate', {target: this});
	},
	loadMarketstrip: function(c) {
		if(c && c.loadMarketstrip) return c.loadMarketstrip();
		if(this.marketstrip !== false) {
			var first;
			while(first = search_option.marketstrip.getFirst())
				first.dispose();
			/*this.marketstrip.injectInside(search_option.marketstrip);*/
			search_option.marketstrip.innerHTML = this.marketstrip;
			if(this.marketstrip=='')	search_option.marketstrip.getPrevious().setStyle('display', 'none');
			else						search_option.marketstrip.getPrevious().setStyle('display', '');
		} else
			this.marketstripLoader.send(this.marketstripOptions);
	},
	receiveMarketstrip: function(r,t) {
		/*this.marketstrip = new Element('div');
		this.marketstrip.innerHTML = r;*/
		if(r == '') this.marketstrip = '';
		else this.marketstrip = r.substr(0, r.indexOf('</h2>')+5)+'<div>'+r.substr(r.indexOf('</h2>')+5)+'</div>';
		this.loadMarketstrip();
	}
});
search_option.interaction = new Element('div').set({'class': 'interaction_content'});
search_option.marketstrip = new Element('div').set({'class': 'marketstrip_content'});
var search_option_price = new Class({
	Extends: search_option,
	initialize: function(name, o, form) {
		o.name_hidden = name;
		this.parent(name);
		this.interaction = new search_interaction_slider(o, this, form).cont;
		this.marketstripOptions = 'type=price&pg='+o.pg;
	}
});
var search_option_brand = new Class({
	Extends: search_option,
	initialize: function(name, o, form) {
		o.name_hidden = name;
		this.parent(name);
		this.interaction = new search_interaction_chkbox(o, this, form).cont;
		this.marketstripOptions = 'type=brand&pg='+o.pg;
	}
});
var search_option_feature = new Class({
	Extends: search_option,
	initialize: function(name, o, form) {
		this.parent(name);
		o.name = name;
		if(o.type == 'chkbox')
			this.interaction = new search_interaction_chkbox(o, this, form).cont.addEvent('mousedown', this.loadMarketstrip.bind(this));
		else if(o.type == 'slider')
			this.interaction = new search_interaction_slider(o, this, form).cont.addEvent('mousedown', this.loadMarketstrip.bind(this));
		else
			alert('not supportet: '+o.type);
		this.marketstripOptions = 'type=feature&id='+o.id;
	}
});
var search_option_featuregroup = new Class({
	Extends: search_option,
	initialize: function(name, features) {
		this.parent(name);
		this.interaction = new Element('div');
		$each(features, function(v){
			if(!this.marketstripOptions)
				this.marketstripOptions = v.marketstripOptions;
			v.group = this;
			v.interaction.injectInside(this.interaction);
		}, this);
	}
});
var search_option_package = new Class({
	initialize: function(title, desc, s_options) {
		this.cont = new Element('div').set({'class': 'grouper closed', style: 'overflow: hidden; height: 16px;'});
		this.headLine = new Element('h2').set({'class': 'klappe'}).appendText((s_options)?title:title.name.innerHTML).injectInside(this.cont);
		if(desc)
			new Element('p').injectInside(this.cont).set({style: 'margin: 20px;', html: desc});
		if(s_options) {
			$each(s_options, function(v){
				v.headLine = new Element('h2').set({'class': 'option'}).appendText(html_entity_decode(v.name.innerHTML)).injectInside(this.cont).set({style: 'cursor: pointer;'}).addEvent('click', v.activate.bind(v));
				v.pack = this;
				v.addEvent('activate', search_option_package.setOption.bind(v));
			}, this);
			var f = s_options.shift();
			this.headLine.set({style: 'cursor: pointer;'}).addEvent('click', f.activate.bind(f));
		} else {
			this.cont.getFirst().set({style: 'cursor: pointer;'}).addEvent('click', title.activate.bind(title));
			title.addEvent('activate', search_option_package.setOption.bind(this));
		}
	},
	open: function() {
		if(!this.maxHeight) {
			this.maxHeight = 0;
			$each(this.cont.getChildren(), function(v){ this.maxHeight += v.offsetHeight; }, this);
		}
		this.dir = 5;
		this.startTween();
	},
	startTween: function() {
		this.cont.style.height = this.cont.offsetHeight+'px';
		if(this.tween) {
			window.clearInterval(this.tween);
			this.tween = false;
		}
		this.tween = window.setInterval(this.doTween.bind(this), 10);
	},
	doTween: function() {
		this.cont.style.height = this.cont.offsetHeight + this.dir+'px';
		if(this.cont.offsetHeight > this.maxHeight) this.cont.style.height = this.maxHeight+'px';
		if(this.cont.offsetHeight < 16) this.cont.style.height = '16px';
		if(this.cont.offsetHeight >= this.maxHeight || this.cont.offsetHeight <= 16) {
			window.clearInterval(this.tween);
			this.tween = false;
		}
	},
	close: function() {
		this.dir = -5;
		this.startTween();		
	},
	injectInside: function(e) {
		this.cont.injectInside(e);
		return this;
	}
});
search_option_package.actOption = null;
search_option_package.setOption = function(e, p) {
	if(search_option_package.actOption != null) {
		search_option_package.actOption.headLine.removeClass('act');
		if(search_option_package.actOption.pack) {
			search_option_package.actOption.pack.cont.removeClass('opened').addClass('closed');
			if(!this.pack || search_option_package.actOption.pack != this.pack)
				search_option_package.actOption.pack.close();
		}
	}
	search_option_package.actOption = this;
	search_option_package.actOption.headLine.addClass('act');
	if(search_option_package.actOption.pack) {
		search_option_package.actOption.pack.cont.removeClass('closed').addClass('opened');
		search_option_package.actOption.pack.open();
	}
};
/**************************************************************
** INTERACTION
**************************************************************/
var search_interaction_chkbox = new Class({
	initialize: function(o, opt, form) {
		var i=0;
		this.id = o.id;
		this.opt = opt;
		this.form = form;
		this.form.addInteraction(this);
		this.name = (o.name)?o.name:o.name_hidden;

		this.allString = o.allString;
		this.showAllways = o.showAllways;
		this.translations = o.elems;
		this.elems = {};
		this.anz = 0;
		this.selected = 0;
		
		this.cont = new Element('ul');
		$each(o.elems, function(v,k){
			this.elems[k] = false;
			new Element('li').injectInside(this.cont).setStyle('background-image', 'url(/lib/img.chkbox.php?feature='+((o.id=='p.brand')?'brand'+o.pg:o.id)+'&value='+k.replace(/ /g, '%20')+')');
			new Element('input').set({type: 'checkbox', name: o.id, value: k, id: o.id+'_'+k}).injectInside(this.cont.getLast()).addEvent('change', this.click.bind(this));
			new Element('label').set({'for': o.id+'_'+k}).injectInside(this.cont.getLast()).appendText(" "+v);
			this.anz++;
		}, this);
		this.cont = this.cont.injectInside(new Element('div').set({'class': 'container'})).parentNode;
		if(o.desc)
			new Element('p').set({html: o.desc}).inject(this.cont, 'top');
	},
	click: function(e) {
		var t = e.target;
		if(!this.elems[t.value]) this.selected++;
		this.elems[t.value] = t.checked;
		if(!t.checked) this.selected--;
		this.form.doUpdate();
	},
	sql: function() {
		var text = [];
		$each(this.elems, function(v,k){
			if(v) text.push(this.id+"='"+k.replace(/,/g, "###comma###")+"'");
		}, this);
		if(text.length == 0) return '';
		return ' AND ('+text.toString().replace(/,/g, ' OR ').replace(/###comma###/g, ",")+')';
	},
	humanReadable: function() {
		var text = [];
		$each(this.elems, function(v,k){
			if(v) text.push(this.translations[k]);
		}, this);
		if(text.length == 0) return (this.showAllways)?this.allString:false;
		if(this.allString && text.length == this.anz) return this.allString;
		return text.toString().replace(/,/g, ', ');
	},
	postVal: function() {
		var out = [];
		$each(this.elems, function(v,k){
			if(v) {
				if(this.id=='ref_brand_id')
					out.push(new Element('input').set({type: 'hidden', name: 'current_search[br.name]['+this.translations[k]+']', value: "true"}));
				else
					out.push(new Element('input').set({type: 'hidden', name: 'current_search['+this.id+']['+k+']', value: "true"}));
			}
		}, this);
		return out;
	},
	reset: function() {
		$each(this.elems, function(v,k,o){o[k]=false;});
		$each(this.cont.getElements('input'), function(v){v.checked=false});
	},
	set: function(f, v) {
		this.elems[f] = v;
		$each(this.cont.getElements('input'), function(e){if(e.id == this.id+"_"+f) e.checked=v;}, this);
	}
});
var search_interaction_slider = new Class({
	movingThing: null,
	initialize: function(o, opt, form) {
		var i=0;
		this.id = o.id;
		this.name = (o.name)?o.name:o.name_hidden;
		this.opt = opt;
		this.form = form;
		this.form.addInteraction(this);

		this.allString = o.allString;
		this.showAllways = o.showAllways;
		this.translations = o.elems;
		this.evalation = o.round;
		this.decimals = o.dec;
		this.ranges = [];
		this.anz = 0;
		$each(o.elems, function(v,k){
			this.anz++;
			this.ranges.push(k);
		}, this);
		this.bounds = [this.ranges[0], this.ranges[this.ranges.length-1]];
		this.vMin = this.bounds[0];
		this.vMax = this.bounds[1];
		
		this.moveStart = this.moveStart.bind(this);
		this.moveDo = this.moveDo.bind(this);
		this.moveEnd = this.moveEnd.bind(this);
		if(o.snap) this.click = function(min,max) {
			if(isNaN(min)) min = 0;
			if(isNaN(max)) max = this.ranges.length-1;
			this.vMin = parseFloat(this.ranges[Math.round(min)]);
			this.vMax = parseFloat(this.ranges[Math.round(max)]);

			this.min.style.left = Math.round(min)*(this.back.offsetWidth-this.min.offsetWidth)/(this.anz-1)+'px';
			this.max.style.left = Math.round(max)*(this.back.offsetWidth-this.max.offsetWidth)/(this.anz-1)+'px';
			this.bar.style.left = this.min.offsetLeft+this.min.offsetWidth/2+'px';
			this.bar.style.width = this.max.offsetLeft-this.min.offsetLeft+'px';
		};
		else this.click = function(min,max) {
			if(isNaN(min) || min < 0) min = 0;
			if(isNaN(max) || max > this.ranges.length-1) max = this.ranges.length-1;
			this.vMin = parseFloat(this.ranges[Math.floor(min)])+parseFloat(this.ranges[Math.ceil(min)]-this.ranges[Math.floor(min)])*parseFloat(min-Math.floor(min));
			this.vMax = parseFloat(this.ranges[Math.floor(max)])+parseFloat(this.ranges[Math.ceil(max)]-this.ranges[Math.floor(max)])*parseFloat(max-Math.floor(max));
		}
		
		this.sizes = [300, 21, 1];
		this.cont = new Element('div').set({'class': 'container'});
		if(o.desc)
			new Element('p').set({html: o.desc}).injectInside(this.cont);
		this.back = new Element('div').set({style: 'position: relative; background: url(/lib/img.slider.php?feature='+((o.id.substr(0,4)=='best')?'preis'+o.pg+'||'+o.labels+'||'+this.ranges:o.id)+'&width='+this.sizes[0]+') center center no-repeat; height: 100px;'}).injectInside(this.cont).addEvent('mousedown', this.moveStart);
		this.bar = new Element('img').set({src: '/src/img/bar/slider_g.gif', style: 'position: absolute; top: 70px; left: 10px; height: 9px; width: '+this.sizes[0]+'px;'}).injectInside(this.back).addEvent('mousedown', this.moveStart);
		this.min = new Element('img').set({src: '/src/img/bar/triangle.gif', style: 'position: absolute; top: 67px; left: 0px; cursor: pointer;'}).injectInside(this.back).addEvent('mousedown', this.moveStart);
		this.max = new Element('img').set({src: '/src/img/bar/triangle.gif', style: 'position: absolute; top: 67px; left: '+this.sizes[0]+'px; cursor: pointer;'}).injectInside(this.back).addEvent('mousedown', this.moveStart);
	},
	moveStart: function(e) {
		e.preventDefault();
		if(e.target == this.min || e.target == this.max) {
			if(this.min.offsetLeft == this.max.offsetLeft) {
				this.movingThing = [this.min, this.max];
				this.cMin=0;
				this.cMax=this.back.offsetWidth-this.max.offsetWidth;
			} else {
				this.movingThing = e.target;
				this.cMin=(e.target.nextSibling)?0:this.min.offsetLeft;
				this.cMax=(e.target.nextSibling)?this.max.offsetLeft:this.back.offsetWidth-this.max.offsetWidth;
			}
		} else {
			var mousePos = e.page.x-this.back.getPosition().x;
			if(Math.abs(mousePos-this.min.offsetLeft) > Math.abs(mousePos-this.max.offsetLeft))
				this.movingThing = this.max;
			else
				this.movingThing = this.min;
			this.cMin=(this.movingThing.nextSibling)?0:this.min.offsetLeft;
			this.cMax=(this.movingThing.nextSibling)?this.max.offsetLeft:this.back.offsetWidth-this.max.offsetWidth;
		}
		$(document).addEvent('mousemove', this.moveDo);
		$(document).addEvent('mouseup', this.moveEnd);
		$(document).fireEvent('mousemove', {page:{x:e.page.x}});
	},
	moveDo: function(e) {
		if(this.movingThing == null) return;
		var pos = e.page.x - this.back.getPosition().x;
		pos -= this.max.offsetWidth/2;
		
		if(!this.movingThing.src) {
			if(pos > this.min.offsetLeft)
				this.movingThing = this.max;
			else if(pos < this.max.offsetLeft)
				this.movingThing = this.min;
			this.cMin=(this.movingThing.nextSibling)?0:this.min.offsetLeft;
			this.cMax=(this.movingThing.nextSibling)?this.max.offsetLeft:this.back.offsetWidth-this.max.offsetWidth;
		}
		
		if(pos < this.cMin) pos = this.cMin;
		if(pos > this.cMax) pos = this.cMax;

		if(this.movingThing.src)
			this.movingThing.style.left = pos+'px';
		this.bar.style.left = this.min.offsetLeft+this.min.offsetWidth/2+'px';
		this.bar.style.width = this.max.offsetLeft-this.min.offsetLeft+'px';
	},
	moveEnd: function() {
		var minPer = this.min.offsetLeft/(this.back.offsetWidth-this.min.offsetWidth);
		var maxPer = this.max.offsetLeft/(this.back.offsetWidth-this.max.offsetWidth);
		$(document).removeEvent('mousemove', this.moveDo);
		$(document).removeEvent('mouseup', this.moveEnd);
		minPer *= this.anz-1;
		maxPer *= this.anz-1;
		this.click(minPer,maxPer);
		this.form.doUpdate();
	},
	sql: function() {
		if(this.vMin == this.bounds[0] && this.vMax == this.bounds[1]) return '';
		var n = Math.min(this.vMin,this.vMax);
		var x = Math.max(this.vMin,this.vMax);
		if(n == x && this.decimals === false) return ' AND '+this.id+'='+n;
		if(this.decimals !== false) {
			/*var m=Math.pow(10,this.decimals);
			n = Math.floor(n*m)/m;
			x = Math.ceil(x*m)/m;*/
			n = n.toFixed(this.decimals);
			x = x.toFixed(this.decimals);
		}
		return ' AND '+this.id+'>='+n+' AND '+this.id+'<='+x;
	},
	humanReadable: function() {
		if(this.vMin == this.bounds[0] && this.vMax == this.bounds[1]) {
			if(this.allString && this.showAllways) return this.allString;
			if(!this.showAllways) return false;
		}
		var n = (this.translations[this.vMin])?"'"+this.translations[this.vMin]+"'":this.vMin;
		var x = (this.translations[this.vMax])?"'"+this.translations[this.vMax]+"'":this.vMax;
		var both = (this.vMin != this.vMax);
		return eval(this.evalation.replace(/\$1/, n).replace(/\$2/, x));
	},
	postVal: function() {
		if(this.vMin == this.bounds[0] && this.vMax == this.bounds[1]) return [];
		var n = Math.min(this.vMin,this.vMax);
		var x = Math.max(this.vMin,this.vMax);
		if(this.decimals !== false) {
			/*var m=Math.pow(10,this.decimals);
			n = Math.floor(n*m)/m;
			x = Math.ceil(x*m)/m;*/
			n = n.toFixed(this.decimals);
			x = x.toFixed(this.decimals);
		}
		return [new Element('input').set({type: 'hidden', name: 'current_search['+((this.id.substr(0,6)=='preis_')?'bb.preis':this.id)+']', value: ' FEATURE >= '+n+' AND FEATURE <= '+x+' '})];
	},
	reset: function() {
		this.vMin = this.bounds[0];
		this.vMax = this.bounds[1];
		this.min.style.left='0px';
		this.max.style.left=this.sizes[0]-this.sizes[2]+'px';
		this.bar.style.left = this.sizes[1]/2+'px';
		this.bar.style.width = this.sizes[0]-this.sizes[2]+'px';
	},
	set: function(f, v) {
		var n,x=(this.bounds[0]<this.bounds[1]);
		var i = 0;
		if(x) {
			for(i=0;i<this.ranges.length-1;i++)
				if(this.ranges[i+1] > v) break;
		} else {
			for(i=this.ranges.length;i>0;i--)
				if(this.ranges[i-1] > v) break;
		}
		n=i;
		if(x) {
			for(i=this.ranges.length;i>0;i--)
				if(this.ranges[i-1] < v) break;
		} else {
			for(i=0;i<this.ranges.length-1;i++)
				if(this.ranges[i+1] < v) break;
		}
		x=i;
		i=this.sizes[0]/(this.anz-1);
		if(x==n) {
			i *= x;
		} else {
			i *= (n+(v-this.ranges[n])/Math.abs(this.ranges[x]-this.ranges[n]));
		}
		this['v'+f.replace(/m/, 'M')] = v;
		this[f].style.left = i+'px';
		this.bar.style.left = parseInt(this.min.style.left.substr(0,this.min.style.left.length-2))+this.sizes[1]/2+'px';
		this.bar.style.width = parseInt(this.max.style.left.substr(0,this.max.style.left.length-2))-parseInt(this.min.style.left.substr(0,this.min.style.left.length-2))+'px';
	}
});

function randomChar() {
	var i=Math.round(Math.random()*47);
	if(i>23)
		i+=41;
	else
		i+=97;
	return i;
}
function html_entity_decode(s) {
	var t=document.createElement('textarea');
	t.innerHTML = s;
	return t.value;
}
String.prototype.toFixed = function (n) {
	return this;
}
