// JavaScript Document

/* Rotating Images on Front Page */
var imagePrefix = "/images/home_player/";
var imageSuffix = ".jpg";
var maxImages = 6

// Images
var images = new Array();
images[0]="DC_rightimage.jpg";
images[1]="DecisionDays_rightbox.jpg";
images[2]="11locations_rightbox.jpg";
images[3]="Apply_side.jpg";
images[4]="BAS_rightbox.jpg";
images[5]="BABA_rightbox.jpg";
images[6]="LODside_images.jpg";


// Corresponding URLs
var URLs = new Array();
URLs[0]="/directconnect.asp";
URLs[1]="/decision_days/";
URLs[2]="/locations/";
URLs[3]="/admissions/";
URLs[4]="/bas/";
URLs[5]="/baba/";
URLs[6]="http://www.learningondemand.ucf.edu";

// Corresponding alt tags
var altTags = new Array();
altTags[0]="Direct Connect to UCF";
altTags[1]="Decision Days: Decisions on the Spot";
altTags[2]="11 Convient Locations";
altTags[3]="Applying is Simple";
altTags[4]="Bachelor of Applied Science";
altTags[5]="Bachelor of Arts in Business Administration";
altTags[6]="UCF Leaning on Demand";


// generate a unique random sequence of images and links
function genRandomImagesMarkup(maxImages)
{

	maxImages++;
	// pick images
	selectedImages = new Array();
	for (i=0;i< maxImages;i++)
	{
		aImage =pickRandomImage();
		// check previously picked images
		for (z=0;z<selectedImages.length; z++)
		{		
			// have we already picked this image?
			if (aImage == selectedImages[z])
			{
				//  pick a differnt image
				aImage = pickRandomImage();
				// restart at the begining;
				z=-1;
			}
		}		
		selectedImages[i]= aImage;
	}	
	
	// generate show image and link markup
		
		return "<a href='"+URLs[selectedImages[0]]+"'><img src='"+imagePrefix+images[selectedImages[0]]+"' alt='"+altTags[selectedImages[0]]+"' class='homeBlockRight' border='0'/></a><br/><a href='/admissions/'><img src='images/home_blocks/get_started_now2.jpg' alt='Get Started Now' width='276' height='28' class='homeBlockRight' /></a><br/><a href='"+URLs[selectedImages[1]]+"'><img src='"+imagePrefix+images[selectedImages[1]]+"' alt='"+altTags[selectedImages[1]]+"' class='homeBlockRight' border='0'/></a>";
	
}

// return a random Images
function pickRandomImage ()
{
	return Math.round((Math.random() * (images.length -1)));
}


/* Dynamic Content */

// create preview divs and expand / close buttons for content divs
function generateExpandableMarkup() {
	
	allDivs = document.getElementsByTagName("DIV");
	
	elements = new Array();
	divIndex = 0;
	
	// create an array of the divs we want to process... 
	for (var i = 0; i < allDivs.length; i++) {
			
		aDiv = allDivs[i];
		
		if (aDiv.getAttribute('name') == 'expandable') {
		
			elements[divIndex] = aDiv;
			divIndex++;
	
		}
	
	}
	
	for (var i = 0; i < elements.length; i++) {
			
		aElement = elements[i];
		elementId = i
		
		if (aElement.getAttribute('name') == 'expandable') {
			
			aElement.setAttribute('name','expanded-content');
			
			previewText = truncateHTML(aElement.innerHTML,60) + "..."
			
			previewDiv = document.createElement('DIV');
			previewDiv.setAttribute('name','expanded-preview');
			previewDiv.innerHTML = previewText;
			previewDiv.id = elementId+"_preview";
			previewDiv.className ="expandable-content";
			
			aElement.parentNode.insertBefore(previewDiv, aElement); 
			aElement.id = elementId+"_content";
			aElement.className = "expandable-content";
			aElement.style.display = "none";
			
			toggleLink = document.createElement('a');
			toggleLink.setAttribute('name','expanded-link');
			toggleLink.onclick=function () {
	
					elementRef = this;
					prefix = parseInt(this.getAttribute('contentId'));
					
					//get the preview and content divs
					previewDiv = document.getElementById(prefix+"_preview");
					contentDiv = document.getElementById(prefix+"_content");
					
					// get the image element wrapped by the link
					linkImage = getImageChild(elementRef);
					
					// is the preview visible
					if (contentDiv.style.display == "none") {
								
						// hide the preview and show the content
						previewDiv.style.display = 'none';
						contentDiv.style.display = '';
								
						// update the expand / close image
						imagePath = resourceDirectoriesForPath(linkImage.src);
						linkImage.src = imagePath + "content_close.gif";
					
					} else {
								
						// hide the preview and show the content
						previewDiv.style.display = '';
						contentDiv.style.display = 'none';
								
						// update the expand / close image
						imagePath = resourceDirectoriesForPath(linkImage.src);
						linkImage.src = imagePath + "content_expand.gif";
					}
				}
				
			toggleLink.setAttribute('contentId', elementId);
			toggleImage = document.createElement('IMG');
			toggleImage.src = '/images/buttons_badges/content_expand.gif';
			toggleImage.className = 'toggle-content';
			toggleLink.appendChild(toggleImage);
			
			aElement.parentNode.insertBefore(toggleLink, aElement);
			aElement.parentNode.insertBefore(aElement, toggleLink);
			
		}
	
	}

}

// create preview divs and expand / close buttons for content divs
function revertExpandableMarkup() {
	
	allDivs = document.getElementsByTagName("DIV");
	allLinks = document.getElementsByTagName("A");
	
	elementsToDelete = new Array();
	elementsToProcess = new Array();
	delIndex = 0;
	processIndex = 0;
	
	// create an array of the divs we want to process or delete... 
	for (var i = 0; i < allDivs.length; i++) {
			
		aDiv = allDivs[i];
		
		if (aDiv.getAttribute('name') == 'expanded-preview' || aDiv.getAttribute('name') == 'expanded-link' ) {
		
			elementsToDelete[delIndex] = aDiv;
			delIndex++;
	
		}
		
		if (aDiv.getAttribute('name') == 'expanded-content' ) {
		
			elementsToProcess[processIndex] = aDiv;
			processIndex++;
	
		}
	
	}
	
	// add the links we want to delete
	for (var i = 0; i < allLinks.length; i++) {
			
		aLink = allLinks[i];
		
		if (aLink.getAttribute('name') == 'expanded-link' ) {
		
			elementsToDelete[delIndex] = aLink;
			delIndex++;
	
		}
	
	}
	
	// delete referenced elements
	for (var i = 0; i < elementsToDelete.length; i++) {
			
		aElement = elementsToDelete[i];
		aElement.parentNode.removeChild(aElement);
		
	}
		
	// restore the contenet divs original state
	for (var i = 0; i < elementsToProcess.length; i++) {
			
		aElement = elementsToProcess[i];
		aElement.className = undefined;
		aElement.id = undefined;
		aElement.setAttribute('name','expandable');
		aElement.style.display='block';
		
	}

}

function truncateHTML(str,len) {
	
	text = stripHTML(aElement.innerHTML)
	
	if (text.length <= len){
		return text;
	}
	
	text = text.substring(0,Math.min(len+50,str.length));		
	while (text.length >= len) {
		
		lastWord = text.lastIndexOf(' ');
		if (lastWord != -1) {
			text = text.substring(0,lastWord)
		} else {
			break;
		}

	}
			
	return text;	
	
}
	

function stripHTML(str){
      var re= /<\S[^><]*>/g ;
      return str.replace(re, "") ;
}


function getImageChild (elementRef) {
	
	
	childImage = undefined;
	for (var i = 0; i < elementRef.childNodes.length; i++) {
		if (elementRef.childNodes[i].tagName == "IMG") {
			childImage = elementRef.childNodes[i];
		}
	}
	return childImage;

}

function resourceDirectoriesForPath(path) {
	
	resourceDirectories = ""; 
	lastSlash = path.lastIndexOf('/');
	if (lastSlash != -1) {
		resourceDirectories = path.substring(0,lastSlash+1);
	}
	return resourceDirectories;
}

function quickLinks (selectElement) {
	
	var linkURL = selectElement.options[selectElement.selectedIndex].value;

	if (linkURL != "") {
		document.location = linkURL;
	}
}

// clear default contents of text fields on first click
function clearDefault(element) {

	if(element.value=="SEARCH UCF" || element.value =="SEARCH REGIONAL CAMPUSES") {
	   element.value="";
	   element.style.color="#000";
	   return;
	}
	
	/* if(element.value=="") {
		element.value="SEARCH";
		element.style.color="#999";
		return;
	} */
	
}

function resetSearch() {
	/* 
	element = document.forms.sForm.q;
	
	if (element.value != "SEARCH UCF" && element.value != "SEARCH REGIONAL CAMPUSES") {
		element.style.color="#000";
	} */
}
	