$(document).ready(function() {
	var searchField = $('input#searchPhrase');
	var searchHelpText = 'Keyword, Title, Author, ISBN';
	var signUpField = $('input#signUpEmail');
	var signUpHelpText = 'email address';
	
	if (location.search.indexOf('m=search_results') >= 0) {
		var searchPhrase = location.search.split('&')[1].split('=')[1];
		// since spaces aren't escaped correctly, i have to replace the plus they turn into with a normal space
		searchPhrase = searchPhrase.replace(/\+/g, ' ');
		// i have to reverse the special character escaping that the form is doing
		searchPhrase = unescape(searchPhrase);
		searchField.val(searchPhrase);
	}
	
	//evaluate the search box value
	//add a class if it still has the default text
	function checkHelpText(thisField, thisHelpText) {
		if (thisField.val() == '' || thisField.val() == thisHelpText) {
			thisField.addClass('helpText').val(thisHelpText);
		} else {
			thisField.removeClass('helpText');
		}
	}
	
	checkHelpText(searchField, searchHelpText);
	checkHelpText(signUpField, signUpHelpText);
	
	//put the default text back in on blur, if null and add class
	searchField.blur(function() {
		checkHelpText(searchField, searchHelpText);
	});
	
	signUpField.blur(function() {
		checkHelpText(signUpField, signUpHelpText);
	});

	//remove class on focus and remove help text if its there
	searchField.focus(function() {
		$(this).removeClass('helpText');
		if ($(this).val() == searchHelpText) {
			$(this).val('');
		}
	});
	
	signUpField.focus(function() {
		$(this).removeClass('helpText');
		if ($(this).val() == signUpHelpText) {
			$(this).val('');
		}
	});
	
	//tabs
	$('#pd_infoNav li').click(function() {
		if (!$(this).hasClass('currentTab')) {
			//switch details tab
			var oldTab = $('div#pd_infoNav li.currentTab');
			var oldTabIndex = $('div#pd_infoNav ul li').index(oldTab);
			var newIndex = $('div#pd_infoNav ul li').index(this);
			oldTab.removeClass('currentTab');
			$(this).addClass('currentTab');
			//turn off old content and show new one
			$('div.tabContent').hide();
			$('div.tabContent:eq(' + newIndex + ')').show();
			$('#pd_scroller').scrollTop(0);
		}
	});
	
	// if you just hit search without entering anything on the form we have to prevent a search of 'Keyword, Title, Author, ISBN'
	$('div#productSearch form').submit(function(e) {
		if (searchField.val() == searchHelpText) { e.preventDefault(); }
	})
	
	$('p#writeReview a, p#cancelReview a').click(function(e) {
		e.preventDefault();
		$('div#addReview').toggle();
	})
	
	$('p#readAll a').click(function(e) {
		e.preventDefault();
		$('div.review').show();
	})

	$("div#cartButtons input[value='Checkout']").addClass("checkoutBtn");
	
	// this function captures the click of the add to cart button for
	// a bundle and then sets the useBundle hidden input to Y and
	// submits the form
	$('input#bundleSubmit').click(function() {
		$('input#bundleAdd').val('Y');
		$('input#addToCartButton').click();
	})

	//$('.fancyzoom').myFancyZoom({'title':'Click anywhere to close','overlay':'0.8','color':'#000000','marginRight':'0'});

}); // end of the ready function

function validateReviewRating() {
	//alert('triggered');
	if(!document.reviewForm.reviewRating.value || isNaN(document.reviewForm.reviewRating.value) || document.reviewForm.reviewRating.value < 1 || document.reviewForm.reviewRating.value > 5 || !document.reviewForm.reviewName.value || !document.reviewForm.reviewBody.value){
		alert("All Review Fields are Required Fields");
		return false;
	}

	return true;
}
