//siteHighlight rotator
//Creates navigation system to site highlights
//Note: Prototype Framework
function highlightCarousel() {
	var content = $('contentMain');
	var highlights = $$('ol#siteHighlights li');
	
	if(!highlights[0]) {
		return false;
	}
	
	var currentHighlight = 0;
	
	var navigation = new Element('ol', { id: 'navHighlights' });
	var navigationItems = [];
	
	highlights.each(function(highlight, index){
		navigationItems[index] = new Element('li');
		var anchorTag = new Element('a', { href: "#"}).update(index+1);
		
		anchorTag.observe('click', function(event){
			previousHighlight = currentHighlight;
			currentHighlight = index;
			
			new Effect.Parallel([new Effect.Opacity(highlights[previousHighlight], {
				sync: true,
				to: 0,
				duration: 2,
				afterFinish: function(){
					highlights[previousHighlight].setStyle({
						left: '-999em'
					});
				}
			}), new Effect.Opacity(highlights[currentHighlight], {
				sync: true,
				to: 1,
				duration: 2,
				beforeStart: function(){
					highlights[currentHighlight].setStyle({
						left: '0'
					});
				}
			})], {
				queue: {
					position: 'end',
					scope: 'highlightQueue',
					limit: 1
				}
			});
			
		});
		
		navigationItems[index].insert({ bottom: anchorTag })
		navigation.insert({ bottom: navigationItems[index] });
		
		if (highlight.down('img') != undefined) {
		    highlight.setStyle ({
			    backgroundImage: 'url(' + highlight.down('img').src + ')',
			    backgroundRepeat: 'no-repeat',
			    backgroundPosition: '0 0'
		    });
    			
		    highlight.down('img').remove();
		}
		
	});
	
	previousButton = new Element('li');
	previousAnchor = new Element('a', { href: "#"}).update("Previous");
	previousAnchor.observe('click', function(event){
		if(currentHighlight == 0) {
			previousHighlight = currentHighlight;
			currentHighlight = highlights.size()-1;
			
			new Effect.Parallel([new Effect.Opacity(highlights[previousHighlight], {
				sync: true,
				to: 0,
				duration: 2,
				afterFinish: function(){
					highlights[previousHighlight].setStyle({
						left: '-999em'
					});
				}
			}), new Effect.Opacity(highlights[currentHighlight], {
				sync: true,
				to: 1,
				duration: 2,
				beforeStart: function(){
					highlights[currentHighlight].setStyle({
						left: '0'
					});
				}
			})], {
				queue: {
					position: 'end',
					scope: 'highlightQueue',
					limit: 1
				}
			});
		}
		else {
			previousHighlight = currentHighlight;
			currentHighlight = currentHighlight - 1;
			
			new Effect.Parallel([new Effect.Opacity(highlights[previousHighlight], {
				sync: true,
				to: 0,
				duration: 2,
				afterFinish: function(){
					highlights[previousHighlight].setStyle({
						left: '-999em'
					});
				}
			}), new Effect.Opacity(highlights[currentHighlight], {
				sync: true,
				to: 1,
				duration: 2,
				beforeStart: function(){
					highlights[currentHighlight].setStyle({
						left: '0'
					});
				}
			})], {
				queue: {
					position: 'end',
					scope: 'highlightQueue',
					limit: 1
				}
			});
		}
	});
	previousButton.insert({ bottom: previousAnchor });
	navigation.insert({ top: previousButton });
	
	nextButton = new Element('li');
	nextAnchor = new Element('a', { href: "#"}).update("Next");
	nextAnchor.observe('click', function(event){
		if(currentHighlight == highlights.size()-1) {
			previousHighlight = currentHighlight;
			currentHighlight = 0;
			
			new Effect.Parallel([new Effect.Opacity(highlights[previousHighlight], {
				sync: true,
				to: 0,
				duration: 2,
				afterFinish: function(){
					highlights[previousHighlight].setStyle({
						left: '-999em'
					});
				}
			}), new Effect.Opacity(highlights[currentHighlight], {
				sync: true,
				to: 1,
				duration: 2,
				beforeStart: function(){
					highlights[currentHighlight].setStyle({
						left: '0'
					});
				}
			})], {
				queue: {
					position: 'end',
					scope: 'highlightQueue',
					limit: 1
				}
			});
		}
		else {
			previousHighlight = currentHighlight;
			currentHighlight = currentHighlight + 1;
			
			new Effect.Parallel([new Effect.Opacity(highlights[previousHighlight], {
				sync: true,
				to: 0,
				duration: 2,
				afterFinish: function(){
					highlights[previousHighlight].setStyle({
						left: '-999em'
					});
				}
			}), new Effect.Opacity(highlights[currentHighlight], {
				sync: true,
				to: 1,
				duration: 2,
				beforeStart: function(){
					highlights[currentHighlight].setStyle({
						left: '0'
					});
				}
			})], {
				queue: {
					position: 'end',
					scope: 'highlightQueue',
					limit: 1
				}
			});
		}
	});
	nextButton.insert({ bottom: nextAnchor });
	navigation.insert({ bottom: nextButton });
	
	content.insert({ top: navigation });
	
	var navigationLinks = navigation.childElements();
	navigationLinks[currentHighlight+1].down('a').toggleClassName('selected');
	
	carousel = new PeriodicalExecuter(function(){
		if(currentHighlight == highlights.size()-1) {
			
			previousHighlight = currentHighlight;
			currentHighlight = 0;
			
			new Effect.Parallel([new Effect.Opacity(highlights[previousHighlight], {
				sync: true,
				to: 0,
				duration: 2,
				afterFinish: function(){
					highlights[previousHighlight].setStyle({
						left: '-999em'
					});
				}
			}), new Effect.Opacity(highlights[currentHighlight], {
				sync: true,
				to: 1,
				duration: 2,
				beforeStart: function(){
					highlights[currentHighlight].setStyle({
						left: '0'
					});
				}
			})], {
				queue: {
					position: 'end',
					scope: 'highlightQueue',
					limit: 1
				}
			});
			
			navigationLinks[previousHighlight+1].down('a').toggleClassName('selected');
			navigationLinks[1].down('a').toggleClassName('selected');
		}
		else {
			previousHighlight = currentHighlight;
			currentHighlight = currentHighlight + 1;
			
			new Effect.Parallel([new Effect.Opacity(highlights[previousHighlight], {
				sync: true,
				to: 0,
				duration: 2,
				afterFinish: function(){
					highlights[previousHighlight].setStyle({
						left: '-999em'
					});
				}
			}), new Effect.Opacity(highlights[currentHighlight], {
				sync: true,
				to: 1,
				duration: 2,
				beforeStart: function(){
					highlights[currentHighlight].setStyle({
						left: '0'
					});
				}
			})], {
				queue: {
					position: 'end',
					scope: 'highlightQueue',
					limit: 1
				}
			});
			
			
			navigationLinks[previousHighlight+1].down('a').toggleClassName('selected');
			navigationLinks[currentHighlight+1].down('a').toggleClassName('selected');
		}
	}, 5);

	navigationLinks.each(function(navLink, index){
		
		if(index){
			navLink.down('a').toggleClassName('carouselLink');
		}
		
		navLink.observe('click', function(event){
			navigationLinks.each(function(hiddenLink){
				hiddenLink.down('a').removeClassName('selected');
			});
			navigationLinks[currentHighlight+1].down('a').addClassName('selected');
			carousel.stop();
		});
	});
	
}


// Site Search Categories
// Note: Prototype driven.
function siteSearch() {
	var categoryButtons = $$('#siteSearch h3, #searchCategories li label dfn');
	
	$('siteSearch').down('h3').setStyle({
		display: 'block'
	});
	
	categoryButtons.each(function(categoryButton){
		categoryButton.observe('click', function(event){
			categoryButtons[0].innerHTML = "<span>"+categoryButton.innerHTML+"</span>";
			$('siteSearch').toggleClassName('open');
			if(categoryButton.previous('input')) {
				categoryButton.previous('input').checked = true;
			}
		});
	});
	
	var categoryLabels = $$('#searchCategories li label');
	categoryLabels.each(function(categoryLabel){
		categoryLabel.observe('mouseover', function(event){
			categoryLabel.toggleClassName('hover');
		});
		categoryLabel.observe('mouseout', function(event){
			categoryLabel.toggleClassName('hover');
		});
	});
}

//Sliding Content Bars
//Note: Prototype & Scriptaculous Driven
function slidingContent() {
	var slidingButtons = $$('div.highlight.expandable > h3');
	var queue = Effect.Queues.get('slidingQueue');

	slidingButtons.each(function(slidingButton){
		
		morphThis = slidingButton.up('div.highlight');
		morphThis.initialHeight = morphThis.getHeight();
		
		if (readCookie(slidingButton.up().identify()) != 'closed') {
			slidingButton.up('div.highlight').setStyle({
				height: morphThis.initialHeight-36 + 'px'
			});
			slidingButton.setStyle({
				cursor: 'pointer'
			});
		}
		else {
			slidingButton.up('div.highlight').setStyle({
				height: '0px'
			});
			slidingButton.setStyle({
				cursor: 'pointer'
			});
			slidingButton.up().toggleClassName('closed');
		}
		
		slidingButton.observe('click', function(event) {
			if(slidingButton.up().hasClassName('closed') == false) {
				if (!queue.effects.size()) {
					setCookie(slidingButton.up().identify(), 'closed');
					
					morphThis = slidingButton.up('div.highlight');
					morphThis.initialHeight = morphThis.getHeight();
					new Effect.Morph(morphThis, {
						style: 'height:0px;', // CSS Properties
						duration: 0.8, // Core Effect properties
						queue: {
							position: 'end',
							scope: 'slidingQueue',
							limit: 1
						}
					});
					slidingButton.up().toggleClassName('closed');
				}
			}
			else {
				if (!queue.effects.size()) {
					setCookie(slidingButton.up().identify(), 'open');
					
					morphThis = slidingButton.up('div.highlight');
					new Effect.Morph(morphThis, {
						style: 'height:' + (morphThis.initialHeight - (morphThis.getHeight())) + 'px;', // CSS Properties
						duration: 0.8, // Core Effect properties
						queue: {
							position: 'end',
							scope: 'slidingQueue',
							limit: 1
						}
					});
					slidingButton.up().toggleClassName('closed');
				}
			}
		});
	});
	
}

//Navigation Hover Assignment
//Note: Prototype Driven
function navHover() {
	var navLIs = $$('#navMain > li');
	
	navLIs.each(function(navLI){
		navLI.observe('mouseenter', function(event){
			navLI.toggleClassName('hover');
		});
		navLI.observe('mouseleave', function(event){
			navLI.removeClassName('hover');
		});
	});
	
	var subnavLIs = $$('#navMain > li > ul > li');
	
	subnavLIs.each(function(subnavLI){
		subnavLI.observe('mouseover', function(event){
			subnavLI.toggleClassName('subhover');
		});
		subnavLI.observe('mouseout', function(event){
			subnavLI.removeClassName('subhover');
		});
	});
}

//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');
	});
	
}

//First and Last OL LI Selector
//Note: Prototype Driven
function olliFirstLast() {
	var firstolLIs =	$$('ol > li:first-child');
	var lastolLIs = $$('ol > li:last-child');
	
	firstolLIs.each(function(olliFirst) {
		olliFirst.addClassName('first');
		});
		
	lastolLIs.each(function(olliLast) {
		olliLast.addClassName('last');
	});
	
}
//Table Striper
//Note: Prototype Driven
function tableStriper() {
	var tableRows = $$('#contentMain #contentArticle div.articleSection table tbody tr:nth-child(2n+1)');
	
	tableRows.each(function(tableRow){
		tableRow.addClassName('odd');
	});
}

//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;
			}
		});
	});
}

// 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 flashReplacement() {
	if($('FlashContent')){
		var flashvars = {};
		flashvars.XMLpath = "/xml/FlashContent.xml";
		flashvars.AllPhonespath = "/Personal/Wireless-Phones/Phones.aspx";
		var params = {};
		params.wmode = "transparent";
		var attributes = {};
		attributes.id = "FlashContent";
		swfobject.embedSWF("/flash/homepage_phones.swf", "FlashContent", "234", "210", "9.0.0", false, flashvars, params, attributes);
	}
}

//Replacement for Window Onload - Loads before images, cross-browser
document.observe("dom:loaded", function() {
	
	//slidingContent();
	highlightCarousel();
	liFirstLast(); // Adds classes 'first' and 'last' to respective LIs
	olliFirstLast();
	navHover();
	inputClear();
	tableStriper();
	flashReplacement();  
	siteSearch();
	
});
