

function stretcher() {
	
	window.undefined = window.undefined;
	
	/**
	* Wrapper function fo the getH-function in mainElement
	*/
	this.getH = function(elementId) {
		
		var h = 0;
		
		if(document.getElementById(elementId)) {	
			h = mainElm.get(elementId).getH();
			(h == undefined) ? h = 0 : "";
		}
		
		return h;
	}	
	
	/**
	* Wrapper function fo the getW-function in mainElement
	*/
	this.getW = function(elementId) {
		
		var w = 0;
		
		if(document.getElementById(elementId)) {	
			w = mainElm.get(elementId).getW();
			(w == undefined) ? w = 0 : "";
		}
		
		return w;
	}	
	
	/**
	* Get the prefix to use for elements. This is based on the id of the wrapper.
	*/
	this.getElementPrefix = function () {
		
		var prefix = false;
		
		if(document.getElementById("frontPage2Columns1")) {
			prefix = "frontPage2Columns1";
		} else if(document.getElementById("frontPage3Columns1")) {
			prefix = "frontPage3Columns1";
		} else if(document.getElementById("frontPage3Columns2")) {
			prefix = "frontPage3Columns2";
		} else if(document.getElementById("frontPage3Columns3")) {
			prefix = "frontPage3Columns3";
		} else if(document.getElementById("contentPage2Columns1")) {
			prefix = "contentPage2Columns1";
		} else if(document.getElementById("contentPage3Columns1")) {
			prefix = "contentPage3Columns1";
		} else if(document.getElementById("imagePage2Columns1")) {
			prefix = "imagePage2Columns1";
		} else if(document.getElementById("imagePage3Columns1")) {
			prefix = "imagePage3Columns1";
		} else if(document.getElementById("imagePagePanorama3Columns1")) {
			prefix = "imagePagePanorama3Columns1";
		}
		
		return prefix;
	}
	
	/**
	* Stretches the vertical lines inside the columns so that their heights matches the columns
	*/
	this.stretchVerticalLines = function() {
		
		var prefix = this.getElementPrefix();
		
		if(prefix != false) {
			
			var verticalLines = new Array(prefix + "VerticalLine1", prefix + "VerticalLine2");
			
			var newHeight = 0;
		
			if(document.getElementById(prefix + "Column1Header")) {
				newHeight += this.getH(prefix + "Column1Header");
			}
			
			if(document.getElementById(prefix + "Column1")) {
				newHeight += this.getH(prefix + "Column1");
			}
			
			for(var i = 0; i < verticalLines.length; i++) {
				
				if(document.getElementById(verticalLines[i])) {
						
					var vertLineObj = mainElm.get(verticalLines[i]);
					
					if(this.getH(vertLineObj.id) != 0) {
						
						vertLineObj.setH(newHeight + "px");
						
					}
					
				}
			
			}
		
		}
		
	}
	
	/**
	* Sets the wrappers width based on its content.
	* The wrapper needs a width to be positioned correctly.
	**/
	this.setWrapperWidth = function() {
		
		var frameTopWidth = this.getW("frameTop");
		var frameBottomWidth = this.getW("frameBottom");
		var vertFramesAndContentWidth = this.getW("frameLeft") + this.getW("frameRight") + this.getW("wrapperInsideFrames");
		
		var maxWidth = frameTopWidth;
		var wrapperInsideFramesMarginLeft = 0;
		
		if(frameBottomWidth > frameTopWidth) {
			maxWidth = frameBottomWidth;
		}
		
		if(vertFramesAndContentWidth > frameBottomWidth) {
			maxWidth = vertFramesAndContentWidth;
		}
		
		// If the top- or bottom frame is the widest, we want to position the wrapperInsideFrames in the middle
		if((maxWidth == frameTopWidth || maxWidth == frameBottomWidth) && maxWidth != vertFramesAndContentWidth) {
			wrapperInsideFramesMarginLeft = Math.floor((maxWidth - vertFramesAndContentWidth) / 2);
		}
		
		mainElm.get("wrapper").setW(maxWidth + "px");
		mainElm.get("wrapperInsideFrames").obj.style.marginLeft = wrapperInsideFramesMarginLeft + "px";
		
	}
	
	/**
	* Stretches the inner columns
	**/
	this.stretchColumns = function () {
		
		var newColumn1Height = 0;
		var newColumn2Height = 0;
		var newColumn3Height = 0;
		var maxHeight = 0;
		var prefix = "";
		var column1HeaderSuffix = "Column1Header";
		var column2HeaderSuffix = "Column2Header";
		var column3HeaderSuffix = "Column3Header";
		var column3HeaderHeight = 0;
		
		prefix = this.getElementPrefix();
		
		// Setting all column heights to auto
		mainElm.get(prefix + "Column1").obj ? mainElm.get(prefix + "Column1").obj.style.height = "auto" : "";
		mainElm.get(prefix + "Column2").obj ? mainElm.get(prefix + "Column2").obj.style.height = "auto" : "";
		mainElm.get(prefix + "Column3").obj ? mainElm.get(prefix + "Column3").obj.style.height = "auto" : "";
		
		// The height of the wrapper will be the same as the height of the highest column.
		maxHeight = this.getH("columnsWrapper");	
		
		if(prefix == "frontPage3Columns1") {
			column2HeaderSuffix = "Column1Header";
		} else if(prefix == "frontPage3Columns2") {
			column3HeaderSuffix = "Column2Header";
		}
		
		newColumn1Height = maxHeight - this.getH(prefix + column1HeaderSuffix) + "px";
		mainElm.get(prefix + "Column1").setH(newColumn1Height);
		
		newColumn2Height = maxHeight - this.getH(prefix + column2HeaderSuffix) + "px";
		mainElm.get(prefix + "Column2").setH(newColumn2Height);
		
		// If a third column exists
		if(document.getElementById(prefix + "Column3")) {
			
			if(document.getElementById(prefix + column3HeaderSuffix)) {
				column3HeaderHeight = this.getH(prefix + column3HeaderSuffix)
			}
			
			newColumn3Height = maxHeight - column3HeaderHeight + "px";
			mainElm.get(prefix + "Column3").setH(newColumn3Height);
		}
		
		return true;
		
	}
	 
	/**
	* Makes sure that side frame divs are as high as the wrapper div.
	* Note; this function must be called after all calls to the equalizeChildColumns-function
	* has been made.
	*/
	this.stretchFrames = function() {	
		
		var wrapperHeight = this.getH("wrapper");
		var frameTopHeight = this.getH("frameTop");
		var frameLeftWidth = this.getW("frameLeft");
		var frameRightWidth = this.getW("frameRight");
		var frameLeftTopHeight = this.getH("frameLeftTop");
		var frameLeftBottomHeight = this.getH("frameLeftBottom");
		var frameRightTopHeight = this.getH("frameRightTop");
		var frameRightBottomHeight = this.getH("frameRightBottom");
		var frameBottomHeight = this.getH("frameBottom");
		var newFrameLeftMiddleHeight = (wrapperHeight - frameLeftTopHeight - frameLeftBottomHeight - frameTopHeight - frameBottomHeight) + "px";
		var newFrameRightMiddleHeight = (wrapperHeight - frameRightTopHeight - frameRightBottomHeight - frameTopHeight - frameBottomHeight) + "px";
		
		mainElm.get("frameLeftMiddle").setH(newFrameLeftMiddleHeight);
		mainElm.get("frameRightMiddle").setH(newFrameRightMiddleHeight);
		
		// setting width
		var regEx = new RegExp("(MSIE 5\.5)|(MSIE 5)","gi");
		
		var browserOK = regEx.test(navigator.userAgent);
		
		// only force width for old IE
		if(browserOK) {
			mainElm.get("frameLeftTop").setW(frameLeftWidth);
			mainElm.get("frameLeftMiddle").setW(frameLeftWidth);
			mainElm.get("frameLeftBottom").setW(frameLeftWidth);
			mainElm.get("frameRightTop").setW(frameRightWidth);
			mainElm.get("frameRightMiddle").setW(frameRightWidth);
			mainElm.get("frameRightBottom").setW(frameRightWidth);
		}
		
		return false;
	}

}