// Image Rotation Settings

bgImagePool = [

["images/bg/007.jpg",
"left"],

["images/bg/008.jpg",
"right"],

["images/bg/002.jpg",
"right"],

["images/bg/003.jpg",
"right"],

["images/bg/004.jpg",
"left"],

["images/bg/005.jpg",
"left"],

["images/bg/006.jpg",
"right"]
];

bgImagesTimeout = 5000;
bgImagesFade = 75;

bgImageCount = 3;

bgImages = getRandomImages(bgImagePool, bgImageCount);

function getRandomImages(imgSourceArray, imgCount)
{
	var imgArray = new Array();
	var indexArray = new Array();
	
	while (imgArray.length < imgCount)
	{
		var randomImageIndex = Math.floor(Math.random()*imgSourceArray.length);
		var found = false;
		
		for (var i=0; i<indexArray.length; i++)
		{
			if (indexArray[i] == randomImageIndex)
			{
				found = true;
			}
		}
		
		if (!found)
		{
			indexArray.push(randomImageIndex);
			imgArray.push(imgSourceArray[randomImageIndex]);
		}
	}
	
	return imgArray;
		
}

Math.round(Math.random()*bgImagePool.length);

function rotateImages()
{
	var theImageDiv = document.getElementById("imgcontent");
	var theImageGraphic = document.getElementById("maingraphic");

	if (theImageDiv && theImageGraphic)
	{
		theImageGraphic.style.height = "265px";
		theImageGraphic.style.margin = "0px";

		theImageGraphic.currentOpacity = 0;
		
		theImageGraphic.parentDiv = theImageDiv;
		theImageGraphic.theIndex = 0;

		theImageGraphic.fadeIn = function() {
			this.currentOpacity = this.currentOpacity + .25;
			if (this.currentOpacity <= 10)
			{
				this.setOpacity(this.currentOpacity);
				setTimeout(function() { theImageGraphic.fadeIn(); } , bgImagesFade);
			}
			else 
			{
				this.updateBackgroundImage();
				this.currentOpacity = 0;
				this.setOpacity(this.currentOpacity);
				this.updateIndex();
				this.updateImage();
				setTimeout(function() { theImageGraphic.fadeIn(); }, bgImagesTimeout);
			}
		}

		theImageGraphic.setOpacity = function(theValue) {
			this.style.opacity = theValue/10;
			this.style.filter = 'alpha(opacity=' + theValue*10 + ')';
		}

		theImageGraphic.updateIndex = function() {
			this.theIndex = this.theIndex + 1;

			if (this.theIndex >= bgImages.length)
			{
				this.theIndex = 0;
			}			
		}

		theImageGraphic.updateImage = function() {
			this.style.background = "url(" + bgImages[this.theIndex][0] + ")";
			this.style.backgroundPosition = "top " + bgImages[this.theIndex][1];
		}

		theImageGraphic.updateBackgroundImage = function () {
			this.parentDiv.style.backgroundImage = "url(" + bgImages[this.theIndex][0] + ")";
			this.parentDiv.style.backgroundPosition = "top " + bgImages[this.theIndex][1];
		}

		theImageGraphic.setOpacity(theImageGraphic.currentOpacity);
		theImageGraphic.updateBackgroundImage();
		theImageGraphic.updateIndex();
		theImageGraphic.updateImage();
		setTimeout(function() { theImageGraphic.fadeIn(); } , bgImagesTimeout);

	}
}
	
function init()
{
	// Create Flash object
	var mainGraphic = document.getElementById("maingraphic");
	var flashContent = document.getElementById("flashcontent");
	if (mainGraphic && flashContent)
	{
		mainGraphic.style.display = "none";
		var randomnumber = Math.floor(Math.random()*5) + 1;
		var so = new SWFObject("flash/cashome" + randomnumber + ".swf", "thinkAgain", "585", "265", "8", "#FFFFFF");
		so.write("flashcontent");
	}
	
	// Set up Multi level navigation
	setNav();

	// Show County Dropdown	
	setCountyDropdown()

	
	// Resize columns to fit
	resizeColumns();
	

	// Rotate the front page images
	rotateImages();

	// Set External Links
	setExternalLinks()
}

function initTextResizeDetector()  {
   var iBase = TextResizeDetector.addEventListener(resizeColumns,null);
}

//id of element to check for and insert control
TextResizeDetector.TARGET_ELEMENT_ID = 'inner';

//function to call once TextResizeDetector has init'd
TextResizeDetector.USER_INIT_FUNC = initTextResizeDetector;

function resizeColumns()
{
	// Magic Number: Total vertical (top + bottom) padding of leftnav
	// If we change the CSS, we have to change it here, too
	var theLeftNavPadding = 142;

	var theLeft = document.getElementById("leftcontent");
	var theLeftNav = document.getElementById("leftnav");
	var theRight = document.getElementById("rightcontent");
	var theContact = document.getElementById("contact");
	var theAdminLinks = document.getElementById("adminlinks");
	var theGraphic = document.getElementById("imgcontent");
	var therightitems = document.getElementById("rightitems");
	var theArticle = document.getElementById("articles");

	if (! theGraphic)
	{
		theGraphic = document.createElement("div");
	}

	if (! theAdminLinks)
	{
		theAdminLinks = document.createElement("div");
	}

	// Setting the heights for all pages
	if (theLeft && theRight && theLeftNav && theContact)
	{
		if (theLeft.offsetHeight && theRight.offsetHeight && 
			theLeftNav.offsetHeight && theContact.offsetHeight)
		{
			// Initialize all heights to "auto" to deal with font size changes
			theLeft.style.height = "auto";
			theRight.style.height = "auto";
			theLeftNav.style.height = "auto";

			if (theArticle)
			{
				theArticle.style.height = "auto";	
			}
		
			// Calculate max height of leftcontent and rightcontent
			var maxHeight=Math.max(theLeft.offsetHeight,theRight.offsetHeight);
						
			// Set both heights to the same (max) value
			theLeft.style.height = maxHeight + "px";
			theRight.style.height = maxHeight + "px";

			// Set the leftnav height to the leftcontent, minus the contact, minus the padding
			theLeftNav.style.height = (theLeft.offsetHeight - theContact.offsetHeight - theLeftNavPadding) + "px";

			// Setting the height for the additional elements on the front page					

			if (theAdminLinks && theGraphic && therightitems)
			{
				if (theAdminLinks.offsetHeight || theGraphic.offsetHeight || 
					therightitems.offsetHeight)
				{
					// Initialize all heights to "auto" to deal with font size changes
					therightitems.style.height = "auto";
			
					// Total height of all elements inside 
					var totalHeight = theAdminLinks.offsetHeight + theGraphic.offsetHeight + therightitems.offsetHeight;

					// Here's a fudge factor to get the gray "rightitems" div to the right height.  Sorry.
					var rightFudgeFactor = 7;
					
					if (theAdminLinks.offsetHeight == 0)
					{
						rightFudgeFactor++;
					}

					if (theGraphic.offsetHeight == 0)
					{
						rightFudgeFactor++;
					}

					
					// If it's IE, make it a -2.
					if (document.all)
					{
						rightFudgeFactor = 0;
					}
					
					// Calculate height difference
					var theHeightDiff = maxHeight - totalHeight - rightFudgeFactor;							
					
					// If it's greater than zero, set a new height.
					if (theHeightDiff > 0)
					{
						var newHeight = therightitems.offsetHeight + theHeightDiff;
						therightitems.style.height = newHeight + "px";
					}

				}
			}
		
		}
		else if (theLeft.pixelHeight && theRight.pixelHeight && 
			theLeftNav.pixelHeight && theContact.pixelHeight)
		{
			// Initialize all heights to "auto" to deal with font size changes
			theLeft.style.height = "auto";
			theRight.style.height = "auto";
			theLeftNav.style.height = "auto";	
			
			// Calculate max height of leftcontent and rightcontent
			var maxHeight=Math.max(theLeft.pixelHeight,theRight.pixelHeight);
						
			// Set both heights to the same (max) value
			theLeft.style.height = maxHeight + "px";
			theRight.style.height = maxHeight + "px";
			
			// Set the leftnav height to the leftcontent, minus the contact, minus the padding
			theLeftNav.style.height = (theLeft.pixelHeight - theContact.pixelHeight - theLeftNavPadding) + "px";

			// Setting the height for the additional elements on the front page					

			if (theAdminLinks && theGraphic && therightitems)
			{
				if (theAdminLinks.pixelHeight || theGraphic.pixelHeight || 
					therightitems.pixelHeight)
				{
					// Initialize all heights to "auto" to deal with font size changes
					therightitems.style.height = "auto";
					
					// Total height of all elements inside 
					var totalHeight = theAdminLinks.pixelHeight + theGraphic.pixelHeight + therightitems.pixelHeight;

					// Here's a fudge factor to get the gray "rightitems" div to the right height.  Sorry.
					var rightFudgeFactor = 4;

					if (theAdminLinks.offsetHeight == 0)
					{
						rightFudgeFactor++;
					}

					if (theGraphic.offsetHeight == 0)
					{
						rightFudgeFactor++;
					}
										
					// If it's IE, make it a -2.
					if (document.all)
					{
						rightFudgeFactor = -2;
					}
					
					// Calculate height difference
					var theHeightDiff = maxHeight - totalHeight - rightFudgeFactor;							
					
					// If it's greater than zero, set a new height.
					if (theHeightDiff > 0)
					{
						var newHeight = therightitems.pixelHeight + theHeightDiff;
						therightitems.style.height = newHeight + "px";
					}

				}
			}
		
		}
		
	}
	
	// Focus and blur on search box
	theSearch = document.getElementById("searchbox");

	if(theSearch)
	{
		theSearch.onfocus = function () {
			this.value = "";
		}
		
		theSearch.onblur = function () {
			if (this.value == "auto")
			{
				this.value = "Search College";
			}
		}
	}

}


function setCountyDropdown()
{
	var theDiv = document.getElementById("countylistdiv");
	var theDropDown = document.getElementById("countylist");
	var theButton = document.getElementById("gotocounty");	

	if (theDiv && theDropDown && theButton)
	{
		theButton.myList = theDropDown;
		
		theButton.onclick = function() {
			theURL = this.myList.options[this.myList.selectedIndex].value;
			if (theURL)
			{
				document.location.href = theURL;
			}
		}
		
		theDiv.style.display = "block";
	}

}

function setExternalLinks()
{
	var links = document.getElementsByTagName("a");
	for (i=0; i<links.length; i++)
	{
		if (links[i].href.match(/^https*:\/\//) && !links[i].href.match(/psu\.edu/))
		{
			links[i].target = "_blank";
		}
	}
}
