$.noConflict();
if ('undefined' == typeof console) {
	console = {log: function () {}};
}
(function($) {
var carousels = [];
function Carousel(container, items) {
	if (this == window) {throw 'whoops';}
	this.container = container;
	this.items = items;
	this.fix(items);
	this.scrollevery = 30; // ms mellem hvert ryk
	this.scrollby = 3; // antal pixels ved hvert ryk
	this.playing = true;
	var carousel = this;
	this.scroll = function () {
		var newscroll = carousel.container.scrollLeft()+carousel.scrollby;
		carousel.container.scrollLeft(newscroll);
		if (carousel.leftmost.right < newscroll) {
			var th = $(carousel.leftmost.obj);
			th.css('left', carousel.nextleft);
			carousel.leftmost.right = carousel.nextleft += th.outerWidth();
			carousel.items.each(function () {
				var th = $(this);
				var right = this.offsetLeft+th.outerWidth();
				if (right < carousel.leftmost.right) {
					carousel.leftmost = {right: right, obj: this};
				}
			});
		}
	};
	this.getoff();
	this.onwindowscroll();
	carousels.push(this);
}
Carousel.prototype.start = function () {
	if (this.scrollinterval) {return;}
	this.scrollinterval = setInterval(this.scroll, this.scrollevery);
	console.log('Carousel started');
}
Carousel.prototype.stop = function () {
	if (!this.scrollinterval) {return;}
	clearInterval(this.scrollinterval);
	this.scrollinterval = false;
	console.log('Carousel stopped');
}
Carousel.prototype.play = function () {
	this.playing = true;
	if (this.visible) {this.start();}
}
Carousel.prototype.pause = function () {
	this.playing = false;
	this.stop();
}
Carousel.prototype.getoff = function () {
	var el = $(this.container);
	var off = el.offset();
	this.topy = off.top;
	this.bottomy = off.top+el.height();
}
Carousel.prototype.fix = function (items) {
	var max = -1, maxitem = null, leftmost = null, leftmostitem = null, positions = [], carousel = this;
	$(items).each(function () {
		var pos = {left: this.offsetLeft, top: this.offsetTop}; // $().position() doesn't work in jquery+chrome?
		if (pos.left > max) {
			max = pos.left;
			maxitem = this;
		}
		if (leftmost == null || leftmost > pos.left) {
			leftmost = pos.left;
			leftmostitem = this;
		}
		positions.push([this, pos]);
	});
	for (var i = 0; i < positions.length; ++i) {
		var pos = positions[i][1];
		$(positions[i][0]).css('left', pos.left).css('top', pos.top).css('position', 'absolute');
	}
	this.nextleft = max+$(maxitem).outerWidth();
	this.leftmost = {right: leftmost+$(leftmostitem).outerWidth(), obj: leftmostitem};
}
Carousel.prototype.onwindowscroll = function (ev) {
	if (!this.playing) {return;}
	var topy = $(window).scrollTop();
	var bottomy = topy+$(window).height();
	if ((topy < this.bottomy && this.bottomy < bottomy || topy < this.topy && this.topy < bottomy || this.topy < topy && bottomy < this.bottomy)) {
		this.visible = true;
		this.start();
	} else {
		this.visible = false;
		this.stop();
	}
}
$(window).load(function () {
	var container = $('.sponsors .bannergroup').eq(0),
	carousel = new Carousel(container, container.find('.banneritem')),
	btn = $('.sponsors .controls .pause')
	.click(function (ev) {ev.preventDefault();})
	.attr('href', 'carousel: start/stop')
	.toggle(function () {
		carousel.pause();
		$(this).addClass('play').removeClass('pause');
	}, function () {
		carousel.play();
		$(this).addClass('pause').removeClass('play');
	});
	$('.sponsors').hover(function () {btn.fadeIn('slow');}, function () {btn.fadeOut('slow');});
});
$(window).scroll(function () {
	for (var i = 0; i < carousels.length; ++i) {
		carousels[i].onwindowscroll();
	}
});
})(jQuery);

