/*****************************************************************
** MAIN KLASSE
*****************************************************************/
var showcase = new Class({
	hmin: .5,
	hoff: .5,
	max: 5,
	offSet: 5,
	width: 292,
	height: 80,
	speed: .02,
	acceleration: .05,
	movieSpeed: 2000,
	Timer: 0,
	barTimer: 1,
	loaded: {},
	initialize: function(oCarousel, oCarouselBar) {
		this.wrapper = [];
		this.wrapper[0]				= $(oCarousel);
		this.wrapper[0].empty();
		this.wrapper[0].className	= 'load';
		this.wrapper[1]				= $(oCarouselBar);
		this.wrapper[1].empty();
				
		this.cItems = [];
		
		new Request({url: '/partner/lib/ajax.get_small.php', method: 'post', onSuccess: this.loadFromAjax.bind(this)}).send('pg='+this.wrapper[0].title);
	},
	loadFromAjax: function(r, d) {
		this.kids = r.split("\n");
		
		this.loadQueue = 0;
		var loadImgs = [];
		loadImgs.push('/partner/src/img/player/bar_a.gif');
		loadImgs.push('/partner/src/img/player/bar_i.gif');
		loadImgs.push('/partner/src/img/player/play.gif');
		loadImgs.push('/partner/src/img/player/pause.gif');
		loadImgs.push('/partner/src/img/player/stop.gif');
		loadImgs.push('/partner/src/img/player/next.gif');
		loadImgs.push('/partner/src/img/player/prev.gif');

		$each(this.kids, function(v, k, o){
			o[k] += "|"+(this.loadQueue++/this.kids.length);
			loadImgs.push(v.split("|")[1]);
		}, this);
		this.loadQueue += 7;
		
		$each(loadImgs, function(v, k, o){
			o[k] = new Image();
			o[k].onload = this.checkQueue.bind(this);
			o[k].src = v;
		}, this);
		
		this.checkQueue();
	},
	checkQueue: function() {
		this.loadQueue--;
		if(this.loadQueue < 0)
			this.start();
	},
	start: function() {
		this.carousel		= new c_flow(this.wrapper[0], this.wrapper[1]);
		this.carousel.set({'tickFunc': this.render.bind(this), 'width': this.width, 'height': this.height, 'hoff': this.hoff, 'hmin': this.hmin, 'paddingTop': this.offSet, 'paddingLeft': this.offSet, 'max': this.max});
		this.selectDesc		= new Element('div').injectInside(this.carousel.container);
		this.carousel.setPos = 0;
		$each(this.kids, function(v){
			this.cItems.push(new showcaseItem(this, v));
		}, this);
		this.carousel.start();
		this.selectDesc.set({'class': 'selectDesc'});
	},
	render: function() {
		var ni = this.carousel.getNearestItem();
		$each(this.cItems, function(e){
			e.render(ni);
		});
	}
});

var showcaseItem = new Class({
	parent: null,
	carouselObject: null,
	selectObject: null,
	displayObject: null,
	displayTitle: null,
	img: null,
	name: "",
	desc: "",
	src: "",
	pos: 0,
	imgWidth: 354,
	initialize: function(p, e) {
		this.parent = p;
		e = e.split("|");
		this.name = e[0];
		this.desc = e[2];
		this.pos = parseFloat(e[4]);
		this.src = e[1];
		this.link = e[3];
		
		this.img = new Element('img').injectInside(this.parent.wrapper[1]).set({title: (this.desc != "")?this.desc:this.name, src: this.src, style: 'position: absolute; top: 100%;'});
		this.img.addEvent('click', this.click.bind(this));
		if(this.img.offsetWidth != 0)
			this.imgWidth = this.img.offsetWidth;
		this.img.set({style: ''});
		this.carouselObject = new c_flowItem(this.parent.carousel, this.img, this.pos);
		
		if(Math.abs(this.pos-this.parent.carousel.setPos) < 0.00000001) this.parent.carousel.setPos = this.pos;
	},
	click: function(e) {
		if(Math.abs(this.parent.carousel.currPos-this.pos) < 0.00000001)
			window.open(this.link);
	},
	render: function(nItem) {
		if(this.carouselObject == nItem)
			this.parent.selectDesc.innerHTML = this.desc;
	}
});

/*****************************************************************
** CAROUSEL
*****************************************************************/
var c_flow = new Class({
	renderTimer: 0,
	playTimer: 1,
	initialize: function(Container, Bar) {
		this.container = Container;
		this.barContainer = Bar;
		
		this.width = 302;
		this.height = 30;
		this.hmin = .7;
		this.max = 5;
		this.speed = .02;
		this.acceleration = .05;
		this.movieSpeed = 2000;
		this.paddingTop = 0;
		this.paddingLeft = 0;
		this.hoff = .5;
		this.eWidth = 0;
		this.eHeight = 0;
		this.currPos = 0;
		this.setPos = 0;
		this.itemLength = 0;
		this.items = [];
		this.tickFunc = function(){};
		this.clearFunc = function(){};
		this.renderType = this.renderSmooth;
		this.eHeight = 256/354;

		this.barMoveStart = this.barMoveStart.bind(this);
		this.barMove = this.barMove.bind(this);
		this.barMoveEnd = this.barMoveEnd.bind(this);

	},
	set: function(e) {
		$each(e, function(v, k){
			this[k] = v;
		}, this);
	},
	start: function() {
		this.currPos = this.setPos;
		if(this.itemLength == 0) return false;
		this.barContainer.empty();
		
		var fc = this.container.firstChild;
		var border = fc.style.border;
		fc.set({style: 'position: absolute; top: 100%; left: 100%; display: block;'});
		this.eWidth = fc.width;
		this.eHeight = fc.height;
		fc.set({style: 'position: absolute;'});
		fc.style.border = border;
		var d = this.height/this.eHeight;
		this.eHeight*=d;
		this.eWidth*=d;
		
		this.m2 = 1-this.hmin;
		if(this.max%2 == 0) this.max--;
		this.maxCurr = (2*this.itemLength)/(this.max-1);

		new Element('div').injectInside(this.barContainer).set({style: 'background: url(/partner/src/img/player/bar_a_15.gif); position: absolute; top: 3px; left: 94px; width: 150px; height: 15px; cursor: pointer;'}).addEvent('mousedown', this.barMoveStart);
		new Element('img').injectInside(this.barContainer.firstChild).set({src: '/partner/src/img/player/bar_i.gif', style: 'position: absolute; top: 0px; left: 0px; width: 20px; height: 15px; cursor: pointer;'}).addEvent('mousedown', this.barMoveStart);
		new Element('img').injectInside(this.barContainer).set({src: '/partner/src/img/player/play.gif', style: 'position: absolute; top: 3px; left: 45px; width: 15px; height: 15px; cursor: pointer;'}).addEvent('click', this.barPlay.bind(this));
		new Element('img').injectInside(this.barContainer).set({src: '/partner/src/img/player/pause.gif', style: 'position: absolute; top: 3px; left: 45px; width: 15px; height: 15px; cursor: pointer; display: none;'}).addEvent('click', this.barPause.bind(this));
		new Element('img').injectInside(this.barContainer).set({src: '/partner/src/img/player/stop.gif', style: 'position: absolute; top: 3px; left: 63px; width: 15px; height: 15px; cursor: pointer;'}).addEvent('click', this.barStop.bind(this));
		new Element('img').injectInside(this.barContainer).set({src: '/partner/src/img/player/next.gif', style: 'position: absolute; top: 3px; left: 242px; width: 15px; height: 15px; cursor: pointer;'}).addEvent('click', this.barNext.bind(this));
		new Element('img').injectInside(this.barContainer).set({src: '/partner/src/img/player/prev.gif', style: 'position: absolute; top: 3px; left: 81px; width: 15px; height: 15px; cursor: pointer;'}).addEvent('click', this.barPrev.bind(this));
		
		if(document.all) {
			this.barContainer.firstChild.firstChild.ondragstart = function(){return false;};
		}

		this.render();
		
		this.container.set({'class': ''});
		this.renderTimer = window.setInterval(this.tick.bind(this), 100);
	},
	render: function() {
		var camera = false;
		var c = this.currPos;
		
		$each(this.items, function(e){
			e.render(-1*(e.pos-c), camera);
		});
		var bar = this.barContainer.firstChild.firstChild;
		bar.style.left = this.currPos*(bar.parentNode.offsetWidth-bar.offsetWidth-2)+1+'px';
	},
	renderHard: function() {
		this.currPos = this.setPos;
	},
	renderSmooth: function() {
		var a = this.currPos-this.setPos;
		if(a == 0) return;
		a = Math.abs(a);
		if(a > .5) a = 1-a;
		
		if(a < this.speed)
			this.currPos = this.setPos;
		else {
			a = this.speed+this.acceleration*a;
			if((this.currPos-this.setPos+1)%1 > .5)
				this.currPos += a;
			else
				this.currPos -= a;
			if(this.currPos >= 1) this.currPos--;
			if(this.currPos < 0) this.currPos++;
		}
	},
	tick: function() {
		this.renderType();
		this.render();
		this.tickFunc();
	},
	barMoveStart: function(e) {
		if(this.renderType == this.renderHard) return true;
		e.preventDefault();
		this.clearFunc();
		this.renderType = this.renderHard;
		$(document).addEvent('mousemove', this.barMove);
		$(document).addEvent('mouseup', this.barMoveEnd);
		$(document).fireEvent('mousemove', {page:{x:e.page.x}});
		this.renderHard();
	},
	barMove: function(e) {
		var bar = this.barContainer.firstChild;
		var pos = e.page.x - bar.getPosition().x;
		pos -= bar.firstChild.offsetWidth/2;
		if(pos < 0) pos = 0;
		if(pos > bar.offsetWidth-bar.firstChild.offsetWidth) pos = bar.offsetWidth-bar.firstChild.offsetWidth;
		pos /= bar.offsetWidth-bar.firstChild.offsetWidth;
		this.setPos = pos;
	},
	barMoveEnd: function() {
		$(document).removeEvent('mousemove', this.barMove);
		$(document).removeEvent('mouseup', this.barMoveEnd);
		this.renderType = this.renderSmooth;
		this.setPos = this.getNearestItem().pos;
	},
	barPlay: function() {
		var p = this.barContainer.firstChild.nextSibling;
		p.nextSibling.style.display = 'block';
		p.style.display = 'none';
		this.playTimer = window.setInterval(this.next.bind(this), this.movieSpeed);
	},
	barPause: function() {
		var p = this.barContainer.firstChild.nextSibling;
		p.style.display = 'block';
		p.nextSibling.style.display = 'none';
		window.clearInterval(this.playTimer);
	},
	barStop: function() {
		this.clearFunc();
		this.barPause();
		this.setPos = this.std;
	},
	barNext: function(pause) {
		this.clearFunc();
		this.next();
	},
	next: function(pause) {
		if(pause && pause.target && pause.target.parentNode)
			this.barPause();
		var o = this.getNearestItem().pos + 1/this.itemLength;
		while(o >= 1) o--;
		this.setPos = o;
	},
	barPrev: function(pause) {
		this.clearFunc();
		this.prev();
	},
	prev: function(pause) {
		if(pause && pause.target && pause.target.parentNode)
			this.barPause();
		var o = this.getNearestItem().pos - 1/this.itemLength;
		while(o < 0) o++;
		this.setPos = o;
	},
	getNearestItem: function() {
		var a = 1;
		var o = null
		$each(this.items, function(e){
			var b = Math.abs(this.currPos-e.pos);
			if(b > .5) b = 1-b;
			if(a > b) {
				a = b;
				o = e;
			}
		}.bind(this));
		return o;
	}
});
var c_flowItem = new Class({
	parent: null,
	img: null,
	pos: 0,
	initialize: function(p, o, i) {
		if(!o) return false;
		this.parent = p;
		if(this.parent.container.firstChild && !this.parent.container.firstChild.src)
			this.img = o.inject(this.parent.container.firstChild, 'before').set({style: 'position: absolute; cursor: pointer;'}).addEvent('click', this.select.bind(this));
		else
			this.img = o.injectInside(this.parent.container).set({style: 'position: absolute; cursor: pointer;'}).addEvent('click', this.select.bind(this));
		this.img.style.border = 'solid 1px #85858a';
		this.parent.items.push(this);
		this.parent.itemLength = this.parent.items.length;
		this.pos = i;
	},
	render: function(currPos, camera) {
		var zIndex = currPos;
		zIndex = Math.abs(zIndex);
		if(zIndex < .5) zIndex = 1-zIndex;
		zIndex = Math.round(zIndex*this.parent.itemLength);
		
		
		var l = currPos;
		while(l>.5)l--;
		while(l<-0.5)l++;
		l*=this.parent.maxCurr;
		if(l>1||l<-1) l/=Math.abs(l);
		//l=l/2;
		var t = this.parent.m2*l*l;
		
		var w = (1-t)*this.parent.eWidth;
		var h = (1-t)*this.parent.eHeight;
		t *= this.parent.height;
		l *= (this.parent.width-this.parent.eWidth)/2;
		if(l<=0)
			l += (this.parent.width-this.parent.eWidth)/2;
		else
			l += (this.parent.width+this.parent.eWidth)/2-w;
		
		this.img.style.width	= Math.round(w)+'px';
		this.img.style.height	= Math.round(h)+'px';
		this.img.style.top		= Math.round(t*this.parent.hoff+this.parent.paddingTop)+'px';
		this.img.style.left		= Math.round(l+this.parent.paddingLeft)+'px';
		this.img.style.zIndex	= zIndex;
	},
	select: function(e) {
		this.parent.clearFunc();
		this.parent.setPos = this.pos;
		this.parent.barPause();
	}
});


/*****************************************************************
** AKTIVIEREN
*****************************************************************/
var sc = null;

/*bei begin laden*/
$(window).addEvent('load', function(e){
	sc = new showcase('carousel_main', 'carousel_foot');
});
