// JavaScript Document

var fcounter= 1;
var imageId = 'photo' + fcounter;
var image = document.getElementById(imageId);
var gCounter = 1;
var gMaxPhotos = 26;
var gPath = "/Image/homefotos/homefoto";
var ran_unrounded=Math.random()*(gMaxPhotos-1);
var gCounter=1+Math.round(ran_unrounded); //Start with a random picture

function initImage() {
	setFooter();
	window.status = gCounter;
	//gCounter = gCounter - 1;
	
	var fcounter= 2;
	var imageId = 'photo' + fcounter;
	var image = document.getElementById(imageId);
	setOpacity(image, 0);
	
	imageURL = gPath +gCounter+'.jpg';
	newImg = new Image();
	newImg.src = imageURL;
	newname = 'backgr' + fcounter
	document[newname].src = newImg.src;
	
	
	gCounter = gCounter + 1;
	if (gCounter>gMaxPhotos) {gCounter = 1;}
	var fcounter= 1;
	imageURL = gPath +gCounter+'.jpg';
	newImg = new Image();
	newImg.src = imageURL;
	newname = 'backgr' + fcounter
	document[newname].src = newImg.src;
	
	gCounter = gCounter + 2;
	if (gCounter>gMaxPhotos) {gCounter = 1;}
	var imageId = 'photo' + fcounter;
	var image = document.getElementById(imageId);
	setOpacity(image, 0);
	imageURL = gPath +gCounter+'.jpg';
	newImg = new Image();
	newImg.src = imageURL;
	newname = 'photo' + fcounter
	document[newname].src = newImg.src;
	
	
	
	window.setTimeout("fadeIn('"+imageId+"',0)", 1500); //go and start fading
}
function fadeIn(objId,opacity) {
	if (document.getElementById) {
		var imageId = 'photo' + fcounter;
		objId = imageId
		obj = document.getElementById(imageId);
		if (opacity <= 100) {
			opa = (2-(opacity/100))*opacity; //Fading out
			if (opacity<40) {opa = opacity * opacity / 25;} //Fading in
			setOpacity(obj,opa);
			opacity += 1; //fade speed
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 50);
    		//window.status = opa;
		} else {
			//Fading is done
			setOpacity(obj, 100);
			// Set background the same url  as foreground
			imageURL = gPath +gCounter+'.jpg';
			newImg = new Image();
			newImg.src = imageURL;
			newname = 'backgr' + fcounter
			document[newname].src = newImg.src;
			window.setTimeout("nextImage('"+objId+"')",500); //wait before updating - stupid IE6!
		}
	}
}

function nextImage(objId) {
	fcounter = fcounter + 1;
	if (fcounter>2) {fcounter = 1;}
	
	obj = document.getElementById(objId);
	imageId = 'photo' + fcounter;
	objId = imageId
	obj = document.getElementById(imageId);
	setOpacity(obj, 0);
	gCounter = gCounter + 1;
	if (gCounter>gMaxPhotos) {gCounter = 1;}
	imageURL = gPath +gCounter+'.jpg';
	newImg = new Image();
	newImg.src = imageURL;
	newname = 'photo' + fcounter
	document[newname].src = newImg.src;
	//document['photo1'].src = newImg.src;		
	
	
	window.status = fcounter;
	var imageId = 'photo' + fcounter;
	window.setTimeout("fadeIn('"+imageId+"',0)", 100); //wait 5 seconds before fading
}




function fadeOut(objId,opacity) {
    window.status = opacity;
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity >= 0) {
			setOpacity(obj, opacity);
			opacity -= 10; //fade speed
			window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 50);
		} else {
		  //Fading is done
		  //obj.style.display = "none";  // Make the layer dissapear
		  setOpacity(obj, 0);
		  window.setTimeout("fadeIn('"+imageId+"',0)", 5000); //wait 2 seconds before fading in again
		}
	}
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}
window.onload = function() {initImage()}







function setFooter() {
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		
		if (windowHeight > 0) {
			var wrapperHeight = document.getElementById('wrapper').offsetHeight;
			var topperHeight = 220 //document.getElementById('topper').offsetHeight;
			var maincontentHeight = document.getElementById('maincontent').offsetHeight;
			var mainMenuHeight = document.getElementById('menu').offsetHeight;
			//alert(wrapperHeight);
			if (wrapperHeight<windowHeight) {
				document.getElementById('wrapper').style.height = windowHeight + 'px';
			}
			if (maincontentHeight+topperHeight > wrapperHeight) {
				document.getElementById('wrapper').style.height = maincontentHeight+topperHeight + 'px';
			}
			var wrapperHeight = document.getElementById('wrapper').offsetHeight;
			if (mainMenuHeight+topperHeight > wrapperHeight) {				document.getElementById('wrapper').style.height = mainMenuHeight+topperHeight + 'px';
			}
			//alert(wrapperHeight + ", " + (mainMenuHeight+topperHeight));
						
			}
		}
	}

function getWindowHeight() {
			var windowHeight = 0;
			if (typeof(window.innerHeight) == 'number') {
				windowHeight = window.innerHeight;
			}
			else {
				if (document.documentElement && document.documentElement.clientHeight) {
					windowHeight = document.documentElement.clientHeight;
				}
				else {
					if (document.body && document.body.clientHeight) {
						windowHeight = document.body.clientHeight;
					}
				}
			}
			return windowHeight;
		}
		
		window.onresize = function() {
			setFooter();
		}
