salmon.namespace.addNamespace("boots.product.details");
boots.product.details = {
	reviewsPageUrl: "",
	reviewsPaginationUrl: "",
	emailPageFormInProgress:false,
	closeShareHover:false,
	setEvents:function(){
		$(".btn_print").bind("click",function(){
			window.print();
			return false;
		});
		$(".dialogContent").bind("hover",function(){
			//console.log("showme");
		});
		$('a[href*=#]').click(function() {
			if($(this).attr("href") != "#"){
				if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
					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;
					}
				}
			}
		});
		this.setPaginationEvents();
	},
	validateProductEmailForm:function(thisForm){
		var formComplete = true;
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		thisForm = ".dialog .pd_sendProductEmail";
		$(thisForm).find("input").each(function(){
			if($(this).hasClass("pd_text")){
				if($(this).val() == ""){
					$(this).addClass("pdFormError");
					formComplete = false;
				} else {
					$(this).removeClass("pdFormError");
				}
			} else if($(this).hasClass("pd_email")) {
				if(!filter.test($(this).val())){
					$(this).addClass("pdFormError");
					formComplete = false;
				} else {
					$(this).removeClass("pdFormError");
				}
			}
		});
		$(thisForm).find("textarea").each(function(){
			if($(this).val() == ""){
				$(this).addClass("pdFormError");
				formComplete = false;
			} else {
				$(this).removeClass("pdFormError");
			}
		});
		if(formComplete){
			var dataUrl = $(thisForm).attr("action");
			function urlHasQuestion(){
				if(dataUrl.indexOf("?")>0){
					return true;
				} else {
					return false;
				}
			}
			$(thisForm).find("input").each(function(){
				if($(this).attr("rel") != "submit"){
					dataUrl += (urlHasQuestion()) ? "&" : "?";
					dataUrl += $(this).attr("name") + "=" + $(this).val();
				}
				$(this).attr("disabled","disabled");
			});
			$(thisForm).find("textarea").each(function(){
				dataUrl += (urlHasQuestion()) ? "&" : "?";
				dataUrl += $(this).attr("name") + "=" + $(this).val();
				$(this).attr("disabled","disabled");
			});
			if(!boots.product.details.emailPageFormInProgress){
				boots.product.details.emailPageFormInProgress = true;
				$.ajax({
					url: encodeURI(dataUrl),
					dataType: 'text',
					type: "post",
					success: function(data, status, xhr) {
						data = data.replace(/^\s*|\s*$/g,'');
						if(data == "SUCCESS"){
							$(thisForm).remove();
							$(".dialog .content .wrapper .bd").append("<p><strong>Thanks for sharing this product with a friend</strong></p>");
							$("iframe#iframe").height(100);
							window.setTimeout(boots.product.details.removeDialog, 2000);
							boots.product.details.emailPageFormInProgress = false;
						}
					},
					error: function(xhr, status, err) {
						alert('Error sending product email');
					}
				});
			}
		}
		return false;
	},
	removeDialog:function(){
		$(".dialog .close a").trigger("click");
	},
	setPaginationEvents:function(){
		$(".page_productDetails .pagination a").bind("click",function(){
			boots.product.details.reviewsPageUrl = $(this).attr("href");
			boots.product.details.reviewsPaginationUrl = boots.product.details.reviewsPageUrl.replace(/ReviewsPageView/g,"ReviewsPaginationView");
			boots.product.details.setReviewsLoadingBlocker();
			boots.product.details.loadReviewPagination();
			$(this).blur();
			return false;
		});
		$(".page_productDetails form.reviewsFilterForm").submit(function(){
			var theReviewUrl = "/webapp/wcs/stores/servlet/ReviewsPaginationView?pageToDisplay=1"
			theReviewUrl += "&storeId=" + $(this).find("#storeId").val();
			theReviewUrl += "&productId=" + $(this).find("#productId").val();
			theReviewUrl += "&orderBy=" + $(this).find("#orderBy").val();
			theReviewUrl += "&catalogId=" + $(this).find("#catalogId").val();
			theReviewUrl += "&langId=" + $(this).find("#langId").val();
			boots.product.details.reviewsPaginationUrl = theReviewUrl;
			boots.product.details.setReviewsLoadingBlocker();
			boots.product.details.loadReviewPagination();
			return false;
		});
	},
	loadReviewPagination:function(){
		$.ajax({
			url: this.reviewsPaginationUrl,
			dataType: 'html',
			success: function(data, status, xhr) {
				boots.product.details.setNewPaginationData(data);
				boots.product.details.loadReviewPage(data);
			},
			error: function(xhr, status, err) {
				boots.product.details.removeReviewsLoadingBlocker();
				alert('Error getting product reviews');
			}
		});
	},
	loadReviewPage:function(){
		$.ajax({
			url: this.reviewsPageUrl,
			dataType: 'html',
			success: function(data, status, xhr) {
				boots.product.details.setNewReviewsData(data);
			},
			error: function(xhr, status, err) {
				boots.product.details.removeReviewsLoadingBlocker();
				alert('Error getting product reviews');
			}
		});
	},
	setNewPaginationData:function(newHTML){
		$(".paginationHolder .pagination").remove();
		$(".paginationHolder").append(newHTML);
	},
	setNewReviewsData:function(newHTML){
		$(".pd_reviewsHolder .customerReviewPanel").remove();
		$(".pd_reviewsHolder").append(newHTML);
		this.allNewsLoadedShowNewOnes();
	},
	allNewsLoadedShowNewOnes:function(){
		this.removeReviewsLoadingBlocker();
		this.setPaginationEvents();
	},
	setReviewsLoadingBlocker:function(){
		var reviewsHeight = $(".pd_customerReviews").outerHeight();
		var reviewsWidth = $(".pd_customerReviews").outerWidth();
		var reviewsTop = $(".pd_customerReviews").position().top;
		var reviewsLeft = $(".pd_customerReviews").position().left;
		var reviewsBlocker = "<div id=\"reviews_blocker\">&nbsp;</div>";
		var reviewsLoading = "<img id=\"reviews_loading\" src=\"/wcsstore/ConsumerDirectStorefrontAssetStore/en_US/i/pd/ajax-loader.gif\" />";
		$("body").append(reviewsBlocker);
		$("body").append(reviewsLoading);
		$("#reviews_blocker").css({
			"height":reviewsHeight,
			"width":reviewsWidth,
			"top":reviewsTop,
			"left":reviewsLeft,
			"position":"absolute",
			"backgroundColor":"#ffffff",
			"opacity":"0.75"
		});
		$("#reviews_loading").css({
			"top":(reviewsTop + (reviewsHeight/2) - 33) + "px",
			"left":(reviewsLeft + (reviewsWidth/2) - 33) + "px",
			"position":"absolute"
		});
	},
	removeReviewsLoadingBlocker:function(){
		$("#reviews_blocker").remove();
		$("#reviews_loading").remove();
	},
	init:function(){
		this.setEvents();
	}
};

$(document).ready(function(){
	boots.product.details.init();
});

