//Event Highlight Rotator
//Creates navigation for event highlights
//Note: Prototype Framework
function eventCarousel() {
	var eventHighlights = $$('ul.eventListing li');
	
	if(!eventHighlights[0]) return false;
	
	var eventsContainer = eventHighlights[0].up('div');
	var currentEvent = 0;
	
	var eventNavigation = new Element('ol');
	eventNavigation.addClassName('navEvents');
	
	previousEventButton = new Element('li');
	previousEventButton.addClassName('previous');
	previousEventAnchor = new Element('a', { href: "#"}).update("Previous");
	
	previousEventAnchor.observe('click', function(event){
		if(currentEvent == 0) {
			eventHighlights[currentEvent].setStyle({
				position: 'absolute',
				left: '-999em'
			});
			var lastEvent = eventHighlights.size()-1;
			eventHighlights[lastEvent].setStyle({
				position: 'relative',
				left: 'auto'
			});
			currentEvent = lastEvent;
		}
		else {
			eventHighlights[currentEvent].setStyle({
				position: 'absolute',
				left: '-999em'
			});
			currentEvent = currentEvent - 1;
			eventHighlights[currentEvent].setStyle({
				position: 'relative',
				left: 'auto'
			});
		}
		event.stop();
	});
	
	previousEventButton.insert({ bottom: previousEventAnchor });
	eventNavigation.insert({ top: previousEventButton });
	
	nextEventButton = new Element('li');
	nextEventButton.addClassName('next');
	nextEventAnchor = new Element('a', { href: "#"}).update("Next");

	nextEventAnchor.observe('click', function(event){
		if(currentEvent == eventHighlights.size()-1) {
			eventHighlights[currentEvent].setStyle({
				position: 'absolute',
				left: '-999em'
			});
			var lastEvent = 0;
			eventHighlights[lastEvent].setStyle({
				position: 'relative',
				left: 'auto'
			});
			currentEvent = lastEvent;
		}
		else {
			eventHighlights[currentEvent].setStyle({
				position: 'absolute',
				left: '-999em'
			});
			currentEvent = currentEvent + 1;
			eventHighlights[currentEvent].setStyle({
				position: 'relative',
				left: 'auto'
			});
		}
		event.stop();
	});

	nextEventButton.insert({ bottom: nextEventAnchor });
	eventNavigation.insert({ bottom: nextEventButton });

	eventsContainer.insert({ bottom: eventNavigation });
	
}

//Site Highlight Rotator
//Creates navigation for Breakout Carousel
//Note: Prototype Framework
function breakoutCarousel() {
	var breakoutHighlights = $$('#carouselContent li');
	
	if(!breakoutHighlights[0]) return false;
	
	var queue = Effect.Queues.get('highlightQueue');
	
	breakoutHighlights.each(function(breakoutHighlight, index) {
		if(breakoutHighlight.empty()){
			breakoutHighlight.remove();
		}
	});
	
	var breakoutHighlights = $$('#carouselContent li');
	
	breakoutHighlights.each(function(breakoutHighlight){
		breakoutHighlight.setStyle ({
			backgroundImage: 'url(' + breakoutHighlight.down('img').src + ')',
			backgroundRepeat: 'no-repeat',
			backgroundPosition: '0 0'
		});
			
		breakoutHighlight.down('img').remove();
	});
	
	var breakoutContainer = breakoutHighlights[0].up('div');
	var currentHighlight = 0;
	var previousHighlight = breakoutHighlights.size();
	var highlightIndex = breakoutHighlights.size();
	
	var breakoutNavigation = new Element('ol');
	breakoutNavigation.addClassName('navHighlights');
	
	previousHighlightButton = new Element('li');
	previousHighlightButton.addClassName('previous');
	previousHighlightAnchor = new Element('a', { href: "#"}).update("Previous");
	previousHighlightAnchor.setOpacity(0.25);
	
	previousHighlightButton.insert({ bottom: previousHighlightAnchor });
	breakoutNavigation.insert({ top: previousHighlightButton });
	
	nextHighlightButton = new Element('li');
	nextHighlightButton.addClassName('next');
	nextHighlightAnchor = new Element('a', { href: "#"}).update("Next");
	nextHighlightAnchor.setOpacity(0.25);

	nextHighlightButton.insert({ bottom: nextHighlightAnchor });
	breakoutNavigation.insert({ bottom: nextHighlightButton });

	breakoutContainer.insert({ bottom: breakoutNavigation });
	
	document.observe('jumpstart:updateHighlight', function(event){
		if (queue.effects.size() == 0) {
			
			var highlightMove = event.memo.indexMove;
			
			previousHighlight = currentHighlight;
			currentHighlight = (breakoutHighlights.size() + ((currentHighlight - highlightMove) % breakoutHighlights.size())) % breakoutHighlights.size();

			breakoutHighlights[currentHighlight].setStyle({
				left: -(highlightMove*breakoutHighlights[previousHighlight].getWidth()) + 'px'
			});
			
			new Effect.Parallel([new Effect.Move(breakoutHighlights[previousHighlight], {
				sync: true,
				x: highlightMove*breakoutHighlights[previousHighlight].getWidth(),
				y: 0,
				mode: 'absolute'
			}), new Effect.Move(breakoutHighlights[currentHighlight], {
				sync: true,
				x: 0,
				y: 0,
				mode: 'absolute'
			})], {
				queue: {
					position: 'end',
					scope: 'highlightQueue',
					limit: 1
				}
			});
			
		}
	});
	
	previousHighlightAnchor.observe('click', function(event){
		previousHighlightAnchor.fire('jumpstart:updateHighlight', { indexMove: 1 });
		event.stop();
	});
	
	nextHighlightAnchor.observe('click', function(event){
		nextHighlightAnchor.fire('jumpstart:updateHighlight', { indexMove: -1 });
		event.stop();
	});

}

//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');
	});
}

//CSS3 Selector Classification
//Note: Prototype Driven
function cssSelectors() {
	var firstLIs =	$$('ul > li:first-child, ol > li:first-child');
	var lastLIs = $$('ul > li:last-child, ol > li:last-child');
	
	firstLIs.each(function(liFirst) {
		liFirst.addClassName('first');
		});
		
	lastLIs.each(function(liLast) {
		liLast.addClassName('last');
	});
	
	var secondChildren = $$('ul li:first-child + li');
	
	secondChildren.each(function(secondChild){
		secondChild.addClassName('second');
	});
}

//Input Clear
//Clears text inputs on a page on focus
//Note: Prototype driven
function inputClear() {
	var textInputs = $$('div.highlight 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;
			}
		});
	});
}

// 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 externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}

//Replacement for Window Onload - Loads before images, cross-browser
document.observe("dom:loaded", function() {
	//dynamicShadow('/images/global/shadow.png', 'page-container', 16, 0);
    externalLinks();
	cssSelectors();
});