$(document).ready(function () {
	if(typeof selectedProductID != 'undefined') {					
		/* Start : Related Products */
		$.ajax({
			type: "GET",
			url: GLOBAL_BASE_PATH + "/product/ajaxGetRelatedProduct/productID/"+selectedProductID+"/type/related/selectedProductCategoryID/"+selectedProductCategoryID+"/",
			beforeSend:function(){},
			success: function (data) { 
				$("#related_product").html(data);
			}
		});
		/* END : Related Products */
		
		/* Start : Upsell Products */
		$.ajax({
			type: "GET",
			url: GLOBAL_BASE_PATH + "/product/ajaxGetRelatedProduct/productID/"+selectedProductID+"/type/upsell/selectedProductCategoryID/"+selectedProductCategoryID+"/",
			beforeSend:function(){},
			success: function (data) { 
				$("#upsell_product").html(data);
			}
		});
		/* END : Upsell Products */
		
		/* Start : Cross Sell Products */
		$.ajax({
			type: "GET",
			url: GLOBAL_BASE_PATH + "/product/ajaxGetRelatedProduct/productID/"+selectedProductID+"/type/crossel/selectedProductCategoryID/"+selectedProductCategoryID+"/",
			beforeSend:function(){},
			success: function (data) { 
				$("#crosssell_product").html(data);
			}
		});
		/* END : Cross Sell Products */
	}
	
	$(document).on("click",".detail-tab",function(){
		var thisVar = $(this);
	$(".detail-tab").removeClass("active");	
		thisVar.addClass("active");	
		var thisIndex = $(".detail-tab").index(thisVar);
	$(".detail-div").hide();
		$(".detail-div:eq("+thisIndex+")").show();
	});
    $(".counting-box:last").css("margin-right", "0");
    $('.main-nav ul li a[rel=products]').addClass('active');
    $(document).on('click','.AjaxUnFollow', function () {
        var loadUnfollow = changeFollowBusiness('unfollow');
    });
    $(document).on('click','.AjaxFollow', function () {
        var loadFollow = changeFollowBusiness('follow');
    });
    $(document).on('click',"#optionclose", function () {
        $(".buy-pop-section").hide();
        $('#AjaxProductOptions').hide();
    });
	/* $(document).on('click',"#review-tab", function () {	
		if($("#ajaxReviewList").html()=='')
			reviewPagination('next');
		}); */		
	$(document).on('click',".prev,.next", function () {	
		reviewPagination($(this).attr('class'));
    });	
    var Bheight = $('.banner-wrapper-pane').height();
    Bheight = Bheight - 80;
    /*$('.content-wrapper-top ').css('height',Bheight);*/
	
	//to add product to wish list
	$(document).on('click','#wish-req,.addToWishList',function() {
		var obj 	=  	$(this);
		var status 	= 	obj.attr('wishlist');
		var label 	= 	obj.attr('text');
		var productID 	= 	obj.attr('productid');
		var userID 	= 	obj.attr('user');
		if(userID>0) {
			if(productID>0) {
				$.ajax({
					type : 'POST',
					data : 'productID='+productID+'&userID='+userID,
					url	 : GLOBAL_BASE_PATH+"/product/ajaxWishList/",
					success:function() {
						if(typeof label != 'undefined') {
							obj.text(label);
							obj.attr('href',GLOBAL_BASE_PATH+"/user/wishlist/");
						}
						$('#wish-req').remove();
						$('#wishlist-div').removeClass("hide");
					}
				});
			}
		}
		else {
			
			window.location.href =GLOBAL_BASE_PATH + '/login/?reurl=user/wishlist/productid/'+productID;
		}
	});
	
	//to remove product from wish list
	$('.wishlist ul li #wish-closeBtn').click(function() {
		var itemid 	= $(this).attr('itemid');
		var link 	= $(this);
		if(itemid > 0) {
			$.ajax({
				type	:'POST',
				data	:'itemid='+itemid,
				url		: GLOBAL_BASE_PATH+"/user/ajaxRemoveFromWishlist/",
				success:function() {
					link.parent().parent().remove();
					if($(".wishlist ul li").length > 0) {
						$(".wishlist ul li").each(function(index,value) {
						if((index+1)%2==0)
							$(this).addClass ("marR0");
						else
							$(this).removeClass ("marR0");
						});
					} else {
						$('.warning').attr('style','display:block');
					}
				}
			});
		}
	});
	
	$('#show-review').click(function() {
		
		$("#review-tab .detail-tab").trigger('click');
		var scrollTop=$('#Reviews').offset().top;
		$('html,body').animate({scrollTop:scrollTop},'slow');	
		return false;
	});
	$('.shipping_heading').click(function() 
	{
		var id_val = $(this).attr('rel');
		$("#shiping_exp_"+id_val).toggle();
	});
	
});
 $(document).on('change','#buyboxSort',function() {
        loadBuyboxProducts('sort');
     });
     $(document).on('click','.viewallbuybox',function() {
        //loadBuyboxProducts('viewmore');
        var rel = $(this).attr('rel');
        var buyboxCount = $('#hiddenBuyBoxCount').val();
        if(rel=='hide'){
           var hideCnt = parseInt(buyboxCount)-5;
           $('.buyboxMore').show();
           $(this).attr('rel','show');
           var textval =  'Hide '+hideCnt+' Sellers';
           $(this).text(textval);
           $(this).attr('title',textval);
        }else{
           $('.buyboxMore').hide(); 
           $(this).attr('rel','hide');
           var textval =  'View all '+buyboxCount+' Sellers';
           $(this).text(textval);
           $(this).attr('title',textval);
        }
     });
     $(document).on('click','.seller-section',function(){
         $('html, body').animate({
            scrollTop: $('#buyboxSec').offset().top - 80
          }, 'slow');
     })
// Auto Height

function equalHeight(group) {
    tallest = 0;
    group.each(function () {
        thisHeight = $(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}
$(document).ready(function () {
    equalHeight($(".AjaxAutoHeight"));
});



function changeFollowBusiness(arg) {
    var businessID = $("#businessID").val();
    if (arg == 'unfollow') {
        $(".AjaxUnFollow").text(lblFollow);
        $(".AjaxUnFollow").attr('title', lblFollow);
        $(".AjaxUnFollow").attr('id', 'linkfollow');
        $(".AjaxUnFollow").attr('class', 'slip iconappend2 AjaxFollow product-follow');
    } else {
        $(".AjaxFollow").text(lblUnFollow);
        $(".AjaxFollow").attr('title', lblUnFollow);
        $(".AjaxFollow").attr('id', 'linkUnfollow');
        $(".AjaxFollow").attr('class', 'slip iconappend2 AjaxUnFollow product-follow');
    }
    $.ajax({
        type: "POST",
        url: GLOBAL_BASE_PATH + "/store/ajaxFollowMerchant/",
        data: 'businessID=' + businessID,
        success: function (data) { }
    });
}

$(document).on('change', '#reviewsort', function () {
	reviewPagination('',1);
});
function reviewPagination(type,start){

		var limitFrom ='';
		if(start > 0) $("#limitFrom").val('');
		if(typeof $("#limitFrom").val()!='undefined')
		limitFrom =$("#limitFrom").val();
		if(typeof $("#reviewsort").val()!='undefined')
			var reviewsort = $("#reviewsort").val();
		 else var reviewsort = 0;
			$.ajax({
				type:"POST",
				url:GLOBAL_BASE_PATH+'/productreview/ajaxReview/id/'+productID+"/type/"+type+"/sort/"+reviewsort+"/limitFrom/"+limitFrom,
				success:function(msg){
					$("#ajaxReviewList").html(msg);
					limitFrom =$("#limitFrom").val();
					if(typeof $("#limitFrom").val()=='undefined' || limitFrom<=20){
						$(".prev").addClass('hide');
						$(".prevDisable").removeClass('hide');
					}else{
						$(".prev").removeClass('hide');
						$(".prevDisable").addClass('hide');		
					}
					if(limitFrom>=productReviewsCount){
						$(".next").addClass('hide');
						$(".nextDisable").removeClass('hide');
						$(".countPage").html(limitFrom-20+"-"+productReviewsCount);
					}else{
						$(".next").removeClass('hide');
						$(".nextDisable").addClass('hide');	
						$(".countPage").html(limitFrom-20+"-"+limitFrom);						
					}
					$('html,body').animate({scrollTop:$("#tabs").offset().top});
		
				}
			});
}

/////////////////////////////////////////////stock notify
$(document).ready(function () {
	//Event triger while click on product detail page buy now button
    $(document).on('click', '#stock-notify-varient', function () {
		var registeredUserID	=	$('#registeredUserID').val();
		var validateEmail =  $(this).validate( { form : "frmBackInStock" , field : "subscriberEmailID" , key : "required~*email" , error: errEmailID , inline:"1" , group: "", row: ""} ); 
		var validateName  =  $(this).validate( { form : "frmBackInStock" , field : "subscriberName" , key : "required" , error: errName, inline:"1" , group: "", row: ""} ); 
		if((validateEmail == false || validateName == false)&& registeredUserID == 0){
			return false;
		}
		else{
			sendNotification('varient');
		}	
	});
	$(document).on('click', '#stock-notify-combination', function () {
		var registeredUserID	=	$('#registeredUserID').val();
		var validateEmail =  $(this).validate( { form : "frmBackInStock" , field : "subscriberEmailID" , key : "required~*email" , error: errEmailID, inline:"1" , group: "", row: ""} ); 
		var validateName  =  $(this).validate( { form : "frmBackInStock" , field : "subscriberName" , key : "required" , error: errName, inline:"1" , group: "", row: ""} ); 
		if((validateEmail == false || validateName == false)&& registeredUserID == 0){
			return false;
		}
		else{
			sendNotification('combination');
		}
	});
});
function sendNotification(type){

		var registeredUserID	=	$('#registeredUserID').val();
        //Product custom options validation
		
			var emailID	=	$('#subscriberEmailID').val();
			var name	=	$('#subscriberName').val();
			if(type == 'varient'){
				var productOptionID	=	$("#selectedProductOption").val();
				var combHash			=	0;
			}
			else{
				var productOptionID	=	0;
				var combHash		=	combinationHash;
			}
			$.ajax({
				type: "POST",
				url: GLOBAL_BASE_PATH + "/product/ajaxStockNotify/",
				data:'productID='+productID+'&productOptionID='+productOptionID+'&combinationHash='+combHash+'&emailID='+emailID+'&name='+name+'&userID='+registeredUserID,
						success:function(msg) {
							$('.backInStock').html(backInStockInfo);
						}
			});
		
}
