$(document).ready(function() {

		$(function() {
			$('#scroll').jScrollPane({scrollbarWidth:18, scrollbarMargin:0, showArrows:true, arrowSize:18});
			$('.jScrollPaneContainer').parent().css('padding-right', 0);
			$('.jScrollPaneContainer').css('width', 435);
		});
		
		$('#floor_plans').parent().parent().css('padding-right', 0);
		$('.contact .space').hide();

		// Preload all rollovers
		$("#nav img, #options.l4 img").each(function() {
			// Set the original src
			rollsrc = $(this).attr("src");
			rollON = rollsrc.replace(/.jpg$/ig,"_over.jpg");
			$("<img>").attr("src", rollON);
		});
		
		// Navigation rollovers
		$("#nav a, #options a").mouseover(function(){
			imgsrc = $(this).children("img").attr("src");
			if(imgsrc) {
				matches = imgsrc.match(/_over/);
			
				// don't do the rollover if state is already ON
				if (!matches) {
				imgsrcON = imgsrc.replace(/.jpg$/ig,"_over.jpg"); // strip off extension
				$(this).children("img").attr("src", imgsrcON);
				}
			}
			
		});
		$("#nav a, #options a").mouseout(function(){
			if(imgsrc && $(this).hasClass('active') == false) {
				$(this).children("img").attr("src", imgsrc);
			}
		});
		
		// Determine the active section button
		if (location.pathname.substring(1)) {
			// get the path
			thePath = location.pathname.substring(1);
			thePath = thePath.match(/^\/?([a-z0-9_\.-]+)/); // only get the first part of the path

			thePath = '/' + thePath[0];
			
			navLink = $('#nav a[@href$="' + thePath + '"]');
			imgsrc = $(navLink).children("img").attr("src");
			if (imgsrc) {
			imgsrcON = imgsrc.replace(/.jpg$/ig,"_over.jpg"); // strip off .gif
			$(navLink).children("img").attr("src", imgsrcON);
			}
		}
		
		// Determine the active option icon
			if (location.pathname.substring(1)) {
			// get the path
			optPath = location.pathname.substring(1);
			optPath = '/' + optPath;
			
			optLink = $('#options a[@href$="' + optPath + '"]');
			imgsrc = $(optLink).children("img").attr("src");
			$(optLink).addClass('active');

			if (imgsrc) {
			imgsrcON = imgsrc.replace(/.jpg$/ig,"_over.jpg");
			$(optLink).children("img").attr("src", imgsrcON);
			}
		}
		
		// Determine the active subnav
		// Adds the 'ON' class to the link
		if (location.pathname.substring(1)) {
			// get the path
			subPath = location.pathname.substring(1).match(/^\/?([a-z0-9_\.-]+)\/([a-z0-9_\.-]+)/);
			if (subPath) {
				subPath = '/' + subPath[1] + '/' + subPath[2];

				subnavLink = $('#nav div a[@href$="' + subPath + '"]');
				$(subnavLink).addClass("ON");
			}
		}
		
		if (location.pathname == '/progress/images' && location.hash) {
			var image_id = location.hash.substr(1);
		}

	
	$(".models a").click(function() {
		url = $(this).attr("href");
		d = $(this).attr("id").split(/_/);
		window.open(url, "floorplan", "height="+(parseInt(d[1])+164)+",width="+(parseInt(d[0])+25)+",scrollbars=0,resizable=0");
		return false;
	});
	
	$("#bottom a").click(function() {
		url = $(this).attr("href");
		window.open(url, "option", "height=505,width=625,scrollbars=0,resizable=0");
		return false;
	});
	
	// Contact us!
	$('.contact #type').change(function() {
		$('input[@name="forwardaddress"]').val(
			($(this).val() == "Residential")?
			"http://www.whiteprovision.com/contact/residential":
			"http://www.whiteprovision.com/contact/commercial"
		);
		$('.contact .space').css('display', ($(this).val() == "Residential")?"none":"block")
	});
	
	$('.contact').submit(function() {
		var error = false;
		if ($('.contact #name').val().length <= 2) {
			$('.contact #name').parent().addClass('error');
			error = true;
		} else {
			$('.contact #name').parent().removeClass('error');
		}
		
		if ($('.contact #type').val().length <= 2) {
			$('.contact #type').parent().addClass('error');
			error = true;
		} else {
			$('.contact #type').parent().removeClass('error');
		}
		
		if ($('.contact #address').val().length <= 2) {
			$('.contact #address').parent().addClass('error');
			error = true;
		} else {
			$('.contact #address').parent().removeClass('error');
		}
		
		if ($('.contact #city').val().length <= 2) {
			$('.contact #city').parent().addClass('error');
			error = true;
		} else {
			$('.contact #city').parent().removeClass('error');
		}
		
		if ($('.contact #state').val().length < 2) {
			$('.contact #state').parent().addClass('error');
			error = true;
		} else {
			$('.contact #state').parent().removeClass('error');
		}
		
		if ($('.contact #zip').val().length <= 2) {
			$('.contact #zip').parent().addClass('error');
			error = true;
		} else {
			$('.contact #zip').parent().removeClass('error');
		}
		
		if ($('.contact #phone').val().length <= 2) {
			$('.contact #phone').parent().addClass('error');
			error = true;
		} else {
			$('.contact #phone').parent().removeClass('error');
		}
		
		if ($('.contact #space').parent().css("display") == "block" && $('.contact #space').val().length <= 2) {
			$('.contact #space').parent().addClass('error');
			error = true;
		} else {
			$('.contact #space').parent().removeClass('error');
		}
		
		if ($('.contact #comments').val().length <= 2) {
			$('.contact #comments').parent().addClass('error');
			error = true;
		} else {
			$('.contact #comments').parent().removeClass('error');
		}

		var test = /^([a-z0-9\-_\+\.]+)@(([a-z0-9\-_]+)\.)?([a-z0-9\-_]+)\.([a-z0-9]{1,})(\.[a-z0-9]+)?$/i;
		if ($('.contact #email').val().match(test) == null) {
			$('.contact #email').parent().addClass('error');
			error = true;
		} else {
			$('.contact #email').parent().removeClass('error');
		}
		
		if (error) {
			alert("You must fill out all required (bold) form fields!");
		}
		return (!error); // No errors = submit form
	});
});

	// Function, called from homepage flash banner, to rotate sidebar photos in and out
	function sidebarSwap(swapin) {
		inpic = 'homeright_' + swapin + '.jpg';
		//outpic = 'homeright_' + swapout + '.jpg';
		
		$('#homeright').fadeOut(500, function() {
		$(this).hide();
		$('#homeright').html('<img src="/pub/layout/' + inpic + '" width="293" height="342" />');
		$('#homeright').fadeIn(500);
		});
	}