/* Footer "Fix" */
function setFooterHeight(containerH){
	var headerH = $('#header').height();
	var mainH = $('.container > .main').height();
	var footerH = $('#footer').height();
	var windowH = $(window).height();
	var bottomH = windowH - footerH - headerH - mainH;
	if ( bottomH > 0 ){
		$('#footer').css('height', footerH + bottomH - 1 +'px' );
	}
}
$(window).resize(function() {
  setFooterHeight();
});

$(document).ready(function(){
	
	/* Detect JS */
	$('body').addClass('js');


	/* Request Information Tab */
	$('#requestinformation').hover(function(){
		$(this).animate({height:'89px'}, 100);
	},function(){
		$(this).animate({height:'84px'}, 200);
	});
	$('#requestinformation').click(function(){
		$(this).animate({height:'88px'}, 0);
	});


	/* Start Exploring */
	$('#startexploring, #propertysearchinset').hoverIntent(function(){
		$(this).find('a.tab').animate({bottom:'8px'}, 100);
	},function(){
		$(this).find('a.tab').animate({bottom:'0px'}, 200);
	});
	$('#startexploringtab').hoverIntent(function(){
		$(this).animate({bottom:'8px'}, 100);
	},function(){
		$(this).animate({bottom:'0px'}, 200);
	});

	/* Wide Tabs */
	$('.widetab, .learnmembershiptab').hoverIntent(function(){
		$(this).animate({marginTop:'2px', marginBottom:'18px'}, 100);
	},function(){
		$(this).animate({marginTop:'10px', marginBottom:'10px'}, 100);
	});


	/* Main Menu */
	$('#mainmenu > ul li').hoverIntent(function(){
		$(this).find('ul').css('opacity', '0')
							.css('display', 'block')
							.animate({opacity: '1'},200);
	},function(){
		$(this).find('ul').animate({opacity: '0'},200, function(){
			$(this).css('opacity', '1')
					.css('display', 'none');
		});
	});


	/* Home Slides */
	if ( $('#homehero').length ){
		$('#homehero').cycle({
			fx:    'fade',
			speed: 800,
			timeout: 5000,
			startingSlide: 0
		});
		var timer, 
			index, 
			captiondiv;
		$('.solutions > div').hoverIntent(function(){
			if ( $(this).hasClass('golfhere') ){
				$('#caption').css('display', 'block').animate({opacity:'1'}, 400);
			}
			window.clearTimeout(timer);
			index = $(this).index()+2;
			$('#captions > div.active').css('display','none');
			$('#homehero').cycle('pause');
			$('#homehero').cycle( index );
			$(this).find('.tab').animate({bottom:'12px'}, 100);
		},function(){
			if ( $(this).hasClass('golfhere') ){
				$('#caption').animate({opacity:'0'}, 300, function(){
					$(this).css('display', 'none');
				});
			}
			$(this).find('.tab').animate({bottom:'5px'}, 200);
			window.clearTimeout(timer);
			timer = window.setTimeout(function(){
				$('#homehero').cycle('resume');
			},6000); 
		});
		$('.home #cscclogo').hoverIntent(function(){
			window.clearTimeout(timer);
			$('#captions > div.active').css('display','none');
			$('#homehero').cycle('pause');
			$('#homehero').cycle( 0 );
		},function(){
			if ( $(this).hasClass('golfhere') ){
				$('#caption').animate({opacity:'0'}, 300, function(){
					$(this).css('display', 'none');
				});
			}
			window.clearTimeout(timer);
			timer = window.setTimeout(function(){
				$('#homehero').cycle('resume');
			},6000); 
		});
	}

	var firstrun = 0;
	function onBefore(curr,next,opts){
		if ( !$("#hero nav").hasClass('lifestyle') && !$("body").hasClass('page-template-page-clubhouse-php') ){
			var index1 = opts.nextSlide;
			var index2 = opts.nextSlide - 1;

			if ( firstrun === 0 ) {
				index1 = 0;
				index2 = 0;
			}
			firstrun++;

			// if ( index1 === 0 )index2 = 5;
			if ( index1 === 0 )index2 = $("#hero .heromenu > ul > li").size()-1;

			// DOWN
			var nextTab = $("#hero .heromenu > ul > li:eq("+index2+")");
			var heroheight = nextTab.height();
			var setBottom = '-' + (heroheight - 50) +'px';
			nextTab.animate({bottom:setBottom}, 200);
			nextTab.removeClass('activetab');
			nextTab.find('ul').animate({opacity: '0'}, 200, function(){
				$(this).css('visibility', 'visible');
				$(this).removeClass('visible');
			});

			// UP
			var curTab = $("#hero .heromenu > ul > li:eq("+index1+")");
			curTab.animate({bottom:'0px'}, 100).addClass('activetab');
			curTab.find('ul').css('visibility', 'visible')
			.animate({opacity: '1'}, 300);
			curTab.find('ul').addClass('visible');
		}
	}
	function onAfter(curr,next,opts){
	}

	/* Slideshows for Nicklaus Golf & Play Here */
	if ( $('body:not(".home") #slideshow').length ){
		$('#slideshow').cycle({
			fx:    'fade',
			speed: 800,
			timeout: 5000,
			before: onBefore,
			after: onAfter,
			cleartypeNoBg:true
		});
		var timer, 
		index;

		/* Set up heights for menu with submenu */
		$('.page-template-page-nicklaus-golf-php #hero .heromenu > ul > li, .page-template-page-play-here-php #hero .heromenu > ul > li, .page-template-page-clubhouses-php #hero .heromenu > ul > li').each(function(){
			var heroheight = $(this).height();
			// if ( heroheight > 50 ){
				$(this).css('bottom','-' + (heroheight - 50) +'px')
						   .css('top','auto');
			// }
		});

		$('#ie8 .page-template-page-play-here-php .heromenu > ul li:first-child ul, #ie8 .page-template-page-nicklaus-golf-php .heromenu > ul li:first-child ul').addClass('visible');

		$('.page-template-page-nicklaus-golf-php #hero .heromenu > ul > li, .page-template-page-play-here-php #hero .heromenu > ul > li, .page-template-page-clubhouses-php #hero .heromenu > ul > li').hoverIntent(function(){
			// If this is not an active tab.
			if ( !$(this).hasClass('activetab') ){
			
				$("#hero .heromenu > ul > li").each(function(){
					var heroheight = $(this).height();
					var setBottom = '-' + (heroheight - 50) +'px';
					$(this).animate({bottom:setBottom}, 200).removeClass('activetab');
					$(this).find('ul').addClass('hidden');
					$(this).find('ul').animate({opacity: '0'}, 200);
					$(this).find('ul').css('visibility','hidden');
					$(this).find('ul').removeClass('visible');
				});

				$(this).animate({bottom:'0px'}, 100);
				$(this).find('ul').animate({opacity: '1'}, 300);
				$(this).find('ul').css('visibility','hidden');
				$(this).find('ul').removeClass('hidden');
				$(this).find('ul').addClass('visible');

				if ( !$("#hero nav").hasClass('lifestyle') ){
			
					window.clearTimeout(timer);
					index = $(this).index();
					$('#slideshow').cycle('pause');
					$('#slideshow').cycle( index );	
				
				}			
				
			} 

		},function(){
			window.clearTimeout(timer);
			timer = window.setTimeout(function(){
				$('#slideshow').cycle('resume');
			},6000); 

			if ( $("#hero nav").hasClass('lifestyle') ){
				$(this).animate({bottom:-15}, 200);
			}
		});
	}



	/* Slideshows */
	if ( $('body:not(".home") #slideshow').length && $('body:not(".page-template-page-nicklaus-golf-php") #slideshow').length && $('body:not(".page-template-page-play-here-php") #slideshow').length && $('body:not(".page-template-page-clubhouses-php") #slideshow').length ){
		$('#slideshow').cycle({
			fx:    'fade',
			speed: 800,
			timeout: 5000
		});
		var timer, 
		index;

		/* Clubhouse Menu */
		$('.page-template-page-clubhouse-php #hero .heromenu > ul > li').hoverIntent(function(){
			$(this).animate({marginTop: '0'},200);
			window.clearTimeout(timer);
			index = $(this).index();
			$('#slideshow').cycle('pause');
			$('#slideshow').cycle( index );
		},function(){
			$(this).animate({marginTop: '10px'},100);
			window.clearTimeout(timer);
			timer = window.setTimeout(function(){
				$('#slideshow').cycle('resume');
				},6000); 
		});
		$('.page-template-page-clubhouse-php #hero .heromenu > ul > li a').click(function(){return false;});
	}


	/* No Slideshow w/ Tabs */
	$('.page-template-page-amenities-php #hero .heromenu > ul > li, #hero .heromenu.linknav > ul > li, #hero .heromenu.realestate > ul > li, #hero .heromenu.contactmenu > ul > li, #hero .heromenu.realestatepropertiesmenu > ul > li').hoverIntent(function(){
		if ( !$(this).hasClass('activetab') ){
			$(this).animate({marginTop: '0px'},200);
		}
	},function(){
		if ( !$(this).hasClass('activetab') ){
			$(this).animate({marginTop: '10px'},100);
		}
	});
	$('#hero .heromenu.nonlinknav > ul > li a').click(function(){return false;});
	
	/* Footer Fix */
	setFooterHeight();

	/* Select Course */
	$("#selectcourse .course, #selectclubhouse .clubhouse").click(function(){
		console.log('test');
    	window.location=$(this).find("a").attr("href");return false;
	});


	/* Sales Team Select */
	if ( $('#salespersonform').length ){
		$('#salespersonform #person').change(function() {
			window.location = $(this).val();
		});
	}
	/* Villages Select */
	if ( $('#villagesform').length ){
		$('#villagesform #villageselect').change(function() {
			window.location = $(this).val();
		});
	}


	/* Testimonials Cycle */
	if ( $('#testimonialcycle').length ){
		$('#testimonialcycle').cycle({
			fx:    'fade',
			speed: 800,
			timeout: 5000
		});
	}
	

	/* Colorbox */
	if ( $('#agentlistings').length ){
		$('#agentlistings .lightboxme').fancybox({
			openEffect     : 'elastic',
			closeEffect    : 'fade',
			helpers   : { 
		      overlay  : {
		        opacity : 0.9,
		        css : {
		          'background-color' : '#241404'
		        }
		      }
		    }
		});
	}
	if ( $('.colorboxtime').length ){
		$('li.colorboxtime a').fancybox({
			openEffect     : 'elastic',
			closeEffect    : 'fade',
			helpers   : { 
		      overlay  : {
		        opacity : 0.9,
		        css : {
		          'background-color' : '#241404'
		        }
		      }
		    }
		});
	}
	if ( $('.viewscorecard').length ){
		$('.viewscorecard').fancybox({
			openEffect     : 'elastic',
			closeEffect    : 'fade',
			helpers   : { 
		      overlay  : {
		        opacity : 0.9,
		        css : {
		          'background-color' : '#241404'
		        }
		      }
		    }
		});
	}
	if ( $('.enhance').length ){
		$('.enhance').fancybox({
		    prevEffect    : 'fade',
		    nextEffect    : 'fade',
		    openEffect   : 'elastic',
		    closeBtn      : false,
		    arrows      : false,
		    closeEffect     : 'fade',
		    closeBtn    : true,
		    loop        : true,
		    helpers   : { 
		      buttons : {},
			thumbs	: {
				width	: 50,
				height	: 50
			},
		      overlay  : {
		        opacity : 0.9,
		        css : {
		          'background-color' : '#241404'
		        }
		      }
		    }
		});
	}


	/* Page Specific Tabs */
	$('.page-id-321 #hero .heromenu ul li.overview, .page-id-407 #hero .heromenu ul li.details, .page-id-628 #hero .heromenu ul li.tab1, .page-id-728 #hero .heromenu ul li.tab2, .page-id-738 #hero .heromenu ul li.tab3, .page-id-743 #hero .heromenu ul li.tab4, .page-id-761 #hero .heromenu ul li.tab5, .page-id-665 #hero .heromenu ul li.scottsdale, .page-id-153 #hero .heromenu ul li.localattractions, .page-id-667 #hero .heromenu ul li.sonoran-desert, .page-id-145 #hero .heromenu ul li.history, .page-id-147 #hero .heromenu ul li.awards').addClass('activetab');


 	// Center window if less than 1230
	var windowW = $(window).width();
 	if ( !$('body').hasClass('page-id-1520') ) {
		if ( windowW < 1230 ){
			$(window).scrollLeft( (1230-windowW)/2 );
		}
	} else {
		if ( windowW < 1010 ){
			$(window).scrollLeft( (1010-windowW)/2 );
		}
	}
 	$(window).resize(function(){
 		var windowW = $(this).width();
 		if ( !$('body').hasClass('page-id-1520') ) {
 			if ( windowW < 1230 ){
 				$(window).scrollLeft( (1230-windowW)/2 );
 			}
 		} else {
			if ( windowW < 1010 ){
				$(window).scrollLeft( (1010-windowW)/2 );
				console.log((1010-windowW)/2);
			}
		}
 	});

});
