// Start scroll functions //
	function scrollContent(origin, destination, travelTime, scrollMode) {
		switch (scrollMode) {
		case "x-first":
			scroll_x(destination, travelTime, false);
			break;
		case "x-only":
			scroll_x(destination, travelTime, true);
			break;
		case "y-first":
			scroll_y(destination, travelTime, false);
			break;
		case "y-only":
			scroll_y(destination, travelTime, true);
			break;
		default:
			scrollBoth(destination, travelTime);
			break;
		}
		return false;
	}
	function scroll_x(destination, travelTime, singleAxis) {
		if(travelTime.x < 0) travelTime.x = travelTime.x * (-1);
		if (singleAxis == true) {
			$("html, body").animate({scrollLeft: destination.x}, travelTime.x);
		} else {
			$("html, body").animate({scrollLeft: destination.x}, travelTime.x, '', function() {
				scroll_y(destination, travelTime, true);
			});
		}
		return false;
	}
	function scroll_y(destination, travelTime, singleAxis) {
		if(travelTime.y < 0) travelTime.y = travelTime.y * (-1);
		if (singleAxis == true) {
			$("html, body").animate({scrollTop: destination.y}, travelTime.y);
		} else {
			$("html, body").animate({scrollTop: destination.y}, travelTime.y, '', function() {
				scroll_x(destination, travelTime, true);
			});
		}
		return false;
	}
	function scrollBoth(destination, travelTime) {
		$("html, body").animate({scrollLeft: destination.x, scrollTop: destination.y}, travelTime.xy);
		return false;
	}
	function getCoordinates(element) {
		if (element == null) {
			var coordsObject = {
				x : $(window).scrollLeft(),
				y : $(window).scrollTop()
			}
		} else if($(element).offsetParent().position().left > $(element).offsetParent().offsetParent().position().left) {
			var coordsObject = {
				x : $(element).offsetParent().position().left,
				y : $(element).position().top
			}
		} else {
			var coordsObject = {
				x : $(element).position().left,
				y : $(element).position().top
			}
		}
		return coordsObject;
	}
	function getDistance(origin, destination) {
		var distanceObject = {
			x : Math.sqrt(Math.pow((origin.x - destination.x), 2)),
			y : Math.sqrt(Math.pow((origin.y - destination.y), 2))
		}
		function getXYDistance(x, y) {
			var xy = Math.sqrt((x * x) + (y * y));
			return xy;
		}
		distanceObject.xy = getXYDistance(distanceObject.x, distanceObject.y);
		return distanceObject;
	}
	function getTravelTime(distance, rate) {
		var	timeObject = {
			x : distance.x * rate,
			y : distance.y * rate,
			xy : distance.xy * rate
		}
		return timeObject;
	}
// End scroll functions //

	function getPixelsAsInt(pixelValue) {
		var pixInt = parseInt(pixelValue.replace("px",""));
		return pixInt;
	}
	
	function formSubmitted(submitCount) {
		$("body").addClass("formSubmitted");
		var origin = getCoordinates();
		var destination = {
			x : origin.x,
			y : 0
		}
		var distance = getDistance(origin, destination);
		var travelTime = getTravelTime(distance, .55);
		scrollContent(origin, destination, travelTime, "y-only");
		$("body a[href]").not(".category a").not(".rollover").each(function() {
			$(this).attr("href", replaceURLsWithColumns($(this).attr("href")));
		});
		setRolloverTriggers("rollover", submitCount);
		$("#modal_PrivacyPolicy").jqm({trigger: "a.modal.privacy", closeClass: "closeLink"});
//		alert("Form submitted");
		aggregatePageContent();
	}
	
	function initialNavHighlight() {
		var bodyClass = $("body").attr("class");
		if (bodyClass.indexOf("aggregate") > -1) {
			var activeColumn = 0;
			$(".navMenu a").not(".category a").each(function() {
				if ($(this).attr("class").indexOf("active") > -1) {
					activeColumn++;
				}
			});
			if(activeColumn < 1 ) $(".navMenu .home").addClass("active");
		} else {
			bodyClass = $.trim(bodyClass.replace("commercial","").replace("residential",""));
			$(".navMenu ."+bodyClass).addClass("active");
		}
	}
	
	function parseQueryString() {
		if(window.location.toString().indexOf("?") > -1) {
			var queryString = window.location.toString().split("?")[1];
			var pairs = queryString.split("&");
			for(var i=0;i<pairs.length;i++) {
				var pair = pairs[i].split("=");
				var key = pair[0];
				var val = pair[1];
				switch(key) {
				case "p":
					initialScroll(val);
					break;
				default:
					break;
				}
			}
		}
	}
	
	function classifyPDFLinks() {
		$("body a[href$=pdf]").addClass("pdf");	
	}

	function initialScroll(columnClass) {
		$(".navMenu ."+columnClass).click();
	}
	
	function isolateNavClass(elementClass) {
		var returnClass = $.trim($(elementClass).attr("class")
			.replace("column","")
			.replace("contentArea","")
			.replace("form","")
			.replace("first","")
			.replace("last",""));
		return returnClass;
	}

	function addAnchorsToHeadlines() {
		$(".column h1").not(".column.first h1").each(function() {
//			alert($(this).text());
			var tempHref = $(this).parent().parent().attr("id");
			$(this).addClass("navMenu").wrapInner("<a href=\"#"+tempHref+"\"></a>");
			var hrefAttr = $(this).children().filter("a").attr("href");
			if (hrefAttr.indexOf("http") > -1) {
				var tempElement = hrefAttr.split("#");
				$(this).children().filter("a").attr("href", "#" + tempElement[1]);
			}
		});
	}

	function aggregatePageContent(elem) {
		$("body").addClass("aggregated");
		var columnWidth = getPixelsAsInt($(".column").css("width")) +
			getPixelsAsInt($(".column").css("padding-left")) +
			getPixelsAsInt($(".column").css("padding-right")) +
			getPixelsAsInt($(".column").css("margin-left")) +
			getPixelsAsInt($(".column").css("margin-right"));

		var totalColumns = $(".column").size();
		if($(window).width() > columnWidth) {
			var wrapperPadding = ($(window).width() % columnWidth);
		} else {
			var wrapperPadding = 0;	
		}
		$("#wrapper").add(".header").css("width", totalColumns * columnWidth +"px");
		//.css("padding-right", wrapperPadding);
		$(window).add("html, body").scroll(0,0);
		$(window).bind('scroll', function() {
			var origin = getCoordinates();
			$(".header").css({backgroundPosition: "-"+origin.x+"px 0"});
		});
		addAnchorsToHeadlines();
		$("a[href^=#]").not(".category a").not(".modal").not(".rollover").each(function() {
			var hrefTarget = $(this).attr("href");
			if(hrefTarget != "#") {
			}
			$(this).css("cursor","pointer");
			if (hrefTarget.indexOf("http") > -1) {
				var tempElement = hrefTarget.split("#");
				var hrefTarget = "#" + tempElement[1];
			}
			$(this).click(function() {
				$(this).removeAttr("href");
				var activeClass = isolateNavClass(hrefTarget);
				var scrollMode = '';
				var origin = getCoordinates();
				var destination = getCoordinates(hrefTarget);
				$(".header .navMenu a").not(".category a").removeClass("active");
				$(".navMenu ."+activeClass).addClass("active");
					destination.y = destination.y + (getPixelsAsInt($(hrefTarget).css("margin-top")) - $(".header").height());
					if(destination.y <= $(".header").height()) {
						destination.y = 0;
					}
				var distance = getDistance(origin, destination);
				$("div.modal").jqmHide();
				if ((distance.x == 0) && (distance.y ==0)) {
					return false;
				} else {
					var travelTime = getTravelTime(distance, .55);
					scrollContent(origin, destination, travelTime);
				}
				$(this).attr("href", hrefTarget);
				return false;
			});
		});
		parseQueryString();
	}
	
	function splitPairs(pairString, token, limit) {
		var pairString = pairString;
		var limit = limit || "";
		if (limit == "") {
			var pairArray = pairString.split(token);
		} else {
			var pairArray = pairString.split(token,limit);
		}
		return pairArray;
	}
	
	function replaceURLsWithColumns(replaceText, categoryReplace) {
		if(categoryReplace == null) {
			categoryReplace = false;
		} else {
			categoryReplace = categoryReplace;
		}
	var replaceObj = new Array;
		replaceObj[0] = "/Residential/Default.aspx|#content_Home";
		replaceObj[1] = "/Residential/Service.aspx|#content_Service";
		replaceObj[2] = "/Residential/Specials.aspx|#content_Specials";
		replaceObj[3] = "/Residential/Warranty.aspx|#content_Warranty";
		replaceObj[4] = "/Residential/Tips.aspx|#content_Helpful-Tips";
		replaceObj[5] = "/Residential/Supplies.aspx|#content_Supplies";
		replaceObj[6] = "/Residential/About.aspx|#content_About";
		replaceObj[7] = "/Residential/Contact.aspx|#content_Contact";
		replaceObj[8] = "/Commercial/Default.aspx|#content_Home";
		replaceObj[9] = "/Commercial/Service.aspx|#content_Service";
		replaceObj[10] = "/Commercial/About.aspx|#content_About";
		replaceObj[11] = "/Commercial/Contact.aspx|#content_Contact";
	
		for(var i=0; i<replaceObj.length;i++) {
			var pair = splitPairs(replaceObj[i], "|");
			if(replaceText.indexOf(pair[0]) > -1) {
				if(categoryReplace == true) {
					if(pair[0].indexOf("Residential") > -1) {
						replaceText = "/Residential.aspx";
					} else if (pair[0].indexOf("Commercial") > -1) {
						replaceText = "/Commercial.aspx";
					}
				} else {
					replaceText = pair[1];
				}
			}
		}
		return replaceText;
	}
	
	function setRolloverTriggers(rolloverClass) {
		$("a."+rolloverClass).each(function() {
			var hrefTarget = $(this).attr("href");
			if (hrefTarget.indexOf("http") > -1) {
				var tempElement = hrefTarget.split("#");
				var hrefTarget = "#" + tempElement[1];
			}
				$(this).css("cursor", "pointer");
			var triggerCoords = getCoordinates($(this));
				triggerCoords.x = triggerCoords.x + $(this).parent().position().left + 144;
				if($("body").hasClass("formSubmitted")) {
					triggerCoords.y = triggerCoords.y + $(this).offsetParent().position().top;
				} else {
					triggerCoords.y = triggerCoords.y + $(this).offsetParent().position().top+241;
				}
//			alert(hrefTarget);
			$(hrefTarget).css("left",triggerCoords.x+"px").css("top",triggerCoords.y+"px");
			$(this).parent().add(hrefTarget).hover(
				function() {
					$(this).removeAttr("href");
					$(hrefTarget).css("margin-left",0);
//					alert("Top: "+$(hrefTarget).css("top")+"\nLeft: "+$(hrefTarget).css("left"));
				},
				function() {
					$(this).attr("href", hrefTarget);
					$(hrefTarget).css("margin-left","-9999em");
				}
			);
		});
	}

	$(function() {
		classifyPDFLinks();
		// Start Modal work
		initialNavHighlight();
		setRolloverTriggers("rollover");
		$("#modal_PrivacyPolicy").jqm({trigger: "a.modal.privacy", closeClass: "closeLink"});
	});

