function goToByScroll(name){
	name = $("a[name = '" + name + "']");
	$("html, body").animate({scrollTop: $(name).offset().top - 10}, "slow");
}

function changePage(page) {
	if(page == "about") {
		$("#aboutButton").click();
	} else if(page == "services") {
		$("#servicesButton").click();
	} else if(page == "office") {
		$("#officeButton").click();
	} else if(page == "smile") {
		$("#smileButton").click();
	} else if(page == "financing") {
		$("#financingButton").click();
	} else if(page == "contact") {
		$("#contactButton").click();
	} else if(page == "map") {
		$("#mapButton").click();
	} else { //(page == "home")
		$("#homeButton").click();
	}
}

function slideUp() {
	if($("#serviceTypesRow").is(":visible")) {
		$("#serviceTypesRow div").slideUp("slow", function() {
			$("#serviceTypesRow").hide();
		});
	}
}

function resetFields() {
	$("#questionForm input:text").val("");
	$("#questionForm input:checkbox").prop("checked", false);
	$("#questionForm input[name $= '_report'][name != 'free_report']").attr("disabled", "disabled");
}

$(function() {
	var navigate = false;;
	
	$(window).scroll(function() {
		if($(window).scrollTop() > 230) {
			$("#links").stop().animate({"top": $(window).scrollTop() - 210}, "slow");
		} else {
			$("#links").stop().animate({"top": 20}, "slow");
		}
	});
	
	$.fn.moveDiv = function(fromLeft, fromTop) {
        this.css("left", ((($(window).width() - 800) / 2) + fromLeft) + "px");
        this.css("top", fromTop + "px");
        return this;
    };
	
	$("#headerDiv").moveDiv(0, 10);
	$("#bodyDiv").moveDiv(0, 230);
	
	//dialogs
	$("#dialog-required").dialog({
		autoOpen: false,
		resizable: false,
		height: 160,
		width: 325,
		modal: true,
		buttons: {
			"Ok": function() {
				$(this).dialog("close");
			}
		}
	});
	$("#dialog-noreport").dialog({
		autoOpen: false,
		resizable: false,
		height: 250,
		width: 500,
		modal: true,
		buttons: {
			"Submit": function() {
				$(this).dialog("close");
				$("#questionForm").submit();
			}
		}
	});
	$("#dialog-noreport input:checkbox").click(function() {
		$("#questionForm input[name = '" + $(this).attr("name") + "']").prop("checked", $(this).is(":checked"));
	});
	$("#dialog-success").dialog({
		autoOpen: false,
		resizable: false,
		height: 160,
		width: 500,
		modal: true,
		buttons: {
			"Ok": function() {
				$(this).dialog("close");
			}
		}
	});
	
	$("#officeGallery").coinslider({
		width: 500, // width of slider panel
		height: 375, // height of slider panel
		spw: 7, // squares per width
		sph: 5, // squares per height
		delay: 5000, // delay between images in ms
		sDelay: 30, // delay beetwen squares in ms
		opacity: 0.7, // opacity of title and navigation
		titleSpeed: 500, // speed of title appereance in ms
		effect: '', // random, swirl, rain, straight
		navigation: true, // prev next and buttons
		links : false, // show images as links
		hoverPause: true // pause on hover
	});
	
	var page = window.location.hash.substring(1);
	if(page == "about") {
		$("#" + page + "Button").addClass("activeButton");
		$("#" + page + "Page").show();
	} else if(page == "services") {
		$("#" + page + "Button").addClass("activeButton");
		$("#" + page + "Page").show();
	} else if(page == "office") {
		$("#" + page + "Button").addClass("activeButton");
		$("#" + page + "Page").show();
	} else if(page == "smile") {
		$("#" + page + "Button").addClass("activeButton");
		$("#" + page + "Page").show();
	} else if(page == "financing") {
		$("#" + page + "Button").addClass("activeButton");
		$("#" + page + "Page").show();
	} else if(page == "contact") {
		$("#" + page + "Button").addClass("activeButton");
		$("#" + page + "Page").show();
	} else if(page == "contact-dialog") {
		$("#contactButton").addClass("activeButton");
		$("#contactPage").show();
		$("#dialog-success").dialog("open");
	} else if(page == "map") {
		$("#" + page + "Button").addClass("activeButton");
		$("#" + page + "Page").show();
		if($("#mapDiv").html().length == 0) {
			$("#mapDiv").html('<iframe style="border: 1px solid #888888;" width="500" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;hl=en&amp;q=1+windcrest+drive+manorville+ny+11949&amp;ie=UTF8&amp;source=embed&amp;hq=&amp;hnear=1+Windcrest+Dr,+Manorville,+New+York+11949&amp;t=m&amp;vpsrc=0&amp;ll=40.848619,-72.766743&amp;spn=0.02597,0.042915&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe><br /><a target="_blank" href="http://maps.google.com/maps?f=q&amp;hl=en&amp;q=1+windcrest+drive+manorville+ny+11949&amp;ie=UTF8&amp;source=embed&amp;hq=&amp;hnear=1+Windcrest+Dr,+Manorville,+New+York+11949&amp;t=m&amp;vpsrc=0&amp;ll=40.848619,-72.766743&amp;spn=0.02597,0.042915&amp;z=14&amp;iwloc=A" style="text-align:left">View Larger Map</a>');
		}
	} else { //home
		$("#homeButton").addClass("activeButton");
		$("#homePage").show();
	}
	
	//buttons
	$("#homeButton").button().click(function() {
		navigate = true;
		window.location.hash = "home";
		$("#links").stop().animate({"top": 20}, "fast");
		slideUp();
		$("button").button("option", "disabled", true);
		$("button").removeClass("activeButton");
		$(this).addClass("activeButton");
		$("div[id $= 'Page']:visible").fadeOut("fast", function() {
			$("#homePage").fadeIn("fast", function() {
				$("button").button("option", "disabled", false);
			});
		});
	});
	
	$("#aboutButton").button().click(function() {
		navigate = true;
		window.location.hash = "about";
		$("#links").stop().animate({"top": 20}, "fast");
		slideUp();
		$("button").button("option", "disabled", true);
		$("button").removeClass("activeButton");
		$(this).addClass("activeButton");
		$("div[id $= 'Page']:visible").fadeOut("fast", function() {
			$("#aboutPage").fadeIn("fast", function() {
				$("button").button("option", "disabled", false);
			});
		});
	});
	
	$("#servicesButton").button();
	$("#serviceTypesButton").button({
		text: false,
		icons: {
			primary: "ui-icon-triangle-1-s"
		}
	});
	
	$("#servicesButton, #serviceTypesButton").click(function() {
		navigate = true;
		window.location.hash = "services";
		$("#links").stop().animate({"top": 20}, "fast");
		$("button").button("option", "disabled", true);
		$("button").removeClass("activeButton");
		$("#servicesButton").addClass("activeButton");
		$("div[id $= 'Page']:visible").fadeOut("fast", function() {
			$("#servicesPage").fadeIn("fast", function() {
				$("button").button("option", "disabled", false);
				if(!$("#serviceTypesRow").is(":visible")) {
					$("#serviceTypesRow").show();
					$("#serviceTypesRow div").slideDown();
				}
			});
		});
	});
	$("#serviceButtonsDiv").buttonset();
	
	$("#officeButton").button().click(function() {
		navigate = true;
		window.location.hash = "office";
		$("#links").stop().animate({"top": 20}, "fast");
		slideUp();
		$("button").button("option", "disabled", true);
		$("button").removeClass("activeButton");
		$(this).addClass("activeButton");
		$("div[id $= 'Page']:visible").fadeOut("fast", function() {
			$("#officePage").fadeIn("fast", function() {
				$("button").button("option", "disabled", false);
				$("#cs-button-officeGallery-1").click();
			});
		});
	});
	
	$("#smileButton").button().click(function() {
		navigate = true;
		window.location.hash = "smile";
		$("#links").stop().animate({"top": 20}, "fast");
		slideUp();
		$("button").button("option", "disabled", true);
		$("button").removeClass("activeButton");
		$(this).addClass("activeButton");
		$("div[id $= 'Page']:visible").fadeOut("fast", function() {
			$("#smilePage").fadeIn("fast", function() {
				$("button").button("option", "disabled", false);
			});
		});
	});
	
	$("#financingButton").button().click(function() {
		navigate = true;
		window.location.hash = "financing";
		$("#links").stop().animate({"top": 20}, "fast");
		slideUp();
		$("button").button("option", "disabled", true);
		$("button").removeClass("activeButton");
		$(this).addClass("activeButton");
		$("div[id $= 'Page']:visible").fadeOut("fast", function() {
			$("#financingPage").fadeIn("fast", function() {
				$("button").button("option", "disabled", false);
			});
		});
	});
	
	$("#contactButton").button().click(function() {
		navigate = true;
		window.location.hash = "contact";
		$("#links").stop().animate({"top": 20}, "fast");
		slideUp();
		$("button").button("option", "disabled", true);
		$("button").removeClass("activeButton");
		$(this).addClass("activeButton");
		$("div[id $= 'Page']:visible").fadeOut("fast", function() {
			$("#contactPage").fadeIn("fast", function() {
				$("button").button("option", "disabled", false);
			});
		});
	});
	
	$("#mapButton").button().click(function() {
		navigate = true;
		window.location.hash = "map";
		$("#links").stop().animate({"top": 20}, "fast");
		slideUp();
		$("button").button("option", "disabled", true);
		$("button").removeClass("activeButton");
		$(this).addClass("activeButton");
		$("div[id $= 'Page']:visible").fadeOut("fast", function() {
			$("#mapPage").fadeIn("fast", function() {
				$("button").button("option", "disabled", false);
				if($("#mapDiv").html().length == 0) {
					$("#mapDiv").html('<iframe style="border: 1px solid #888888;" width="500" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;hl=en&amp;q=1+windcrest+drive+manorville+ny+11949&amp;ie=UTF8&amp;source=embed&amp;hq=&amp;hnear=1+Windcrest+Dr,+Manorville,+New+York+11949&amp;t=m&amp;vpsrc=0&amp;ll=40.848619,-72.766743&amp;spn=0.02597,0.042915&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe><br /><a target="_blank" href="http://maps.google.com/maps?f=q&amp;hl=en&amp;q=1+windcrest+drive+manorville+ny+11949&amp;ie=UTF8&amp;source=embed&amp;hq=&amp;hnear=1+Windcrest+Dr,+Manorville,+New+York+11949&amp;t=m&amp;vpsrc=0&amp;ll=40.848619,-72.766743&amp;spn=0.02597,0.042915&amp;z=14&amp;iwloc=A" style="text-align:left">View Larger Map</a>');
				}
			});
		});
	});
	
	$(".ui-button-small").button().click(function() {
		var buttonElement = $(this);
		if(!$("#servicesButton").hasClass("activeButton")) {
			navigate = true;
			window.location.hash = "services";
			$("#links").stop().animate({"top": 20}, "fast");
			$("button").button("option", "disabled", true);
			$("button").removeClass("activeButton");
			$("#servicesButton").addClass("activeButton");
			$("div[id $= 'Page']:visible").fadeOut("fast", function() {
				$("#servicesPage").fadeIn("fast", function() {
					$("button").button("option", "disabled", false);
					goToByScroll($(buttonElement).attr("name"));
				});
			});
		} else {
			goToByScroll($(buttonElement).attr("name"));
		}
	});
	
	$("#checkbox_free_report").click(function() {
		if($(this).is(":checked")) {
			$("#questionForm input[name $= '_report'][name != 'free_report']").removeAttr("disabled");
		} else {
			$("#questionForm input[name $= '_report'][name != 'free_report']").attr("disabled", "disabled");
		}
	});
	
	$("#submitButton").button().click(function() {
		if($("#questionForm input[name = 'first_name']").val() == "" || 
				$("#questionForm input[name = 'last_name']").val() == "" || 
				$("#questionForm input[name = 'address']").val() == "" || 
				$("#questionForm input[name = 'city']").val() == "" || 
				$("#questionForm input[name = 'state']").val() == "" || 
				$("#questionForm input[name = 'zip']").val() == "" || 
				$("#questionForm input[name = 'email']").val() == "") {
			$("#dialog-required").dialog("open");
			return false;
		} else if($("#checkbox_free_report").is(":checked") && $("#questionForm input[name $= '_report'][name != 'free_report']:checked").length == 0) {
			$("#dialog-noreport").dialog("open");
			return false;
		} else {
			$("#questionForm").submit();
		}
	});
	
	//links
	$("#logoLink, #homeLink").click(function() {
		$("#homeButton").click();
	});
	$("#aboutLink").click(function() {
		$("#aboutButton").click();
	});
	$("#servicesLink").click(function() {
		$("#servicesButton").click();
	});
	$("#officeLink").click(function() {
		$("#officeButton").click();
	});
	$("#smileLink").click(function() {
		$("#smileButton").click();
	});
	$("#financingLink").click(function() {
		$("#financingButton").click();
	});
	$("#phoneLink, #contactLink").click(function() {
		$("#contactButton").click();
	});
	$("#mapLink").click(function() {
		$("#mapButton").click();
	});
	$("#addressLink, #addressLinkFooter").click(function() {
		$("#mapButton").click();
	});
	
	$("#year").text((new Date()).getFullYear());
	
	$(window).bind("hashchange", function() {
		if(!navigate) {
			changePage(window.location.hash.substring(1));
		}
		navigate = false;
	});
	
	$("#headerDiv").show();
	$("#bodyDiv").show();
});
