var preloadFlag = false;
var activeTabId;
var closingSquares = ""; // A string that holds the names of the DHTML squares that are closing, seperated by '|'.
var openSquares = ""; // A string that holds the ids of the DHTML squares which are open, seperated by the '|' character.
var flyoutsOpen = ""; // A string that holds the ids of the flyouts which are open, seperated by the '|' character.
var squareMaxHeightWidth = 300;
var squareMinHeightWidth = 0;
var flyoutMaxWidth = 375;
var flyoutMinWidth = 0;
var animationSize = 20;
var animationTime = 5;

var tabData = new Array(6);
//Column Format: tab ID, div ID, image ID , active image, ,inactive image
tabData[0] = ['tabPla','tabPap','tabHol'];
tabData[1] = ['divPla','divPap','divHol'];
tabData[2] = ['market_img_Pla','market_img_Pap','market_img_Hol'];
tabData[3] = ['images/ajax_plastics-hi.jpg','images/ajax_paper-hi.jpg','images/ajax_holographic-hi.jpg'];
tabData[4] = ['images/ajax_plastics.jpg','images/ajax_paper.jpg','images/ajax_holographic.jpg'];
//tabData[5] = ['#C70BAC', '#009DD9', '#FFCE00', '#EA0437', '#87D300'];
	
/*  Functions which were generated by Adobe GoLive */	
function preloadImages() {
	if (document.images) {
		pre_market_img_Pla = newImage('images/ajax_plastics.jpg');
		pre_market_img_Pap = newImage('images/ajax_paper.jpg');
		pre_market_img_Hol = newImage('images/ajax_holographic.jpg');
		
		preloadFlag = true;
	}
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImagesArray(array) {
	//if (preloadFlag == true) {
		var d = document; var img;
		for (i=0;i<array.length;i+=2) {
			img = null; var n = array[i];
			if (d.images) {
				if (d.layers) {img = findElement(n,0);}
				else {img = d.images[n];}
			}
			if (!img && d.getElementById) {img = d.getElementById(n);}
			if (!img && d.getElementsByName) {
				var elms = d.getElementsByName(n);
				if (elms) {
					for (j=0;j<elms.length;j++) {
						if (elms[j].src) {img = elms[j]; break;}
					}
				}
			}
			if (img) {img.src = array[i+1];}
		}
	//}
}
function changeImages() {
	changeImagesArray(changeImages.arguments);
}
	
function switchTab(newActiveTabId) {
	//debugger;
	var j;
   	for (j = 0; j < tabData[0].length; j ++) {
        if (tabData[0][j] == newActiveTabId) {
			document.getElementById(tabData[2][j]).src = tabData[3][j];
			document.getElementById(tabData[1][j]).style.display = 'block';
			//document.getElementById('tdTabBase').style.backgroundColor = tabData[5][j];
			activeTabId = tabData[0][j];
        } else {
           	document.getElementById(tabData[2][j]).src = tabData[4][j];
            document.getElementById(tabData[1][j]).style.display = 'none';
        }
    }
}
	
function hoverImages(imgId) {
	var j;
	var tab;
	var img;
	//debugger;
	for (j = 0 ; j < tabData[2].length ; j ++) {
		if (tabData[2][j] == imgId) {
			tab = document.getElementById(tabData[0][j]);
			if (tab.id != activeTabId) {
				img = document.getElementById(imgId);
				if ( 'images/' + img.src.substring(img.src.lastIndexOf('/') + 1, img.src.length) == tabData[3][j]) {
					img.src = tabData[4][j];
				} else {
					img.src = tabData[3][j];
				}
				//break;
			}
		}
	}
}

/* Randomize the image for the banner background */
function setBannerUrl() {
	var i;
	var banners = new Array(8);
	banners[0] = "http://www.scholle.com/images/Scholle_site_header_v2.jpg"
	banners[1] = "http://www.scholle.com/images/Scholle_site_header_v3.jpg"
	banners[2] = "http://www.scholle.com/images/Scholle_site_header_v4.jpg"
	banners[3] = "http://www.scholle.com/images/Scholle_site_header_v5.jpg"
	banners[4] = "http://www.scholle.com/images/Scholle_site_header_v6.jpg"
	banners[5] = "http://www.scholle.com/images/Scholle_site_header_v7.jpg"
	banners[6] = "http://www.scholle.com/images/Scholle_site_header_v8.jpg"
	banners[7] = "http://www.scholle.com/images/Scholle_site_header_v9.jpg"
	//banners[8] = "images/Scholle_site_header_v10.jpg"
	
	i = Math.floor(Math.random() * banners.length);
		
	document.getElementById('tdHeader').style.backgroundImage = "url('" + banners[i] + "')";
	document.getElementById('tdHeader').style.backgroundRepeat = "no-repeat";
}

/* Randomize the image for the banner background */
function setArrowUrl() {
	var i;
	var arrows = new Array(9);
	arrows[0] = "/images/home_arrow.gif";
	arrows[1] = "/images/home_arrow_blue.gif";
	arrows[2] = "/images/home_arrow_bluegreen.gif";
	arrows[3] = "/images/home_arrow_darkgrey.gif";
	arrows[4] = "/images/home_arrow_green.gif";
	arrows[5] = "/images/home_arrow_greygreen.gif";
	arrows[6] = "/images/home_arrow_wine.gif";
	arrows[7] = "/images/home_arrow_brightgreen.gif";
	arrows[8] = "/images/home_arrow_yellow.gif";
	
	i = Math.floor(Math.random() * arrows.length);
		
	document.getElementById('arrow').src = arrows[i];
}

/* Functions to drive the sidebar flyouts */
	
function flyoutMenuClose(divId) {
	var div = document.getElementById(divId);
	var width;
	
	if (div.style.width == "") {
		div.style.width = flyoutMinWidth + 'px';
	}
	width = div.style.width.replace("px", "");
		
	if (width > flyoutMinWidth) {
		width = (parseInt(width) - animationSize);
		if (width < flyoutMinWidth)  {
			width  = flyoutMinWidth;
		}
		div.style.width = width + 'px'
		if (width < flyoutMaxWidth) {
			document.getElementById(divId + 'Inner').style.display = 'none'
			if (width == flyoutMinWidth) {
				flyoutsOpen = flyoutsOpen.replace(divId + "|", "");
			}
		}
		setTimeout("flyoutMenuClose('" + divId + "')", animationTime);
	}
}
	
function flyoutMenuOpen(divId) {
	//debugger;
	var div = document.getElementById(divId);
	var width;
	
	// Close any of the DHTML squares which may be open.
	if (openSquares.length > 0) {
		var openSquaresArray = openSquares.split("|");
		for (var i = 0 ; i < openSquaresArray.length ; i ++ ) {
			var square = openSquaresArray[i].replace("|", "");
			if (square.length > 0) {
				closeDhtmlSquare(square);
			}
		}
	}
	
	// Make sure the width property has a value.
	if (div.style.width == "") {
		div.style.width =  flyoutMinWidth + 'px';
	}
	// Get the numeric value of the width.
	width = div.style.width.replace("px", "");
		
	if (width < flyoutMaxWidth) {
		width = (parseInt(width) + animationSize);
		if (width > flyoutMaxWidth) {
			width = flyoutMaxWidth;
		}
		div.style.width = width + 'px'
		if (width == flyoutMaxWidth) {
			document.getElementById(divId + 'Inner').style.display = 'block'
			flyoutsOpen += divId + '|';
		}
		setTimeout("flyoutMenuOpen('" + divId + "')", animationTime);
	}
}
	
function flyoutClick(divId) {
	var div = document.getElementById(divId);
	var width;
		
	if (div.style.width == "") {
		div.style.width = '0px';
	}
	width = div.style.width.replace("px", "");
	(width < flyoutMaxWidth) ? flyoutMenuOpen(divId) : flyoutMenuClose(divId);
}

/* function flyoutMouseOver(divId, imgId, imgSrc) {
	//debugger;
	changeImages(imgId, imgSrc);
	flyoutMenuOpen(divId);
	return true;
}

function flyoutMouseOut(divId, imgId, imgSrc) {
	//debugger;
	var div = document.getElementById(divId);
	if (window.event != null) {
		//debugger;
		if (!isMouseOverChildLayer(div, window.event)) {
				changeImages(imgId, imgSrc);
			flyoutMenuClose(divId);
			return true;
		}
	}
} */

function dhtmlSquareClick(divId) {
	var div = document.getElementById(divId);
	var width;
		
	if (div.style.width == "") {
		div.style.width = '0px';
	}
	width = div.style.width.replace("px", "");
	(width < squareMaxHeightWidth) ? openDhtmlSquare(divId) : closeDhtmlSquare(divId);
}

/* Functions to drive the DHTML squares */	
function openDhtmlSquare(divId) {
	//debugger;
	var div = document.getElementById(divId);
	var openSquaresArray = openSquares.split("|")
	var width;
	var height;
	var left;
	var top;

	//If one of the flyouts are open, make sure we close this square
	if (flyoutsOpen.length > 0) {
		setTimeout("closeDhtmlSquare('" + divId + "')", animationTime);
	}
	// If the element is not in the process of closing, open it.
	else if (closingSquares.indexOf(divId + '|') < 0) {
		
		// Close any other open square.
		for (var i = 0 ; i < openSquaresArray.length ; i ++ ) {
			var id = openSquaresArray[i].replace("|", "");
			if (id.length > 0) {
				closeDhtmlSquare(id);
			}
		}
		
		// Make sure all of the properties have values.
		if (div.style.width == "") {
			div.style.width = squareMinHeightWidth + 'px';
		}
		if (div.style.height == "") {
			div.style.height = squareMinHeightWidth + 'px';
		}
		if (div.style.left == "") {
			div.style.left = '50px';
		}
		if (div.style.top == "") {
			div.style.top = '50px';
		}
	
		// Get the numeric value of the properties.
		width = div.style.width.replace("px", "");
		height = div.style.height.replace("px", "");
		left = div.style.left.replace("px", "");
		top = div.style.top.replace("px", "");				
		
		// If the element is less than the maximum size increase the width and height by 20
		// and move the elment down and to the left by 10 to keep it centered.
		if (width < squareMaxHeightWidth) {
			width = (parseInt(width) + animationSize);
			left = (parseInt(left) - 10);
		} else {
			width = squareMaxHeightWidth;
			left = 0;
		}
		if (height < squareMaxHeightWidth) {
			height = (parseInt(height) + animationSize);
			top = (parseInt(top) - 10);
		} else {
			height = squareMaxHeightWidth;
			top = 0;
		}
	
		// Set the properties.
		div.style.width = width + 'px';
		div.style.height = height + 'px';
		div.style.top = top + 'px';
		div.style.left = left + 'px';
		
		// If the element is still smaller than the maximum size, call self in 5 miliseconds.
		if (width < squareMaxHeightWidth ||  height < squareMaxHeightWidth) {
			setTimeout("openDhtmlSquare('" + divId + "')", animationTime);
		} else {
			div.style.border = "1px solid gray";
			// Mark the element as open;
			openSquares += divId + "|";
		}
	}
}

function closeDhtmlSquare(divId) {
	//debugger;
	var div = document.getElementById(divId);
	var width;
	var height;
	var left;
	var top;
	var proceed = false;

	/*
		Get the event object so we can see if the mouseout is leaving the square into one of its child layers.
		if there is no event, that means this function is being called by setTimeout, and is the continuation
		of the animation.  If the mouse is not over one of the child layers or this is the continuation of a previously
		started animation continue, otherwise dont close the square.
	*/
	if (window.event == null) {
		proceed = true;
	} else {
		//debugger;
		if (!isMouseOverChildLayer(div, window.event)) {
			proceed = true;
		}
	}
	
	if (proceed) {
		// Mark the element as closing
		if (closingSquares.indexOf(divId + '|') < 0) {
			closingSquares += divId + "|";
		}
		if (openSquares.indexOf(divId + '|') >= 0) {
			openSquares = openSquares.replace(divId + '|', '');
		}
		
		//Make sure all of the properties have values
		if (div.style.width == "") {
			div.style.width = squareMaxHeightWidth + 'px';
		}
		if (div.style.height == "") {
			div.style.height = squareMaxHeightWidth + 'px';
		}
		if (div.style.left == "") {
			div.style.left = '0px';
		}
		if (div.style.top == "") {
			div.style.top = '0px';
		}
		
		// Get the numeric values of the properties
		width = div.style.width.replace("px", "");
		height = div.style.height.replace("px", "");
		left = div.style.left.replace("px", "");
		top = div.style.top.replace("px", "");				
		
		//Shrink the element by 20px and move it to the left by 10px so that it stays centered
		if (width > squareMinHeightWidth) {
			width = (parseInt(width) - animationSize);
			left = (parseInt(left) + 10);
		} else {
			width = squareMinHeightWidth;
			left = 50;
		}
		if (height > squareMinHeightWidth) {
			height = (parseInt(height) - animationSize);
			top = (parseInt(top) + 10);
		} else {
			height = squareMinHeightWidth;
			top = 50;
		}
	
		// Set the properties;
		div.style.width = width + 'px';
		div.style.height = height + 'px';
		div.style.top = top + 'px';
		div.style.left = left + 'px';
	
		// If the element's height and witdth are still greater than 0px recurse.
		if (width > squareMinHeightWidth ||  height > squareMinHeightWidth) {			
			setTimeout("closeDhtmlSquare('" + divId + "')", animationTime);
		} else {
			div.style.border = "0px none white";
			// Remove the element from the list of closing squares.
			closingSquares = closingSquares.replace(divId + "|", "");
		}
	}
}
function isMouseOverChildLayer(div, e) {
	var src = div
	var dest = e.toElement;
	
	return isChildLayer(src, dest);
}

function isChildLayer(elem1, elem2) {
	
	for ( var i = 0 ; i < elem1.children.length ; i ++ ) {
		var obj = elem1.children[i];
		
		if (obj.children) {
			if (obj.children.length > 0) {
				if (isChildLayer(obj, elem2)) {
					return true;
				}
			}
		}
		
		if (elem1.children[i] == elem2) {
			return true;
		}
	}
	return false;
}