//==========================================================================================
// standardpacifichomes.com - GlobalJavascript.js
//==========================================================================================
/* In this file:
	[Global Variables] - any variables needed globally
	[Javascript Functions] - all none jQuery functions
	[jQuery functions on load] - all modules / controls of the site
*/

//==========================================================================================
// BEGIN: Global Variables
var $j = jQuery.noConflict();	
// End: Global Variables


//==========================================================================================
// BEGIN: Functions

// -------------------------------------------------------------------------
// Begin: Image Gallery Overlay
function createPhotoGallery() {
	// Initially set opacity on thumbs and add additional styling for hover effect on thumbs
	var onMouseOutOpacity = 1;
	$j('#thumbs ul.thumbs li').opacityrollover({
		mouseOutOpacity:   onMouseOutOpacity,
		mouseOverOpacity:  1.0,
		fadeSpeed:         'fast',
		exemptionSelector: '.selected'
	});
	
	// Initialize Advanced Galleriffic Gallery
	var gallery = $j('#thumbs').galleriffic({
		delay:                     2500,
		numThumbs:                 5,
		preloadAhead:              10,
		enableTopPager:            false,
		enableBottomPager:         false,
		maxPagesToShow:            7,
		imageContainerSel:         '#slideshow',
		controlsContainerSel:      '#controls',
		captionContainerSel:       '#caption',
		loadingContainerSel:       '#loading',
		renderSSControls:          false,
		renderNavControls:         true,
		//playLinkText:              'Play Slideshow',
		//pauseLinkText:             'Pause Slideshow',
		prevLinkText:              '&lsaquo; Previous Photo',
		nextLinkText:              'Next Photo &rsaquo;',
		nextPageLinkText:          'Next &rsaquo;',
		prevPageLinkText:          '&lsaquo; Prev',
		enableHistory:             false,
		autoStart:                 false,
		syncTransitions:           true,
		defaultTransitionDuration: 900,
		onSlideChange:             function(prevIndex, nextIndex) {
			// 'this' refers to the gallery, which is an extension of $('#thumbs')
			this.find('ul.thumbs').children()
				.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
				.eq(nextIndex).fadeTo('fast', 1.0);
		},
		onPageTransitionOut:       function(callback) {
			this.fadeTo('fast', 0.0, callback);
		},
		onPageTransitionIn:        function() {
			this.fadeTo('fast', 1.0);
		}
	});
}
// End: Image Gallery Overlay

// END: Functions

//==========================================================================================
// BEGIN: jQuery functions on load
$j(document).ready(function() {
    //$j replaces the normal $ in jQuery for this document
	 
	// --------------------------------------------------------------------------------------
	// Begin: Global Functionality
	 
		// If JavaScript is enabled this adds "j_on" to body tag.
		$j("body").addClass("j_on");
		


	// --------------------------------------------------------------------------------------
	// End: Global Functionality

	// --------------------------------------------------------------------------------------
	// Begin: Homepage Functionality

		// Begin: Home Hero Slider
		if ($j('.slider').length) {
		  $j('.slider')
			.before('<div class="filmstrip-index"></div>')
			.cycle({
				 fx: 'fade',
				 timeout: 7000,
				 pause: 2,
				 startingSlide: 0,
				 cleartypeNoBg: true,
				 speed: 'slow',
				 pager: '.filmstrip-index'
			});
		}
		
		$j('.homepage-callouts .callout').click(function(e) {
			var calloutLink = $j(this).children('.callout-link').children('a');
			var linkURL = calloutLink.attr('href');
			document.location = linkURL;
		});
		
		$j('.homepage-callouts .callout').mouseover(function() {
			$j(this).addClass('hover');
		}).mouseout(function(){
			$j(this).removeClass('hover');
		});
		
		// Functionality for play/pause button on homepage slider
		$j('.filmstrip-play-pause').click(function(e) {
		  e.preventDefault();
		  if ($j(this).children('span').hasClass('active')) {
				$j('.slider').cycle('resume');
				$j('.filmstrip-play-pause span').removeClass('active');
		  } else {
				$j('.slider').cycle('pause');
				$j('.filmstrip-play-pause span').addClass('active');
		  }
		});

		// Begin: Home Flash Section
		$j('.jquery-select').selectmenu({
			style:'dropdown',
			menuWidth: 153
		}); // Uses jQUery UI to transform select boxes
	 
		// Added cursor:pointer when hovering over "Find" button
		$j('input.find-button').mouseover(function() {
			$j(this).addClass('hover');
		}).mouseout(function(){
			$j(this).removeClass('hover');
		});
	  
	// --------------------------------------------------------------------------------------
	// End: Homepage Functionality
	
	
	// --------------------------------------------------------------------------------------
	// Begin: Filmstrip Hero Functionality (on Neighborhood & Plan/Spec pages)
	
		// If promotional tab is available on filmstrip hero, display overlay content
		if ($j('.neighborhood-overlay-trigger').length) {
			$j('.content').addClass('overlay-active'); 
		}
		
		// If filmstrip-gallery exists
		if ($j('.filmstrip-gallery').length) {
			
			/* BEGIN: dynamic tab width adjustment for primary tabs */
			var navWidth = $j('ul.filmstrip-gallery-nav','.filmstrip-gallery').width();
			var navLength = $j('ul.filmstrip-gallery-nav','.filmstrip-gallery').children('li').length;
			
			$j('ul.filmstrip-gallery-nav li','.filmstrip-gallery').each(function() {
				// each li has 2px margin on left and right
				$j(this).width( Math.floor((navWidth / navLength) - 4));
			});
			/* END: dynamic tab width adjustment for primary tabs */
			
			/* BEGIN: dynamic thumbnail alignment adjustment for sub nav tabs */
			$j('div.filmstrip-gallery-content','.filmstrip-gallery').each(function(i) {
				var _this = $j(this);
				var tabNavWidth = $j('ul.filmstrip-gallery-nav').width();
				var tabNavLength = $j('ul.filmstrip-gallery-nav').children('li').length;
				var thisTab = $j('ul.filmstrip-gallery-nav li:nth-child('+(i+1)+')');
				var thisTabWidth = thisTab.width()+4;
				
				if($j('.filmstrip-gallery-subtab', _this).length > 0) {
					
					var thisSubNav = $j('ul.filmstrip-gallery-subtab-nav', _this);
					var thisLeftPosition = thisTab.position().left;
					var thisSubNavLength = thisSubNav.children('li').length;
					var calcSubNavWidth = thisSubNavLength * (86+4);
					
					// initially center all thumbs over the tab
					thisSubNav.css({left:thisLeftPosition+(thisTabWidth*.5), marginLeft: (calcSubNavWidth*.5)*-1});
					
					// case specific adjustments for dynamically centered or left/right aligned thumbs, depending on number of thumbs and which tab parent
					if(calcSubNavWidth > thisTabWidth) {
						if(tabNavLength == 5) {
							if(thisTab.index() == 0) {
								thisSubNav.css({left:0, marginLeft: 0});
							}
							if(thisTab.index() == 1 && calcSubNavWidth > (thisTabWidth*2)) {
								thisSubNav.css({left:0, marginLeft: 0});
							}
							if(thisTab.index() == 2 && calcSubNavWidth > (thisTabWidth*3)) {
								//thisSubNav.css({left:0, marginLeft: 0});
							}
							if(thisTab.index() == 3 && calcSubNavWidth > thisTabWidth*2) {
								thisSubNav.css({left:'auto', marginLeft: 0, right:0});
							}
							if(thisTab.index() == 4) {
								thisSubNav.css({left:'auto', marginLeft: 0, right:0});
							}
						} 
						
						if(tabNavLength == 4) {
							if(thisTab.index() == 0) {
								thisSubNav.css({left:0, marginLeft: 0});
							}
							if(thisTab.index() == 1 && calcSubNavWidth > (thisTabWidth*2)) {
								thisSubNav.css({left:0, marginLeft: 0});
							}
							if(thisTab.index() == 2 && calcSubNavWidth > (thisTabWidth*2)) {
								thisSubNav.css({left:'auto', marginLeft: 0, right:0});
							}
							if(thisTab.index() == 3 && calcSubNavWidth > thisTabWidth*2) {
								thisSubNav.css({left:'auto', marginLeft: 0, right:0});
							}
						}
						
						if(tabNavLength == 3) {
							if(thisTab.index() == 0) {
								thisSubNav.css({left:0, marginLeft: 0});
							}
							if(thisTab.index() == 1 && calcSubNavWidth > (thisTabWidth*2)) {
								//thisSubNav.css({left:0, marginLeft: 0});
							}
							if(thisTab.index() == 2 && calcSubNavWidth > (thisTabWidth*2)) {
								thisSubNav.css({left:'auto', marginLeft: 0, right:0});
							}
						}
						
						if(tabNavLength == 2) {
							if(thisTab.index() == 0) {
								thisSubNav.css({left:0, marginLeft: 0});
							}
							if(thisTab.index() == 1) {
								thisSubNav.css({left:'auto', marginLeft: 0, right:0});
							}
						}
					}
				}
			});
			/* END: dynamic thumbnail alignment adjustment for sub nav tabs */
			
			$j(".filmstrip-gallery").tabs({
				fx: { opacity: 'toggle' },
				show: function(event, ui) {
					 $j(ui.panel).find('.filmstrip-gallery-subtab-content:visible .panel-quote').addClass('active_quote').delay(2200).fadeIn('slow');
					 $j(ui.panel).find('.across-panel').addClass('active_quote').delay(2200).fadeIn('slow');
				}
			});
			  
			$j(".filmstrip-gallery-subtab").tabs({
				fx: { opacity: 'toggle' },
				show: function(event, ui) {
					 $j('.filmstrip-gallery .active_quote', this).hide();
					 $j('.panel-quote', ui.panel).addClass('active_quote').delay(2200).fadeIn('slow');
				}
			});
			
			$j(".filmstrip-gallery-nav").hover(function() {
				$j(".filmstrip-gallery-subtab-nav").fadeIn('fast');
				}, function() {
				$j(".filmstrip-gallery-subtab-nav").hide();
			});
			  
			$j(".filmstrip-gallery-subtab-nav").hover(function() {
				$j(".filmstrip-gallery-subtab-nav").show();
				}, function() {
				$j(".filmstrip-gallery-subtab-nav").hide();
			});
			
			$j('.neighborhood-overlay-trigger, .neighborhood-hero-overlay .close-overlay').click(function() {
				$j('.neighborhood-hero-overlay').toggle().parents('.neighborhood-overlay:first').toggleClass('overlay-on');
			});
		 
		 }
		
		 //filmstrip-container (is this old???? )
		 if ($j('ul.filmstrip-container').length) {
			  $j("ul.filmstrip-container").each(function() {
					var item_li = $j(this).find("li");
					var items_width = item_li.width() * item_li.length;
					$j(this).width(items_width + 'px');
			  });
		 }
	// --------------------------------------------------------------------------------------
	// End: Filmstrip Hero Functionality (on Neighborhood & Plan/Spec pages)
	 
	// --------------------------------------------------------------------------------------
	// Begin: Misc Functionality
		
		// Displays additional content hidden on load upon clicking the "More" button.
		// Sometimes used on Plan/Spec pages. 
		$j('.more-expand').click(function(e) {
		  e.preventDefault();
		  var content = $j(this).attr('rel');
		  $j(content).slideToggle();
		  $j(this).hide();
		});
		
		$j('.more-chevron').live('click',function(e) {
		  e.preventDefault();
		  var content = $j(this).next('.district-group');
		  $j(content).show();
		  $j(this).removeClass('more-chevron');
		  $j(this).addClass('less-chevron');
		});
		
		$j('.less-chevron').live('click',function(e) {
		  e.preventDefault();
		  var content = $j(this).next('.district-group');
		  $j(content).hide();
		  $j(this).removeClass('less-chevron');
		  $j(this).addClass('more-chevron');
		});
		
		/* Share and See Callout - Hover functions to switch background image */
		$j('.share-with-friend img').mouseover(function() {
			$j(this).attr('src','/_Images/Global/share-with-friend-hover.png');
		}).mouseout(function(){
			$j(this).attr('src','/_Images/Global/share-with-friend.png');
		});
		
		$j('.see-more-neighborhoods img').mouseover(function() {
			$j(this).attr('src','/_Images/Global/see-more-neighborhoods-hover.png');
		}).mouseout(function(){
			$j(this).attr('src','/_Images/Global/see-more-neighborhoods.png');
		});
	 
	// --------------------------------------------------------------------------------------
	// End: Misc Functionality

});

