var Frontpage = function() {
	var self = this;

	this.strLocalBaseURI = "";

	this.$divFoto = null;
	this.divWidth = 0;
	this.divHeight = 0;

	this.$img = null;
	this.imgWidth = 0;
	this.imgHeight = 0;

	this.blnAnimateX = false;
	this.blnAnimateY = false;

	this.intStepSizeY = 0;
	this.intStepSizeX = 0;

	this.intX = 1;
	this.intY = 1;
	this.intPrevX = 0;
	this.intPrevY = 0;

	this.intMinX = 0;
	this.intMinY = 0;

	this.init = function() {
		this.$divFoto = $("#divFrontpageFotoL");
		this.$img = this.$divFoto.find("img");
		
		this.divWidth = this.$divFoto.width();
		this.divHeight = this.$divFoto.height();

		this.blnAnimateX = this.imgWidth > this.divWidth;
		this.blnAnimateY = this.imgHeight > this.divHeight;

		if (this.blnAnimateX) {
			this.intMinX = this.divWidth - this.imgWidth;
			this.intStepSizeX = Math.floor(this.imgWidth / 10);

		}
		if (this.blnAnimateY) {
			this.intMinY = this.divHeight - this.imgHeight;
			this.intStepSizeY = Math.floor(this.imgHeight / 10);
		}

		if (this.blnAnimateX || this.blnAnimateY) {
			var src = this.$img.attr("src");
			this.$img.attr("src", this.strLocalBaseURI + "images/blank.gif");
			this.$divFoto.css("background-image", "url(" + src + ")");

			// Centreer
			this.intX = -(this.imgWidth / 2) + (this.divWidth / 2);
			this.intY = -(this.imgHeight / 2) + (this.divHeight / 2);
			this.$divFoto.css("backgroundPosition", this.intX + "px " + this.intY + "px");

			this.startAnim();
		}
	};

	this.randomPos = function(min, max) {
		return Math.floor(Math.random()*(max - min + 1) + min); 
	};

	this.startAnim = function() {
		var intDuration = 3000;
		var strEasing = "linear";

		var intDiffX = 0, intDiffY = 0;
		var newX = 1, newY = 1;

		while (intDiffX == 0 && intDiffY == 0) {
			intDiffX = 0; intDiffY = 0; newX = 1; newY = 1;

			if (this.blnAnimateX) {
				while (newX > 0 || newX < this.intMinX) {
					intDiffX = this.intStepSizeX * this.randomPos(-1, 1);
					newX = this.intX - intDiffX;
				}
			}
			else
				newX = 0;
			if (this.blnAnimateY) {
				while (newY > 0 || newY < this.intMinY) {
					intDiffY = this.intStepSizeY * this.randomPos(-1, 1);
					newY = this.intY - intDiffY;
				}
			}
			else
				newY = 0;
		}

		this.intX = newX;
		this.intY = newY;

		this.$divFoto.animate({
			"backgroundPosition": "" + this.intX + "px " + this.intY + "px"
		}, intDuration, strEasing, function() {
			this.intPrevX = this.intX;
			this.intPrevY = this.intY;
			self.startAnim.call(self);
		});
	}
};