/* Old Palm javascript */

search_block_status = "shown";



function toggleHmSrchImg(siteID) {
	var currentImg = $('#search_block_image').attr("src");
	if(currentImg.substring(currentImg.length - 8) == "plus.gif") {
		$('#search_block_image').attr("src","/content/" + siteID + "/images/minus.gif");
	} else {
		$('#search_block_image').attr("src","/content/" + siteID + "/images/plus.gif");
	}
	if(search_block_status == "shown") {
		$('#inventory_search_block_form').animate({ height:"hide" }, { queue:true, duration:500 });
		search_block_status = "hidden";
	} else {
		$('#inventory_search_block_form').animate({ height:"show" }, { queue:true, duration:500 });
		search_block_status = "shown";
	}
	return false;
}

// search left-side nav for correct active link for residence detail pages
function check_url() {
	if(location.href.search(/new-homes\/inventory\//) > -1 ) {
		$("#block-menu-79 a[href='/new-homes/search/quick-delivery']").addClass("active");
	} else if (location.href.search(/new-homes\/custom-homes\/builders\//) > -1 ) {
		$("#block-menu-79 a[href='/new-homes/builders']").addClass("active");
	}
	return false;	
}



/**
 * old palm menu setup 
 */
var wci_menu = {
	no_menu: false,
	menu_top: "38px",
	init: function() {
		if (!this.no_menu) {
			$('#experience,#new-homes,#thelocation').hover(
				function() {
					$(this).find('.sub_menu').css({
						top: wci_menu.menu_top,
						display:"none"
					}).show();
				},
				function() {
					$(this).find('.sub_menu').slideUp('fast',  // fade out menu 
						function() {
							// only after the fade to do move the menu off screen
							$(this).css({
								top: "-999em"
							});
							
							var p = $(this).parent();
							var pid = p.attr("id");
							
							// for the 3 drop down menus, only reposition the image after the menu is gone
							switch (pid) {
								case "experience":
									p.find("a:not(.active_menu)").css({
										backgroundPosition:"-142px -38px"
									});
								break;
								case "new-homes":
									p.find("a:not(.active_menu)").css({
										backgroundPosition:"-330px -38px"
									});
								break;
								case "thelocation":
									p.find("a:not(.active_menu)").css({
										backgroundPosition:"-470px -38px"
									});
								break;						
							}
						}
					);
				}
			);
					
		}
		
		// dropdown menu item hover
		$('.sub_menu li').bind("mouseenter mouseleave", 
			function(e){
				$(this).toggleClass("sfHover");
			}
		);
	
		// reposition bg img for home menu button
		$('#home').hover(
			function(){
				$(this).find("a:not(.active_menu)").css({
					backgroundPosition:"1px 0px"
				});
			},
			function() {
				$(this).find("a:not(.active_menu)").css({
					backgroundPosition:"1px -38px"
				});
			}
		);
		
		// reposition bg img for experience menu button
		$('#experience').hover(
			function(){
				$(this).find("a:not(.active_menu)").css({
					backgroundPosition:"-142px 0px"
				});
			},
			function() {
				if (wci_menu.no_menu) {
					$(this).find("a:not(.active_menu)").css({
						backgroundPosition:"-142px -38px"
					});
				}
			}
		);
		
		// reposition bg img for new-homes menu button
		$('#new-homes').hover(
			function(){
				$(this).find("a:not(.active_menu)").css({
					backgroundPosition:"-330px 0px"
				});
			},
			function() {
				if (wci_menu.no_menu) {
					$(this).find("a:not(.active_menu)").css({
						backgroundPosition:"-330px -38px"
					});
				}
			}
		);
		
		// reposition bg img for thelocation menu button
		$('#thelocation').hover(
			function(){
				$(this).find("a:not(.active_menu)").css({
					backgroundPosition:"-470px 0px"
				});
			},
			function() {
				if (wci_menu.no_menu) {
					$(this).find("a:not(.active_menu)").css({
						backgroundPosition:"-470px -38px"
					});
				}
			}
		);
		
		// reposition bg img for contact menu button
		$('#contact').hover(
			function(){
				$(this).find("a:not(.active_menu)").css({
					backgroundPosition:"-641px 0px"
				});
			},
			function() {
				$(this).find("a:not(.active_menu)").css({
					backgroundPosition:"-641px -38px"
				});
			}
		);
			

	}
};


/**
 * homepage / landing page tab setup 
 */
var tab_status = {
	no_tabs: true,
	active: 'tab-resgallery',
	tab_phase: false,
	tab_status: {
	},
	init: function() {
		if (!this.no_tabs) {
			$('#tabs-nav a').click( 
				function() {
					tab_status.select_tab($(this).attr('id'));
				}
			).hover(
				function() {
					tab_status.hover_tab($(this).attr('id'), true);
				},
				function() {
					tab_status.hover_tab($(this).attr('id'), false);
				}
			);
			this.tab_phase = this.current_hash();
			if (this.tab_phase) {
				this.select_tab(this.tab_phase);
				$('#homepage-tabs').show();
			}
		} 
	},
	select_tab: function(id) {
		if (this.active != id) {
			this.init_tab(id);			
			var stab = $('#' + id);
			var ctab = $('#' + this.active);
			if (stab.hasClass('tab-hover')) {
				stab.removeClass('tab-hover');	
			}
			ctab.addClass('tab-inactive').removeClass('tab-active');
			stab.addClass('tab-active').removeClass('tab-inactive');
			$('#' + this.active + '-content').hide();

			if (id == 'tab-blog') {
				$('#homepage-tabs').css('height', '380px');
			} else {
				$('#homepage-tabs').css('height', '320px');
			}
			$('#' + id + '-content').show();
			this.active = id;
		}
	},
	hover_tab: function(id, h) {
		if (this.active != id) {
			if (h) {
				$('#' + id).removeClass('tab-inactive').addClass('tab-hover')
			} else {
				$('#' + id).removeClass('tab-hover').addClass('tab-inactive')
			}
		}
	},
	current_hash: function() {
		if (location.hash.length > 0) {
			switch(location.hash) {
				case '#tab-golfcourse':
				case '#tab-amenities':
				case '#tab-blog':
					return location.hash.substring(1);
				break;
				default:
					return false;
			}
		}
		return false;
	},
	init_tab: function(tab) {
		if (!this.tab_status[tab]) {
			switch(tab) {
				case 'tab-golfcourse':
					newImage('/content/42/images/homepage_hole2.gif');
					newImage('/content/42/images/homepage_hole6.gif');
					newImage('/content/42/images/homepage_hole18.gif');
					$('#homepage-hole1 img').attr('src', '/content/42/images/homepage_hole2.gif');
					$('#homepage-hole2 img').attr('src', '/content/42/images/homepage_hole6.gif');
					$('#homepage-hole3 img').attr('src', '/content/42/images/homepage_hole18.gif');
				break;
				case 'tab-amenities':
					newImage('/content/42/images/homepage_golfclub.jpg');
					newImage('/content/42/images/homepage_golfstudio.jpg');
					newImage('/content/42/images/homepage_spa.jpg');
					$('#tab-amenities-content .photo2 img').attr('src', '/content/42/images/homepage_golfclub.jpg');
					$('#tab-amenities-content .photo1 img').attr('src', '/content/42/images/homepage_golfstudio.jpg');
					$('#tab-amenities-content .photo3 img').attr('src', '/content/42/images/homepage_spa.jpg');
				break;
			}
			this.tab_status[tab] = true;
		}
		
	}
};



function trackPage(uri) {
	if (typeof(pageTracker) != "undefined") {
		pageTracker._trackPageview(uri);
	}
}


$(document).ready(function() {


	wci_menu.init();
	tab_status.init();

	if (!jQuery.support.opacity) {
		$('.img_png').pngFix();
	}

	check_url();
});


