/*
 * functions for sabalhomes.com
 * written by burnkit.com
 */
// <![CDATA[

/********* text replacement *********/
Cufon.replace('h5');
Cufon.replace('#promo_content h2');


/*
preloader = new Image(); 
images = new Array("smaller_subnav_bg.png", "subrightnav_bg.png","sub_bg.png","orange_arrow.gif","attached_subnav.jpg","laned_subnav.jpg","front_garage_subnav.jpg","move_up_subnav.jpg","calgary_area_subnav.jpg","edmonton_area_subnav.jpg");
for (img in images) { preloader.src = "/img/" + images[img]; }
*/

var area;
$(document).ready(function() {

	//$("#subnav_communities").show();
	//$("#nav").css('backgroundPosition', '0px -41px');

	/********* start main nav *********/
	$('#nav_item_communities').hover(function() {
		$("#nav").css('backgroundPosition', '0px -41px');
		$("#subnav_communities").show();
	}, function() {
		$("#nav").css('backgroundPosition', $("#nav").attr("class"));
		$("#subnav_communities").hide();
		$("#subnav_communities_image").hide();
		$("#subnav_communities_text").hide();
		$("#subnav_communities_bottom").html('<img src="/img/subnav_small_bottom.png" width="213" height="19" alt="" border="0" />');
		$("#subnav_communities_main").width(196).css('background-image','url(/img/smaller_subnav_bg.png)');		
	});
	$('#nav_item_homes').hover(function() {
		$("#nav").css('backgroundPosition', '0px -82px');
		$("#subnav_homes").show();
	}, function() {
		$("#nav").css('backgroundPosition', $("#nav").attr("class"));
		$("#subnav_homes").hide();
		$("#subnav_homes_image").hide();
		$("#subnav_homes_text").hide();
		$("#subnav_homes_bottom").html('<img src="/img/subnav_small_bottom.png" width="213" height="19" alt="" border="0" />');
		$("#subnav_homes_main").width(196).css('background-image','url(/img/smaller_subnav_bg.png)');
	});
	$('#nav_condos').hover(function() {
		$("#nav").css('backgroundPosition', '0px -123px');
	}, function() {
		$("#nav").css('backgroundPosition', $("#nav").attr("class"));
	});
	$('#nav_item_style').hover(function() {
		$("#nav").css('backgroundPosition', '0px -164px');
		$("#subnav_style").show();
	}, function() {
		$("#nav").css('backgroundPosition', $("#nav").attr("class"));
		$("#subnav_style").hide();
	});
	$('#nav_item_resources').hover(function() {
		$("#nav").css('backgroundPosition', '0px -205px');
		$("#subnav_resources").show();
	}, function() {
		$("#nav").css('backgroundPosition', $("#nav").attr("class"));
		$("#subnav_resources").hide();
	});
	$('#nav_item_company').hover(function() {
		$("#nav").css('backgroundPosition', '0px -246px');
		$("#subnav_company").show();
	}, function() {
		$("#nav").css('backgroundPosition', $("#nav").attr("class"));
		$("#subnav_company").hide();
	});
	/********* end main nav *********/

	/********* start subnav *********/
		//$("#subnav_communities").hide();
		//$("#subnav_homes").hide();
		// rollover content for subnav areas
		//$("#hover_calgary").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});

		$("#hover_calgary").hover( function() {
			//alert($("#subnav_communities_image").css("background-image"));			
			$("#subnav_communities_bottom").html('<img src="/img/sub_bottom.png" width="513" height="19" alt="" border="0" />');
			$("#subnav_communities_main").width(497).css("background-image","url(/img/sub_bg.png)");
			$("#subnav_communities_image").css("background-image","url(/img/calgary_area_subnav.jpg)").show();
			$("#subnav_communities_text").text("Home to the Calgary Stampede and heart of the new West, the vibrant energy of the city extends in to the innovative new communities, and to the surrounding areas that include Cochrane and Airdrie.").show();
		});
		$("#hover_edmonton").hover(function() {
			$("#subnav_communities_bottom").html('<img src="/img/sub_bottom.png" width="513" height="19" alt="" border="0" />');
			$("#subnav_communities_main").width(497).css("background-image","url(/img/sub_bg.png)");
			$("#subnav_communities_image").css("background-image","url(/img/edmonton_area_subnav.jpg)").show();
			$("#subnav_communities_text").text("Edmonton, the Festival City, is the provincial capital and an established cultural hub. Here, exciting new communities bask under the glow of the big Alberta skies.").show();
		});
		// subnav for hometypes
		$(".attached").hover(function() {			
			$("#subnav_homes_bottom").html('<img src="/img/sub_bottom.png" width="513" height="19" alt="" border="0" />');
			$("#subnav_homes_main").width(497).css('background-image','url(/img/sub_bg.png)');
			$("#subnav_homes_image").css("background-image","url(/img/attached_subnav.jpg)").show();
			$("#subnav_homes_text").text("A great way in to home ownership, Sabal Attached Homes balance our signature flair for good design and features with our best price point. Welcome to the neighbourhood.").show();
		});
		// if there's no area set
		if ($(".attached").attr("href") == "") {
			$(".attached").click(function() {
				$("#calgary_btn").attr("href","/hometypes/Calgary/attached");
				$("#edmonton_btn").attr("href","/hometypes/Edmonton/attached");
				$("#subnav_homes").hide();
				$("#dialog").show();
				return false;
			});
		}
		$(".laned").hover(function() {							   
			$("#subnav_homes_bottom").html('<img src="/img/sub_bottom.png" width="513" height="19" alt="" border="0" />');
			$("#subnav_homes_main").width(497).css('background-image','url(/img/sub_bg.png)');
			$("#subnav_homes_image").css("background-image","url(/img/laned_subnav.jpg)").show();
			$("#subnav_homes_text").text("Once considered just a starter home, the Sabal laned home collection has evolved and now includes larger models with more style, more features and knock-out curb appeal.").show();
		});
		// if there's no area set
		if ($(".laned").attr("href") == "") {
			$(".laned").click(function() {
				$("#calgary_btn").attr("href","/hometypes/Calgary/laned");
				$("#edmonton_btn").attr("href","/hometypes/Edmonton/laned");
				$("#subnav_homes").hide();
				$("#dialog").show();
				return false;
			});
		}
		$(".front-garage").hover(function() {
			$("#subnav_homes_bottom").html('<img src="/img/sub_bottom.png" width="513" height="19" alt="" border="0" />');
			$("#subnav_homes_main").width(497).css("background-image","url(/img/sub_bg.png)");
			$("#subnav_homes_image").css("background-image","url(/img/front_garage_subnav.jpg)").show();
			$("#subnav_homes_text").text("The staple home in today's communities is also where Sabal really shines. You'll find award-winning design ideas that create more comfort per square foot in every model.").show();			
		});
		// if there's no area set
		if ($(".front-garage").attr("href") == "") {
			$(".front-garage").click(function() {
				$("#calgary_btn").attr("href","/hometypes/Calgary/front_garage");
				$("#edmonton_btn").attr("href","/hometypes/Edmonton/front_garage");
				$("#subnav_homes").hide();
				$("#dialog").show();
				return false;
			});
		}
		$(".move-up-garage").hover(function() {
			$("#subnav_homes_bottom").html('<img src="/img/sub_bottom.png" width="513" height="19" alt="" border="0" />');
			$("#subnav_homes_main").width(497).css("background-image","url(/img/sub_bg.png)");
			$("#subnav_homes_image").css("background-image","url(/img/move_up_subnav.jpg)").show();
			$("#subnav_homes_text").text("They say go big or go home. Why not do both? These spacious homes show off our stylish personality and feature an enhanced level of architectural character - both inside and out.").fadeIn();
		});
		// if there's no area set
		if ($(".move-up-garage").attr("href") == "") {
			$(".move-up-garage").click(function() {
				$("#calgary_btn").attr("href","/hometypes/Calgary/move-up_garage");
				$("#edmonton_btn").attr("href","/hometypes/Edmonton/move-up_garage");
				$("#subnav_homes").hide();
				$("#dialog").show();
				return false;
			});
		}
		
		$(".quick-possession-homes").hover(function() {
			$("#subnav_homes_image").hide();
			$("#subnav_homes_text").hide();
			$("#subnav_homes_bottom").html('<img src="/img/subnav_small_bottom.png" width="213" height="19" alt="" border="0" />');
			$("#subnav_homes_main").width(196).css('background-image','url(/img/smaller_subnav_bg.png)');
		});
	
		
		
		// dialog popup to specify area
		$("#facebook_dialog").click(function() { $(this).hide(); });
		$("#dialog").click(function() { $(this).hide(); });		
		$("#promo_overlay").overlay({ expose:{ color:'#000', loadSpeed:150, opacity:0.80 }, top: 50, left: "center", fixed: false });	
		$("#registration_form").submit(function() {
			$("#submit").click();
			return false;
		});
		$("#submit").click(function() {
			$("#message").html('').hide();	
								
			// validate name and email
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if($("input[name=name]").val() == "") $('#message').html('Please enter your name').fadeIn();
			else if(reg.test($("input[name=email]").val()) == false) $('#message').html('Please enter a valid email address').fadeIn();
			else if($("input[name=phone]").val() == "") $('#message').html('Please enter your phone number').fadeIn();
			else {
				var send_data = "action=register&name=" + escape($("input[name=name]").val()) + "&email=" + escape($("input[name=email]").val()) + "&phone=" + escape($("input[name=phone]").val());
				$.post("/inc/addSignup.php",send_data,function(result) {
					if(result=="success") {
						$('#message').html('Thank you for registering!').fadeIn();	
						$("input[name=name]").val("");
						$("input[name=email]").val("");
						$("input[name=phone]").val("");
						setTimeout('$("#promo_overlay").overlay().close()',3000);
					} else if(result=="duplicate") {
						$('#message').html('You\'ve already registered.').show();
					} else {
						$('#message').html('There was an error submitting your registration. Please try again later.').show();
					}
				});
			}
			return false;
		});								
		$("#promo_close").click(function() {
			$("#promo").hide();			
		});
	/********* end subnav *********/

	/********* start rightnav *********/
		$("#rightnav_item_homes").hover(function() {
			$("#rightnav_top").css('backgroundPosition', '0px -33px');
			$("#subrightnav_homes").show();
		}, function() {
			$("#rightnav_top").css('backgroundPosition', '0px 0px');
			$("#subrightnav_homes").hide();
		});
		$("#rightnav_item_bookmarked").hover(function() {
			$("#rightnav_top").css('backgroundPosition', '0px -66px');
			$("#subrightnav_bookmarked").show();
		}, function() {
			$("#rightnav_top").css('backgroundPosition', '0px 0px');
			$("#subrightnav_bookmarked").hide();
		});	
	/********* end rightnav *********/

	/********* start newsletter *********/
		// validate email newsletter signup
		$("#newsletter_btn").click(function() {
			$("#newsletter_form").submit();
			return false;
		});
		$("#subscribe_calgary").click(function() {
			$("#subscribe_dialog").hide();
			area = "Calgary";
			submitNewsletterForm();
			return false;			
		}); 
		$("#subscribe_edmonton").click(function() {
			$("#subscribe_dialog").hide();
			area = "Edmonton";
			submitNewsletterForm();
			return false;			
		});
		$("#newsletter_form").submit(function() {
			//set area input
			var email = $("#newsletter_input").val();
			var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
			if (emailfilter.test(email)) {
				$("#subscribe_dialog").height($(document).height());
				$("html, body").animate({ scrollTop: 0 }, "slow");
				$("#subscribe_dialog").show();
			} else {
				$("#newsletter_status_link").text("Invalid email address");
				$("#newsletter_status").fadeIn();				
			}
			return false;
		});
		$("#newsletter_status_link").click(function() {
			$("#newsletter_status").fadeOut();
			return false;
		});
	/********* end newsletter *********/

	/********* start swfobject *********/
	/*
	if ($("#landing_flash").length) {
		$.get("/inc/getTicker.php", function(data) {
			var flashvars = {
				loadVar: "/img/s1.jpg,/img/s2.jpg,/img/s3.jpg",
				tickerVar: data
			};
			var params = {
			  menu: "false",
			  allowscriptaccess: "always",
			  bgcolor: "#F5F3F1",
			  wmode: "transparent"
			};
			swfobject.embedSWF("/sabal_Slideshow_v2.swf", "landing_flash", "738", "485", "9.0.0","expressInstall.swf", flashvars, params);
		});
	}
	*/
	/*
	if ($("#community_flash").length) {
		// send the area and the community if selected
		community = "";
		area = window.location.href.replace(/(.*)\/communities\/(.*)\/?/, '$2');
		if (area.search("/") > -1) {
			community = area.split("/")[1];	
			area = area.split("/")[0];
		}
		inData = "area=" + area + "&community=" + community;
		$.get("/inc/getArea.php", inData, function(data) {
			var flashvars = {
				loadVar: data
			};
			var params = {
			  menu: "false",
			  allowscriptaccess: "always",
			  bgcolor: "#F5F3F1",
			  wmode: "transparent"
			};
			swfobject.embedSWF("/community_v1.swf", "community_flash", "207", "379", "9.0.0","expressInstall.swf", flashvars, params);
		});
	}
	*/
	/********* end swfobject *********/
	
	$("#setBookmark").click(function() {
		temp = $(this).attr("class").split("|");
		homeName = temp[0];
		area = temp[1];
		$.post('/inc/addBookmark.php', "home=" + homeName, function(data) {
			if (data == "success") {
				$("#remove_bookmark").show();
				$("#noBookmarks").hide();
				$("#bookmarks").append('<a href="/homes/' + area + '/' + homeName + '" class="arrowed">' + homeName + '</a><a href="' + homeName + '" class="subnav_ex_new">x</a><br />');
				$(".subnav_ex_new").click(function() {
					temp = $(this);
					$.post('/inc/removeBookmark.php', "home=" + $(this).attr("href"), function(data) {
						if (data == "removed") {
							temp.prev().hide();
							temp.hide();
						}
						else if (data == "empty") {
							$("#remove_bookmark").hide();
							$("#noBookmarks").show();
							$(this).parent().hide();
						}
						else alert(data);
					});
					return false;
				});
			}
			else if (data == "duplicate") alert("This house is already bookmarked");
			else alert(data);
		});
		return false;
	});
	
	$(".subnav_ex").click(function() {
		temp = $(this);
		$.post('/inc/removeBookmark.php', "home=" + $(this).attr("href"), function(data) {
			if (data == "removed") {
				temp.prev().hide();
				temp.hide();
			}
			else if (data == "empty") {
				$("#remove_bookmark").hide();
				$("#noBookmarks").show();
				$(this).parent().hide();
			}
			else alert(data);
		});
		return false;
	});

	$('#overview_btn').hover(function() { $("#homes_right_bg").css('backgroundPosition', '102px -86px'); }, function() { $("#homes_right_bg").css('backgroundPosition', $("#homes_right_bg").attr("class")); });
	$('#floorplans_btn').hover(function() { $("#homes_right_bg").css('backgroundPosition', '102px -43px'); }, function() { $("#homes_right_bg").css('backgroundPosition', $("#homes_right_bg").attr("class")); });
	$('#elevations_btn').hover(function() { $("#homes_right_bg").css('backgroundPosition', '102px 0px'); }, function() { $("#homes_right_bg").css('backgroundPosition', $("#homes_right_bg").attr("class")); });
	$('.changeImg').click(function() {
		$('#source-image').attr("src", $(this).attr("href"));
		return false;
	});
	
	$("#showNext").click(function() {
		num = parseInt($("#showNext").attr("href"));
		plus1 = num + 1;
		plus2 = num + 2;
		$("#showPrev").attr("href",plus1);
		$("#showNext").attr("href",plus1);
		$("#showPrev").show();
		$(".row_" + num).fadeOut("fast",function() {
			$(".row_" + plus1).fadeIn();						 
		 });
		if (!$(".row_" + plus2).length) $(this).hide();
		return false;
	});
	$("#showPrev").click(function() {
		num = parseInt($("#showPrev").attr("href"));
		minus1 = num - 1;
		minus2 = num - 2;
		$("#showPrev").attr("href",minus1);
		$("#showNext").attr("href",minus1);
		$("#showNext").show();
		$(".row_" + num).fadeOut("fast", function() {
			$(".row_" + minus1).fadeIn();												  
		});
		if (!$(".row_" + minus2).length) $(this).hide();
		return false;
	});
	if ($.browser.msie && $.browser.version.substr(0,1)<8) {
		$("#showNext").css("position","absolute");
		//$("#showNext").css("left","0");
	}


	$(".hometype_home").hover( function() {$(this).addClass("home_over");}, function() {$(this).removeClass("home_over");} );
	$(".hometype_home").click(function(){ window.location = $(this).find(".big_link").attr("href"); });
	$("#hometype_main_content").hover( function() {$(this).addClass("home_over");}, function() {$(this).removeClass("home_over");} );
	$("#hometype_main_content").click(function(){ window.location = $(this).find(".big_link").attr("href"); });
	
	
	$("#loader").show( function() { $(this).hide(); } );
	$("#loader2").show( function() { $(this).hide(); } );

	$("#facebook").click(function() {
		$("#facebook_dialog").height($(document).height());
		//window.scrollTo(0,0);
		$("html, body").animate({ scrollTop: 0 }, "slow");
		$("#facebook_dialog").show();
		return false;
	});
	$("#calgary_facebook").click(function() {
		$("#facebook_dialog").hide();
		window.open("https://www.facebook.com/SabalHomesCalgary", "_blank");
		return false;
	}); 
	$("#edmonton_facebook").click(function() {
		$("#facebook_dialog").hide();
		window.open("https://www.facebook.com/SabalHomesEdmonton", "_blank");
		return false;
	});
	
});

function submitNewsletterForm() {
	$("#newsletter_status").fadeOut();
	var email = $("#newsletter_input").val();
	$.post('/inc/addEmail.php', "email=" + email + "&area=" + area, function(data) {
		if(data == "success") {					
			$("#newsletter_status_link").text("Sign-up successful!");
			$("#newsletter_status").fadeIn();	
		} else if(data == "duplicate") {
			$("#newsletter_status_link").text("This email is already signed up");
			$("#newsletter_status").fadeIn();	 
		} else if(data = "fail") {
			$("#newsletter_status_link").text("The field was empty!");
			$("#newsletter_status").fadeIn();	 
		} else {
			$("#newsletter_status_link").text("Error registering email");
			$("#newsletter_status").fadeIn();	 
		}			
	});
}
// ]]>
