$(document).ready(function(){
	// Color Animation Test
	//$('#wrapper').animate({backgroundColor:'#0f0'}, 1000);
	
	// ELEVATOR BUTTONS ////////////////////////////////////////////////
	
	// Background image properties
	var nBGHeight = 490;
	var nImagePadding = 10;
	var nTotalImages = 3;
	var nCurImage = 2;
	var bEBUpEnabled = true;
	var bEBDownEnabled = true;
	var nCurBGPosY = (-((nBGHeight*(nCurImage-1))+((nCurImage-1)*nImagePadding)));
	
	// Large message properties
	var nMessageHeight = 150;
	var nTotalMessages = nTotalImages;
	var nCurMessage = nCurImage;
	
	$('#mid').css({backgroundPosition:'0px '+nCurBGPosY+'px'}); // BG init
	$('#largetext').css({backgroundPosition:'0px '+(-(nMessageHeight*(nCurMessage-1)))+'px'}); // Message init
	
	$('.eb-top').mouseenter(function(){
		//if ( bEBUpEnabled ) {
			//$(this).parent().css({backgroundPosition:'0px -87px'});
			$('.elevatorbutton').css({backgroundPosition:'0px -87px'});
		//}
	}).mouseleave(function(){
		//if ( bEBUpEnabled ) {
			$('.elevatorbutton').css({backgroundPosition:''});
		//}
	}).click(function(event){
		// Image scrolls down
		//$('#mid').animate({backgroundPosition:'0px 0px'}, 1500);
		event.preventDefault();
		buttonPress('up');
	});
	
	$('.eb-bottom').mouseenter(function(){
		//if ( bEBDownEnabled ) {
			//$(this).parent().css({backgroundPosition:'0px -151px'});
			$('.elevatorbutton').css({backgroundPosition:'0px -151px'});
		//}
	}).mouseleave(function(){
		//if ( bEBDownEnabled ) {
			$('.elevatorbutton').css({backgroundPosition:''});
		//}
	}).click(function(event){
		// Image scrolls up
		//$('#mid').animate({backgroundPosition:'0px -500px'}, 1500);
		event.preventDefault();
		buttonPress('down');
	});
	
	// Scroll background image after elevator button is clicked
	function buttonPress(s){
		switch ( s ) {
			
			case 'up':
			// Image Scroll
			if ( nCurImage != 1 ) {
				nCurImage--;
				nCurBGPosY = (-((nBGHeight*(nCurImage-1))+((nCurImage-1)*nImagePadding)));
				$('#mid').animate({backgroundPosition:'0px '+nCurBGPosY+'px'}, 1500, function(){
					
				});
				// Message Change
				$('#largetext').animate({opacity:0}, 750, function(){
					// Change message, fade in
					nCurMessage --;
					$(this)
						.css({backgroundPosition:'0px '+(-(nMessageHeight*(nCurMessage-1)))+'px'})
						.animate({opacity:1}, 750);
				})
			} else {
				// Grey out button
				bEBUpEnabled = false;
				bEBDownEnabled = true;
				$('.elevatorbutton').css({backgroundPosition:'-37px -23px'});
			}
			break;
			
			case 'down':
			// Image Scroll
			if ( nCurImage != nTotalImages ) {
				nCurImage++;
				nCurBGPosY = (-((nBGHeight*(nCurImage-1))+((nCurImage-1)*nImagePadding)));
				$('#mid').animate({backgroundPosition:'0px '+nCurBGPosY+'px'}, 1500, function(){
					
				});
				// Message Change
				$('#largetext').animate({opacity:0}, 750, function(){
					// Change message, fade in
					nCurMessage ++;
					$(this)
						.css({backgroundPosition:'0px '+(-(nMessageHeight*(nCurMessage-1)))+'px'})
						.animate({opacity:1}, 750);
				})
			} else {
				// Grey out button
				bEBUpEnabled = true;
				bEBDownEnabled = false;
				$('.elevatorbutton').css({backgroundPosition:'-37px -87px'});
			}
			// Message Change
			break;
				
		} // switch
	} // buttonPress()
	
	/* ====================================================
	Height of footer-wrapper
	======================================================= */
	
	/*var footHeight = $(document).height() - $('#footer-wrapper').offset().top;
	footHeight = footHeight > $(window).height() / 2 ? (footHeight < $('footer').height() ? 'auto' : footHeight) : footHeight;
	$('#footer-wrapper').css({height:( footHeight )})*/
	
	/* ====================================================
	Height of subfooter
	======================================================= */
	
	var footHeight = $(document).height() - $('#subfooter-wrapper').offset().top;
	footHeight = footHeight > $(window).height() / 2 ? (footHeight < $('#subfooter-wrapper').height() ? 'auto' : footHeight) : footHeight;
	$('#subfooter-wrapper').css({height:( footHeight )})
	
	/* ====================================================
	MAIN NAVIGATION
	======================================================= */
	
	var sActiveNav;
	var defaultNavColor = '#0065A4';
	
	// Main Nav State - Based on main nav's HREF and the HREF's of #subnav a
	$('#mainnav li').each(function(){
		$(this)
			.mouseenter(function(){
				//$(this).animate({backgroundColor:getMainNavColor($(this).attr('id'))}, 250)
				$(this).css({backgroundColor:getMainNavColor($(this).attr('id'))})
			})
			.mouseleave(function(){
				if ( $(this).find('a:first').attr('class') != 'active' ) {
					//$(this).animate({backgroundColor:defaultNavColor}, 250)
					$(this).css({backgroundColor:defaultNavColor})
				}
			})
	})
	
	// Sub nav states
	$('.tier2').css({display:'none'})
	$('.tier2 li').each(function(){
		$(this)
			.css({backgroundColor:'#000'}) // Overrides tier1 css without use of !important
			.mouseenter(function(){
				$(this)
					.clearQueue()
					.animate({backgroundColor:'#0065A4'}, 250)
			})
			.mouseleave(function(){
				$(this).animate({backgroundColor:'#000'}, 150)
			})
			.unbind('mouseup')
	})
		
	function getMainNavColor(id){
		switch ( id ) {
			case 'mn-1':
			return '#000000';
			break;
			
			case 'mn-2':
			return '#EF4136';
			break;
			
			case 'mn-3':
			return '#F4B70C';
			break;
			
			case 'mn-4':
			return '#F15A29';
			break;
			
			case 'mn-5':
			return '#262262';
			break;
			
			case 'mn-6':
			return '#999';
			break;
		} // switch 
	} // getMainNavColor
		
	// Set active state for main nav - applies when page is loaded normally without use of ajax
	$('#mainnav li').each(function(){
		if ( location.href.search($(this).find('a').attr('href')) != -1 ) {
			$(this).find('a:first').addClass('active')
			$(this).mouseenter()
		}
	});
	
	// Set active state for sub nav
	$('#subnav a').each(function(){
		if ( location.href.search($(this).attr('href')) != -1 ) {
			$(this).addClass('active')
			//$(this).mouseenter()
		}
	});
	
	/* ----------------------------------------------
	
	Sub page Column Height Constraint
	
	* Match all column heights with the height of ajax-holder
	
	---------------------------------------------- */
	
	function constrainSubCols(){
		//$('.col1, .col2, .col3').css({height:$('#ajax-holder').height()-80}) // Minus height of image-banner (after subtracting negative margin)
		if ( $('.col1').outerHeight() > $('.col2').outerHeight() ) {
			$('.col3').css({height:$('.col1').outerHeight()+5})
		} else {
			$('.col1, .col3').css({height:$('.col2').outerHeight()+5})
			$('.col1').css({height:$('.col1').height()-145})
		}
	}
	
	constrainSubCols();
	
	/* ----------------------------------------------
	
	Redisplay Main Nav Labels
	
	---------------------------------------------- */
	
	/*$('#mainnav').after('<div id="labels" style="position:absolute;margin-top:-28px"></div>');
	$('#mainnav').clone()
		.appendTo('#labels')
	
	$('#labels .tier2').detach();
	$('#labels li').css({background:'none'})*/
	
	/* --------------------------------------------------
	getAnchor : Returns the part of the URL after the '#'
	----------------------------------------------------- */
	
	function getAnchor(){
		var URL = window.location.href;
		return  URL.split('#!')[1];
	}
	
	
	/* ====================================================
	After Page Load Actions
	======================================================= */
	
	function afterLoad(){
		
		// Subnav Behavior
		$('#subnav a').each(function(){
			$(this)
				.click(function(event){
					loadPage($(this))
					event.preventDefault()
				}) // click
				// Set active state based on URL anchor #
				if ( getAnchor() == $(this).attr('id') ) {
					$(this).addClass('active')
				}
		}) // each
		
		$('.image-banner').css({visibility:'visible'})
		
	} // afterLoad()
	
	/* ====================================================
	Drop down navigation
	======================================================= */
	
	// Behavior
	
	$('.tier2').each(function(){
		var curSub = $(this)
		$(this).parent().hoverIntent({
			over:function(){
				curSub
					.css({opacity:0, display:'block'})
					.animate({
						opacity:1
					}, 250)
			},
			out:function(){
				curSub
					.animate({
						opacity:0
					}, 250, function(){$(this).css({display:'none'})})
			},
			timeout:500
		})
	})
	
	/* ====================================================
	Loading Testimonials Into col3 and displaying one at random
	======================================================= */
	
	$('.col3').load('ajax-testimonials.html', function(responseText){
		$(this).html(responseText)
		var whichQuote = Math.floor(Math.random()*$('.quote').length)
		var quoteOffset = $('.sidebarlink').offset().top - $('.image-banner').offset().top - $('.image-banner').height()
		$('.quote:eq('+whichQuote+')')
			.css({display:'block',marginTop:quoteOffset})
	})
	
	/* ====================================================
	Side Bar Links Aligning vertically with Testimonials on right
	======================================================= */
	
	//alert($('.sidebarlink').top())
	
});

/**
* hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne brian(at)cherne(dot)net
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev])}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev])};var handleHover=function(e){var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob)},cfg.timeout)}}};return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover)}})(jQuery);
