var motiveManager = {
	motives: [],
	init: function(e, r, p, q) {
		this.queries = q;
		this.results = $(r);
		this.post = p;
		this.ajax = new Request({url: '/lib/ajax.buyingtypes.php'}).addEvent('success', this.redrawResults.bind(this));
		e = $(e);
		e.getChildren('dd').each(function(v){
			this.motives.push(new motive(v, this.update.bind(this)).addEvent('slide', this.updateMotives.bind(this)));
		}, this);
		this.mLength = this.motives.length-1;
		this.mLength *= 100;
	},
	updateMotives: function(e) {
		var add = e.difference/this.mLength;
		this.motives.each(function(v){
			if(v != e.sender)
				v.slider.add(add);
		}, this);
	},
	update: function(e) {
		var q = '';
		this.motives.each(function(v){
			if(this.queries[v.id])
				q += ' %2B '+this.queries[v.id]+' * '+Math.round(v.value)/100;
		}, this);
		this.ajax.send(this.post+'&q='+q.substr(5));
	},
	redrawResults: function(t,x) {
		this.results.set('html', t);
	}
};

var motive = new Class({
	Implements: Events,
	initialize: function(e, onFinish) {
		if(!onFinish) onFinish = null;
		this.id = e.id.substr(7);
		this.span = e.getFirst();
		this.value = this.span.get('html').substr(0, this.span.get('html').length-1);
		e.setStyles({position: 'relative', 'background-image': 'url("/lib/img.qzbar.php?size=narrow&perc=0")'});
		this.slider = new domSlider(new Element('div').setStyles({position: 'absolute', top: 1, left: 1, width: 170, height: 22}).injectInside(e), {value: this.value/100, onSlide: this.slide.bind(this), onFinish: onFinish});
		//this.span = new Element('span').set('html', this.span).injectInside(e);
	},
	slide: function(e) {
		var tmpVal = e.value*100;
		this.span.set('html', Math.round(tmpVal)+'%');
		if(!e.set)
			this.fireEvent('slide', {sender: this, difference: this.value-tmpVal});
		this.value = tmpVal;
	}
});

var character_chooser = new Class({
	groups: [],
	actGroup: null,
	initialize: function(p) {
		this.dl = $(p);
		this.dim = this.dl.getCoordinates();
		this.dim.width -= 60;
		this.dim.width2 = this.dim.width/2;
		this.dl.setStyles({position: 'relative', height: this.dim.height, width: this.dim.width, left: 30});
		this.maxWidth = 0;
		this.dl.getChildren().each(function(v){
			if(v.get('tag') == 'dt') {
				this.actGroup = this.groups.length;
				this.groups.push(new character_chooser_group(v, this, this.actGroup));
				var w = v.setStyle('position', 'absolute').getCoordinates().width;
				if(this.maxWidth < w) this.maxWidth = w;
			} else if(this.actGroup != null) {
				this.groups[this.actGroup].add(v);
			}
		}, this);
		if(this.actGroup == null) return;
		this.groups.each(function(v){
			v.init();
			v.hide();
		}, this);
		this.groups[0].show();
		window.setTimeout(this.tickFunc.bind(this), 50);
	},
	tickFunc: function() {
		var f = parseFloat(this.groups[this.actGroup].fpos);
		var p = parseFloat(this.groups[this.actGroup].pos);

		if(f < p -parseFloat(.1))
			this.groups[this.actGroup].fpos = f +parseFloat(.1);
		else if(f > p +parseFloat(.1))
			this.groups[this.actGroup].fpos = f -parseFloat(.1);
		else
			this.groups[this.actGroup].fpos = p;

		if(f != p)
			this.groups[this.actGroup].draw();
		window.setTimeout(this.tickFunc.bind(this), 50);
	}
});
var character_chooser_group = new Class({
	pos: 0,
	fpos: 0,
	chars: [],
	initialize: function(dt, p, id) {
		this.p = p;
		this.id = id;
		this.dt = dt.addEvent('click', this.show.bind(this));
	},
	add: function(dd) {
		this.chars.push(dd);
	},
	init: function() {
		this.nButt = new Element('img').set({src: '/src/img/button/next_w.gif'}).setStyles({position: 'absolute', top: this.p.dim.height/2-14, right: -28, cursor: 'pointer'}).injectInside(this.p.dl).addEvent('click', this.selectNext.bind(this));
		this.pButt = new Element('img').set({src: '/src/img/button/prev_w.gif'}).setStyles({position: 'absolute', top: this.p.dim.height/2-14, left: -28, cursor: 'pointer'}).injectInside(this.p.dl).addEvent('click', this.selectPrev.bind(this));
		this.dt.setStyles({cursor: 'pointer', top: -this.dt.getCoordinates().height, right: this.p.maxWidth*(this.p.groups.length-this.id-1), width: this.p.maxWidth});
		this.cont = new Element('div').setStyles({position: 'absolute', width: this.p.dim.width, height: this.p.dim.height, top: 0, left: 0, overflow: 'hidden'}).injectInside(this.dt.getParent());
		this.chars.each(function(v, k){
			//v.setStyles({position: 'absolute', width: this.p.dim.width, height: this.p.dim.height, top: 0, left: 0, overflow: 'hidden'});
			v.injectInside(this.cont);
			if(v.getFirst().get('tag') == 'p') {
				v.getFirst().setStyles({width: this.p.dim.width, height: this.p.dim.height, 'line-height': this.p.dim.height});
			} else {
				v.getFirst().set('for', '').getFirst().set({alt: k}).setStyles({position: 'absolute', width: 'auto', height: this.p.dim.height, top: 0, left: 0}).addEvent('click', this.select.bind(this)).getParent().getNext().setStyle('opacity', 0);
			}
		}, this);
		if(this.chars[0].getFirst().get('tag') == 'p') return;
		this.fpos = this.pos = Math.floor(this.chars.length/2);
		this.charWidth = this.chars[0].getFirst().getFirst().getCoordinates().width;
		this.draw();
	},
	show: function() {
		this.p.groups[this.p.actGroup].hide();
		this.p.actGroup = this.id;
		this.dt.addClass('act');
		this.cont.setStyles({display: 'block'});
		if(this.chars[0].getFirst().get('tag') != 'p') {
			this.nButt.setStyles({display: 'block'});
			this.pButt.setStyles({display: 'block'});
		}
		this.selectItem(this.pos);
	},
	hide: function() {
		this.dt.removeClass('act');
		this.cont.setStyles({display: 'none'});
		this.nButt.setStyles({display: 'none'});
		this.pButt.setStyles({display: 'none'});
	},
	draw: function() {
		this.chars.each(function(v, k){
			if(v.getFirst().get('tag') == 'p') return;
			var z;
			var off = k-this.fpos;v.getFirst().getFirst().title = k+' - '+this.fpos;
			if(off > 1 || off < -1) z = .65;
			else z = 1-Math.abs(off)*.35;
			
			v.getFirst().getFirst().setStyles({left: this.p.dim.width2+(off)*this.charWidth-z*this.charWidth/2, height: this.p.dim.height*z, top: this.p.dim.height*2*(1-z)/3});
			
			this.pButt.setStyles({opacity: (this.fpos < 1)?.5:1, cursor: (this.fpos < 1)?'default':'pointer'});
			this.nButt.setStyles({opacity: (this.fpos > this.chars.length-2)?.5:1, cursor: (this.fpos > this.chars.length-2)?'default':'pointer'});
		}, this);
	},
	select: function(e) {
		this.selectItem(parseInt(e.target.alt));
	},
	selectNext: function(e) {
		if(this.pos < this.chars.length-1)
			this.selectItem(this.pos+1);
	},
	selectPrev: function(e) {
		if(this.pos > 0)
			this.selectItem(this.pos-1);
	},
	selectItem: function(e) {
		this.pos = e;
		this.chars[e].getLast().checked = true;
	}
});
var motiveSlider = new Class({
	initialize: function(e) {
		e = $(e);
		this.input = e.getFirst();
		if(this.input.get('value') == '') this.input.set('value', 0);
		this.input.addEvent('change', this.change.bind(this));
		this.input.addEvent('keydown', this.enterSim);
		this.slider = new domSlider(new Element('div').injectInside(e.setStyle('position', 'relative')).setStyles({position: 'absolute', top: 7, left: 1, width: 170, height: 22}), {value: this.input.get('value')/100, onFinish: this.update.bind(this)});
	},
	change: function(e) {
		this.input.set('value', parseFloat(this.input.get('value')));
		if(this.input.get('value') == 'NaN' || this.input.get('value') < 0)
			this.input.set('value', 0);
		if(this.input.get('value') > 100) this.input.set('value', 100);

		this.slider.set(this.input.get('value')/100);
	},
	update: function(e) {
		this.input.set('value', Math.round(e.value*100));
	},
	enterSim: function(e) {
		if(e.key == 'enter') {
			e.preventDefault();
			var p = $(e.target).getParent();
			if(p.getNext()) p = p.getNext();
			if(p.getNext()) p = p.getNext();
			if(p.getNext()) p = p.getNext();
			if(p.getFirst()) { p.getFirst().focus(); p.getFirst().select(); }
		}
	}
});
