

window.addEvent('load', function() {

	externalLinks(); //Replace all "rel=external" with "target = _blank" to ensure XHTML strict validation

	$('nav_prev').setStyle ('opacity','0');

	var content = 'content';
	var overlay = 'overlay';

	fadeArrows = new Fx.Styles('overlay', {duration:300, wait:false});	
	fadeNav = new Fx.Styles('nav', {duration:300, wait:false});	
	slide = new Fx.Styles('content', {duration:1500, transition:Fx.Transitions.Back.easeInOut, wait:false});
	
	baseCoord = -340;
	increment = 1200;
	var iteration = 1;
	
	sectionNum = 0;
	currentSection = 'home';
	currentService = 'overview';	

	// --------- add the click event and slide coordinate to the header home a tag------------		
	
		
		var slideCoord = ( baseCoord );
		var homesection = 'home';
		
		$$('#header a').addEvent('click',function(){ 	 
		
			slideContent(homesection)

		});



	// --------- add the click events to main nav  and populate the sectionArray		
	

	sectionList = ['home'];
	sectionCoords = [baseCoord];
	sectionArr =[];

	var list = $$('#nav li');
	
	list.each(function(element) { 
	
		var slideCoord = ( baseCoord  - (increment * iteration));

		
		var el = element.id;
		var sectNameArr = el.split("_");
		var sectionName = sectNameArr[1];
		
		
		sectionList.push(sectionName);	
		sectionCoords.push(slideCoord);
		
		element.addEvent('click',function(){ 	 
			
			if (sectionName != currentSection) {
			
			slideContent(sectionName);
			
			}
		});

		iteration++;

		

	// --------- add the mouseover events to main nav------------		


		var mofx = new Fx.Styles(element, {duration:200, wait:false})

		element.addEvent('mouseenter', function(){ 
		
			mofx.start ({ 'color':'#0083a9' });
		
		});


		element.addEvent('mouseleave', function(){ 
		
			mofx.start ({ 'color':'#000000' });
		
		});
	
	
	}); // --------- close the list.each loop ---------------			
	

	//Create an associative array with section names and coordinates
	sectionPos = sectionCoords.associate(sectionList); 



	// --------- add the click events homepage nav------------		
	
	var iteration = 1;
	
	var homelist = $$('#homenav li');
	
	homelist.each(function(element) { 
	
		var elName = element.id;
		var nameArr = elName.split("_");
		var sectName = nameArr[1];

		element.addEvent('click',function(){ 	 
		
			slideContent(sectName);

		});

		iteration++;

	}); // --------- close the homelist.each loop ---------------			
	



	
	// ---- add the overlay next and prev button code -------
	
	$('nav_next').addEvent ('click', function(){
	
		sectionNum = sectionList.indexOf(currentSection); //determine numerical value of the current section
		sectionNum ++;
		section = (sectionList[sectionNum]);
		slideContent(section);
	
	});
	

	$('nav_prev').addEvent ('click', function(){
	
		sectionNum = sectionList.indexOf(currentSection);
		sectionNum --;
		section = (sectionList[sectionNum]);
		slideContent(section);
		
	});




// ---- add the 'green' button code ----

	$('greenbtn').addEvent ('click', function(){
	
		var greenslide = new Fx.Styles('greentext', {duration:300, transition:Fx.Transitions.Back.easeOut, wait:false});
	
		var currPos = $('greentext').getStyle('margin-top').toInt();

		if (currPos == 11) { //text is currently deployed so set the margin to hide it
			togglePos = 155;
		} else {
			togglePos = 11;
		}
			
			greenslide.start ({
				
				'margin-top':togglePos
				
			});
	});


//----- Setup the Services Subnav ------

	
	var list = $$('#subnav dt');

	
	list.each(function(element) { 

		var mofx = new Fx.Styles(element, {duration:200, wait:false})

		element.addEvent('mouseenter', function(){ 
		
			mofx.start ({ 
			
				'left':'80'
				
				 });
		
			$E('a', element.id).setStyle('background-position-y','-24px');
		
		});


		element.addEvent('mouseleave', function(){ 
		

				mofx.start ({ 
			
					'left':'70'
				
				 });

			if (element.id != "dt_" + currentService) { //only unhiglight if it is not the currently selected service

				$E('a', element.id).setStyle('background-position-y','0');

				 
			} else {
			
				$E('a', element.id).setStyle('background-position-y', '-24px');
				
			}
		
		
		});

		
		
		var elName = element.id;
		var serviceArr = elName.split("_");
		var newService = serviceArr[1];

		element.addEvent('click',function(){ 	 
		
			slideSubnav(newService)

		});
		
		
});


// ---- Setup the AJAX form submission ----



$('requestform').addEvent('submit', function(e) {

	new Event(e).stop();
 
	var formstatus = $('formstatus').empty().addClass('ajax-loading');
 
	this.send({
		update: formstatus,
		onComplete: function() {
			formstatus.removeClass('ajax-loading');
			
		}
	});
	
});



// ----subnav content sliding function -----

 function slideSubnav(newService) {
 
 	var currentEl = "dd_" + currentService;
 	var newEl = "dd_" + newService;
 	var currentNav = "dt_" + currentService;
 	var slideOff = new Fx.Styles(currentEl, {duration:400, transition:Fx.Transitions.Expo.easeIn, wait:false});
 	var slideOn = new Fx.Styles(newEl, {duration:400, transition:Fx.Transitions.Expo.easeOut, wait:false});
	var slidePrevNav = new Fx.Styles(currentNav, {duration:200, wait:false})


			
	var clickHL = $E('a', currentNav);
	
	clickHL.setStyle('background-position-y', '0');



	slideOff.start({
	
	'opacity':0,
	'top':'-300px'
	
	}).chain(function(){
			
				$(currentEl).setStyle('top','500px'); //resets the position
	});


	slideOn.start.delay(400, slideOn, {
	
	'opacity':1,
	'top':'100px'
	
	});
	
		
	currentService = newService;
 
 }




// -------- the main sliding method -----------
	
	function slideContent(sectName) {
		
		newSection = sectName;
		
		newPos = sectionPos[newSection];
			
		fadeArrows.start({'opacity':0});
	
		titleDisplay(currentSection, 1);

		slide.start({ 

			
			'left': newPos 
			
			}).chain(function(){
			
				fadeArrows.start({
			
				'opacity':1
				
				});
				
	
					
	
				currentSection = newSection;

				titleDisplay(newSection, 0);
			
				resetSubs(); //reset the subnavigation to their openining states

				navDisplay();
				
			});
			
			

	}	// end main sliding method -----------	



// ---- Reset the Subnav function

	function resetSubs() {

	//first reset current sub dt and dd elements to their original states
	$("dd_" + currentService).setStyle('top','500px'); 	
	$E('a', "dt_" + currentService).setStyle('background-position-y', '0');

	//now switch the currentService to "overview" and set the dt and dd elements appropriately
	currentService = "overview";
	$E('a', "dt_" + currentService).setStyle('background-position-y', '-24px');
	$('dd_overview').setStyle('opacity', '1');
	$('dd_overview').setStyle('top','100px');
	
	}




//-------------- display or hide the next or prev nav --------

function navDisplay() {

//checks to see if we are displaying the first or last page based or section array index


	if (sectionList.indexOf(currentSection) == 0 ) {

		$('nav_prev').setStyle('opacity','0');
						
	} 	else {
	
	
		$('nav_prev').setStyle('opacity','1');
		
	}

	if (sectionList.indexOf(currentSection) == (sectionList.length-1))   {

		$('nav_next').setStyle('opacity','0');
						
	} 	else {
	

		$('nav_next').setStyle('opacity','1');
	
	}
	

} //--------- end navDisplay ---------




// -----------Fade the appropriate Section Title to reveal content -----------

function titleDisplay(section,value) {

			
		var h3Fade = $E('h3', section).effects({duration: 300});
		
		if (currentSection != "home") {
		
		
			if (value == 1){ //we want to delay fading back in of the title until after the section has slide away
				
				h3Fade.start.delay(1900, h3Fade, { 'opacity': value });
			
			} else {
			
				h3Fade.start({ 'opacity': value });
			
			}
		
		}
}




// --- Function to replace "rel=external" with "target = _blank" to ensure XHTML strict validation

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"; 
 } 
} 




}); // -----------  close the onload handler ---------------






function getElementPosition(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}




