function getOffsets(e) {
	var o = {
		height: e.offsetHeight,
		width: e.offsetWidth
	};
	
	var x = e.offsetLeft;
	var y = e.offsetTop;
	var p = e.offsetParent;
	
	while(p && (p.nodeType != 9)) {
		x += p.offsetLeft;
		y += p.offsetTop;
		p = p.offsetParent;
	}
	o.left = x;
	o.top = y;
	return o;
}
function createActionAnimation(step,maxsteps,animationElement,oldHeight,newHeight,completeFunction) {
	var currentHeight = oldHeight - (((oldHeight - newHeight) / maxsteps) * step);
	
	animationElement.style.height =  currentHeight + 'px';
	
	if (step < maxsteps) {
		window.setTimeout(function() { createActionAnimation(step + 1,maxsteps,animationElement,oldHeight,newHeight,completeFunction); },15);
	}
	else {
		completeFunction();
	}
}

var hidingTimeoutID = false;

var shown = false;
var hidden = true;

var showing = false;
var hiding = false;

function hoverProductsIn() {
	clearTimeout(hidingTimeoutID);
	if (!showing) {
		showing = true;
		var productsBox = document.getElementById('productsBox');
		var productsMenuItem = document.getElementById('productsMenuItem');
		var offsets = getOffsets(productsMenuItem);
		productsBox.style.display = 'block';
		productsBox.style.top = (offsets.top + offsets.height) + 'px';
		productsBox.style.left = (offsets.left) + 'px';
		productsBox.style.height = '0px';
		createActionAnimation(0,35,productsBox,0,50,function() {
			shown = true;
			hidden = false;
		});
	}
}
function hoverProductsOut() {
	hidingTimeoutID = setTimeout(function() {
		hideProducts();
	},300);
}
function hideProducts() {
	showing = false;
	hiding = true;
	
	var productsBox = document.getElementById('productsBox');
	productsBox.style.display = 'none';
	shown = false;
	hidden = true;
}






/************************************************
* Forside billede fade
************************************************/


var imageNumber = 0;
var fadein = true;
var topimage = false;

function startExchange() {
/*	window.onload = function() {

	}*/
}
function fadeTimout() {
	if (fadein) {
		if (imageNumber > topimages.length - 1) {
			imageNumber = 0;
		}
		topimage.src = topimages[imageNumber];
		fadeTopImageIn(0);
		imageNumber++;
	}
	else {
		fadeTopImageOut(100);
	}
}
function fadeTopImageIn(step) {
	var newStep = step + 4;
	
	if (document.all) {
		topimage.filters.item('DXImageTransform.Microsoft.Alpha').opacity = newStep;
	}
	else if (topimage.style.MozOpacity) {
		topimage.style.MozOpacity = newStep / 100;
	}
	else {
		topimage.style.opacity = newStep / 100;	
	}

	if (newStep < 100) {
		setTimeout(function() {
			fadeTopImageIn(newStep);
		},30);
	}
	else {
		fadein = false;
		setTimeout(function() {
			fadeTimout()
		},3000);
	}
}
function fadeTopImageOut(step) {
	var newStep = step - 4;
	
	if (document.all) {
		topimage.filters.item('DXImageTransform.Microsoft.Alpha').opacity = newStep;
	}
	else if (topimage.style.MozOpacity) {
		topimage.style.MozOpacity = newStep / 100;
	}
	else {
		topimage.style.opacity = newStep / 100;	
	}

	if (newStep > 0) {
		setTimeout(function() {
			fadeTopImageOut(newStep);
		},30);
	}
	else {
		fadein = true;
		fadeTimout()
	}
}