$(document).ready(function () {
	if(typeof selectedProductID != 'undefined') {					
		/* Start : Related Products */
                CountDownTimer();
		$.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 */
	}
		$('.detil').click(function() {
		var scrollTop=$('#other-detail').offset().top;
		$('html,body').animate({scrollTop:scrollTop},'slow');	
		return false;
	});
	$(document).on("click",".viewAllOffer",function(){
         $(this).hide();
		 $('.showOffer' ).slideDown('slow');
         
	});
    $(document).on("click",".hideOffer",function(){
		 $('.showOffer' ).slideUp();
         $('.viewAllOffer').fadeIn();
         
	});
        $(document).on('click',".tooltip-box .gotit",function(){
            $(this).parents("li").removeClass("demo");
         $.ajax({
                type: "POST",
                url: GLOBAL_BASE_PATH + "/cart/ajaxSetGotIt/",
                data: '',
		async: false,
                success: function (data) {
                    
                }
         })
        
    });
	/*$(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('id'));
    });	
    $(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');
     })
    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");
                        if(obj.hasClass('icon-wish-list')) {
                           obj.removeClass("icon-wish-list");
                           obj.addClass("icon-wish-list-fill");
                        }
					}
				});
			}
		}
		else {
			$("#CategoryPopup").hide();
			$('#iframeLogin').attr('src', popup_iframe_url+'/login/ajaxPopupLogin/?reurl=user/wishlist/productid/'+productID);
            $("body,html").animate({scrollTop: 0}, 1);
			$('.quickpopup, .popupsectionlogin').removeClass('popupShowLogin'); 
            $("body").css("overflow","hidden");
			FB.XFBML.parse();
		}
	});
	
	//to remove product from wish list
	$('.wishlist .closeBtn3').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().remove();
					if($(".wishlist").length == 0) {
						$('.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;
	});
	//to set a review as helpful or not
	$(document).on('click','.review-helpful',function() {
		var rate = $(this).attr('rate');
		var revID = $(this).attr('reviewid');
		var link = $(this);
		if(userid>0) {
			if(revID>0) {
				$.ajax({
					type:'POST',
					data:'revID='+revID+'&rating='+rate,
					url: GLOBAL_BASE_PATH+"/productreview/ajaxReviewHelpful/",
					success:function(text) {
						if(text == 'ok') {
							link.closest('div').find('.thanks-text').attr('style','display:block');
							link.closest('div').find('.thanks-text').html(thanksforvote);
						} 
						else {
							link.closest('div').find('.thanks-text').attr('style','display:block');
							link.closest('div').find('.thanks-text').html(errorMsgforvote);
						}
					}
				});
			}
		}
		else {
		$('#iframeLogin').attr('src', popup_iframe_url+'/login/ajaxPopupLogin/');
		$('.quickpopup, .popupsectionlogin').removeClass('popupShowLogin');              
		FB.XFBML.parse();
		}
	});
	
	$('.shipping_heading').click(function() 
	{
		var id_val = $(this).attr('rel');
		$("#shiping_exp_"+id_val).toggle();
	});
	
});
function CountDownTimer(uniqueID)
{
	if(uniqueID){
		$('.AjaxProductTimer').each(function () {
		if($(this).attr('uniqueid')==uniqueID){
			var product_id = $(this).attr("rel");
			setCounter($(this),product_id);
			
		}
	});
	}
	else{
		$('.AjaxProductTimer').each(function () {
			var product_id = $(this).attr("rel");
			setCounter($(this),product_id);
		});
	}

}

function setCounter(obj,productID){
	
        var timerFromDate = $("#timerFromDate_" + productID).val();
	var timerToDate = $("#timerToDate_" + productID).val();
        if(timerFromDate!=''&&timerFromDate!='undefined')
            var timerFromDateArray = timerFromDate.split("-");
        if(timerToDate!=''&&timerToDate!='undefined')
            var timerToDateArray = timerToDate.split("-");
	obj.countDown({
		fromDate: {
			'day': timerFromDateArray[0],
			'month': timerFromDateArray[1],
			'year': timerFromDateArray[2],
			'hour': timerFromDateArray[3],
			'min': timerFromDateArray[4],
			'sec': timerFromDateArray[5]
		},
		targetDate: {
			'day': timerToDateArray[0],
			'month': timerToDateArray[1],
			'year': timerToDateArray[2],
			'hour': timerToDateArray[3],
			'min': timerToDateArray[4],
			'sec': timerToDateArray[5]
		}
	});
}

// Auto Height
function equalHeight(group) {
//    tallest = 0;
//    group.each(function () {
//        thisHeight = $(this).height();
//        if (thisHeight > tallest) {
//            tallest = thisHeight;
//        }
//    });
//    group.height(tallest);
}

function changeFollowBusiness(arg) {
    var businessID = $("#businessID").val();
    if (arg == 'unfollow') {
        $(".AjaxUnFollow").text(lblFollow);
        $(".AjaxUnFollow").attr('title', lblFollow);
        $(".AjaxUnFollow").attr('id', 'linkfollow');
        $(".AjaxUnFollow").attr('class', 'icon-like AjaxFollow product-follow');
    } else {
        $(".AjaxFollow").text(lblUnFollow);
        $(".AjaxFollow").attr('title', lblUnFollow);
        $(".AjaxFollow").attr('id', 'linkUnfollow');
        $(".AjaxFollow").attr('class', 'icon-like AjaxUnFollow product-follow');
    }
    $.ajax({
        type: "POST",
        url: GLOBAL_BASE_PATH + "/stores/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:$(".review-block").offset().top});
		
				}
			});
}
function loadBuyboxProducts(type){
    var productID         =  $('#hiddenProductID').val();
    var sortBy         =  $('#buyboxSort').val();
    var page;
    if(type=='viewmore'){
        page       =  $('#hiddenBuyboxPage').val();
    }else{
        page       =  0;
    }
    var data={
                "productID" : productID ,
                "sortBy"    : sortBy,
                "page"      : page,
                "type"      : type
            };
    $.ajax({
        type: "POST",
        url: GLOBAL_BASE_PATH + "/product/ajaxGetBuyboxProducts/",
        data: data,
        beforeSend: function() {
            $("#loadProcessImage").show();
        },
        success: function(msg) {
            $("#loadProcessImage").hide();
            if (msg != '') {
                $('#hiddenBuyboxPage').val(parseInt(page)+1);
                if(type=='viewmore'){
                    $('.buyBoxTable').append(msg);
                }else{
                    $('.buyBoxTable').html(msg);
                }
                //webrupee();
            }
        }
    });
}
