//First and Last LI Selector
//Note: Prototype Driven
function liFirstLast() {
	var firstLIs =	$$('ul > li:first-child');
	var lastLIs = $$('ul > li:last-child, dl > dd: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 = $$('#header 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;
			}
		});
	});
}

function externalLinks(){
	var externalLinks = $$('a[rel=external]');
	
	externalLinks.windowWidth = 300;
	externalLinks.windowHeight = 300;
	
	externalLinks.each(function(externaLink){
		externalLink.observe('click', function(event){
			// window.open code
			event.stop(); //capital E?  Can't recall
		});
	});
}

// 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; 
}

// Breakout Helper
// Adjusts opacity, etc. for the breakout
function breakoutHelper() {
	var breakoutLinks = $$('#breakout p.more a');
	
	breakoutLinks.each(function(breakoutLink){
		breakoutLink.setOpacity(.80);
	});
}

// Highlight Helper
// Evens out heights across all highlights
function highlightHelper() {
	var siteFooter = $('footer');
	var mainHighlights = $$('#contentMain div.highlight');
	var mainSections = $$('#contentMain #contentRelated div.section');
	var footerHighlights = $$('#footer div.highlight');
	
	var mainMaxHeight = mainHighlights.max(function(mainHighlight){
		return mainHighlight.getHeight();
	});
	
	var footerMaxHeight = footerHighlights.max(function(footerHighlight){
		return footerHighlight.getHeight();
	});
	
	mainHighlights.each(function(mainHighlight, index){
		mainHighlight.setStyle({
			height: mainMaxHeight - 120 + 'px'
		});
		switch(index){
			case 0:
				mainHighlight.addClassName('firstHighlight');
				break;
			case 1:
				mainHighlight.addClassName('secondHighlight');
				break;
			case 3:
				mainHighlight.addClassName('lastHighlight');
				break;
			default:
				break;
		}
	});
	
	mainSections.each(function(mainSection, index){
		switch(index){
			case 0:
				mainSection.addClassName('firstSection');
				break;
			case 1:
				mainSection.addClassName('secondSection');
				break;
			case 3:
				mainSection.addClassName('lastSection');
				break;
			default:
				break;
		}
	});
	
	footerHighlights.each(function(footerHighlight, index){
		footerHighlight.setStyle({
			height: footerMaxHeight + 'px'
		});
		siteFooter.setStyle({
			backgroundPosition: 'center ' + footerMaxHeight + 'px'
		});
		switch(index){
			case 0:
				footerHighlight.addClassName('firstHighlight');
				break;
			case 1:
				footerHighlight.addClassName('secondHighlight');
				break;
			case 3:
				footerHighlight.addClassName('lastHighlight');
				break;
			default:
				break;
		}
	});
}

// Breakout Flash Helper
// Sets up an array of all available Flash SWFs for breakout
// Randomizes the results and handles the SWF Object Replacement
// of the various Flash breakout headers
function breakoutHelper(){
	var breakoutSection = $('breakout');
	var backpage = $$('.backpage');
	
	if(!breakoutSection){
		return false;
	}
	
	if(backpage[0]){
		
	}
	else {
	
		var breakoutSWFs = [["/flash/GuitarStrings.swf"], ["/flash/flourishTrail.swf"], ["/flash/PBrush.swf"]]  // List of SWF header files
		
	    var randomNumber = Math.floor(Math.random()*(breakoutSWFs.length))  //Randomization math based on the amount of SWFs
				
		var flashvars = {
			
		}
		var params = {
			wmode: 'transparent'
		}
		var attributes = {
			
		}
		
		swfobject.embedSWF(breakoutSWFs[randomNumber], "breakout", "1094", "460", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes); 
	
	}

}

//Site Highlight Rotator
//Creates navigation for Breakout Carousel
//Note: Prototype Framework
function breakoutCarousel() {
	var breakoutHighlights = $$('.carousel li');
	
	if(!breakoutHighlights[0]) return false;
	
	var queue = Effect.Queues.get('highlightQueue');
	
	breakoutHighlights.each(function(breakoutHighlight, index) {
		if(breakoutHighlight.empty()){
			breakoutHighlight.remove();
		}
	});
	
	var breakoutHighlights = $$('.carousel 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('ul');
	var currentHighlight = 0;
	var previousHighlight = breakoutHighlights.size();
	var highlightIndex = breakoutHighlights.size();
	
	var breakoutNavigation = new Element('ol');
	breakoutNavigation.addClassName('navCarousel');
	
	previousHighlightButton = new Element('li');
	previousHighlightButton.addClassName('previous');
	previousHighlightAnchor = new Element('a', { href: "#"}).update("Backward");
	
	previousHighlightButton.insert({ bottom: previousHighlightAnchor });
	breakoutNavigation.insert({ top: previousHighlightButton });
	
	nextHighlightButton = new Element('li');
	nextHighlightButton.addClassName('next');
	nextHighlightAnchor = new Element('a', { href: "#"}).update("Forward");

	nextHighlightButton.insert({ bottom: nextHighlightAnchor });
	breakoutNavigation.insert({ bottom: nextHighlightButton });

	breakoutContainer.insert({ before: breakoutNavigation });
	
	document.observe('optiem: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
				}
			});
			
		}
	});
	
	carousel = new PeriodicalExecuter(function(){
		nextHighlightAnchor.fire('optiem:updateHighlight', { indexMove: -1 });
	}, 10);
	
	previousHighlightAnchor.observe('click', function(event){
		previousHighlightAnchor.fire('optiem:updateHighlight', { indexMove: 1 });
		carousel.stop();
		event.stop();
	});
	
	nextHighlightAnchor.observe('click', function(event){
		nextHighlightAnchor.fire('optiem:updateHighlight', { indexMove: -1 });
		carousel.stop();
		event.stop();
	});

}

// Font Replacement
function cufonReplace() {
	Cufon.replace('h1');
	Cufon.replace('#breakout h2');
	Cufon.replace('#breakout p:not(p.more)');
	Cufon.replace('#breakout p.more strong');
	Cufon.replace('#breakout p.more a', {
		hover: {
			color: '#313131'
		}
	});
	Cufon.replace('#navSecondary > li.active > a');
	Cufon.replace('#contentArticle div.section h2');
	Cufon.replace('#contentArticle div.section dl dt');
	Cufon.replace('#contentArticle dl.news dt', {hover: true});
	Cufon.replace('#contentArticle dl.caseStudies dt');
	Cufon.replace('#contentArticle p.introduction');
	Cufon.replace('#contentRelated h2');
	Cufon.replace('div.highlight h2');
	Cufon.replace('#footer h2');
	Cufon.replace('p.whitepaper a');
}

//Replacement for Window Onload - Loads before images, cross-browser
document.observe("dom:loaded", function() {
	//dynamicShadow('/images/global/shadow.png', 'page-container', 16, 0);
	highlightHelper();
	cufonReplace();
	breakoutCarousel();
	liFirstLast(); // Adds classes 'first' and 'last' to respective LIs
	breakoutHelper();
	inputClear();
	breakoutHelper();
});