// controls the submenus  
jQuery(function(){

	// we need the immediate descendant selector here
	// this will have to be changed if/when we implement submenus
	var MenuLIs = jQuery('#nav > ul > li');
	MenuLIs.bind('mouseenter', function(){
		var realTarget = jQuery(this);
		
		// fade in the child of the target
		realTarget.children('ul').fadeIn('fast');
		return false;
	})
	.bind('mouseleave', function(){
		var realTarget = jQuery(this);
		realTarget.children('ul').fadeOut('slow');
		return false;
	});
});

// controls the tertiary menus
jQuery(function(){

	// we need the immediate descendant selector here
	// this will have to be changed if/when we implement submenus
	var MenuLIs = jQuery('#nav > ul > li > ul > li');
		
	MenuLIs.bind('mouseenter', function(){
		var realTarget = jQuery(this);
		var ParentWidth = realTarget.outerWidth() + 2;

		// fade in the child of the target
		if (realTarget.offset().left + realTarget.width() + 200 > jQuery(document).width()) {
			// if too far right, display to the left
			realTarget.children('ul').css('left', (-1 * ParentWidth) + 'px');
		}
		else {
			// otherwise, display on right
			realTarget.children('ul').css('left', ParentWidth + 'px');
		}
		realTarget.children('ul').fadeIn('fast');
		// return false;
	})
	.bind('mouseleave', function(){
		var realTarget = jQuery(this);
		realTarget.children('ul').fadeOut('slow');
		// return false;
	});
});

// controls the office rollovers
var currentOffice = 'Washington';

jQuery(function(){

	var OfficeLIs = jQuery('#offices > ul > li');
	
	OfficeLIs.bind('mouseenter', function(){
		var theTarget = jQuery(this);
		var theOffice = theTarget.attr('id');
		var contentDiv = jQuery('#address' + theOffice);
		
		if(theOffice != currentOffice){
			theTarget.children('img').css('display','block');
			// contentDiv.css('display','block');
		}
	})
	.bind('click',function(){
		var theTarget = jQuery(this);
		var theOffice = theTarget.attr('id');

		jQuery('#arrow' + currentOffice).hide();
		jQuery('#address' + currentOffice).hide();
		
		jQuery('#arrow' + theTarget.attr('id')).css('display','block');
		jQuery('#address' + theTarget.attr('id')).css('display','block');

		currentOffice = theOffice;
		
		return false;
	})
	.bind('mouseleave',function(){
		var theTarget = jQuery(this);
		var theOffice = theTarget.attr('id');
		var contentDiv = jQuery('#address' + theOffice);

		if(theOffice != currentOffice){
			theTarget.children('img').hide();
			// contentDiv.hide();
		}
	})

});


// for the home page tabs
var currentTab = "Photos";

jQuery(function(){
	var allTabs = jQuery('.tab');
	
	allTabs.bind('click',function(){
		var clickedTab = jQuery(this).attr('id');
				
		jQuery('#content' + currentTab).hide();
		jQuery('#tab' + currentTab + '> .more').hide();
		jQuery('#tab' + currentTab + '> .tab').show();
		jQuery('#tab' + currentTab).css('backgroundPosition','top left');

		switch(clickedTab){
			case 'Photos':
				jQuery('#tabPhotos > .tab').hide();
				jQuery('#tabPhotos').css('backgroundPosition','bottom right');
				jQuery('#tabSpacerA').css('backgroundPosition','center');
				jQuery('#tabSpacerB').css('backgroundPosition','top');
				jQuery('#contentPhotos').show();
				jQuery('#tabPhotos > .more').show();
				break;
			case 'Video':
				jQuery('#tabVideo > .tab').hide();
				jQuery('#tabVideo').css('backgroundPosition','bottom right');
				jQuery('#tabSpacerA').css('backgroundPosition','bottom');
				jQuery('#tabSpacerB').css('backgroundPosition','center');
				jQuery('#contentVideo').show();
				jQuery('#tabVideo > .more').show();
				break;
			case 'Audio':
				jQuery('#tabAudio > .tab').hide();
				jQuery('#tabAudio').css('backgroundPosition','bottom right');
				jQuery('#tabSpacerA').css('backgroundPosition','top');
				jQuery('#tabSpacerB').css('backgroundPosition','bottom');
				jQuery('#contentAudio').show();
				jQuery('#tabAudio > .more').show();
				break;
			default: break;
		}

		currentTab = clickedTab;
		
		return false;
	})
});


// for the home page photo gallery
var currentPhoto = 'photo1';

jQuery(function(){
	allNavButtons  = jQuery('.photoNavButton');
	
	allNavButtons.bind('click',function(){
		var clicked = jQuery(this).attr('id');
		
		if(clicked != currentPhoto){
			jQuery('#' + currentPhoto + 'Img').hide();							// hide the current photo
			jQuery('#' + currentPhoto + 'Caption').hide();						// hide the current caption
			jQuery('#' + currentPhoto).removeClass('photoNavButton_Active');	// remove the active class from the current photo button
			jQuery('#' + clicked + 'Img').show();								// show the new photo
			jQuery('#' + clicked + 'Caption').show();							// show the new caption
			jQuery('#' + clicked).addClass('photoNavButton_Active');			// set the active class on the new photo button
			currentPhoto = clicked;												// set the current photo var
		}
		
		return false;
	})
});

jQuery(function(){
	allNavButtons  = jQuery('.photoNavPrevNext');
	
	allNavButtons.bind('click',function(){
		var clicked = jQuery(this).attr('id');
		var maxLen = jQuery('.featuredImage').length;
		var prevPosition = parseInt(currentPhoto.substring(5)) - 1;
		var nextPosition = parseInt(currentPhoto.substring(5)) + 1
		
		if(clicked == 'navButtonPrev' && prevPosition != 0){
			jQuery('#' + currentPhoto + 'Img').hide();								// hide the current photo
			jQuery('#' + currentPhoto + 'Caption').hide();							// hide the current caption
			jQuery('#' + currentPhoto).removeClass('photoNavButton_Active');		// remove the active class from the current photo button
			jQuery('#photo' + prevPosition + 'Img').show();							// show the previous photo
			jQuery('#photo' + prevPosition + 'Caption').show();						// show the new caption
			jQuery('#photo' + prevPosition).addClass('photoNavButton_Active');		// set the active class on the selected photo button
			currentPhoto = 'photo' + prevPosition;									// set the current photo var

		} else if(clicked == 'navButtonNext' && nextPosition <= maxLen) {
			jQuery('#' + currentPhoto + 'Img').hide();								// hide the current photo
			jQuery('#' + currentPhoto + 'Caption').hide();							// hide the current caption
			jQuery('#' + currentPhoto).removeClass('photoNavButton_Active');		// remove the active class from the current photo button
			jQuery('#photo' + nextPosition + 'Img').show();							// show the next photo
			jQuery('#photo' + nextPosition + 'Caption').show();						// show the new caption
			jQuery('#photo' + nextPosition).addClass('photoNavButton_Active');		// set the active class on the next photo button
			currentPhoto = 'photo' + nextPosition;									// set the current photo var
		}
		
		return false;
	})
});

