//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;
}

//First and Last LI Selector
//Note: Prototype Driven
function liFirstLast() {
	var firstLIs =	$$('ul > li:first-child');
	var lastLIs = $$('ul > li:last-child');
	
	firstLIs.each(function(liFirst) {
		liFirst.addClassName('first');
		});
		
	lastLIs.each(function(liLast) {
		liLast.addClassName('last');
	});
}

//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;
			}
		});
	});
}

//Breakout Mover
//Shifts Breakout DIV down the page, adjusts elements
//Note: Prototype Driven
function moveBreakout() {
	var breakoutDiv = $('breakout');
	
	if(breakoutDiv) {
		$('contentRelated').setStyle({
			margin: breakoutDiv.getHeight()-6 + 'px 0 6px'
		});
		$('navMain').setStyle({
			marginBottom: breakoutDiv.getHeight() + 'px'
		});
		
		breakoutDiv.setStyle({
			position: 'absolute',
			width: '100%'
		});
		
		breakoutDiv.setStyle({
			top: ($('contentRelated').positionedOffset()[1]-6)-(breakoutDiv.getHeight()-6)  + 'px',
			left: 0
		});
	}
}

//Header Mover
//Shifts the H1 of a backpage, adjusts appropriate elements
//Note: Prototype Driven
function moveHeader() {
	var pageHeader = $$('#contentMain h1')[0];
	
	if(pageHeader) {
		var headerSpan = new Element('span');
		headerSpan.innerHTML = pageHeader.innerHTML;
		
		var newHeader = new Element('h1');
		newHeader.addClassName('replaced');
		newHeader.insert({ bottom: headerSpan });
				
		pageHeader.replace(newHeader);
		
		$('contentMain').setStyle({
			paddingTop: (newHeader.getHeight()/10)+'em'
		})

	}
}



//Font Sizer Utility
//Note: Prototype Driven
function fontSizer() {
	var fontSizer = $$('#siteFontSizer a');
	var sizeThese = $$('#breakout, #contentRelated, #contentMain');
	styleSize = 1;

	if($('siteFontSizer')) {
		$('siteFontSizer').setStyle({
			display: 'block'
		});
		initialSize = readCookie('pageFontSize');
		if(initialSize) {
			sizeThese.each(function(sizeThis){
				sizeThis.setStyle({
					fontSize: initialSize
				});
			});
		}
	}
	
	fontSizer[0].observe('click', function(event){
		styleSize -= .1;
		sizeThese.each(function(sizeThis){
			sizeThis.setStyle({
				//fontSize: styleSize + 'em'
				fontSize: '.9em'
			});
		});
			
		setCookie('pageFontSize', '.9em');
	});
	
	fontSizer[1].observe('click', function(event){
		styleSize = 1;
		sizeThese.each(function(sizeThis){
			sizeThis.setStyle({
				//fontSize: styleSize + 'em'
				fontSize: '1em'
			});
		});
			
		setCookie('pageFontSize', '1em');
	});
	
	fontSizer[2].observe('click', function(event){
		styleSize += .1;
		sizeThese.each(function(sizeThis){
			sizeThis.setStyle({
				//fontSize: styleSize + 'em'
				fontSize: '1.1em'
			});
		});
			
		setCookie('pageFontSize', '1.1em');
	});
}

//Print Page Utility
//Note: Prototype Driven
function printPage(printPageID) {
	var printLink = $$('#' + printPageID + ' a');
	if(printLink) {
		$(printPageID).setStyle({
			display: 'block'
		});
		printLink[0].observe('click', function(event){
			window.print();
		});
	}
}

function eventPrinter(){
	var eventListings = $$('dl.eventListing');
	if(!eventListings[0]) return false;
	
	var viewport = $('page-container');
	
	viewport.addClassName('event');
}



// 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 loadInternalFlash () {
	    var getElement = document.getElementById("sidebarimages").getElementsByTagName("img");
		var FlashString;
		var flashvars = {};
		
		for (var w=0;w<getElement.length;++w) {
			if (w==0) { 
			flashvars.img1 = getElement[w].getAttribute('src'); 
            flashvars.text1 = getElement[w].getAttribute('alt');
			}
			if (w==1) {
			flashvars.img2 = getElement[w].getAttribute('src');
            flashvars.text2 = getElement[w].getAttribute('alt');
			}
			if (w==2) {
			flashvars.img3 = getElement[w].getAttribute('src');
            flashvars.text3 = getElement[w].getAttribute('alt');
			}
			if (w==3) {
			flashvars.img4 = getElement[w].getAttribute('src');
            flashvars.text4 = getElement[w].getAttribute('alt');
			}
       }
	    if(flashvars.img1){
			
		var params = {};
        params.wmode = "transparent";
        var attributes = {};
        attributes.id = "sidebarimages";
        swfobject.embedSWF("/flash/flash_BkPg.swf", "sidebarimages", "220", "600", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
}

}

//Event Rotator on Homepage
//Cycles through an unordered list of most recent event listings on the homepage
//Prototype and Scriptaculous required.
function eventRotator(){
	var rotatorContainer = $('eventRotator');
	var items = $$('#eventRotator li');
	
	if(!rotatorContainer) return false;
	
	var queue = Effect.Queues.get('highlightQueue');
	
	items.each(function(item, index) {
		if(item.empty()){
			item.remove();
		}
	});
	
	var items = $$('#eventRotator li');
	
	
	var currentItem = 0
	var previousItem = items.size();
	var highlightIndex = items.size();
	
	//Create controls
	var carouselNavigation = new Element('ol');
	carouselNavigation.addClassName('navHighlights');
	
	previousItemButton = new Element('li');
	previousItemButton.addClassName('previous');
	previousItemAnchor = new Element('a', { href: "#"}).update("Previous");
	previousItemButton.setOpacity(0.85);
	
	
	previousItemButton.insert({ bottom: previousItemAnchor });
	carouselNavigation.insert({ top: previousItemButton });
	
	nextItemButton = new Element('li');
	nextItemButton.addClassName('next');
	nextItemAnchor = new Element('a', { href: "#"}).update("Next");
	nextItemButton.setOpacity(0.85);

	nextItemButton.insert({ bottom: nextItemAnchor });
	carouselNavigation.insert({ bottom: nextItemButton });

	rotatorContainer.up('div').insert({ bottom: carouselNavigation });
	
	document.observe('fms:eventRotation', function(event){
		
		if (queue.effects.size() == 0) {
			
			var itemMove = event.memo.indexMove;
			
			previousItem = currentItem;
			currentItem = (items.size() + ((currentItem - itemMove) % items.size())) % items.size();

			items[currentItem].setStyle({
				left: -(itemMove*items[previousItem].getWidth()) + 'px'
			});
			
			new Effect.Parallel([new Effect.Move(items[previousItem], {
				sync: true,
				x: itemMove*items[previousItem].getWidth(),
				y: 0,
				mode: 'absolute'
			}), new Effect.Move(items[currentItem], {
				sync: true,
				x: 0,
				y: 0,
				mode: 'absolute'
			})], {
				queue: {
					position: 'end',
					scope: 'highlightQueue',
					limit: 1
				}
			});
			
		}
		
	});
	
	carousel = new PeriodicalExecuter(function(){
		rotatorContainer.fire('fms:eventRotation', { indexMove: -1 });
	}, 15);
	
	previousItemAnchor.observe('click', function(event){
		previousItemAnchor.fire('fms:eventRotation', { indexMove: 1 });
		carousel.stop();
		event.stop();
	});
	
	nextItemAnchor.observe('click', function(event){
		nextItemAnchor.fire('fms:eventRotation', { indexMove: -1 });
		carousel.stop();
		event.stop();
	});
	
	nextItemAnchor.observe('mouseenter', function(event){
		nextItemAnchor.up('li').setOpacity(1);
	});
	
	previousItemAnchor.observe('mouseenter', function(event){
		previousItemAnchor.up('li').setOpacity(1);
	});
	
	nextItemAnchor.observe('mouseleave', function(event){
		nextItemAnchor.up('li').setOpacity(.85);
	});
	
	previousItemAnchor.observe('mouseleave', function(event){
		previousItemAnchor.up('li').setOpacity(.85);
	});
}

//Replacement for Window Onload - Loads before images, cross-browser
document.observe("dom:loaded", function() {
	//dynamicShadow('/images/global/shadow.png', 'page-container', 16, 0);
	liFirstLast(); // Adds classes 'first' and 'last' to respective LIs
	//eventRotator();
	moveBreakout();
	moveHeader();
	eventPrinter();
	printPage('sitePrinter');
	fontSizer();
	inputClear();
	loadInternalFlash();
});