/******************************************************
 * HOME/SPLASH NAVIGATION CODE
 *****************************************************/
var SplashNavigation = new Object();

SplashNavigation.Initialize = function () {
    $( 'div#splashNav ul > li' ).live( 'mouseover', function() { 
		$( 'div#splashNav ul' ).removeClass();
		$( 'div#splashNav ul' ).addClass( this.id );
    } );

    $( 'div#splashNav ul' ).live( 'mouseout', function() { 
		$( 'div#splashNav ul' ).removeClass();
    } );

    $( 'div#splashNav ul > li' ).css( 'cursor', 'pointer' );
} // SplashNavigation.Initialize

/******************************************************
 * MAIN NAVIGATION CODE
 *****************************************************/
var MainNavigation = new Object();

/*
MainNavigation.Initialize = function () {
    $('ul#nav li ul li').hover( 
			       function() { 
				   $(this).children( 'ul' ).toggle();
			       },
			       function() { 
				   $(this).children( 'ul' ).toggle();
			       } );

    $('ul#nav li ul li ul li').hover( 
				     function() { 
	                                 $(this).children( 'ul' ).toggle();
                                     },
				     function() { 
					 $(this).children( 'ul' ).toggle();
				     } );
} // MainNavigation.Initialize
*/

/******************************************************
 * MAIN NAVIGATION CODE - PAGER
 *****************************************************/

jQuery.fn.navPager = function(op) {
    
    var showPagedItem = function (idx) {
		// Update paged item index
		this.paged_item_index = parseInt( idx );
		
		// Grab the first "real" LI (the current paged item)
		var pagedLI = $( 'li', this ).eq( this.paged_item_index + 2 );
		
		// Populate the top empty LI with the info from the current paged
		$( 'li:eq(0)', this ).html( pagedLI.html() );
	
		// If this we are currently showing this item
		var isShowing = this.selected_index == this.paged_item_index;
		$( 'li:eq(0)', this )
		[ isShowing ? 'addClass' : 'removeClass' ]
		( 'sf-breadcrumb' );
	
		// Handle disabled status of prev/next
		var disablePrev = this.paged_item_index == 0;
		$( 'li:eq(1) a:eq(0)', this )
		[ disablePrev ? 'addClass' : 'removeClass' ]( 'disabled' );
	
		var disableNext = this.paged_item_index + 1 >= this.paged_items_count;
		$( 'li:eq(1) a:eq(1)', this )
		[ disableNext ? 'addClass' : 'removeClass' ]( 'disabled' );
	
	};
	
	var goToPageAtIdx = function (idx) {
		// Update paged item index
		this.paged_item_index = parseInt( idx );
		
		// Grab the first "real" LI (the current paged item)
		var pagedLI = $( 'li', this ).eq( this.paged_item_index + 2 );

		document.location.href = $('a', pagedLI).attr("href");
	}
	
	var showNextPagedItem = function () {
		// If we aren't disabled
		if( !$( 'li:eq(1) a:eq(1)', this ).hasClass( 'disabled' ) ) {
		    // Get next index
		    var nextIdx = this.paged_item_index + 1;

			goToPageAtIdx.call( this, [ nextIdx ] );
	
		    // Show next item
/* 		    showPagedItem.call( this, [ nextIdx ] ); */
		}
    };

    var showPrevPagedItem = function () {
		
		// If we aren't disabled
		if( !$( 'li:eq(1) a:eq(0)', this ).hasClass( 'disabled' ) ) {
		    // Get prev index
		    var prevIdx = this.paged_item_index - 1;

			goToPageAtIdx.call( this, [ prevIdx ] );

		    // Show next item
/* 		    showPagedItem.call( this, [ prevIdx ] ); */
		}
    };

    this.each( function () {
		var meth = 'navPager: ';
	
		// Store number of items that will be paged
		this.paged_items_count = $( 'li', this ).length;
		
		// Hide all LIs
		$( 'li', this ).hide();
		
		// Add a pager LI to the top of the list
		$( this ).prepend( '<li class="pager"><a href="#">Previous</a> | <a href="#">Next Project</a></li>' );
	
		// Add an empty LI to the top of the list
		$( this ).prepend( '<li><a href="#"></a></li>' );
	
		// Make sure first two elements are shown
		$( 'li:lt(2)', this ).show();
		
		// Setup handlers for prev/next links
		var containerUL = this;
		$( 'li:eq(1) a:eq(1)', this ).click( function () {
		    showNextPagedItem.call( containerUL );
		    return false;
		} );
	
		$( 'li:eq(1) a:eq(0)', this ).click( function () {
		    showPrevPagedItem.call( containerUL );
		    return false;
		} )
		
		// Figure out if we currently showing a paged item
		var selectedIdx = null;
		for( var i = 0; i < this.paged_items_count+2; i++ ) {
		    var item = $( 'li', this ).eq( i );
		    if( item.hasClass( 'sf-breadcrumb' ) ) {
			selectedIdx = i - 2;
			break;
		    }
		}
		
		this.selected_index = selectedIdx;
	
		// If we are not showing a paged item
		if( selectedIdx == null ) {
		    selectedIdx = 0; // then default to first item
		}
	
		showPagedItem.call( this, [ selectedIdx ] );
    } );

    return this;
};



/******************************************************
 * ABOUT NAVIGATION CODE
 *****************************************************/
var AboutNavigation = new Object();

AboutNavigation.Initialize = function () {
    $( 'li#nav-mi-about-company' ).hover( 
	function() { 
	    $('body#about li#nav-mi-about-company').addClass("selected");
	    $('body#about ul#nav-menu-about-company').addClass("hover");
	},
	function() { 
	    $('body#about li#nav-mi-about-company').removeClass("selected");
	    $('body#about ul#nav-menu-about-company').removeClass("hover"); 
	} );

    $( 'li#nav-mi-about-people' ).hover( 
	function() { 
	    $('body#about li#nav-mi-about-people').addClass("selected");
	    $('body#about ul#nav-menu-about-people').addClass("hover");
	},
	function() { 
	    $('body#about li#nav-mi-about-people').removeClass("selected");
	    $('body#about ul#nav-menu-about-people').removeClass("hover"); 
	} );

    $( 'li#nav-mi-about-stories' ).hover( 
	function() { 
	    $('body#about li#nav-mi-about-stories').addClass("selected");
	    $('body#about ul#nav-menu-about-stories').addClass("hover");
	},
	function() { 
	    $('body#about li#nav-mi-about-stories').removeClass("selected");
	    $('body#about ul#nav-menu-about-stories').removeClass("hover"); 
	} );
} // AboutNavigation.Initialize

/******************************************************
 * WORK NAVIGATION CODE
 *****************************************************/
var WorkNavigation = new Object();

WorkNavigation.Initialize = function () {
    $( 'li#nav-mi-work-by-market' ).hover( 
	function() { 
	    $('body#work li#nav-mi-work-by-market').addClass("selected");
	    $('body#work ul#nav-menu-work-by-market').addClass("hover");
	},
	function() { 
	    $('body#work li#nav-mi-work-by-market').removeClass("selected");
	    $('body#work ul#nav-menu-work-by-market').removeClass("hover"); 
	} );

    $( 'li#nav-mi-work-by-service' ).hover( 
	function() { 
	    $('body#work li#nav-mi-work-by-service').addClass("selected");
	    $('body#work ul#nav-menu-work-by-service').addClass("hover");
	},
	function() { 
	    $('body#work li#nav-mi-work-by-service').removeClass("selected");
	    $('body#work ul#nav-menu-work-by-service').removeClass("hover"); 
	} );
} // WorkNavigation.Initialize

/******************************************************
 * SERVICE ICONS CODE
 *****************************************************/
var ServiceIcons = new Object();

ServiceIcons.Initialize = function () {
    $( 'ul#icons > li.sustainable-design' ).hover( 
	function() { $( 'ul#iconCaptions li.sustainable-design' ).show(); },
        function() { $( 'ul#iconCaptions li.sustainable-design' ).hide(); } );

    $( 'ul#icons > li.wayfinding' ).hover( 
	function() { $( 'ul#iconCaptions li.wayfinding' ).show(); },
        function() { $( 'ul#iconCaptions li.wayfinding' ).hide(); } );

    $( 'ul#icons > li.information-design' ).hover( 
	function() { $( 'ul#iconCaptions li.information-design' ).show(); },
        function() { $( 'ul#iconCaptions li.information-design' ).hide(); } );

    $( 'ul#icons > li.visioning' ).hover( 
	function() { $( 'ul#iconCaptions li.visioning' ).show(); },
        function() { $( 'ul#iconCaptions li.visioning' ).hide(); } );

    $( 'ul#iconCaptions li' ).css( 'cursor', 'pointer' );
} // ServiceIcons.Initialize

/******************************************************
 * GALLERY CODE (depends on jcarousel)
 *****************************************************/
var Gallery = new Object();

Gallery.Initialize = function () { 
    $( '#gallery' ).jcarousel( {
	// Configuration goes here
	scroll         : 1,
	visible        : 1,
	initCallback   : Gallery.InitializeControls,
	buttonNextHTML : null,
	buttonPrevHTML : null
    } ); // jcarousel
} // Initialize

Gallery.InitializeControls = function (carousel) {

    var nextLink    = $( '#gallery-next' );
    var prevLink    = $( '#gallery-prev' );
    var gallerySize = carousel.size();

    //$( '#imageCaptionBox' ).show();
    $( '#imageCaptionBox' ).css( 'visibility', 'visible' );

    // Make prev/next links control carousel
    nextLink.click( function () {
	carousel.startAuto( 0 ); /* disable auto-scrolling is user clicks button */
	carousel.next();
	Gallery.ButtonClicked( carousel );
	return false;
    } );

    prevLink.click( function () {
	carousel.startAuto( 0 ); /* disable auto-scrolling is user clicks button */
	carousel.prev();
	Gallery.ButtonClicked( carousel );
	return false;
    } );

    // Disable next button if 1 item or less
    if( gallerySize <= 1 )
	nextLink.parent( 'li' ).addClass( 'end' );

    // Show total items in gallery
    $( '.gallery-size' ).html( gallerySize );

    return false;
} // InitializeControls

Gallery.ButtonClicked = function (carousel) {
    Gallery.UpdateCaption( carousel );
    return Gallery.UpdatePagination( carousel );
} // ButtonClicked

Gallery.UpdatePagination = function (carousel) {
    var nextLink    = $( '#gallery-next' );
    var prevLink    = $( '#gallery-prev' );
    var gallerySize = carousel.size();
    var index       = carousel.first; // index starts at 1
    
    if( index == gallerySize ) {
	nextLink.parent( 'li' ).addClass( 'end' );
    }
    else {
	nextLink.parent( 'li' ).removeClass( 'end' );
    }

    if( index == 1 ) {
	prevLink.parent( 'li' ).addClass( 'end' );
    }
    else {
	prevLink.parent( 'li' ).removeClass( 'end' );
    }

    $( '.gallery-index' ).html( index );

    return false;
} // UpdatePagination

Gallery.UpdateCaption = function (carousel) {
    var index = carousel.first; /* index of currently active image */

    if( index <= imageGalleryCaptions.length ) {
	if( $('#imageCaptionBox-Caption p').html() != imageGalleryCaptions[index - 1] ) {
	    $('#imageCaptionBox-Caption p').fadeOut('fast').hide().html( imageGalleryCaptions[index - 1] ).fadeIn('slow');
	}
    }
} // UpdateCaption

//For display job listings on the careers page
function displayJobListing(divToShow) {
	$('#sideText').children().hide();
	$(divToShow).show();
}
