var $_ = jQuery.noConflict();

$_(window).load(function() {
	
	var contentHeight = $_('section').outerHeight();
	
	$_('asside').css("height", "" + contentHeight + "");  
	
});

// handle display of the correct form elements on the request a quote page
$_(document).ready(function() {
	// Show Dimensions for Webform
	showDimensionsForm();
	$_("input[name*='submitted[shape_size]']").click(showDimensionsForm);
	
	// Handle the Which One Div
	$_("#webform-component-which-one").hide();
	showWhichOne();
	$_("#edit-submitted-referred-by").change(showWhichOne);
	$_("#edit-submitted-referred-by").trigger('change');
	// Show Dimensions for Product Estimator
	showDimensionsEstimatorForm();
	$_("input[name=shape]").click(showDimensionsEstimatorForm);
	createEventListeners();
});

function showDimensionsForm() {
	
	var val = $_("input[name*='submitted[shape_size]']:checked").val();

	if (val != undefined) {
          $_("#webform-component-dimensions .webform-component").hide();
	  $_("#webform-component-dimensions .webform-component input").each(function() {
		$_(this).val('');
	  });
	  $_("#webform-component-dimensions").slideDown('fast', function() {
	  switch (val) {
		case "L-shaped":
		    $_("#webform-component-dimensions--left-wall-length").show();
		    $_("#webform-component-dimensions--right-wall-length").show();
		    $_("#webform-component-dimensions--height").show();
		    break;
		case "Straight-wall":
		    $_("#webform-component-dimensions--length").show();
		    $_("#webform-component-dimensions--height").show();
		    break;
		case "U-shaped":
		    $_("#webform-component-dimensions--left-wall-length").show();
		    $_("#webform-component-dimensions--right-wall-length").show();
		    $_("#webform-component-dimensions--back-wall-length").show();
		    $_("#webform-component-dimensions--height").show();
		    break;
		case "C-shaped":
		    $_("#webform-component-dimensions--left-wall-length").show();
		    $_("#webform-component-dimensions--right-wall-length").show();
		    $_("#webform-component-dimensions--back-wall-length").show();
		    $_("#webform-component-dimensions--front-wall-length").show();
		    $_("#webform-component-dimensions--height").show();
		    break;
	      }
		
	});	
	  
	} else {
	   $_("#webform-component-dimensions").hide();
	}
	
}



function showDimensionsEstimatorForm() {	
	$val = $_("input[name=shape]:checked").val();
	$_("#edit-dimensions-0").hide();
	$_("#edit-dimensions-1").hide();
	$_("#edit-dimensions-2").hide();
	$_("#c-shaped-message").hide();
	$_("#edit-height").show();
	$_("#edit-submit").show(); 
	if ($val != undefined) {
	  
	  switch ($val) {
		case "u":
		    $_("#edit-dimensions-0").show();
		    $_("#edit-dimensions-1").show();
		    $_("#edit-dimensions-2").show();
		    $_("#edit-dimensions-0 legend").text("Left Wall Length");
		    $_("#edit-dimensions-1 legend").text("Back Wall Length");
		    $_("#edit-dimensions-2 legend").text("Right Wall Length");
		    break;
		case "l":
		    $_("#edit-dimensions-0").show();
		    $_("#edit-dimensions-1").show();
		    $_("#edit-dimensions-0 legend").text("Left Wall Length");
		    $_("#edit-dimensions-1 legend").text("Right Wall Length");
		    break;
		case "s":
		   $_("#edit-dimensions-0").show();
		    $_("#edit-dimensions-0 legend").text("Wall Length");
			break;
		case "c":
		    $_("#edit-height").hide();
		    $_("#edit-submit").hide();
		    $_("#c-shaped-message").show();
		    break;
	      }	 
	  
	}
	
}

function showWhichOne() {
	$val = $_("#edit-submitted-referred-by").val();
	if ($val != "none")
	  $_("#webform-component-which-one").slideDown();
	else
	  $_("#webform-component-which-one").slideUp();
	switch($val) {
		case "search_engine":
			$lbl = "Which Search Engine";
		break;
		case "referral":
			$lbl = "Who";
			break;
		case "other":
			$lbl = "Explain";
		break;
		default:
			$lbl = "Which One";
		break;
	}
	$_("#webform-component-which-one label").text($lbl);
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open('/multimedia/' + theURL,winName,features);
}
function createEventListeners() {
	// Handle Product RollLover
	 $_("#additionalImages a").mouseover(function(){
		$_("#mainImage img").attr("src", $_(this).attr("rollover"))
		$_("#mainImage a").attr("longdesc", $_(this).attr("href"))
		return false;
	});
	 //  RAQ
	 if ($_('body').hasClass('request-quote')) {
		var arrShowList = {
			fullList: '#webform-component-mystudio, #webform-component-are-you-interested-in-installation-services, #webform-component-which-procyc-system-are-you-interested-in, #webform-component-unsure, #webform-component-shape-size, #webform-component-dimensions, #webform-component-required-flooring',
			cycs : '#webform-component-are-you-interested-in-installation-services, #webform-component-which-procyc-system-are-you-interested-in, #webform-component-unsure, #webform-component-shape-size',
			flooring : '#webform-component-required-flooring',
			mystudio : '#webform-component-mystudio'
		}
		// set hidden fields
		$_(arrShowList.fullList).hide();
		$_('div#edit-submitted-freestanding-or-built-in input').change(function() {
			switch($_(this).attr('id')) {
				case 'edit-submitted-freestanding-or-built-in-1':
				case 'edit-submitted-freestanding-or-built-in-2':
					if ($_('#edit-submitted-freestanding-or-built-in-1').is(':checked') ||
					    $_('#edit-submitted-freestanding-or-built-in-2').is(':checked')) 
						$_(arrShowList.cycs).slideDown()
					else
						$_(arrShowList.cycs).slideUp();
						
					var arrWhichProducts = {
						fs : [0,3,4],
						bi : [0,1,2,3,5]
					};
					$_('div.form-type-radio.form-item-submitted-which-procyc-system-are-you-interested-in').hide();
					if ($_('#edit-submitted-freestanding-or-built-in-1').is(':checked')) {
						for (i=0;i<arrWhichProducts.fs.length;i++) {
							$_('div.form-type-radio.form-item-submitted-which-procyc-system-are-you-interested-in').eq(arrWhichProducts.fs[i]).show();
						}
					}
					if ($_('#edit-submitted-freestanding-or-built-in-2').is(':checked')) {
						for (i=0;i<arrWhichProducts.bi.length;i++) {
							$_('div.form-type-radio.form-item-submitted-which-procyc-system-are-you-interested-in').eq(arrWhichProducts.bi[i]).show();
						}
					}
					
					break;
				case 'edit-submitted-freestanding-or-built-in-3':
					if ($_(this).is(':checked')) 
						$_(arrShowList.flooring).slideDown()
					else
						$_(arrShowList.flooring).slideUp();
					break;
				case 'edit-submitted-freestanding-or-built-in-4':
					if ($_(this).is(':checked')) 
						$_(arrShowList.mystudio).slideDown()
					else
						$_(arrShowList.mystudio).slideUp();
					break;
			}
		});
		
		$_('div#edit-submitted-freestanding-or-built-in input').trigger('change');
		// on submit
		$_('#webform-client-form-1612').submit(function() {
			var errs = [];
			if ($_('#edit-submitted-freestanding-or-built-in-1').is(":checked") || $_('#edit-submitted-freestanding-or-built-in-2').is(":checked")) {
				if ($_('#edit-submitted-are-you-interested-in-installation-services input:radio:checked').val() == undefined) {
					errs.push("Please tell us if you'd like installation services.");
					$_('#webform-component-are-you-interested-in-installation-services').addClass('alert');
				}
				if ($_('#webform-component-which-procyc-system-are-you-interested-in input:radio:checked').val() == undefined) {
					errs.push("Please tell us which cyc you are interested in.");
					$_('#webform-component-which-procyc-system-are-you-interested-in').addClass('alert');
				}
				
				if ($_('#edit-submitted-shape-size input:radio:checked').val() == undefined) {
					errs.push("Please tell us the shape and dimensions of your cyc.");
					$_('#webform-component-shape-size').addClass('alert');
				}
				
			}
			else if ($_('#edit-submitted-freestanding-or-built-in-3').is(":checked")) {
				if ($_("input:text[name*='submitted[required_flooring]']").val() == undefined) {
					errs.push("Please tell us how many sq. feet of flooring you need.");
					$_('#webform-component-shape-size').addClass('alert');
				}
			}
			if (errs.length > 0) {
				alert(errs.join("\n"));
				return false;
			}
			
		});
	 }
	 
};

