document.init=new LinkedList(1);
document.reposition = new LinkedList(2);

document.init.add(initBackgroundImage);

function reposition() {document.reposition.run();};

var oldIe = /MSIE (\d+\.\d+);/.test(navigator.userAgent) && new Number(RegExp.$1) < 7;

function bgInit() {

	//document.body.parentNode.style.overflow='hidden';
	document.init.run();
	
};

function initBackgroundImage() {

  var img=document.getElementById('backgroundImage');

  img.nativeWidth = img.width;
  img.nativeHeight = img.height;

  resizeBackgroundImage();

  document.getElementById('backgroundImageWrapper').style.visibility = 'visible';
  document.reposition.add(resizeBackgroundImage);
  
}

function resizeBackgroundImage() {

	var imBG=document.getElementById('backgroundImage');
	var backgr=document.getElementById('backgroundImageWrapper');
	
	var imgWidth = imBG.nativeWidth;
	var imgHeight = imBG.nativeHeight;	

	// Get window width 
	var windowWidth = parseInt(document.body.clientWidth);

	// If IE and older than IE7
	if(oldIe) {
	
		var windowHeight = parseInt(document.getElementById('mainWrapper').offsetHeight) + 20;
	
	} else { // Not IE or better than IE6

		var windowHeight = parseInt(getHeight());
  
	}
	
	// If the images dimension ration is larger or equal to the windows ratio
	if ((imgWidth/imgHeight)>=(windowWidth/windowHeight)) {
		
		// Recalculate image width
		imgWidth = Math.floor((imgWidth*windowHeight) / imgHeight);
		
		// Set the height of the image to that of the window
		imgHeight = windowHeight;
		
		if(oldIe) {
		
			// Calculate the "negative left indent" the image
			imBG.style.marginLeft = Math.floor((windowWidth - imgWidth)) +'px';
		
		} else {

			// Calculate the "negative left indent" the image
			imBG.style.marginLeft = Math.floor((windowWidth - imgWidth) / 2.0) +'px';		
		
		}
		
		// The image should start at the top of the window
		imBG.style.top = '0px';
		
		
	} else { // Image dimension ratio smaller than windows ratio
	
		// Recalculate img height
		imgHeight = Math.floor((imgHeight*windowWidth) / imgWidth);
		
		// Set the width of the image to that of the window
		imgWidth = windowWidth;
		
		// The image should have no left indent
		imBG.style.marginLeft = '0px';
		
		// Calculate the "negative top indent" of the image
		imBG.style.top = Math.floor((windowHeight - imgHeight) / 2.0) +'px';
	}
	
	imBG.width = imgWidth;
	imBG.height = imgHeight;	
	
}

function getHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  }else if( document.documentElement && (document.documentElement.clientHeight ) ) {
  //IE 6+ in 'standards compliant mode'
  myHeight = document.documentElement.clientHeight;
  } else if( document.body && (document.body.clientHeight ) ) {
  //IE 4 compatible
  myHeight = document.body.clientHeight;
  }
  return myHeight;  
};

function LinkedList(action) {
	this.action = action;
	this.head = null;
	this.tail = null;
	this.count = 0;

	this.run = execute;
	this.add = addLinkedListElement;
	this.remove = removeLinkedListElement;

	this.list = [];
	this.list.add = addLinkedListElementObsoleteInterface;
	this.list.parent = this;
};

function linkedListElement(ref) {
	this.ref=ref;
	this.next=null;
	this.prev=null;
};

function removeLinkedListElement(elem) {
	if (elem.prev) elem.prev.next = elem.next;
	if (elem.next) elem.next.prev = elem.prev;
	elem = null;
};

function addLinkedListElement(ref) {
	var newElem = new linkedListElement(ref);
	if (!this.head) {
		this.tail = newElem;
		this.head = newElem;
	} else {
		newElem.prev = this.tail; 
		this.tail = newElem;
		newElem.prev.next = newElem;
	};
	this.count++;
	return newElem;
};

function addLinkedListElementObsoleteInterface(elem) {
	return this.parent.add(elem);
};

function execute() {
	// Execute list of functions or method on objects as defined by action parameter
	var elem = this.head;

	while (elem) {
		switch (typeof(elem.ref)) {
		case 'function': elem.ref(); break;
		case 'string': eval(elem.ref); break;
		default:
			switch (this.action) {
				case 1: elem.ref.init(); break;
				case 2: elem.ref.reposition(); break;
			};
		};
		elem = elem.next;
	};
};

//window.onload=init;
window.onresize=reposition;



