/* Javascript adapted from: http://www.alistapart.com/articles/footers/ */
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;
}

function setFooter() {
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
			var contentHeight = document.getElementById('everything').offsetHeight;
			var footerElement = document.getElementById('footer');
			if (windowHeight >= contentHeight) {
				footerElement.style.position = 'absolute';
				footerElement.style.bottom = 0 + 'px';
			} else {
				footerElement.style.position = 'static';
			}
		}
	}
}


/* These are the functions for preloading the images */
totalWidth = 0;
loadStep = 15;
progress = 0;
loadbarWidth = 0;
percentage = 0;

function initPreload(newTotalWidth){
  totalWidth = parseInt(newTotalWidth);
  
  if(document.getElementById){
    progress   = document.getElementById('progress');
    percentage = document.getElementById('percentage'); 
  } else if(document.all){
    progress   = document.all.progress;
    percentage = document.all.percentage;
  } else if(document.layers){
    progress = seekLayer(document, 'progress');
    percentage = seekLayer(document, 'percentage');
  }  
}

function updateLoadbar(){
  if(parseInt(loadbarWidth) + loadStep < totalWidth){
    progress.style.width = parseInt(loadbarWidth) + loadStep + 'px';
    loadbarWidth = parseInt(loadbarWidth) + loadStep;
  } else{
    progress.style.width = totalWidth + 'px';
    loadbarWidth = totalWidth;
  }
  
  percentage.innerHTML = "Loading: " + parseInt( (loadbarWidth / totalWidth) * 100 ) + "%";
  if(parseInt( (loadbarWidth / totalWidth) * 100 ) == 100){
    percentage.innerHTML = "Loading complete";
  }
}

function preloadImages(){
  if(document.images){
    loadedImages = new Array();
    completedImages = new Array();
    for(var x=0; x < preloadImages.arguments.length; x++){
      loadedImages[x] = new Image();
      loadedImages[x].src = preloadImages.arguments[x];
      completedImages[x] = 0;
    }
    
    loadStep = Math.ceil(totalWidth / parseInt(preloadImages.arguments.length));
    checkImages();
  }
}

function checkImages(){
  for(var x=0; x < loadedImages.length; x++){
    if(!completedImages[x] && loadedImages[x].complete){
      completedImages[x] = 1;
      updateLoadbar();
    }
  }
  setTimeout(checkImages, 1000);
}

function preloadImages2(){
  if(document.images){
    loadedImages = new Array();
    
    for(var x=0; x < preloadImages2.arguments.length; x++){
      loadedImages[x] = new Image();
      loadedImages[x].src = preloadImages2.arguments[x];
    }
  }
}


// swaps the image 'imageID' (could be imageName) with 'swapImage'
// The ID and the NAME of the image must be the same!!
function swap(imageID, swapImage){
  // NS6 and IE5 (DOM1) or later
  if(document.getElementById){
    // If the image isn't locked, swap it
    document.getElementById(imageID).src = swapImage;
  // NS2 and IE3 (DOM 1) or later
  } else if(document.images){
    // If the image isn't locked, swap it
    document.images[imageID].src = swapImage;
  }
}

// centers the div element
function center(divElementId, contentWidth){
  if( parseInt(screen.availWidth) > contentWidth+20 ){
    var room = parseInt(screen.availWidth) - contentWidth;
    var roomSide = room / 2;
    var roomPercentage = ( roomSide / parseInt(screen.availWidth) ) * 100;
    var divElement;
    if(document.getElementById){
      divElement = document.getElementById(divElementId);
    } else if(document.all){
      divElement = document.all(divElementId);	
    } else if(document.layers){
      divElement = seekLayer(document, divElementId);
    }
    divElement.style.left = roomPercentage + "%";
  }
}

// Adjust the size of the content div
// It must always reach te bottom of the browserwindow
function adjustDivSize(){
  var myDiv;
  if(document.all){
    myDiv = document.all('content');
    if(myDiv.style.height < (parseInt(screen.availHeight) - 394)){
      myDiv.style.height = (parseInt(screen.availHeight) - 386) + "px";
    }
  }
  else if(document.getElementById){
    var myDiv = document.getElementById('content');
    var vw = document.defaultView;
    var currStyle = vw.getComputedStyle(myDiv, "");
    if(parseInt(currStyle.getPropertyValue("height")) < (parseInt(screen.availHeight) - 384)){
      myDiv.style.height = (parseInt(screen.availHeight) - 384) + "px";
    }
  }
  else if(document.layers){
    myDiv = seekLayer(document, 'content');
    if(myDiv.style.height < (parseInt(screen.availHeight) - 384)){
      myDiv.style.height = (parseInt(screen.availHeight) - 384) + "px";
    }
  }
}

// Adjust the size of the content div
// It must always reach te bottom of the browserwindow
function adjustBodySize(){
  var myDiv;
  if(document.all){
    myDiv = document.all('content');
    if(myDiv.style.height < (parseInt(screen.availHeight) - 394)){
      myDiv.style.height = (parseInt(screen.availHeight) - 386) + "px";
    }
  }
  else if(document.getElementById){
    var myDiv = document.getElementById('content');
    var vw = document.defaultView;
    var currStyle = vw.getComputedStyle(myDiv, "");
    if(parseInt(currStyle.getPropertyValue("height")) < (parseInt(screen.availHeight) - 384)){
      myDiv.style.height = (parseInt(screen.availHeight) - 384) + "px";
    }
  }
  else if(document.layers){
    myDiv = seekLayer(document, 'content');
    if(myDiv.style.height < (parseInt(screen.availHeight) - 384)){
      myDiv.style.height = (parseInt(screen.availHeight) - 384) + "px";
    }
  }
}
