
//List Height Fix for Teacher Profiles
//Fix List Height
//Clears text inputs on a page on focus
//Note: Prototype driven
function heightClear() {
	var liInputs = $$('ul.profiles li:nth-child(2n)');
	
	
	liInputs.each(function(liInput, index){
		if(liInput.getHeight() > liInput.previous('li').getHeight()){
			liInput.previous('li').setStyle({
			  height: liInput.getHeight()+'px'
			});
			liInput.setStyle({
			  height: liInput.getHeight()+'px'
			});
		}
		else{
			liInput.setStyle({
			  height: liInput.previous('li').getHeight()+'px'
			});
			liInput.previous('li').setStyle({
			  height: liInput.previous('li').getHeight()+'px'
			});
		}
	});
	
}

//dynamicShadow - Creates a dynamic shadow for images
//Defaults to shadow.gif, 10 width, no offset
//Note: Prototype Framework
function dynamicShadow(shadowURL, containerID, shadowWidth, shadowOffset) {
	var shadowURL = (shadowURL == null) ? "../images/global/shadowTest.gif" : shadowURL;
	var containerID = (containerID == null) ? "page-container" : containerID;
	var shadowWidth = (shadowWidth == null) ? 10 : shadowWidth;
	var shadowOffset = (shadowOffset == null) ? 0 : shadowOffset;

	
	var images = $$(
		'#' + containerID + ' img.shadow,'
		+ '#' + containerID + ' img.shadowLeft,'
		+ '#' + containerID + ' img.shadowRight,'
		+ '#' + containerID + ' img.shadowCenter');
	var imageClone;
	var imageHeight;
	var imageWidth;
	var shadowContainer;
	var shadowDiv = [];
	
	images.each(function(imageObject){
		imageClone = Object.extend(imageObject);
		imageHeight = imageObject.getHeight();
		imageWidth = imageObject.getWidth();
		imageClass = imageObject.className;
		
		// Create the Shadow Container
		shadowContainer = new Element('div');
		shadowContainer.addClassName('shadowContainer');
		shadowContainer.addClassName(imageClass);
		shadowContainer.setStyle({
			position: 'relative',
			padding: shadowWidth + 'px',
			width: imageWidth + 'px',
			height: imageHeight + 'px',
			background: 'transparent'
		});

		// Create Top Left Div
		shadowDiv[0] = new Element('div');
		shadowDiv[0].setStyle({
			position: 'absolute',
			top: 0,
			left: 0,
			width: shadowWidth + 'px',
			height: shadowWidth + 'px',
			background: 'transparent url("' + shadowURL + '") top left no-repeat'
		});
		shadowContainer.insert({ bottom: shadowDiv[0] });
		
		// Create Top Right Div
		shadowDiv[1] = new Element('div');
		shadowDiv[1].setStyle({
			position: 'absolute',
			top: 0,
			right: 0,
			width: shadowWidth + 'px',
			height: shadowWidth + 'px',
			background: 'transparent url("' + shadowURL + '") top right no-repeat'
		});
		shadowContainer.insert({ bottom: shadowDiv[1] });
		
		// Create Bottom Right Div
		shadowDiv[2] = new Element('div');
		shadowDiv[2].setStyle({
			position: 'absolute',
			bottom: 0,
			right: 0,
			width: shadowWidth + 'px',
			height: shadowWidth + 'px',
			background: 'transparent url("' + shadowURL + '") bottom right no-repeat'
		});
		shadowContainer.insert({ bottom: shadowDiv[2] });
		
		// Create Bottom Left Div
		shadowDiv[3] = new Element('div');
		shadowDiv[3].setStyle({
			position: 'absolute',
			bottom: 0,
			left: 0,
			width: shadowWidth + 'px',
			height: shadowWidth + 'px',
			background: 'transparent url("' + shadowURL + '") bottom left no-repeat'
		});
		shadowContainer.insert({ bottom: shadowDiv[3] });
		
		// Create Center Top Div
		shadowDiv[4] = new Element('div');
		shadowDiv[4].setStyle({
			position: 'absolute',
			top: 0,
			left: shadowWidth + 'px',
			width: imageWidth + 'px',
			height: shadowWidth + 'px',
			background: 'transparent url("' + shadowURL + '") top center no-repeat'
		});
		shadowContainer.insert({ bottom: shadowDiv[4] });
		
		// Create Center Right Div
		shadowDiv[5] = new Element('div');
		shadowDiv[5].setStyle({
			position: 'absolute',
			top: shadowWidth + 'px',
			right: 0,
			width: shadowWidth + 'px',
			height: imageHeight + 'px',
			background: 'transparent url("' + shadowURL + '") center right no-repeat'
		});
		shadowContainer.insert({ bottom: shadowDiv[5] });
		
		// Create Center Bottom Div
		shadowDiv[6] = new Element('div');
		shadowDiv[6].setStyle({
			position: 'absolute',
			bottom: 0,
			right: shadowWidth + 'px',
			width: imageWidth + 'px',
			height: shadowWidth + 'px',
			background: 'transparent url("' + shadowURL + '") bottom center no-repeat'
		});
		shadowContainer.insert({ bottom: shadowDiv[6] });
		
		// Create Center Right Div
		shadowDiv[7] = new Element('div');
		shadowDiv[7].setStyle({
			position: 'absolute',
			top: shadowWidth + 'px',
			left: 0,
			width: shadowWidth + 'px',
			height: imageHeight + 'px',
			background: 'transparent url("' + shadowURL + '") center left no-repeat'
		});
		shadowContainer.insert({ bottom: shadowDiv[7] });
		
		imageObject.replace(shadowContainer);
		
		shadowContainer.insert({ bottom: imageClone });
		
	});
	
	return false;
}

//CSS Selectors
//Note: Prototype Driven
function cssSelectors() {
	var firstLIs =	$$('ul > li:first-child');
	var lastLIs = $$('ul > li:last-child');
	
	firstLIs.each(function(liFirst, index) {
		liFirst.addClassName('first');
	});
		
	lastLIs.each(function(liLast) {
		liLast.addClassName('last');
	});
	
	var siblings = $$('#contentMain #article div.highlight + div.highlight, #contentMain #article div.section + div.section');
	
	siblings.each(function(sibling){
		sibling.addClassName('sibling');
	});
}

//Input Clear
//Clears text inputs on a page on focus
//Note: Prototype driven
function inputClear() {
	var textInputs = $$('input[type="text"]');
	
	textInputs.each(function(textInput){
		textInput.initialValue = textInput.value;
		textInput.observe('focus', function(event) {
			if(textInput.value == textInput.initialValue){
				textInput.clear();
			}
		});
		textInput.observe('blur', function(event){
			if(textInput.value.blank() == true) {
				textInput.value = textInput.initialValue;
			}
		});
	});
}

//Bookmark Link
//Adds the bookmark script to links that have a bookmark class
function bookmarkLink() {
	var bookmarkLinks = $$('#aside a.bookmark');
	
	bookmarkLinks.each(function(bookmarkLink){
		bookmarkLink.observe('click', function(event){
			window.external.AddFavorite(location.href, document.title);
			window.Add
		console.log('test');
			event.stop();
		});
	});
}

// Cookie Functions
// Set the cookie 
function setCookie(name,value,days) { 
	if (days) { 
		var date = new Date(); 
		date.setTime(date.getTime()+(days*24*60*60*1000)); 
		var expires = ";expires="+date.toGMTString(); 
	} else { 
		expires = ""; 
	} 
	document.cookie = name+"="+value+expires+";"; 
}

// Read the cookie 
function readCookie(name) { 
	var needle = name + "="; 
	var cookieArray = document.cookie.split(';'); 
	for(var i=0;i < cookieArray.length;i++) { 
		var pair = cookieArray[i]; 
		while (pair.charAt(0)==' ') { 
			pair = pair.substring(1, pair.length); 
		} 
		if (pair.indexOf(needle) == 0) { 
			return pair.substring(needle.length, pair.length); 
		} 
	} 
	return null; 
}

function flashBreakout() {
	if(!$('breakout')){
		return false;
	}
	
	var flashvars = {};
	var params = {};
	params.wmode = "transparent";
	var attributes = {};
	attributes.id = "breakout";
	swfobject.embedSWF("/flash/main.swf", "breakout", "707", "345", "9.0.0", false, flashvars, params, attributes);

}

function flashMap() {
	if(!$('mapStates')){
		return false;
	}
	var flashvars = {};
	var params = {};
	params.wmode = "transparent";
	var attributes = {};
	attributes.id = "mapStates";
	swfobject.embedSWF("/flash/map.swf", "mapStates", "465", "520", "9.0.0", false, flashvars, params, attributes);
}

// Resources List Helper
// Expands and Contracts various nested lists
function resourcesListHelper() {
	var resourceLIs = $$('ul.resources li');
	var currentURL = document.URL;
	
	resourceLIs.each(function(resourceLI, resourceIndex){
		resourceLI.originalHeight = resourceLI.getHeight();
		
		if(resourceLI.down('ul')){
			
			resourceLI.addClassName('hasChildren');
			
			var resourceLIchildLists = resourceLI.getElementsBySelector('ul ul');
			resourceLIchildLists.each(function(resourceLIchildList){
				resourceLIchildList.addClassName('childList');
				resourceLI.originalHeight = resourceLI.originalHeight - resourceLIchildList.getHeight();
			});
			
			resourceLI.setStyle({
				height: resourceLI.down('a').getHeight() + 'px',
				overflow: 'hidden'
			});
			
			resourceLI.addClassName('closed');
			
			resourceLI.down('a').observe('click', function(event){
				if(resourceLI.hasClassName('closed')) {
					resourceLI.setStyle({
						height: 'auto'
					});
					resourceLI.removeClassName('closed');
				}
				else {
					var resourceULs = resourceLI.getElementsBySelector('ul');
					resourceULs.each(function(resourceUL, resourceIndex){
						resourceUL.up('li').setStyle({
							height: resourceUL.previous('a').getHeight() + 'px',
							overflow: 'hidden'
						});
						resourceUL.up('li').addClassName('closed');
					});
				}
				event.stop();
			});
		}
		
	});
}

// General FAQ/Warranty List Helper
// Expands and Contracts various nested paragraphs using a headline H3
function detailListHelper() {
	var resourceLIs = $$('ul.detail li');
	var currentURL = document.URL;
	//console.log(currentURL);
	
	resourceLIs.each(function(resourceLI, resourceIndex){
		resourceLI.originalHeight = resourceLI.getHeight();
		
		if(resourceLI.down('p')){
			
			resourceLI.addClassName('hasChildren');
			
			var resourceLIchildLists = resourceLI.getElementsBySelector('ul p');
			resourceLIchildLists.each(function(resourceLIchildList){
				resourceLIchildList.addClassName('childList');
				resourceLI.originalHeight = resourceLI.originalHeight - resourceLIchildList.getHeight();
			});
			
			var queryParams = currentURL.sub('%99', '');
			queryParams = queryParams.toQueryParams();
			
			if(queryParams.doc){
				var resourceName = resourceLI.down('h3').innerHTML;
				resourceName = resourceName.sub('&trade;','');
				resourceName = resourceName.sub('™','');
				if(queryParams.doc == resourceName.strip()){
					//console.log(true);
					resourceLI.setStyle({
						height: 'auto'
					});
					resourceLI.scrollTo();
				}
				else {
					resourceLI.setStyle({
						height: resourceLI.down('h3').getHeight() + 'px',
						overflow: 'hidden'
					});
					resourceLI.addClassName('closed');
				}
			}
			else {
				resourceLI.setStyle({
					height: resourceLI.down('h3').getHeight() + 'px',
					overflow: 'hidden'
				});
				resourceLI.addClassName('closed');
			}
			
			resourceLI.down('h3').observe('click', function(event){
				if(resourceLI.hasClassName('closed')) {
					resourceLI.setStyle({
						height: 'auto'
					});
					resourceLI.removeClassName('closed');
				}
				else {
					var resourceULs = resourceLI.getElementsBySelector('p');
					resourceULs.each(function(resourceUL, resourceIndex){
						resourceUL.up('li').setStyle({
							height: resourceUL.previous('h3').getHeight() + 'px',
							overflow: 'hidden'
						});
						resourceUL.up('li').addClassName('closed');
					});
				}
				event.stop();
			});
		}
		
	});
}

// Product List Background Replacement
// Strips images from product list and puts them into the background
// of the containing list item
function productListHelper() {
	var productLIs = $$('ul.products li');
	
	productLIs.each(function(productLI){
		productLI.setStyle ({
			backgroundImage: 'url(' + productLI.down('img').src + ')',
			backgroundRepeat: 'no-repeat',
			backgroundPosition: '16px top'
		});
			
		productLI.down('img').remove();
	});

}


//Replacement for Window Onload - Loads before images, cross-browser
document.observe("dom:loaded", function() {
	//dynamicShadow('/images/global/shadow.png', 'page-container', 16, 0);
	detailListHelper();
	resourcesListHelper();
	cssSelectors(); // Adds classes 'first' and 'last' to respective LIs
	flashBreakout();
	flashMap();
	productListHelper();
	heightClear();
});
