$(document).ready(function() {

	fancyNavInit();

	function fancyNavInit() {
		//Set up
		var on = '';
		var off = '';
		var subon = 0;
		var suboff = 0;
		//$('#drawer').hide();

		//Do drawer open and close
		$('.drawer-handle, #drawer').hover(
			function(){
				//Open Drawer
				$('#drawer').stop(true, true).delay(250).slideDown('slow',
				    function() {
				      $('.sub-nav-item-default .plane').animate({left: '0'}, 600);
				    }
				);
				$('.drawer-handle').each(function(){ $(this).css('opacity', '.5'); } );
				$(this).css('opacity', '1');
			},
			function(){
				//Close Drawer
				$('#drawer').stop(true, true).delay(300).slideUp('slow',
					function(){
						on = off = subon = suboff = '';
						subon = 0;
						suboff = 0;
						hideAll('.nav-item');
						hideAll('.sub-nav-item');
						$( '.nav-handle' ).removeClass('selected');
						$( '.sub-nav-handle' ).removeClass('selected');
						if ( $.browser.msie && $.browser.version <= 7 ) {
						  $('.plane').css('left', '-320px');
						} else {
						  $('.plane').css('left', '-450px');
						}
						
					}
				);
				$('.drawer-handle').each(function(){ $(this).css('opacity', '1'); } );
			}
		);

		//Do fading between nav items
		function NavFader(className, fadeoff, fadeon) {
			//If on and off are the same thing, we're going from a nav-handle to a nav-item, so do nothing
			if(fadeon !== fadeoff) {
			    var handle = '.' + className.replace('item', 'handle');
			    $( handle ).removeClass('selected');
				//Fade in on

				if(fadeoff === '') {
				  $( handle ).eq(fadeon).addClass('selected');
					$('.'+className).eq(fadeon).stop(true, true).delay(10).fadeIn(500);
					$('.'+className.replace('item', 'handle')+'.'+fadeon).addClass('selected');
					if(className == 'sub-nav-item') {
					$('.'+className).eq(fadeon).children().find('.plane').animate({left: '0'}, 600);
					}

				}
				//Fade out fadeoff, fade in fadeon
				else {
					$('.'+className).eq(fadeoff).stop(true, true).delay(10).fadeOut(500,
						function(){
						    if ( $.browser.msie && $.browser.version <= 7 ) {
    						  $('.plane').css('left', '-320px');
    						} else {
    						  $('.plane').css('left', '-450px');
    						}
						    $( handle).eq(fadeon).addClass('selected');
							$('.'+className).eq(fadeon).stop(true, true).delay(10).fadeIn(500,
								function(){
								  $('.'+className).eq(fadeon).children().find('.plane').animate({left: '0'}, 600);
									//Hide all sub-items
									subon = suboff = 0;
									hideAll('.sub-'+className);
								}
							);
            
						}
					);
					$('.'+className).eq(fadeon).css('background-color', '#fff');
				}
			}
		}

		function hideAll(identifier) {
			$(identifier).each(
				//Hides all the nav-items so we start over when the drawer closes
				function(index) {
					$(this).hide();
				}
			);

			$(identifier+'-default').each(
				function(){
					$(this).show();
				}
			);
			if ( $.browser.msie && $.browser.version <= 7 ) {
			  $('.plane').css('left', '-320px');
			} else {
			  $('.plane').css('left', '-450px');
			}
		}



		//Handle Fade between Nav Items
		$('.nav-handle').each(
			function(index) {
				//this.className += " "+index;
				setUpNavs(this);
			}
		);
		$('.nav-item').each(
			function(index) {
				//this.className += " "+index;
				setUpNavs(this);
				$(this).hide();
			}
		);

		//Handle Fade between SubNav Items
		$('.sub-nav-handle').each(
			function(index) {
				//this.className += " "+index;
				setUpSubNavs(this);
			}
		);
		$('.sub-nav-item').each(
			function(index) {
				//this.className += " "+index;
				setUpSubNavs(this);
				$(this).hide();
			}
		);

		//Add the Fader to each nav-item's hover
		function setUpNavs(item) {
			$(item).hoverIntent(
				function() {
					//nav hover on
					on = $('.' + item.className.replace(' selected', '').replace(' ', '.') ).index(this);
					NavFader('nav-item', off, on);
				},
				function() {
					//nav hover off
					off = $('.' + item.className.replace(' selected', '').replace(' ', '.') ).index(this);
				}
			);
		}

		//Add the Fader to each sub-nav-item's hover
		function setUpSubNavs(item) {
			$(item).hoverIntent(
				function() {
					//subnav hover on
					subon = $('.' + item.className.replace(' selected', '').replace(' ', '.') ).index(this);
					NavFader('sub-nav-item', suboff, subon);
				},
				function() {
					//subnav hover off
					suboff = $('.' + item.className.replace(' selected', '').replace(' ', '.') ).index(this);
				}
			);
		}
	}
		$('.sub-nav-item-default').each(
			function(){
				$(this).show();
			}
		);

});

