var promotion = {
	groups: {},
	promos: [],
	
	init: function(pg, flow_id, group_id, show_id, product_id, def) {
		this.flow = $(flow_id).removeClass('noscript');
		this.group = $(group_id);
		this.show = $(show_id);
		this.preview = $(product_id);
		this.def = def?def:0;
		new Request({url: '/lib/ajax.promotion.php', onSuccess: this.start.bind(this)}).send('pg='+pg);
	},
	start: function(t, xml) {
		var g = t.split('|||');
		g.each(function(v,k,o){
			var a = v.split('||');
			var id = a.shift();
			a.each(function(v,k,o){
				var g = this.group.split('|');
				var p = new promotion_item(v.split('|'), g);
				if(!this.oG[g[0]]) this.oG[g[0]] = [];
				this.oG[g[0]].push(p);
				this.oP.push(p);
			}, {'group':id,'oG':this.groups,'oP':this.promos});
		}, this);
		if(this.promos.length == 0) {
			//fatal error
			this.flow.empty().appendText('Sorry no promotions available!').addClass('noscript');
			return false;
		}
		
		this.flow = new promotion_flow(this.flow, this.promos, this.def);
		this.show = new promotion_show(this.show, this.promos, this.def);
		this.flow.addEvent('tick', this.show.update.bind(this.show));
		this.group = new promotion_group(this.group, this.def);
		this.flow.addEvent('groupchange', this.group.check.bind(this.group));
		this.preview = new promotion_prev(this.preview, this.def);
		PreviewIn = this.preview.show.bind(this.preview);
		this.flow.addEvent('itemchange', function(o, n){PreviewIn(promotion.promos[n].first);});
	},
	getOffsets: function(pos) {
		/*
		* pos 0 zeige element 0 in der mitte
		* pos 1 zeige element 1 in der mitte
		*/
		/* correct pos-value */
		while(pos < 0) pos += this.promos.length;
		while(pos >= this.promos.length) pos -= this.promos.length;
		var t, out = [], min = Math.floor(pos)-2, max = Math.ceil(pos)+2;
		while(min < 0) min += this.promos.length;
		while(max >= this.promos.length) max -= this.promos.length;
		for(var i=0; i<this.promos.length; i++) {
			t = true;
			if(min<max) {
				if(i<min || i>max) t = false;
			} else {
				if(i<min && i>max) t = false;
			}
			if(t === true) {
				t = pos-i;
				if(Math.abs(t) > 3) {
					if(t < 0) t += this.promos.length;
					else t -= this.promos.length;
				}
			}
			out.push(t);
		}
		return out;
	}
};
var promotion_flow = new Class({
	Implements: Events,

	imgs: [],
	tags: [],
	act_group: 0,
	act_item: 0,
	ticker: 0,
	pos_curr: 0,
	pos_target: 0,
	
	speed: 50,
	
	initialize: function(container, promos, def) {
		this.container = container.setStyles({'position': 'relative', 'overflow': 'hidden'}).empty();
		this.promos = promos;
		this.promos.each(function(v,k,o){
			this.imgs.push(new Element('img').set({'src': '/cache/img/promotion/'+v.id+'_small.jpg'}).setStyles({'position': 'absolute', 'left': -124, 'top': 105, 'width': 62}).addEvent('click', this.moveTo.bind({ob: this, index: k})).injectInside(this.container));
			this.tags.push(new Element('b').setStyles({'display': 'block', 'position': 'absolute', 'left': -300, 'top': 287, 'width': 300, 'height': 60, 'line-height': 60, 'text-align': 'center', 'background': '#e4e4e4'}).appendText(v.name).inject(this.container.getNext(), 'after'));
		}, this);
		this.addEvent('abort', function(e){
			if(this.ticker)
				window.clearTimeout(this.ticker);
		}.bind(this));
		this.addEvent('select', function(e, speed){
			this.select(e, speed);
		}.bind(this));
		this.pos_curr = this.pos_target = def*10;
		this.sort(promotion.getOffsets(this.pos_curr/10));
		new Element('img').set({'src': '/src/img/button/prev_w.gif'}).setStyles({'position': 'absolute', 'left': 120, 'top': 200, 'width': 27, 'cursor': 'pointer'}).addEvent('click', this.prev.bind(this)).injectInside(this.container);
		new Element('img').set({'src': '/src/img/button/next_w.gif'}).setStyles({'position': 'absolute', 'left': 160, 'top': 200, 'width': 27, 'cursor': 'pointer'}).addEvent('click', this.next.bind(this)).injectInside(this.container);
	},
	select: function(e, speed) {
		if(this.pos_target == e*10) return false;
		this.fireEvent('abort', false);
		this.pos_target = e*10;
		this.startTicker(speed?speed:this.speed);
	},
	moveTo: function(e) {
		this.ob.fireEvent('select', this.index);
	},
	next: function(e) {
		var t = this.act_item+1;
		if(t >= this.imgs.length) t -= this.imgs.length;
		this.fireEvent('select', t);
	},
	prev: function(e) {
		var t = this.act_item-1;
		if(t < 0) t += this.imgs.length;
		this.fireEvent('select', t);
	},
	startTicker: function(speed) {
		var dir = (Math.abs(this.pos_target-this.pos_curr) < this.imgs.length*5)?1:-1;
		dir *= (this.pos_target < this.pos_curr)?-1:1;
		this.tick(dir, speed);
	},
	tick: function(dir, speed) {
		this.pos_curr += dir;
		if(this.pos_curr < 0) this.pos_curr += this.imgs.length*10;
		if(this.pos_curr >= this.imgs.length*10) this.pos_curr -= this.imgs.length*10;
		this.sort(promotion.getOffsets(this.pos_curr/10));
		this.fireEvent('tick', this.pos_curr/10);
		if(this.pos_curr != this.pos_target)
			this.ticker = window.setTimeout(function(e){this.tick(dir, speed);}.bind(this), speed);
		else
			this.fireEvent('complete', true);
	},
	sort: function(a) {
		a.each(function(v,k,o){
			if(v === false)
				this.imgs[k].setStyle('display', 'none');
			else {
				var l, w=.5, z=1;
				l = 119-v*65;
				if(Math.abs(v) < 1) {
					w = 1-Math.abs(v)/2;
					l = 150-w*62-v*65;
					z = 2;
					if(Math.abs(v) < .5) {
						z = 3;
						if(this.act_item != k) {
							this.fireEvent('itemchange', [this.act_item, k]);
							this.act_item = k;
							if(this.act_group != promotion.promos[k].groupId) {
								this.fireEvent('groupchange', [this.act_group, promotion.promos[k].groupId]);
								this.act_group = promotion.promos[k].groupId;
							}
						}
					}
				}
				w *= 124;
				this.imgs[k].setStyles({'display': 'block', 'left': l, 'top': 128-(w/2), 'width': w, 'z-index': z});
				if(document.all) this.imgs[k].setStyles({'height': w*0.72580645161290322580645161290323});
				if(Math.abs(v) <= 1) this.tags[k].setStyle('left', -v*300);
			}
		}, this);
	}
});
var promotion_show = new Class({
	tags: [],
	
	initialize: function(container, promos, def) {
		this.container = container;
		this.container.getParent().setStyle('position', 'relative');
		promos.each(function(v,k,o){
			var t = new Element('div').setStyles({'position': 'absolute', 'top': 31, 'left': 362, 'background': 'white', 'height': 428, 'width': 362, 'z-index': 1}).inject(this.container, 'before');
			t.innerHTML = v.table;
			this.tags.push(t);
		}, this);
		this.update(def);
	},
	update: function(a) {
		a = promotion.getOffsets(a);
		a.each(function(v,k,o){
			var l;
			if(v === false || Math.abs(v) >= 1)
				l = 362;
			else
				l = -v*362;
			this.tags[k].setStyle('left', l);
		}, this);
	}
});
var promotion_group = new Class({
	lock: false,
	
	initialize: function(container, def) {
		promotion.flow.addEvent('complete', function(e){this.lock = false;}.bind(this));
		promotion.flow.addEvent('abort', function(e){this.lock = false;}.bind(this));
		this.container = container;
		this.act_name = container.getChildren('span')[0];
		this.act_name.empty();
		this.buttons = container.getChildren('ul')[0].getChildren('li');
		this.buttons.each(function(v,k,o){
			var a = v.getFirst('a');
			var g = a.getFirst().src;
			g = g.substr(g.lastIndexOf('/')+1);
			g = g.substr(0,g.length-4);
			a.addEvent('click', function(e){e.preventDefault();this.select(g);}.bind(this));
		}, this);
		this.check(false, promotion.promos[def].groupId);
	},
	select: function(id) {
		promotion.promos.each(function(v,k,o){
			if(v.groupId == this.id) {
				this.check(this.id, this.id);
				this.lock = true;
				promotion.flow.fireEvent('select', [k, 20]);
				this.id = 0;
			}
		}, {id: id, lock: this.lock, check: this.check.bind(this)});
	},
	check: function(lastid, id) {
		if(!this.lock) {
			this.act_name.empty().appendText(promotion.groups[id][0].groupName);
			this.buttons.each(function(v,k,o){
				var e = v.getFirst().getFirst();
				e.src = e.src.replace(/_act/, '');
				if(e.src.substr(e.src.lastIndexOf('/')+1) == this.id+'.gif')
					e.src = e.src.replace(/\.gif/, '_act.gif');
			}, {id: promotion.groups[id][0].groupId});
		}
	}
});
var promotion_item = new Class({
	initialize: function(aParts, aGroup) {
		this.groupId	= aGroup[0];
		this.groupName	= aGroup[1];
		this.id			= aParts[0];
		this.name		= aParts[1];
		this.first		= aParts[2];
		this.table		= aParts[3];
	}
});
var promotion_prev = new Class({
	cache: [],
	req: null,
	act: null,
	
	initialize: function(container, def) {
		this.container = container;
		this.req = new Request({url: '/lib/ajax.promotion.product.php', onSuccess: this.response.bind(this), link: 'cancel'});
		this.show(promotion.promos[def].first);
	},
	show: function(id) {
		if(this.cache[id]) return this.response(this.cache[id]);
		this.act = null;
		this.req.send('product='+id)
		this.act = id;
	},
	response: function(t, xml) {
		if(t == '0') return false;
		if(this.act == null) return this.container.empty().innerHTML = t;
		this.cache[this.act] = this.container.empty().innerHTML = t;
		this.act = null;
	}
});

