// JavaScript Document for Kitchen Classics Cabinets Website

function right( str, n ){
	if ( n <= 0 )
		return "";
	else if ( n > String( str ).length )
		return str;
	else {
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
} // end right

function initLoadingGIF() {
	$("body").append("<div id=\"loadingImage\">LOADING... <img src=\"images/kccabinets/loading.gif\" alt=\"loading - please wait\" /></div>");
	$("#loadingImage").css("display", "none");
	$("#loadingImage").css("color", "#4b3905");
	$("#loadingImage").css("font-size", "10px");
	$("#loadingImage").css("font-family", "sans-serif");
	$("#loadingImage").css("position", "absolute");
	$("#loadingImage").css("bottom", "50px" );
	$("#loadingImage").css("right", "50px" );
} // end initLoadingGIF

function toggleLoadingGIF() {
	if( $("#loadingImage").css("display") == "none" ) {
		$("#loadingImage").css("display", "block");
	} else {
		$("#loadingImage").css("display", "none");
	} // end if
} // end toggleLoadingGIF

function setDateSelectField() {
	var today = new Date();
	var month = today.getMonth() + 1;
	var day = today.getDate() + 1;
	var year = today.getYear();
	if( year < 1900 ) year += 1900;
	if( month < 10 ) month = "0" + month;
	if( day < 10 ) day = "0" + day;
	$("#q5_mm option[value="+month+"]").attr("selected", "selected");
	$("#q5_dd option[value="+day+"]").attr("selected", "selected");
	$("#q5_yyyy").replaceWith("<select name=\"q5_yyyy\" id=\"q5_yyyy\"><option value=\""+year+"\">"+year+"</option><option value=\""+(year+1)+"\">"+(year+1)+"</option><option value=\""+(year+2)+"\">"+(year+2)+"</option></select>");
	$("#q5_yyyy option[value="+year+"]").attr("selected", "selected");
} // end setDateSelectField

function handleKitchenGalleryRollover() {
	$('#kitchenGallery').tabs({
		fxFade: true,
		fxSpeed: 'fast'
	});

	$("#finishSwatches a").bind('mouseenter',  function() {
		var link = $(this).attr("href");
		$("#listOfFinishes li").css("display", "none");
		$(link).fadeIn("fast");
	});
	
	$("#kitchenGalleryFeatureMap area").mouseover( function() {
		var link = $(this).attr("href");
		$(".kitchenFeature").css("display", "none");
		$(link).fadeIn("fast");
	});
} // end handleKitchenGalleryRollover

function handleConstructionRollover() {
	$('#vanityConstruction').tabs({
		fxFade: true,
		fxSpeed: 'fast'
	});

	$('#kitchenConstruction').tabs({
		fxFade: true,
		fxSpeed: 'fast'
	});
	
	$("#cabinetMap area").mouseover( function() {
		var link = $(this).attr("href");
		$(".constructionPoints li").css("display", "none");
		$(""+link).fadeIn("fast");
	});
	
	$("#drawerMap area").mouseover( function() {
		var link = $(this).attr("href");
		$(".constructionPoints li").css("display", "none");
		$(""+link).fadeIn("fast");
	});
	
	$("#hingeMap area").mouseover( function() {
		var link = $(this).attr("href");
		$(".constructionPoints li").css("display", "none");
		$(""+link).fadeIn("fast");
	});
} // end handleConstructionRollover


/* Perform these functions once the page is finished loading */
$(function() {

	/* Cause PDF Links to open in a new window */
	$("a[@href$=pdf]").click( function() {
		window.open( this.href );
		return false;
	});
	
	/* Cause fully qualified Links to open in a new window */
	$("a[@href^=http]").click( function() {
		window.open( this.href );
		return false;
	});
	$("a.outside").click( function() {
		window.open( this.href );
		return false;
	});
	
	/* When loading a new page - show a little animation to let the user know stuff is happening */
	initLoadingGIF();
	$("a[@href$=html]").click( function() {
		toggleLoadingGIF();
	});
	

	/* Replace the standard select box with something better looking */
	$("#howDoISelector").css("display", "none");
	$("#howDoIButton").css("display", "inline");
	$("#howDoIButton").click( function() {
		$("#howDoIQuestionLinks").slideToggle("slow");
		return false;
	});
	
	
	handleKitchenGalleryRollover();
	handleConstructionRollover();
	
	/* For all same-page links, slowly scroll to them */
	$('#faqQuestions a, #howQuestions a').click(function() {
		var $target = $(this.hash);
		$target = $target.length && $target
		|| $('[name=' + $(this.hash).slice(1) +']');
		if ($target.length) {
			var targetOffset = $target.offset().top;
			$('html,body')
			.animate({scrollTop: targetOffset}, 1000);
		 return false;
		}
  });

	setDateSelectField();
	
	/* If lightbox is available, use it */
	if ( typeof $(this).lightBox != "undefined" ) {
		$("a[@rel*=lightbox]").lightBox(); 
	}
	
	/* When someone submits the form - check the fields */
	$("#dealerForm").submit( function() {
		if( 
			 ( $("input[name=zip]").val() != "" ) &&
			 ( $("input[name=zip]").val() != undefined ) 
			) {
			// Form properly submitted, continue.
		} else {
			alert("Please fill out the Postal Code / ZIP");
			return false; /* do not submit */
		}
	});
	
	/* When someone submits the form - check the fields */
	$("form[name=contactForm]").submit( function() {
		if( 
			 ( $("input[name=fName]").val() != "" ) &&
			 ( $("input[name=lName").val() != "" ) &&
			 ( $("input[name=addr1").val() != "" ) &&
			 ( $("input[name=city").val() != "" ) &&
			 ( $("input[name=stateProv").val() != "" ) &&
			 ( $("input[name=postalCode").val() != "" )
			) {
			// Form properly submitted, continue.
		} else {
			alert("Please fill out all of the required fields");
			return false; /* do not submit */
		}
	});
	
});